Skip to content

fix: custom resource upload should be namespaced by env#3998

Merged
mergify[bot] merged 6 commits intoaws:mainlinefrom
iamhopaul123:acl-fix
Sep 14, 2022
Merged

fix: custom resource upload should be namespaced by env#3998
mergify[bot] merged 6 commits intoaws:mainlinefrom
iamhopaul123:acl-fix

Conversation

@iamhopaul123
Copy link
Copy Markdown
Contributor

@iamhopaul123 iamhopaul123 commented Sep 13, 2022

Fixes #3984

Screen Shot 2022-09-15 at 9 06 01 AM

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

@codecov-commenter
Copy link
Copy Markdown

codecov-commenter commented Sep 13, 2022

Codecov Report

Merging #3998 (a0a7e3b) into mainline (4b45d66) will increase coverage by 0.01%.
The diff coverage is 54.54%.

@@             Coverage Diff              @@
##           mainline    #3998      +/-   ##
============================================
+ Coverage     68.64%   68.65%   +0.01%     
============================================
  Files           248      248              
  Lines         35084    35067      -17     
  Branches        264      264              
============================================
- Hits          24083    24076       -7     
+ Misses         9807     9801       -6     
+ Partials       1194     1190       -4     
Impacted Files Coverage Δ
internal/pkg/aws/s3/s3.go 93.20% <ø> (+6.64%) ⬆️
internal/pkg/cli/deploy/env.go 71.15% <0.00%> (ø)
internal/pkg/cli/deploy/svc.go 47.32% <ø> (+0.14%) ⬆️
...pkg/deploy/upload/customresource/customresource.go 83.51% <66.66%> (ø)
...ternal/pkg/deploy/cloudformation/cloudformation.go 72.47% <0.00%> (-0.55%) ⬇️
internal/pkg/term/selector/selector.go 78.25% <0.00%> (-0.06%) ⬇️
internal/pkg/config/app.go 92.39% <0.00%> (ø)
internal/pkg/template/env.go 45.90% <0.00%> (ø)
internal/pkg/deploy/cloudformation/app.go 74.49% <0.00%> (ø)
internal/pkg/cli/env_init.go 64.89% <0.00%> (+0.05%) ⬆️
... and 2 more

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

Comment thread internal/pkg/aws/s3/s3.go
// ZipAndUpload zips all files and uploads the zipped file to an S3 bucket under the specified key.
// Per s3's recommendation https://docs.aws.amazon.com/AmazonS3/latest/userguide/about-object-ownership.html:
// The bucket owner, in addition to the object owner, is granted full control.
func (s *S3) ZipAndUpload(bucket, key string, files ...NamedBinary) (string, error) {
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.

yay! thank you for deleting dead code


templateFS: template.New(),
s3: s3.New(envRegionSession),
s3: s3.New(envManagerSession),
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 actually don't get why this issue is happening if the object is uploaded with bucket ownership:

ACL: aws.String(s3.ObjectCannedACLBucketOwnerFullControl),

and the bucket allows all actions from the env accounts:

- s3:*
Effect: Allow
Resource:
- !Sub arn:${AWS::Partition}:s3:::${PipelineBuiltArtifactBucket}
- !Sub arn:${AWS::Partition}:s3:::${PipelineBuiltArtifactBucket}/*
Principal:
AWS:
- !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:root{{range $accounts}}
- !Sub arn:${AWS::Partition}:iam::{{.}}:root{{end}}

Then why is there an access denied error? What am I missing? I'd have guessed that as long as the objects are uploaded with BucketOwnership then it wouldn't matter if they're overriden.

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.

thanks for fixing the session here!

@efekarakus
Copy link
Copy Markdown
Contributor

efekarakus commented Sep 14, 2022

OK I finally get what is going on

The buckets are created by default with BucketOwnership set to ObjectWriter.

Everything gets fixed if we change the BucketOwnership to BucketOwnerEnforced or BucketOwnerPreferred in the app template (we don't have to touch any of our s3.go code).
In these scenarios, the access control to the data becomes based on the BucketPolicy instead of these confusing ACLs:

Statement:
-
Action:
- s3:*
Effect: Allow
Resource:
- !Sub arn:${AWS::Partition}:s3:::${PipelineBuiltArtifactBucket}
- !Sub arn:${AWS::Partition}:s3:::${PipelineBuiltArtifactBucket}/*
Principal:
AWS:
- !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:root{{range $accounts}}
- !Sub arn:${AWS::Partition}:iam::{{.}}:root{{end}}

My recommendation is instead of namespacing the custom resources, we should change the control ownership to be BucketOwnerEnforced and force the users that need the fix to run copilot app upgrade.

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.

Should we update the TemplateVersion: 'v1.1.0' field in the app template and deploy.LatestAppTemplateVersion to v1.2.0 ?

f, err := w.Create(file.name)
if err != nil {
return fmt.Errorf("create zip file %q for custom resource %q: %v", file.name, cr.FunctionName(), err)
return fmt.Errorf("create zip file %q for custom resource %q: %v", file.name, cr.name, err)
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: can we use .Name() instead to rely on the public interface of the type rather than the internal data structure

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.

Should we update the TemplateVersion: 'v1.1.0' field in the app template and deploy.LatestAppTemplateVersion to v1.2.0 ?

No we don't because it is not released yet.

Comment thread internal/pkg/aws/s3/s3.go
}
}

// ZipAndUpload zips all files and uploads the zipped file to an S3 bucket under the specified 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.

:rip


templateFS: template.New(),
s3: s3.New(envRegionSession),
s3: s3.New(envManagerSession),
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.

thanks for fixing the session here!

if err != nil {
return nil, fmt.Errorf("create default: %w", err)
}
if err != nil {
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.

:doge

@mergify mergify Bot merged commit 40f2514 into aws:mainline Sep 14, 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.

Pipeline for service fails if service is not already deployed to environment.

5 participants