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

HCP Packer Buckets: Change UpsertBucket to call GetBucket #13059

Merged
merged 2 commits into from
Jun 21, 2024

Conversation

JenGoldstrich
Copy link
Contributor

@JenGoldstrich JenGoldstrich commented Jun 17, 2024

This PR changes the API calls made by the internal function UpsertBucket, previously this function would call CreateBucket and then if that returned a AlreadyExists/Conflict status code it would call UpdateBucket to set the bucket labels and description on the bucket. This lead to failures with bucket level service principals because the principal would be scoped to only access the existing bucket, and so Packer would error out because of making an unauthorized request.

To handle this I have changed UpsertBucket to call GetBucket first, if GetBucket 404s the method then calls Create, if GetBucket succeeds it calls UpdateBucket, and if GetBucket fails with a non 404 status code Packer exits with the error from GetBucket.

@JenGoldstrich JenGoldstrich force-pushed the hpr-1772-upsert-bucket-get-first branch 4 times, most recently from a18699a to 22a8fd2 Compare June 18, 2024 20:12
…evel role based authentication, as CreateBucket uses project level auth
@JenGoldstrich JenGoldstrich force-pushed the hpr-1772-upsert-bucket-get-first branch from 22a8fd2 to 046c8f1 Compare June 18, 2024 20:24
@JenGoldstrich JenGoldstrich changed the title HCP Packer Buckets: Change UpsertBucket to call GetBucket first instead of CreateBucket HCP Packer Buckets: Change UpsertBucket to call GetBucket Jun 18, 2024
@JenGoldstrich JenGoldstrich marked this pull request as ready for review June 18, 2024 21:42
@JenGoldstrich JenGoldstrich requested a review from a team as a code owner June 18, 2024 21:42
Copy link
Contributor

@nywilken nywilken left a comment

Choose a reason for hiding this comment

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

Overall this looks good. I have one question.

_, err := c.Packer.PackerServiceGetBucket(getParams, nil)
if err != nil {
if CheckErrorCode(err, codes.NotFound) {
_, err = c.CreateBucket(ctx, bucketName, bucketDescription, bucketLabels)
Copy link
Contributor

Choose a reason for hiding this comment

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

Question, is the error message that gets displayed to the user if they don't have permission to create a bucket descriptive enough for the user to determine why creation failed?

I don't know that I ever checked the error message and wonder if we need to add a call to action so that a user knows what to do when it fails. You don't need to account for it in this PR but I am curious if there is any changes to the error messaging we need to account for.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I think returning the raw API error is fine as it's what we currently returned, after this change if a user doesn't have access to create a bucket we'll return an error that looks like this

failed to initialize bucket "blah": [PUT
/packer/2023-01-01/organizations/{location.organization_id}/projects/{location.project_id}/buckets][403]
PackerService_CreateBucket default  &{Code:7 Details:[] Message:Not Authorized}

This is the same error that was returned currently where we call CreateBucket first, so I think this will give users enough information to operate on it, I'd wait and see if a user runs into a pain point figuring this out before improving this experience personally as I do think the API error contains the relevant information. I could see a future where we first check what type of principal the user is using and return a more detailed error message though.

@nywilken nywilken added the backport/1.11.x Backport PR changes to `release/1.11.x` label Jun 20, 2024
Copy link

I'm going to lock this pull request because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems related to this change, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Jul 22, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
backport/1.11.x Backport PR changes to `release/1.11.x`
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants