Skip to content

Commit

Permalink
Merge pull request #540 from openshift-cherrypick-robot/cherry-pick-5…
Browse files Browse the repository at this point in the history
…39-to-release-4.11

[release-4.11] OCPBUGS-13792: Determine AWS partition based on region for readOnlyAnonUserPolicyTemplate bucket ARN.
  • Loading branch information
openshift-merge-robot committed May 19, 2023
2 parents bd6c811 + 9623496 commit 7c4c935
Show file tree
Hide file tree
Showing 2 changed files with 39 additions and 2 deletions.
9 changes: 7 additions & 2 deletions pkg/cmd/provisioning/aws/create_identity_provider.go
Expand Up @@ -15,6 +15,7 @@ import (

awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/awserr"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -208,7 +209,7 @@ var (
"s3:GetObject"
],
"Resource": [
"arn:aws:s3:::%s/*"
"arn:%s:s3:::%s/*"
]
}
]
Expand Down Expand Up @@ -657,9 +658,13 @@ func createOIDCEndpoint(client aws.Client, bucketName, name, region, targetDir s
return "", errors.Wrapf(err, "failed to allow public access for the bucket %s", bucketName)
}

partition, found := endpoints.PartitionForRegion([]endpoints.Partition{endpoints.AwsPartition(), endpoints.AwsCnPartition(), endpoints.AwsUsGovPartition()}, region)
if !found {
return "", fmt.Errorf("could not find AWS partition for provided region %s", region)
}
_, err = client.PutBucketPolicy(&s3.PutBucketPolicyInput{
Bucket: awssdk.String(bucketName),
Policy: awssdk.String(fmt.Sprintf(readOnlyAnonUserPolicyTemplate, bucketName)),
Policy: awssdk.String(fmt.Sprintf(readOnlyAnonUserPolicyTemplate, partition.ID(), bucketName)),
})
if err != nil {
return "", errors.Wrapf(err, "failed to apply public access policy to the bucket %s", bucketName)
Expand Down
32 changes: 32 additions & 0 deletions pkg/cmd/provisioning/aws/create_identity_provider_test.go
Expand Up @@ -11,6 +11,7 @@ import (
"testing"

awssdk "github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/aws/endpoints"
"github.com/aws/aws-sdk-go/service/cloudfront"
"github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/s3"
Expand Down Expand Up @@ -342,3 +343,34 @@ func mockPutBucketPolicy(mockAWSClient *mockaws.MockClient) {
mockAWSClient.EXPECT().PutBucketPolicy(gomock.Any()).Return(
&s3.PutBucketPolicyOutput{}, nil).AnyTimes()
}

func TestDeterminePartitionForRegion(t *testing.T) {
tests := []struct {
region string
}{
{
region: "us-east-1",
},
{
region: "us-west-1",
},
{
region: "us-gov-east-1",
},
{
region: "us-gov-west-1",
},
{
region: "cn-north-1",
},
{
region: "cn-northwest-1",
},
}
for _, test := range tests {
t.Run(test.region, func(t *testing.T) {
_, found := endpoints.PartitionForRegion([]endpoints.Partition{endpoints.AwsPartition(), endpoints.AwsCnPartition(), endpoints.AwsUsGovPartition()}, test.region)
require.True(t, found, "expected to find partition for region")
})
}
}

0 comments on commit 7c4c935

Please sign in to comment.