Skip to content

Commit

Permalink
fix: resolve SecretRef and populate mgtCtx Auth before any validation
Browse files Browse the repository at this point in the history
  • Loading branch information
kamiiiel committed Nov 15, 2024
1 parent 206935d commit 8665e92
Show file tree
Hide file tree
Showing 4 changed files with 19 additions and 17 deletions.
8 changes: 1 addition & 7 deletions internal/admission/mctx/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -94,13 +94,7 @@ func validateSecretRef(ctx context.Context, context core.ContextObject) *errors.
}

func validateContextIsAvailable(ctx context.Context, context core.ContextObject) *errors.AdmissionError {
var apimClient *apim.APIM
var err error
if context.HasSecretRef() || (context.HasCloud() && context.GetCloud().HasSecretRef()) {
apimClient, err = apim.FromContextRef(ctx, context.GetRef(), context.GetNamespace())
} else {
apimClient, err = apim.FromContext(ctx, context, context.GetNamespace())
}
apimClient, err := apim.FromContext(ctx, context, context.GetNamespace())

if err != nil {
return errors.NewSevere(err.Error())
Expand Down
4 changes: 4 additions & 0 deletions internal/apim/apim.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,10 @@ func FromContext(ctx context.Context, context core.ContextModel, parentNs string
return nil, err
}

if _, err = dynamic.InjectSecretIfAny(ctx, context, parentNs); err != nil {
return nil, err
}

client := &client.Client{
HTTP: http.NewClient(ctx, toHttpAuth(context)),
URLs: urls,
Expand Down
23 changes: 13 additions & 10 deletions internal/k8s/dynamic/mctx.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,19 +36,19 @@ func ResolveContext(ctx context.Context, ref core.ObjectRef, parentNs string) (*
return nil, err
}

return injectSecretIfAny(ctx, context, parentNs)
return context, err
}

func injectSecretIfAny(ctx context.Context, mCtx *management.Context, parentNs string) (*management.Context, error) {
if mCtx.HasSecretRef() || (mCtx.HasCloud() && mCtx.Cloud.HasSecretRef()) { //nolint:nestif // normal complexity
var ref *refs.NamespacedName
func InjectSecretIfAny(ctx context.Context, mCtx core.ContextModel, parentNs string) (*core.ContextModel, error) {
if mCtx.HasSecretRef() || (mCtx.HasCloud() && mCtx.GetCloud().HasSecretRef()) { //nolint:nestif // normal complexity
var name string
if mCtx.HasSecretRef() {
ref = mCtx.SecretRef()
name = mCtx.GetSecretRef().GetName()
} else {
ref = mCtx.Cloud.SecretRef
name = mCtx.GetCloud().GetSecretRef().GetName()
}

secret, err := ResolveSecret(ctx, ref, parentNs)
secret, err := ResolveSecret(ctx, &refs.NamespacedName{Name: name, Namespace: parentNs}, parentNs)
if err != nil {
return nil, err
}
Expand All @@ -61,8 +61,11 @@ func injectSecretIfAny(ctx context.Context, mCtx *management.Context, parentNs s
username := string(secret.Data[core.UsernameSecretKey])
password := string(secret.Data[core.PasswordSecretKey])

mCtx.SetToken(bearerToken)
mCtx.SetCredentials(username, password)
if mCtx.GetAuth() != nil {
mCtx.GetAuth().SetToken(bearerToken)
mCtx.GetAuth().SetCredentials(username, password)
}
}
return mCtx, nil

return &mCtx, nil
}
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@ var _ = Describe("Validate create", labels.WithContext, func() {
fixtures := fixture.Builder().
WithContext(constants.ContextWithSecretFile).
Build()
fixtures.Context.Spec.SecretRef().Name = "unknown-secret"

Consistently(func() error {
_, err := admissionCtrl.ValidateCreate(ctx, fixtures.Context)
Expand Down

0 comments on commit 8665e92

Please sign in to comment.