Skip to content

Commit

Permalink
Add secret resolver functionality
Browse files Browse the repository at this point in the history
  • Loading branch information
TalShorSap committed Sep 19, 2023
1 parent ae9f992 commit 99cea74
Show file tree
Hide file tree
Showing 8 changed files with 38 additions and 5 deletions.
1 change: 1 addition & 0 deletions api/common.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,4 +78,5 @@ type SAPBTPResource interface {
DeepClone() SAPBTPResource
SetReady(metav1.ConditionStatus)
GetReady() metav1.ConditionStatus
GetSubaccountID() string
}
4 changes: 4 additions & 0 deletions api/v1/servicebinding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,6 +188,10 @@ func (sb *ServiceBinding) SetReady(ready metav1.ConditionStatus) {
sb.Status.Ready = ready
}

func (sb *ServiceBinding) GetSubaccountID() string {
return sb.Spec.SubaccountID
}

// +kubebuilder:object:root=true

// ServiceBindingList contains a list of ServiceBinding
Expand Down
4 changes: 4 additions & 0 deletions api/v1/serviceinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (si *ServiceInstance) SetReady(ready metav1.ConditionStatus) {
si.Status.Ready = ready
}

func (si *ServiceInstance) GetSubaccountID() string {
return si.Spec.SubaccountID
}

// +kubebuilder:object:root=true

// ServiceInstanceList contains a list of ServiceInstance
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/servicebinding_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -183,6 +183,10 @@ func (sb *ServiceBinding) SetReady(ready metav1.ConditionStatus) {
sb.Status.Ready = ready
}

func (sb *ServiceBinding) GetSubaccountID() string {
return sb.Spec.SubaccountID
}

// +kubebuilder:object:root=true

// ServiceBindingList contains a list of ServiceBinding
Expand Down
4 changes: 4 additions & 0 deletions api/v1alpha1/serviceinstance_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -173,6 +173,10 @@ func (in *ServiceInstance) SetReady(ready metav1.ConditionStatus) {
in.Status.Ready = ready
}

func (si *ServiceInstance) GetSubaccountID() string {

Check failure on line 176 in api/v1alpha1/serviceinstance_types.go

View workflow job for this annotation

GitHub Actions / Build

receiver-naming: receiver name si should be consistent with previous receiver name in for ServiceInstance (revive)
return si.Spec.SubaccountID
}

// +kubebuilder:object:root=true

// ServiceInstanceList contains a list of ServiceInstance
Expand Down
4 changes: 2 additions & 2 deletions controllers/base_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func (r *BaseReconciler) getSMClient(ctx context.Context, object api.SAPBTPResou
}
log := GetLogger(ctx)

secret, err := r.SecretResolver.GetSecretForResource(ctx, object.GetNamespace(), secrets.SAPBTPOperatorSecretName)
secret, err := r.SecretResolver.GetSecretForResource(ctx, object.GetNamespace(), secrets.SAPBTPOperatorSecretName, object.GetSubaccountID())
if err != nil {
return nil, err
}
Expand All @@ -96,7 +96,7 @@ func (r *BaseReconciler) getSMClient(ctx context.Context, object api.SAPBTPResou
}

if len(cfg.ClientSecret) == 0 {
tls, err := r.SecretResolver.GetSecretForResource(ctx, object.GetNamespace(), secrets.SAPBTPOperatorTLSSecretName)
tls, err := r.SecretResolver.GetSecretForResource(ctx, object.GetNamespace(), secrets.SAPBTPOperatorTLSSecretName, object.GetSubaccountID())
if client.IgnoreNotFound(err) != nil {
return nil, err
}
Expand Down
18 changes: 17 additions & 1 deletion internal/secrets/resolver.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,11 +27,21 @@ type SecretResolver struct {
Log logr.Logger
}

func (sr *SecretResolver) GetSecretForResource(ctx context.Context, namespace, name string) (*v1.Secret, error) {
func (sr *SecretResolver) GetSecretForResource(ctx context.Context, namespace, name, subaccountID string) (*v1.Secret, error) {
var secretForResource *v1.Secret
var err error
found := false

if subaccountID != "" {
sr.Log.Info(fmt.Sprintf("Searching for secret name %s, for subaccount id %s, in namespace %s",
name, subaccountID, namespace))
secretForResource, err = sr.getSubaccountSecret(ctx, namespace, name, subaccountID)
if err != nil {
sr.Log.Error(err, "Could not fetch subaccount secret")
return nil, err
}
}

if sr.EnableNamespaceSecrets {
sr.Log.Info("Searching for secret in resource namespace", "namespace", namespace, "name", name)
secretForResource, err = sr.getSecretFromNamespace(ctx, namespace, name)
Expand Down Expand Up @@ -85,3 +95,9 @@ func (sr *SecretResolver) getClusterSecret(ctx context.Context, name string) (*v
err := sr.Client.Get(ctx, types.NamespacedName{Namespace: sr.ReleaseNamespace, Name: name}, secret)
return secret, err
}

func (sr *SecretResolver) getSubaccountSecret(ctx context.Context, namespace, name, saID string) (*v1.Secret, error) {
secret := &v1.Secret{}
err := sr.Client.Get(ctx, types.NamespacedName{Namespace: sr.ReleaseNamespace, Name: fmt.Sprintf("%s-%s-%s", saID, namespace, name)}, secret)
return secret, err
}
4 changes: 2 additions & 2 deletions internal/secrets/resolver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,14 +67,14 @@ var _ = Describe("Secrets Resolver", func() {
}

validateSecretResolved := func() {
resolvedSecret, err := resolver.GetSecretForResource(ctx, testNamespace, secrets.SAPBTPOperatorSecretName)
resolvedSecret, err := resolver.GetSecretForResource(ctx, testNamespace, secrets.SAPBTPOperatorSecretName, "")
Expect(err).ToNot(HaveOccurred())
Expect(resolvedSecret).ToNot(BeNil())
Expect(string(resolvedSecret.Data["clientid"])).To(Equal(expectedClientID))
}

validateSecretNotResolved := func() {
_, err := resolver.GetSecretForResource(ctx, testNamespace, secrets.SAPBTPOperatorSecretName)
_, err := resolver.GetSecretForResource(ctx, testNamespace, secrets.SAPBTPOperatorSecretName, "")
Expect(err).To(HaveOccurred())
Expect(err.Error()).To(ContainSubstring("not found"))
}
Expand Down

0 comments on commit 99cea74

Please sign in to comment.