Skip to content

Commit

Permalink
capa-china (#265)
Browse files Browse the repository at this point in the history
* capa-china
  • Loading branch information
calvix authored Mar 7, 2024
1 parent 5adaf69 commit 75011a9
Show file tree
Hide file tree
Showing 15 changed files with 70 additions and 53 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

### Changed

- Create a IAM client with specific Region in order to work with AWS China partition.
- Adjust all IAM policies to include all AWS partitions.
- Change inline policy document attach logic to recreate it if it's already attached to the role.

## [0.16.0] - 2024-02-28
Expand Down
2 changes: 1 addition & 1 deletion controllers/awsmachinepool_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ import (
type AWSMachinePoolReconciler struct {
client.Client
Log logr.Logger
IAMClientFactory func(awsclientgo.ConfigProvider) iamiface.IAMAPI
IAMClientFactory func(awsclientgo.ConfigProvider, string) iamiface.IAMAPI
AWSClient awsclient.AwsClientInterface
}

Expand Down
2 changes: 1 addition & 1 deletion controllers/awsmachinepool_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ var _ = Describe("AWSMachinePoolReconciler", func() {
Client: k8sClient,
Log: ctrl.Log,
AWSClient: mockAwsClient,
IAMClientFactory: func(session awsclientupstream.ConfigProvider) iamiface.IAMAPI {
IAMClientFactory: func(session awsclientupstream.ConfigProvider, region string) iamiface.IAMAPI {
return mockIAMClient
},
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/awsmachinetemplate_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ type AWSMachineTemplateReconciler struct {
EnableRoute53Role bool
Log logr.Logger
AWSClient awsclient.AwsClientInterface
IAMClientFactory func(awsclientgo.ConfigProvider) iamiface.IAMAPI
IAMClientFactory func(awsclientgo.ConfigProvider, string) iamiface.IAMAPI
}

// +kubebuilder:rbac:groups=infrastructure.cluster.x-k8s.io,resources=awsmachinetemplates,verbs=get;list;watch;create;update;patch;delete
Expand Down
2 changes: 1 addition & 1 deletion controllers/awsmachinetemplate_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,7 @@ var _ = Describe("AWSMachineTemplateReconciler", func() {
EnableRoute53Role: true,
Log: ctrl.Log,
AWSClient: mockAwsClient,
IAMClientFactory: func(session awsclientupstream.ConfigProvider) iamiface.IAMAPI {
IAMClientFactory: func(session awsclientupstream.ConfigProvider, region string) iamiface.IAMAPI {
return mockIAMClient
},
}
Expand Down
2 changes: 1 addition & 1 deletion controllers/awsmanagedcontrolplane_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ type AWSManagedControlPlaneReconciler struct {
client.Client
Log logr.Logger
AWSClient awsclient.AwsClientInterface
IAMClientFactory func(awsclientgo.ConfigProvider) iamiface.IAMAPI
IAMClientFactory func(awsclientgo.ConfigProvider, string) iamiface.IAMAPI
}

func (r *AWSManagedControlPlaneReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ctrl.Result, error) {
Expand Down
40 changes: 20 additions & 20 deletions controllers/common_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,15 +37,15 @@ var certManagerRoleInfo = RoleInfo{
{
"Effect": "Allow",
"Action": "route53:GetChange",
"Resource": "arn:aws:route53:::change/*"
"Resource": "arn:*:route53:::change/*"
},
{
"Effect": "Allow",
"Action": [
"route53:ChangeResourceRecordSets",
"route53:ListResourceRecordSets"
],
"Resource": "arn:aws:route53:::hostedzone/*"
"Resource": "arn:*:route53:::hostedzone/*"
},
{
"Effect": "Allow",
Expand Down Expand Up @@ -88,7 +88,7 @@ var externalDnsRoleInfo = RoleInfo{
{
"Action": "route53:ChangeResourceRecordSets",
"Resource": [
"arn:aws:route53:::hostedzone/*"
"arn:*:route53:::hostedzone/*"
],
"Effect": "Allow"
},
Expand Down Expand Up @@ -217,7 +217,7 @@ var ALBControllerRoleInfo = RoleInfo{
"Action": [
"ec2:CreateTags"
],
"Resource": "arn:aws:ec2:*:*:security-group/*",
"Resource": "arn:*:ec2:*:*:security-group/*",
"Condition": {
"StringEquals": {
"ec2:CreateAction": "CreateSecurityGroup"
Expand All @@ -233,7 +233,7 @@ var ALBControllerRoleInfo = RoleInfo{
"ec2:CreateTags",
"ec2:DeleteTags"
],
"Resource": "arn:aws:ec2:*:*:security-group/*",
"Resource": "arn:*:ec2:*:*:security-group/*",
"Condition": {
"Null": {
"aws:RequestTag/elbv2.k8s.aws/cluster": "true",
Expand Down Expand Up @@ -285,9 +285,9 @@ var ALBControllerRoleInfo = RoleInfo{
"elasticloadbalancing:RemoveTags"
],
"Resource": [
"arn:aws:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/app/*/*"
"arn:*:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/app/*/*"
],
"Condition": {
"Null": {
Expand All @@ -303,10 +303,10 @@ var ALBControllerRoleInfo = RoleInfo{
"elasticloadbalancing:RemoveTags"
],
"Resource": [
"arn:aws:elasticloadbalancing:*:*:listener/net/*/*/*",
"arn:aws:elasticloadbalancing:*:*:listener/app/*/*/*",
"arn:aws:elasticloadbalancing:*:*:listener-rule/net/*/*/*",
"arn:aws:elasticloadbalancing:*:*:listener-rule/app/*/*/*"
"arn:*:elasticloadbalancing:*:*:listener/net/*/*/*",
"arn:*:elasticloadbalancing:*:*:listener/app/*/*/*",
"arn:*:elasticloadbalancing:*:*:listener-rule/net/*/*/*",
"arn:*:elasticloadbalancing:*:*:listener-rule/app/*/*/*"
]
},
{
Expand Down Expand Up @@ -334,9 +334,9 @@ var ALBControllerRoleInfo = RoleInfo{
"elasticloadbalancing:AddTags"
],
"Resource": [
"arn:aws:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/app/*/*"
"arn:*:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/app/*/*"
],
"Condition": {
"StringEquals": {
Expand All @@ -356,7 +356,7 @@ var ALBControllerRoleInfo = RoleInfo{
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:DeregisterTargets"
],
"Resource": "arn:aws:elasticloadbalancing:*:*:targetgroup/*/*"
"Resource": "arn:*:elasticloadbalancing:*:*:targetgroup/*/*"
},
{
"Effect": "Allow",
Expand Down Expand Up @@ -423,8 +423,8 @@ var ebsCsiDriverRoleInfo = RoleInfo{
"ec2:CreateTags"
],
"Resource": [
"arn:aws:ec2:*:*:volume/*",
"arn:aws:ec2:*:*:snapshot/*"
"arn:*:ec2:*:*:volume/*",
"arn:*:ec2:*:*:snapshot/*"
],
"Condition": {
"StringEquals": {
Expand All @@ -441,8 +441,8 @@ var ebsCsiDriverRoleInfo = RoleInfo{
"ec2:DeleteTags"
],
"Resource": [
"arn:aws:ec2:*:*:volume/*",
"arn:aws:ec2:*:*:snapshot/*"
"arn:*:ec2:*:*:volume/*",
"arn:*:ec2:*:*:snapshot/*"
]
},
{
Expand Down
5 changes: 3 additions & 2 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@ import (
// Import all Kubernetes client auth plugins (e.g. Azure, GCP, OIDC, etc.)
// to ensure that exec-entrypoint and run can make use of them.

"github.com/aws/aws-sdk-go/aws"
awsclientgo "github.com/aws/aws-sdk-go/aws/client"
awsiam "github.com/aws/aws-sdk-go/service/iam"
"github.com/aws/aws-sdk-go/service/iam/iamiface"
Expand Down Expand Up @@ -107,8 +108,8 @@ func main() {
os.Exit(1)
}

iamClientFactory := func(session awsclientgo.ConfigProvider) iamiface.IAMAPI {
return awsiam.New(session)
iamClientFactory := func(session awsclientgo.ConfigProvider, region string) iamiface.IAMAPI {
return awsiam.New(session, &aws.Config{Region: aws.String(region)})
}

if err = (&controllers.AWSMachineTemplateReconciler{
Expand Down
26 changes: 13 additions & 13 deletions pkg/iam/alb_controller_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -87,7 +87,7 @@ const ALBControllerPolicyTemplate = `{
"Action": [
"ec2:CreateTags"
],
"Resource": "arn:aws:ec2:*:*:security-group/*",
"Resource": "arn:*:ec2:*:*:security-group/*",
"Condition": {
"StringEquals": {
"ec2:CreateAction": "CreateSecurityGroup"
Expand All @@ -103,7 +103,7 @@ const ALBControllerPolicyTemplate = `{
"ec2:CreateTags",
"ec2:DeleteTags"
],
"Resource": "arn:aws:ec2:*:*:security-group/*",
"Resource": "arn:*:ec2:*:*:security-group/*",
"Condition": {
"Null": {
"aws:RequestTag/elbv2.k8s.aws/cluster": "true",
Expand Down Expand Up @@ -155,9 +155,9 @@ const ALBControllerPolicyTemplate = `{
"elasticloadbalancing:RemoveTags"
],
"Resource": [
"arn:aws:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/app/*/*"
"arn:*:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/app/*/*"
],
"Condition": {
"Null": {
Expand All @@ -173,10 +173,10 @@ const ALBControllerPolicyTemplate = `{
"elasticloadbalancing:RemoveTags"
],
"Resource": [
"arn:aws:elasticloadbalancing:*:*:listener/net/*/*/*",
"arn:aws:elasticloadbalancing:*:*:listener/app/*/*/*",
"arn:aws:elasticloadbalancing:*:*:listener-rule/net/*/*/*",
"arn:aws:elasticloadbalancing:*:*:listener-rule/app/*/*/*"
"arn:*:elasticloadbalancing:*:*:listener/net/*/*/*",
"arn:*:elasticloadbalancing:*:*:listener/app/*/*/*",
"arn:*:elasticloadbalancing:*:*:listener-rule/net/*/*/*",
"arn:*:elasticloadbalancing:*:*:listener-rule/app/*/*/*"
]
},
{
Expand Down Expand Up @@ -204,9 +204,9 @@ const ALBControllerPolicyTemplate = `{
"elasticloadbalancing:AddTags"
],
"Resource": [
"arn:aws:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:aws:elasticloadbalancing:*:*:loadbalancer/app/*/*"
"arn:*:elasticloadbalancing:*:*:targetgroup/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/net/*/*",
"arn:*:elasticloadbalancing:*:*:loadbalancer/app/*/*"
],
"Condition": {
"StringEquals": {
Expand All @@ -226,7 +226,7 @@ const ALBControllerPolicyTemplate = `{
"elasticloadbalancing:RegisterTargets",
"elasticloadbalancing:DeregisterTargets"
],
"Resource": "arn:aws:elasticloadbalancing:*:*:targetgroup/*/*"
"Resource": "arn:*:elasticloadbalancing:*:*:targetgroup/*/*"
},
{
"Effect": "Allow",
Expand Down
2 changes: 1 addition & 1 deletion pkg/iam/bastion_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const bastionPolicyTemplate = `{
"s3:GetObjectVersion"
],
"Resource": [
"arn:aws:s3:::*-capa-*"
"arn:*:s3:::*-capa-*"
],
"Effect": "Allow"
}
Expand Down
8 changes: 4 additions & 4 deletions pkg/iam/ebs_csi_driver_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ const EBSCSIDriverPolicyTemplate = `{
"ec2:CreateTags"
],
"Resource": [
"arn:aws:ec2:*:*:volume/*",
"arn:aws:ec2:*:*:snapshot/*"
"arn:*:ec2:*:*:volume/*",
"arn:*:ec2:*:*:snapshot/*"
],
"Condition": {
"StringEquals": {
Expand All @@ -43,8 +43,8 @@ const EBSCSIDriverPolicyTemplate = `{
"ec2:DeleteTags"
],
"Resource": [
"arn:aws:ec2:*:*:volume/*",
"arn:aws:ec2:*:*:snapshot/*"
"arn:*:ec2:*:*:volume/*",
"arn:*:ec2:*:*:snapshot/*"
]
},
{
Expand Down
8 changes: 6 additions & 2 deletions pkg/iam/iam.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type IAMServiceConfig struct {
PrincipalRoleARN string
CustomTags map[string]string

IAMClientFactory func(awsclientgo.ConfigProvider) iamiface.IAMAPI
IAMClientFactory func(awsclientgo.ConfigProvider, string) iamiface.IAMAPI
}

type IAMService struct {
Expand All @@ -61,6 +61,7 @@ type IAMService struct {
}

type Route53RoleParams struct {
AWSDomain string
EC2ServiceDomain string
AccountID string
CloudFrontDomain string
Expand All @@ -86,7 +87,7 @@ func New(config IAMServiceConfig) (*IAMService, error) {
if !(config.RoleType == ControlPlaneRole || config.RoleType == NodesRole || config.RoleType == BastionRole || config.RoleType == IRSARole) {
return nil, fmt.Errorf("cannot create IAMService with invalid RoleType '%s'", config.RoleType)
}
iamClient := config.IAMClientFactory(config.AWSSession)
iamClient := config.IAMClientFactory(config.AWSSession, config.Region)
eksClient := eks.New(config.AWSSession, &aws.Config{Region: aws.String(config.Region)})

l := config.Log.WithValues("clusterName", config.ClusterName, "iam-role", config.RoleType)
Expand Down Expand Up @@ -144,9 +145,11 @@ func (s *IAMService) ReconcileKiamRole() error {
}

params := struct {
AWSDomain string
ControlPlaneRoleARN string
EC2ServiceDomain string
}{
AWSDomain: awsDomain(s.region),
ControlPlaneRoleARN: controlPlaneRoleARN,
EC2ServiceDomain: ec2ServiceDomain(s.region),
}
Expand Down Expand Up @@ -190,6 +193,7 @@ func (s *IAMService) generateRoute53RoleParams(roleTypeToReconcile string, awsAc
}

params := Route53RoleParams{
AWSDomain: awsDomain(s.region),
EC2ServiceDomain: ec2ServiceDomain(s.region),
AccountID: awsAccountID,
CloudFrontDomain: cloudFrontDomain,
Expand Down
2 changes: 1 addition & 1 deletion pkg/iam/iam_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ var _ = Describe("ReconcileRole", func() {
PrincipalRoleARN: "test-principal-role-arn",
Log: ctrl.Log,
AWSSession: sess,
IAMClientFactory: func(session awsclientgo.ConfigProvider) iamiface.IAMAPI {
IAMClientFactory: func(session awsclientgo.ConfigProvider, region string) iamiface.IAMAPI {
return mockIAMClient
},
}
Expand Down
10 changes: 5 additions & 5 deletions pkg/iam/route53_template.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ const trustIdentityPolicyIRSA = `{
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::{{.AccountID}}:oidc-provider/{{.CloudFrontDomain}}"
"Federated": "arn:{{.AWSDomain}}:iam::{{.AccountID}}:oidc-provider/{{.CloudFrontDomain}}"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
Expand All @@ -18,7 +18,7 @@ const trustIdentityPolicyIRSA = `{
{
"Effect": "Allow",
"Principal": {
"Federated": "arn:aws:iam::{{.AccountID}}:oidc-provider/{{.AdditionalCloudFrontDomain}}"
"Federated": "arn:{{.AWSDomain}}:iam::{{.AccountID}}:oidc-provider/{{.AdditionalCloudFrontDomain}}"
},
"Action": "sts:AssumeRoleWithWebIdentity",
"Condition": {
Expand All @@ -38,7 +38,7 @@ const route53RolePolicyTemplate = `{
{
"Action": "route53:ChangeResourceRecordSets",
"Resource": [
"arn:aws:route53:::hostedzone/*"
"arn:*:route53:::hostedzone/*"
],
"Effect": "Allow"
},
Expand All @@ -60,15 +60,15 @@ const route53RolePolicyTemplateForCertManager = `{
{
"Effect": "Allow",
"Action": "route53:GetChange",
"Resource": "arn:aws:route53:::change/*"
"Resource": "arn:*:route53:::change/*"
},
{
"Effect": "Allow",
"Action": [
"route53:ChangeResourceRecordSets",
"route53:ListResourceRecordSets"
],
"Resource": "arn:aws:route53:::hostedzone/*"
"Resource": "arn:*:route53:::hostedzone/*"
},
{
"Effect": "Allow",
Expand Down
Loading

0 comments on commit 75011a9

Please sign in to comment.