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

controller: bugfix aws oidc secret upsert process #397

Open
wants to merge 1 commit into
base: main
Choose a base branch
from

Conversation

xiaolin593
Copy link

@xiaolin593 xiaolin593 commented Feb 21, 2025

Commit Message

This commit fixes the bug when creating secret for AWS OIDC rotation and streamlines secret upsert operation into Kubernetes.

Detailed explanation:
In aws_oidc_rotator.go [1], existing buggy code snippet:

err = r.client.Create(ctx, secret)
if err != nil {
    if !apierrors.IsAlreadyExists(err) { 
	    return r.client.Update(ctx, secret)
    }
    return fmt.Errorf("failed to create secret: %w", err)
}

The intention of above code is to create secret regardless of its existence, then check if k8s returns AlreadyExist error.

However, k8s returns BadRequest error with message resourceVersion can not be set for Create requests if debug through and inspect err at [2]

Discussed with original commit author and fixed the bug by changing secret upsert process with a control flag to improve readability instead of a quick fix to switch condition from errors.IsAlreadyExists to errors.IsBadRequest.

Related Issues/PRs (if applicable)

Related PR: #306

1:

err = r.client.Create(ctx, secret)
if err != nil {
if !apierrors.IsAlreadyExists(err) {
return r.client.Update(ctx, secret)
}
return fmt.Errorf("failed to create secret: %w", err)
}

2:
if !apierrors.IsAlreadyExists(err) {

Signed-off-by: Xiaolin Lin <xlin158@bloomberg.net>
@xiaolin593 xiaolin593 changed the title bugfix: aws oidc secret creation and update process controller: bugfix aws oidc secret creation and update process Feb 21, 2025
@xiaolin593 xiaolin593 changed the title controller: bugfix aws oidc secret creation and update process controller: bugfix aws oidc secret upsert process Feb 21, 2025
@xiaolin593 xiaolin593 marked this pull request as ready for review February 21, 2025 00:37
@xiaolin593 xiaolin593 requested a review from a team as a code owner February 21, 2025 00:37
@mathetake
Copy link
Member

Can you write a test?

Copy link
Contributor

@aabchoo aabchoo left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you for putting up a fix for this bug! Just minor comments added 🙇


return r.client.Update(ctx, secret)
} else {
r.logger.Info(fmt.Sprintf("creating aws secret %s for backend security policy namespace: %s, name: %s",
Copy link
Contributor

@aabchoo aabchoo Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you change the log to specify new aws credential secret? or however to make it clear that a new secret containing aws credentials will be created

return fmt.Errorf("failed to create secret: %w", err)
}
if secretFound {
r.logger.Info(fmt.Sprintf("updating aws secret %s for backend security policy namespace: %s, name: %s",
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

similar to below

if err != nil {
if !apierrors.IsNotFound(err) {
return err
}

Copy link
Contributor

@aabchoo aabchoo Feb 21, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Consider adding a comment to explain this? (Could also add it on line 133?)

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about rewrite the code to

if err == nil {
updateSecret
} else if apierrors.IsNotFound(err) {
createSecret
}

@@ -130,11 +130,15 @@ func (r *AWSOIDCRotator) Rotate(ctx context.Context, token string) error {
return err
}

secretFound := true
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think the code can be written without using this variable.

if err != nil {
if !apierrors.IsNotFound(err) {
return err
}

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

How about rewrite the code to

if err == nil {
updateSecret
} else if apierrors.IsNotFound(err) {
createSecret
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants