Skip to content

Commit

Permalink
Merge pull request #432 from bshephar/validate-auth-enc-key
Browse files Browse the repository at this point in the history
Validate AuthEncryptionKey length
  • Loading branch information
openshift-merge-bot[bot] authored Oct 7, 2024
2 parents b1794ec + e48838a commit 9bd221e
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
20 changes: 19 additions & 1 deletion controllers/heat_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -952,7 +952,10 @@ func (r *HeatReconciler) generateServiceSecrets(
return err
}
password := strings.TrimSuffix(string(ospSecret.Data[instance.Spec.PasswordSelectors.Service]), "\n")
authEncryptionKey := strings.TrimSuffix(string(ospSecret.Data[instance.Spec.PasswordSelectors.AuthEncryptionKey]), "\n")
authEncryptionKey, err := validateAuthEncryptionKey(instance, ospSecret)
if err != nil {
return err
}

transportURLSecret, _, err := oko_secret.GetSecret(ctx, h, instance.Status.TransportURLSecret, instance.Namespace)
if err != nil {
Expand Down Expand Up @@ -1343,3 +1346,18 @@ func renderVhost(httpdVhostConfig map[string]interface{}, instance *heatv1beta1.
}
httpdVhostConfig[endpt.String()] = endptConfig
}

// validateAuthEncryptionKey - the heat_auth_encrption_key needs to be 32 characters long. This function validates
// the length of the user provided key and returns an error if it isn't long enough.
func validateAuthEncryptionKey(instance *heatv1beta1.Heat, ospSecret *corev1.Secret) (string, error) {
const HeatAuthEncKeyLen int = 32

heatAuthEncKey := strings.TrimSuffix(string(ospSecret.Data[instance.Spec.PasswordSelectors.AuthEncryptionKey]), "\n")

if len(heatAuthEncKey) < HeatAuthEncKeyLen {
return "", fmt.Errorf("AuthEncryptionKey must be at least %d characters", HeatAuthEncKeyLen)
}

return heatAuthEncKey, nil

}
7 changes: 5 additions & 2 deletions tests/functional/base_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,9 @@ func GetDefaultHeatSpec() map[string]interface{} {
"heatEngine": GetDefaultHeatEngineSpec(),
"heatAPI": GetDefaultHeatAPISpec(),
"heatCfnAPI": GetDefaultHeatCFNAPISpec(),
"passwordSelectors": map[string]interface{}{
"AuthEncryptionKey": "HeatAuthEncryptionKey",
},
}
}

Expand Down Expand Up @@ -75,8 +78,8 @@ func CreateHeatSecret(namespace string, name string) *corev1.Secret {
return th.CreateSecret(
types.NamespacedName{Namespace: namespace, Name: name},
map[string][]byte{
"HeatPassword": []byte("12345678"),
"AuthEncryptionKey": []byte("1234567812345678123456781212345678345678"),
"HeatPassword": []byte("12345678"),
"HeatAuthEncryptionKey": []byte("1234567812345678123456781212345678345678"),
},
)
}
Expand Down
55 changes: 55 additions & 0 deletions tests/functional/heat_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -644,4 +644,59 @@ var _ = Describe("Heat controller", func() {
return GetEnvVarValue(deployment.Spec.Template.Spec.Containers[0].Env, "CONFIG_HASH", "")
})*/

When("HeatAuthEncryptionKey is too short", func() {

BeforeEach(func() {
DeferCleanup(th.DeleteInstance, CreateHeat(heatName, GetDefaultHeatSpec()))
DeferCleanup(
k8sClient.Delete, ctx, CreateHeatSecret(namespace, SecretName))
DeferCleanup(infra.DeleteMemcached, infra.CreateMemcached(namespace, "memcached", memcachedSpec))
infra.SimulateMemcachedReady(types.NamespacedName{
Name: "memcached",
Namespace: namespace,
})
DeferCleanup(
k8sClient.Delete, ctx, CreateHeatMessageBusSecret(namespace, HeatMessageBusSecretName))
infra.SimulateTransportURLReady(heatTransportURLName)
keystoneAPI := keystone.CreateKeystoneAPI(namespace)
DeferCleanup(keystone.DeleteKeystoneAPI, keystoneAPI)
DeferCleanup(
mariadb.DeleteDBService,
mariadb.CreateDBService(
namespace,
GetHeat(heatName).Spec.DatabaseInstance,
corev1.ServiceSpec{
Ports: []corev1.ServicePort{{Port: 3306}},
},
),
)
mariadb.SimulateMariaDBAccountCompleted(types.NamespacedName{Namespace: namespace, Name: GetHeat(heatName).Spec.DatabaseAccount})
mariadb.SimulateMariaDBDatabaseCompleted(types.NamespacedName{Namespace: namespace, Name: heat.DatabaseCRName})
dbSyncJobName := types.NamespacedName{
Name: "heat-db-sync",
Namespace: namespace,
}
th.SimulateJobSuccess(dbSyncJobName)

})

It("Should complain about the Key length", func() {
Eventually(func(g Gomega) {
heat := GetHeat(heatName)
heat.Spec.PasswordSelectors.AuthEncryptionKey = "TooShortAuthEncKey"
g.Expect(th.K8sClient.Update(ctx, heat)).Should(Succeed())
}, timeout, interval).Should(Succeed())

th.ExpectCondition(
heatName,
ConditionGetterFunc(HeatConditionGetter),
condition.ServiceConfigReadyCondition,
corev1.ConditionFalse,
)

conditions := HeatConditionGetter(heatName)
message := &conditions.Get(condition.ServiceConfigReadyCondition).Message
Expect(*message).Should(ContainSubstring("AuthEncryptionKey must be at least 32 characters"))
})
})
})

0 comments on commit 9bd221e

Please sign in to comment.