Skip to content

Commit 241b7bb

Browse files
committed
Track encryptionKeyLastRotation and regenerate LTPA config password when out of sync
1 parent ce9b00f commit 241b7bb

File tree

2 files changed

+33
-5
lines changed

2 files changed

+33
-5
lines changed

internal/controller/assets/create_ltpa_config.sh

+4-4
Original file line numberDiff line numberDiff line change
@@ -63,10 +63,10 @@ LAST_ROTATION=$(curl --cacert ${CACERT} --header "Content-Type: application/json
6363
PASSWORD=$(curl --cacert ${CACERT} --header "Content-Type: application/json" --header "Authorization: Bearer ${TOKEN}" -X GET ${APISERVER}/api/v1/namespaces/${NAMESPACE}/secrets/${LTPA_SECRET_NAME} | grep -o '"password": "[^"]*' | grep -o '[^"]*$' | base64 -d);
6464

6565
if [ "$ENCRYPTION_KEY_SHARING_ENABLED" == "true" ] && [ $NOT_FOUND_COUNT -eq 0 ]; then
66-
PASSWORD_KEY_LAST_ROTATION=$(curl --cacert ${CACERT} --header "Content-Type: application/json" --header "Authorization: Bearer ${TOKEN}" -X GET ${APISERVER}/api/v1/namespaces/${NAMESPACE}/secrets/${PASSWORD_KEY_SECRET_NAME} | grep -o '"lastRotation": "[^"]*' | grep -o '[^"]*$' | base64 -d);
67-
PASSWORD_KEY=$(curl --cacert ${CACERT} --header "Content-Type: application/json" --header "Authorization: Bearer ${TOKEN}" -X GET ${APISERVER}/api/v1/namespaces/${NAMESPACE}/secrets/${PASSWORD_KEY_SECRET_NAME} | grep -o '"passwordEncryptionKey": "[^"]*' | grep -o '[^"]*$' | base64 -d);
68-
ENCODED_PASSWORD=$(securityUtility encode --encoding=${ENCODING_TYPE} --key=${PASSWORD_KEY} ${PASSWORD});
69-
LTPA_ENCODED_PASSWORD="{\"apiVersion\": \"v1\", \"stringData\": {\"lastRotation\": \"$LAST_ROTATION\", \"password\": \"$ENCODED_PASSWORD\"}, \"kind\": \"Secret\",\"metadata\": {\"name\": \"$LTPA_CONFIG_SECRET_NAME\", \"passwordKeyLastRotation\": \"$PASSWORD_KEY_LAST_ROTATION\", \"namespace\": \"$NAMESPACE\",\"labels\": {\"app.kubernetes.io/name\": \"${LTPA_CONFIG_BASE_NAME}\", \"app.kubernetes.io/instance\": \"${LTPA_CONFIG_SECRET_NAME}\", \"$LTPA_LABEL_KEY\": \"$LTPA_LABEL_VALUE\"}},\"type\": \"Opaque\"}";
66+
ENCRYPTION_KEY_LAST_ROTATION=$(curl --cacert ${CACERT} --header "Content-Type: application/json" --header "Authorization: Bearer ${TOKEN}" -X GET ${APISERVER}/api/v1/namespaces/${NAMESPACE}/secrets/${PASSWORD_KEY_SECRET_NAME} | grep -o '"lastRotation": "[^"]*' | grep -o '[^"]*$' | base64 -d);
67+
ENCRYPTION_KEY=$(curl --cacert ${CACERT} --header "Content-Type: application/json" --header "Authorization: Bearer ${TOKEN}" -X GET ${APISERVER}/api/v1/namespaces/${NAMESPACE}/secrets/${PASSWORD_KEY_SECRET_NAME} | grep -o '"passwordEncryptionKey": "[^"]*' | grep -o '[^"]*$' | base64 -d);
68+
ENCODED_PASSWORD=$(securityUtility encode --encoding=${ENCODING_TYPE} --key=${ENCRYPTION_KEY} ${PASSWORD});
69+
LTPA_ENCODED_PASSWORD="{\"apiVersion\": \"v1\", \"stringData\": {\"encryptionKeyLastRotation\": \"$ENCRYPTION_KEY_LAST_ROTATION\", \"lastRotation\": \"$LAST_ROTATION\", \"password\": \"$ENCODED_PASSWORD\"}, \"kind\": \"Secret\",\"metadata\": {\"name\": \"$LTPA_CONFIG_SECRET_NAME\", \"passwordKeyLastRotation\": \"$PASSWORD_KEY_LAST_ROTATION\", \"namespace\": \"$NAMESPACE\",\"labels\": {\"app.kubernetes.io/name\": \"${LTPA_CONFIG_BASE_NAME}\", \"app.kubernetes.io/instance\": \"${LTPA_CONFIG_SECRET_NAME}\", \"$LTPA_LABEL_KEY\": \"$LTPA_LABEL_VALUE\"}},\"type\": \"Opaque\"}";
7070
else
7171
SECRET_NAME="${LTPA_SECRET_NAME}-password"
7272
ENCODED_PASSWORD=$(securityUtility encode --encoding=${ENCODING_TYPE} ${PASSWORD});

internal/controller/ltpa_keys_sharing.go

+29-1
Original file line numberDiff line numberDiff line change
@@ -609,7 +609,8 @@ func (r *ReconcileOpenLiberty) generateLTPAConfig(instance *olv1.OpenLibertyAppl
609609

610610
ltpaConfigSecret := &corev1.Secret{}
611611
ltpaConfigSecretRootName := OperatorShortName + "-managed-ltpa"
612-
if r.isUsingPasswordEncryptionKeySharing(instance, passwordEncryptionMetadata) {
612+
isPasswordEncryptionKeySharing := r.isUsingPasswordEncryptionKeySharing(instance, passwordEncryptionMetadata)
613+
if isPasswordEncryptionKeySharing {
613614
ltpaConfigSecretRootName += "-keyed-password"
614615
ltpaConfigSecret.Name = ltpaConfigSecretRootName + ltpaConfigMetadata.Name
615616
} else {
@@ -801,6 +802,33 @@ func (r *ReconcileOpenLiberty) generateLTPAConfig(instance *olv1.OpenLibertyAppl
801802
return err
802803
}
803804

805+
// if using encryption key, check if the key has been rotated and requires a regeneration of the LTPA keyed password
806+
if isPasswordEncryptionKeySharing {
807+
internalEncryptionKeySecret, err := r.hasInternalEncryptionKeySecret(instance, passwordEncryptionMetadata)
808+
if err != nil {
809+
return err
810+
}
811+
lastRotation, found := internalEncryptionKeySecret.Data["lastRotation"]
812+
if !found {
813+
// lastRotation field is not present so the Secret was not initialized correctly
814+
err := r.DeleteResource(internalEncryptionKeySecret)
815+
if err != nil {
816+
return err
817+
}
818+
return fmt.Errorf("the internal encryption key secret does not contain field 'lastRotation'")
819+
}
820+
821+
if encryptionKeyLastRotation, found := ltpaConfigSecret.Data["encryptionKeyLastRotation"]; found {
822+
if string(encryptionKeyLastRotation) != string(lastRotation) {
823+
err := r.DeleteResource(ltpaConfigSecret)
824+
if err != nil {
825+
return err
826+
}
827+
return fmt.Errorf("the encryption key has been modified; waiting for a new LTPA password to be generated")
828+
}
829+
}
830+
}
831+
804832
// Create/update the Secret to hold the server.xml that will import the LTPA keys into the Liberty server
805833
// This server.xml will be mounted in /config/configDropins/overrides/ltpaKeysMount.xml
806834
serverXMLMountSecretErr := r.GetClient().Get(context.TODO(), types.NamespacedName{Name: ltpaXMLMountSecret.Name, Namespace: ltpaXMLMountSecret.Namespace}, ltpaXMLMountSecret)

0 commit comments

Comments
 (0)