-
Notifications
You must be signed in to change notification settings - Fork 578
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
ROSA: Support for OCM service account credentials
- Loading branch information
Showing
3 changed files
with
287 additions
and
40 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package rosa | ||
|
||
import ( | ||
"context" | ||
"os" | ||
"testing" | ||
|
||
corev1 "k8s.io/api/core/v1" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/klog/v2" | ||
|
||
rosacontrolplanev1 "sigs.k8s.io/cluster-api-provider-aws/v2/controlplane/rosa/api/v1beta2" | ||
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/cloud/scope" | ||
"sigs.k8s.io/cluster-api-provider-aws/v2/pkg/logger" | ||
"sigs.k8s.io/cluster-api-provider-aws/v2/util/system" | ||
"sigs.k8s.io/controller-runtime/pkg/client/fake" | ||
|
||
. "github.com/onsi/gomega" | ||
) | ||
|
||
func createROSAControlPlaneScope(wlSecret, mgrSecret *corev1.Secret, cp *rosacontrolplanev1.ROSAControlPlane) *scope.ROSAControlPlaneScope { | ||
// k8s mock (fake) client | ||
if wlSecret == nil { | ||
wlSecret = &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "bogus-name-01", | ||
Namespace: "bogus-namespace", | ||
}, | ||
} | ||
} | ||
|
||
if mgrSecret == nil { | ||
mgrSecret = &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: "bogus-name-02", | ||
Namespace: "bogus-namespace", | ||
}, | ||
} | ||
} | ||
|
||
fakeClient := fake.NewClientBuilder().WithObjects(wlSecret, mgrSecret).Build() | ||
|
||
// ROSA Control Plane Scope | ||
rcpScope := &scope.ROSAControlPlaneScope{ | ||
Client: fakeClient, | ||
ControlPlane: cp, | ||
Logger: *logger.NewLogger(klog.Background()), | ||
} | ||
|
||
return rcpScope | ||
} | ||
|
||
func createSecret(name, namespace, token, url, clientId, clientSecret string) *corev1.Secret { | ||
Check failure on line 53 in pkg/rosa/client_test.go GitHub Actions / lint
|
||
return &corev1.Secret{ | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Name: name, | ||
Namespace: namespace, | ||
}, | ||
Data: map[string][]byte{ | ||
"ocmToken": []byte(token), | ||
"ocmApiUrl": []byte(url), | ||
"ocmClientId": []byte(clientId), | ||
"ocmClientSecret": []byte(clientSecret), | ||
}, | ||
} | ||
} | ||
|
||
func createCP(namespace string) *rosacontrolplanev1.ROSAControlPlane { | ||
Check failure on line 68 in pkg/rosa/client_test.go GitHub Actions / lint
|
||
return &rosacontrolplanev1.ROSAControlPlane{ | ||
Spec: rosacontrolplanev1.RosaControlPlaneSpec{ | ||
CredentialsSecretRef: &corev1.LocalObjectReference{ | ||
Name: "rosa-creds-secret", | ||
}, | ||
}, | ||
ObjectMeta: metav1.ObjectMeta{ | ||
Namespace: "default", | ||
}, | ||
} | ||
|
||
} | ||
Check failure on line 80 in pkg/rosa/client_test.go GitHub Actions / lint
|
||
|
||
func TestOcmCredentials(t *testing.T) { | ||
g := NewWithT(t) | ||
|
||
wlSecret := createSecret("rosa-creds-secret", "default", "", "url", "client-id", "client-secret") | ||
mgrSecret := createSecret("rosa-creds-secret", system.GetManagerNamespace(), "", "url", "global-client-id", "global-client-secret") | ||
|
||
cp := createCP("default") | ||
|
||
// Test that ocmCredentials() prefers workload secret to global and environment secrets | ||
cp.Spec = rosacontrolplanev1.RosaControlPlaneSpec{ | ||
CredentialsSecretRef: &corev1.LocalObjectReference{ | ||
Name: "rosa-creds-secret", | ||
}, | ||
} | ||
os.Setenv("OCM_API_URL", "env-url") | ||
os.Setenv("OCM_TOKEN", "env-token") | ||
rcpScope := createROSAControlPlaneScope(wlSecret, mgrSecret, cp) | ||
token, url, clientId, clientSecret, err := ocmCredentials(context.Background(), rcpScope) | ||
Check failure on line 99 in pkg/rosa/client_test.go GitHub Actions / lint
|
||
|
||
g.Expect(err).NotTo(HaveOccurred()) | ||
g.Expect(token).To(Equal(string(wlSecret.Data["ocmToken"]))) | ||
g.Expect(url).To(Equal(string(wlSecret.Data["ocmApiUrl"]))) | ||
g.Expect(clientId).To(Equal(string(wlSecret.Data["ocmClientId"]))) | ||
g.Expect(clientSecret).To(Equal(string(wlSecret.Data["ocmClientSecret"]))) | ||
|
||
// Test that ocmCredentials() prefers global manager secret to environment secret in case workload secret is not specified | ||
cp.Spec = rosacontrolplanev1.RosaControlPlaneSpec{} | ||
rcpScope = createROSAControlPlaneScope(wlSecret, mgrSecret, cp) | ||
token, url, clientId, clientSecret, err = ocmCredentials(context.Background(), rcpScope) | ||
|
||
g.Expect(err).NotTo(HaveOccurred()) | ||
g.Expect(token).To(Equal(string(mgrSecret.Data["ocmToken"]))) | ||
g.Expect(url).To(Equal(string(mgrSecret.Data["ocmApiUrl"]))) | ||
g.Expect(clientId).To(Equal(string(mgrSecret.Data["ocmClientId"]))) | ||
g.Expect(clientSecret).To(Equal(string(mgrSecret.Data["ocmClientSecret"]))) | ||
|
||
// Test that ocmCredentials() returns environment secret in case workload and manager secret are not specified | ||
cp.Spec = rosacontrolplanev1.RosaControlPlaneSpec{} | ||
rcpScope = createROSAControlPlaneScope(nil, nil, cp) | ||
token, url, clientId, clientSecret, err = ocmCredentials(context.Background(), rcpScope) | ||
|
||
g.Expect(err).NotTo(HaveOccurred()) | ||
g.Expect(token).To(Equal(string(os.Getenv("OCM_TOKEN")))) | ||
Check failure on line 124 in pkg/rosa/client_test.go GitHub Actions / lint
|
||
g.Expect(url).To(Equal(string(os.Getenv("OCM_API_URL")))) | ||
Check failure on line 125 in pkg/rosa/client_test.go GitHub Actions / lint
|
||
g.Expect(clientSecret).To(Equal("")) | ||
|
||
// Test that ocmCredentials() returns error in case none of the secrets has been provided | ||
cp.Spec = rosacontrolplanev1.RosaControlPlaneSpec{} | ||
rcpScope = createROSAControlPlaneScope(nil, nil, cp) | ||
os.Unsetenv("OCM_API_URL") | ||
os.Unsetenv("OCM_TOKEN") | ||
token, url, clientId, clientSecret, err = ocmCredentials(context.Background(), rcpScope) | ||
|
||
g.Expect(err).To(HaveOccurred()) | ||
g.Expect(token).To(Equal("")) | ||
g.Expect(url).To(Equal("")) | ||
g.Expect(clientSecret).To(Equal("")) | ||
} |