Skip to content

fix: grant the bucket owner control to addon template artifacts#3485

Merged
mergify[bot] merged 16 commits intoaws:mainlinefrom
Lou1415926:fix/addons/s3-permission
Apr 29, 2022
Merged

fix: grant the bucket owner control to addon template artifacts#3485
mergify[bot] merged 16 commits intoaws:mainlinefrom
Lou1415926:fix/addons/s3-permission

Conversation

@Lou1415926
Copy link
Copy Markdown
Contributor

@Lou1415926 Lou1415926 commented Apr 20, 2022

Fix #3453 by:

  1. Grant bucket owner (which is the app account owner) full control, in addition to the object owner (the account that uploads the artifact)
  2. Use hash for addon template artifacts
  3. Instead of uploading addons to the root dir "stackset/svc.addons.stack.yml", upload it to "stackset/manual/addons/svc/sha.yml"
  4. Instead of uploading env files to "manual/sha/mock.env.file", upload it to "manual/env-files/env.file/sha.env"

By submitting this pull request, I confirm that you can use, modify, copy, and redistribute this contribution, under the Apache 2.0 License.

@Lou1415926 Lou1415926 requested a review from a team as a code owner April 20, 2022 19:06
@Lou1415926 Lou1415926 requested review from iamhopaul123 and removed request for a team April 20, 2022 19:06
@efekarakus efekarakus self-requested a review April 20, 2022 19:13
Copy link
Copy Markdown
Contributor

@efekarakus efekarakus 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 Wanxian!!

Comment thread internal/pkg/aws/s3/s3.go Outdated
Comment thread internal/pkg/aws/s3/s3.go Outdated
Comment on lines +96 to +104
// AddonsArtifactPath prefixes the key with the addons artifact path.
func AddonsArtifactPath(key string) string {
return path.Join(artifactDirName, addonDirName, key)
}

// TemplateArtifactPath prefixes the key with the template artifact path.
func TemplateArtifactPath(key string) string {
return path.Join(artifactDirName, templateDirName, key)
}
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

These don't seem to fit the s3 package 💭
What do you think of moving them to deploy instead?

Copy link
Copy Markdown
Contributor Author

@Lou1415926 Lou1415926 Apr 21, 2022

Choose a reason for hiding this comment

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

I moved them to the template/ package. My motivation was that it might be easier for us to manage/visualize from the codebase what our artifact folder structure would be, if these path definition is put together in a single package.

In the current codebase that are scattered in cli/deploy, deploy and s3.

let me know what ya think!

reader := strings.NewReader(template)
url, err := cf.s3Client.Upload(bucket, fmt.Sprintf(fmtWorkloadCFNTemplateName, config.StackName(), sha256.Sum256([]byte(template))), reader)
artifactName := path.Join(config.StackName(), fmt.Sprintf("%x.yml", sha256.Sum256([]byte(template))))
url, err := cf.s3Client.Upload(bucket, s3.TemplateArtifactPath(artifactName), reader)
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

What do you think of a path like:

templates/<stack name>/<sha>.yml

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Currently it's manual/templates/stackname/sha.yml what do ya think? The new code here doesn't change the path it's just some refactoring!

Comment thread internal/pkg/cli/deploy/deploy.go Outdated
reader := strings.NewReader(template)
url, err := in.uploader.Upload(d.resources.S3Bucket, fmt.Sprintf(deploy.AddonsCfnTemplateNameFormat, d.name), reader)
reader := strings.NewReader(tmpl)
artifactName := path.Join(d.name, fmt.Sprintf("%x.yml", sha256.Sum256([]byte(tmpl))))
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we should move sha256.Sum256 to AddonsArtifactPath. Same for TemplateArtifactPath because it seems like whenever we use them we always call sha256.Sum256

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I would prefer the key being determined by the client but I'm fine either way

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

addressed

Copy link
Copy Markdown
Contributor

@iamhopaul123 iamhopaul123 Apr 21, 2022

Choose a reason for hiding this comment

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

But do we have an assumption that all the addons artifacts should do sha256.Sum256? Same for the env file and template uploads. If so maybe we would want

// Env file upload
url, err := in.uploader.Upload(d.resources.S3Bucket, template.EnvFileArtifactPath(envFilePath, content), reader)

// Addons upload
url, err := in.uploader.Upload(d.resources.S3Bucket, template.AddonsArtifactPath(d.name, content), reader)

// Template upload
url, err := cf.s3Client.Upload(bucket, template.TemplateArtifactPath(config.StackName(), content), reader)

it's ok if you don't want to change. Not a big one tho.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I agree that we should most likely always upload with sha256 :) this way we can eliminate this bug altogether for future code changes

Comment thread internal/pkg/deploy/env.go
Comment thread internal/pkg/aws/s3/s3.go
Bucket: aws.String(bucket),
Key: aws.String(key),
})
ACL: aws.String(s3.ObjectCannedACLBucketOwnerFullControl),
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: is gofmt or a similar formatter running?

Comment thread internal/pkg/template/template.go Outdated
Comment on lines +103 to +104
// AddonsArtifactPath prefixes the key with the addons artifact path.
func AddonsArtifactPath(key string) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I wonder if we should have a new package for all of these? so that it reads a bit better template.MkdirSHA256 is not obvious that it's for s3.

Let me know what you think of this layout:

// Package artifactpath holds functions to generate the S3 object path for artifacts.
package template/artifactpath

// Addons returns the path to store addon artifact files.
func Addons(key string) string

// CFNTemplate returns the path to store cloudformation templates.
func CFNTemplate(key string) string

// SHA256Func returns a function that will namespace a key with the sha256 hash of its content.
func SHA256Func(content []byte) func(string) string  {
    return func(key string) string {
        return path.Join(fmt.Sprintf("%x", sha256.Sum256(content)), key)
    }
}

// Chain applies the path functions sequentially to nest the key.
func Chain(key string, fn ...func(string) string) string 

This would be the client's experience:

artifactpath.Chain(key, artifactpath.SHA256Func(content), artifactpath.CFNTemplate)

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Since we always want to add SHA256, I've inlined it into the function -> chaining was rendered unnecessary when SHA256 is inlined. Let me know what you think!

@iamhopaul123 iamhopaul123 added the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Apr 21, 2022
@Lou1415926 Lou1415926 force-pushed the fix/addons/s3-permission branch from d83668f to 4429ad0 Compare April 21, 2022 22:05
@Lou1415926 Lou1415926 force-pushed the fix/addons/s3-permission branch from 4429ad0 to a57db70 Compare April 21, 2022 22:10

// Addons returns the path to store addon artifact files.
// Example: manual/addons/key/sha.yml.
func AddonsWithSHA256(key string, content []byte) string {
Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

nit: It should be fine to drop the WithSHA256 suffix now that we do it for everything

@Lou1415926 Lou1415926 removed the do-not-merge Pull requests that mergify shouldn't merge until the requester allows it. label Apr 29, 2022
@Lou1415926 Lou1415926 changed the title fix: grant the bucket owner control to addon template artifact fix: grant the bucket owner control to addon template artifacts Apr 29, 2022
@mergify mergify Bot merged commit 0cfb5c3 into aws:mainline Apr 29, 2022
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.

Cloudformation addon templates fail with S3 error: Access Denied after updating to 1.16.0

4 participants