Skip to content

Commit

Permalink
Add support for the region of China (#156)
Browse files Browse the repository at this point in the history
Signed-off-by: QuentinBisson <[email protected]>
  • Loading branch information
QuentinBisson authored Jun 17, 2024
1 parent d479eef commit 679e5fa
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 18 deletions.
4 changes: 4 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

### Changed

- Add support for the region of China.

## [0.5.5] - 2024-05-13

### Fixed
Expand Down
20 changes: 13 additions & 7 deletions internal/pkg/service/objectstorage/cloud/aws/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,14 @@ func (s IAMAccessRoleServiceAdapter) getRole(ctx context.Context, roleName strin
return output.Role, nil
}

func (s IAMAccessRoleServiceAdapter) irsaDomain() string {
if isChinaRegion(s.cluster.Region) {
return fmt.Sprintf("s3.%s.amazonaws.com.cn/%s-g8s-%s-oidc-pod-identity-v2", s.cluster.Region, s.accountId, s.cluster.GetName())
} else {
return fmt.Sprintf("irsa.%s.%s", s.cluster.Name, s.cluster.GetBaseDomain())
}
}

func (s IAMAccessRoleServiceAdapter) ConfigureRole(ctx context.Context, bucket *v1alpha1.Bucket) error {
roleName := bucket.Spec.AccessRole.RoleName
role, err := s.getRole(ctx, roleName)
Expand Down Expand Up @@ -94,8 +102,8 @@ func (s IAMAccessRoleServiceAdapter) ConfigureRole(ctx context.Context, bucket *
var trustPolicy bytes.Buffer
err = s.trustIdentityPolicy.Execute(&trustPolicy, TrustIdentityPolicyData{
AccountId: s.accountId,
CloudDomain: s.cluster.GetBaseDomain(),
Installation: s.cluster.GetName(),
AWSDomain: awsDomain(s.cluster.Region),
CloudFrontDomain: s.irsaDomain(),
ServiceAccountName: bucket.Spec.AccessRole.ServiceAccountName,
ServiceAccountNamespace: bucket.Spec.AccessRole.ServiceAccountNamespace,
})
Expand Down Expand Up @@ -148,11 +156,9 @@ func (s IAMAccessRoleServiceAdapter) ConfigureRole(ctx context.Context, bucket *

var rolePolicy bytes.Buffer
var data = RolePolicyData{
BucketName: bucket.Spec.Name,
}

if len(bucket.Spec.AccessRole.ExtraBucketNames) > 0 {
data.ExtraBucketNames = bucket.Spec.AccessRole.ExtraBucketNames
AWSDomain: awsDomain(s.cluster.Region),
BucketName: bucket.Spec.Name,
ExtraBucketNames: bucket.Spec.AccessRole.ExtraBucketNames,
}

err = s.rolePolicy.Execute(&rolePolicy, data)
Expand Down
5 changes: 4 additions & 1 deletion internal/pkg/service/objectstorage/cloud/aws/s3.go
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,10 @@ func (s S3ObjectStorageAdapter) setLifecycleRules(ctx context.Context, bucket *v

func (s S3ObjectStorageAdapter) setBucketPolicy(ctx context.Context, bucket *v1alpha1.Bucket) error {
var policy bytes.Buffer
err := s.bucketPolicyTemplate.Execute(&policy, BucketPolicyData{bucket.Spec.Name})
err := s.bucketPolicyTemplate.Execute(&policy, BucketPolicyData{
AWSDomain: awsDomain(s.cluster.Region),
BucketName: bucket.Spec.Name,
})
if err != nil {
return err
}
Expand Down
40 changes: 30 additions & 10 deletions internal/pkg/service/objectstorage/cloud/aws/templates.go
Original file line number Diff line number Diff line change
@@ -1,10 +1,29 @@
package aws

import (
"strings"
)

type RolePolicyData struct {
AWSDomain string
BucketName string
ExtraBucketNames []string
}

func awsDomain(region string) string {
domain := "aws"

if isChinaRegion(region) {
domain = "aws-cn"
}

return domain
}

func isChinaRegion(region string) bool {
return strings.Contains(region, "cn-")
}

const rolePolicy = `{
"Version": "2012-10-17",
"Statement": [
Expand All @@ -18,11 +37,11 @@ const rolePolicy = `{
],
"Resource": [
{{ range .ExtraBucketNames }}
"arn:aws:s3:::{{ . }}",
"arn:aws:s3:::{{ . }}/*",
"arn:{{ .AWSDomain }}:s3:::{{ . }}",
"arn:{{ .AWSDomain }}:s3:::{{ . }}/*",
{{ end }}
"arn:aws:s3:::{{ .BucketName }}",
"arn:aws:s3:::{{ .BucketName }}/*"
"arn:{{ .AWSDomain }}:s3:::{{ .BucketName }}",
"arn:{{ .AWSDomain }}:s3:::{{ .BucketName }}/*"
]
},
{
Expand All @@ -39,8 +58,8 @@ const rolePolicy = `{

type TrustIdentityPolicyData struct {
AccountId string
CloudDomain string
Installation string
AWSDomain string
CloudFrontDomain string
ServiceAccountName string
ServiceAccountNamespace string
}
Expand All @@ -51,19 +70,20 @@ const trustIdentityPolicy = `{
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::{{ .AccountId }}:oidc-provider/irsa.{{ .Installation }}.{{ .CloudDomain }}"
"Federated": "arn:{{ .AWSDomain }}:iam::{{ .AccountId }}:oidc-provider/{{ .CloudFrontDomain }}"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
"StringEquals": {
"irsa.{{ .Installation }}.{{ .CloudDomain }}:sub": "system:serviceaccount:{{ .ServiceAccountNamespace }}:{{ .ServiceAccountName }}"
"{{ .CloudFrontDomain }}:sub": "system:serviceaccount:{{ .ServiceAccountNamespace }}:{{ .ServiceAccountName }}"
}
}
}
]
}`

type BucketPolicyData struct {
AWSDomain string
BucketName string
}

Expand All @@ -76,8 +96,8 @@ const bucketPolicy = `{
"Principal": "*",
"Action": "s3:*",
"Resource": [
"arn:aws:s3:::{{ .BucketName }}",
"arn:aws:s3:::{{ .BucketName }}/*"
"arn:{{ .AWSDomain }}:s3:::{{ .BucketName }}",
"arn:{{ .AWSDomain }}:s3:::{{ .BucketName }}/*"
],
"Condition": {
"Bool": {
Expand Down

0 comments on commit 679e5fa

Please sign in to comment.