Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Bug 2004814: OCM controller - change type of the secret #500

Merged
merged 1 commit into from
Sep 16, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 9 additions & 7 deletions pkg/ocm/ocm.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,8 +20,10 @@ import (
)

const (
targetNamespaceName = "openshift-config-managed"
secretName = "etc-pki-entitlement" //nolint: gosec
targetNamespaceName = "openshift-config-managed"
secretName = "etc-pki-entitlement" //nolint: gosec
entitlementAttrName = "entitlement.pem"
entitlementKeyAttrName = "entitlement-key.pem"
)

// Controller holds all the required resources to be able to communicate with OCM API
Expand Down Expand Up @@ -161,10 +163,10 @@ func (c *Controller) createSecret(ocmData *ScaResponse) (*v1.Secret, error) {
Namespace: targetNamespaceName,
},
Data: map[string][]byte{
v1.TLSCertKey: []byte(ocmData.Cert),
v1.TLSPrivateKeyKey: []byte(ocmData.Key),
entitlementAttrName: []byte(ocmData.Cert),
entitlementKeyAttrName: []byte(ocmData.Key),
},
Type: v1.SecretTypeTLS,
Type: v1.SecretTypeOpaque,
}
cm, err := c.coreClient.Secrets(targetNamespaceName).Create(c.ctx, newSCA, metav1.CreateOptions{})
if err != nil {
Expand All @@ -176,8 +178,8 @@ func (c *Controller) createSecret(ocmData *ScaResponse) (*v1.Secret, error) {
// updateSecret updates provided secret with given data
func (c *Controller) updateSecret(s *v1.Secret, ocmData *ScaResponse) (*v1.Secret, error) {
s.Data = map[string][]byte{
v1.TLSCertKey: []byte(ocmData.Cert),
v1.TLSPrivateKeyKey: []byte(ocmData.Key),
entitlementAttrName: []byte(ocmData.Cert),
entitlementKeyAttrName: []byte(ocmData.Key),
}
s, err := c.coreClient.Secrets(s.Namespace).Update(c.ctx, s, metav1.UpdateOptions{})
if err != nil {
Expand Down
22 changes: 11 additions & 11 deletions pkg/ocm/ocm_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import (
)

var (
tlsSecretCrt = "tls.crt"
tlsSecretKey = "tls.key"
secTestData = "secret testing data"
entitlementPem = "entitlement.pem"
entitlementKeyPem = "entitlement-key.pem"
secTestData = "secret testing data"
)

var testRes = &ScaResponse{
Expand All @@ -31,10 +31,10 @@ func Test_OCMController_SecretIsCreated(t *testing.T) {

testSecret, err := coreClient.Secrets(targetNamespaceName).Get(context.Background(), secretName, metav1.GetOptions{})
assert.NoError(t, err, "can't get secret")
assert.Contains(t, testSecret.Data, tlsSecretKey, "can't find %s in the %s secret data", tlsSecretKey, secretName)
assert.Contains(t, testSecret.Data, tlsSecretCrt, "can't find %s in the %s secret data", tlsSecretCrt, secretName)
assert.Equal(t, "secret key", string(testSecret.Data[tlsSecretKey]), "unexpected data in %s secret", secretName)
assert.Equal(t, "secret cert", string(testSecret.Data[tlsSecretCrt]), "unexpected data in %s secret", secretName)
assert.Contains(t, testSecret.Data, entitlementKeyPem, "can't find %s in the %s secret data", entitlementKeyPem, secretName)
assert.Contains(t, testSecret.Data, entitlementPem, "can't find %s in the %s secret data", entitlementPem, secretName)
assert.Equal(t, "secret key", string(testSecret.Data[entitlementKeyPem]), "unexpected data in %s secret", secretName)
assert.Equal(t, "secret cert", string(testSecret.Data[entitlementPem]), "unexpected data in %s secret", secretName)
}

func Test_OCMController_SecretIsUpdated(t *testing.T) {
Expand Down Expand Up @@ -62,8 +62,8 @@ func Test_OCMController_SecretIsUpdated(t *testing.T) {

testSecret, err := coreClient.Secrets(targetNamespaceName).Get(context.Background(), secretName, metav1.GetOptions{})
assert.NoError(t, err, "can't get secret")
assert.Contains(t, testSecret.Data, tlsSecretKey, "can't find %s in the %s secret data", tlsSecretKey, secretName)
assert.Contains(t, testSecret.Data, tlsSecretCrt, "can't find %s in the %s secret data", tlsSecretCrt, secretName)
assert.Equal(t, "new secret testing key", string(testSecret.Data[tlsSecretKey]), "unexpected data in %s secret", secretName)
assert.Equal(t, "new secret testing cert", string(testSecret.Data[tlsSecretCrt]), "unexpected data in %s secret", secretName)
assert.Contains(t, testSecret.Data, entitlementKeyPem, "can't find %s in the %s secret data", entitlementKeyPem, secretName)
assert.Contains(t, testSecret.Data, entitlementPem, "can't find %s in the %s secret data", entitlementPem, secretName)
assert.Equal(t, "new secret testing key", string(testSecret.Data[entitlementKeyPem]), "unexpected data in %s secret", secretName)
assert.Equal(t, "new secret testing cert", string(testSecret.Data[entitlementPem]), "unexpected data in %s secret", secretName)
}