Skip to content

Commit

Permalink
feat: support China region
Browse files Browse the repository at this point in the history
  • Loading branch information
gnought committed Aug 25, 2024
1 parent dcdfeae commit 3563a72
Show file tree
Hide file tree
Showing 6 changed files with 47 additions and 31 deletions.
7 changes: 7 additions & 0 deletions builder/common/helper_funcs.go
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,10 @@ func DestroyAMIs(imageids []*string, ec2conn *ec2.EC2) error {
}
return nil
}

func AwsPartition(isRestricted bool) string {
if isRestricted {
return "aws-cn"
}
return "aws"
}
35 changes: 20 additions & 15 deletions builder/common/step_iam_instance_profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import (
)

const (
AmazonSSMManagedInstanceCorePolicyArn = "arn:aws:iam::aws:policy/AmazonSSMManagedInstanceCore"
AmazonSSMManagedInstanceCorePolicyArnPart = "iam::aws:policy/AmazonSSMManagedInstanceCore"
)

type StepIamInstanceProfile struct {
Expand All @@ -27,6 +27,7 @@ type StepIamInstanceProfile struct {
SkipProfileValidation bool
TemporaryIamInstanceProfilePolicyDocument *PolicyDocument
SSMAgentEnabled bool
IsRestricted bool
createdInstanceProfileName string
createdRoleName string
createdPolicyName string
Expand Down Expand Up @@ -81,18 +82,22 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
}

ui.Sayf("Creating temporary role for this instance: %s", profileName)
trustPolicy := `{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "ec2.amazonaws.com"
},
"Action": "sts:AssumeRole"
}
]
}`
service := "ec2.amazonaws.com"
if s.IsRestricted {
service = "ec2.amazonaws.com.cn"
}
trustPolicy := fmt.Sprintf(`{
"Version": "2012-10-17",
"Statement": [
{
"Effect": "Allow",
"Principal": {
"Service": "%s"
},
"Action": "sts:AssumeRole"
}
]
}`, service)
roleResp, err := iamsvc.CreateRole(&iam.CreateRoleInput{
RoleName: aws.String(profileName),
Description: aws.String("Temporary role for Packer"),
Expand Down Expand Up @@ -136,7 +141,7 @@ func (s *StepIamInstanceProfile) Run(ctx context.Context, state multistep.StateB
s.createdPolicyName = profileName
}
if s.SSMAgentEnabled {
ssmPolicyArn := aws.String(AmazonSSMManagedInstanceCorePolicyArn)
ssmPolicyArn := aws.String(fmt.Sprintf("arn:%s:%s", AwsPartition(s.IsRestricted), AmazonSSMManagedInstanceCorePolicyArnPart))
_, err = iamsvc.AttachRolePolicy(&iam.AttachRolePolicyInput{
PolicyArn: ssmPolicyArn,
RoleName: aws.String(s.createdRoleName),
Expand Down Expand Up @@ -204,7 +209,7 @@ func (s *StepIamInstanceProfile) Cleanup(state multistep.StateBag) {

if s.SSMAgentEnabled {
iamsvc.DetachRolePolicy(&iam.DetachRolePolicyInput{

Check failure on line 211 in builder/common/step_iam_instance_profile.go

View workflow job for this annotation

GitHub Actions / Lint check

Error return value of `iamsvc.DetachRolePolicy` is not checked (errcheck)
PolicyArn: aws.String(AmazonSSMManagedInstanceCorePolicyArn),
PolicyArn: aws.String(fmt.Sprintf("arn:%s:%s", AwsPartition(s.IsRestricted), AmazonSSMManagedInstanceCorePolicyArnPart)),
RoleName: aws.String(s.createdRoleName),
})
}
Expand Down
9 changes: 5 additions & 4 deletions builder/ebs/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -340,10 +340,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Ctx: b.config.ctx,
},
&awscommon.StepIamInstanceProfile{
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
IsRestricted: b.config.IsChinaCloud(),
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
Tags: b.config.RunTags,
Ctx: b.config.ctx,
Expand Down
9 changes: 5 additions & 4 deletions builder/ebssurrogate/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -424,10 +424,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Ctx: b.config.ctx,
},
&awscommon.StepIamInstanceProfile{
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
IsRestricted: b.config.IsChinaCloud(),
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
},
&awscommon.StepCleanupVolumes{
Expand Down
9 changes: 5 additions & 4 deletions builder/ebsvolume/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -312,10 +312,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Ctx: b.config.ctx,
},
&awscommon.StepIamInstanceProfile{
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
IsRestricted: b.config.IsChinaCloud(),
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
},
instanceStep,
Expand Down
9 changes: 5 additions & 4 deletions builder/instance/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -383,10 +383,11 @@ func (b *Builder) Run(ctx context.Context, ui packersdk.Ui, hook packersdk.Hook)
Ctx: b.config.ctx,
},
&awscommon.StepIamInstanceProfile{
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
PollingConfig: b.config.PollingConfig,
IamInstanceProfile: b.config.IamInstanceProfile,
SkipProfileValidation: b.config.SkipProfileValidation,
SSMAgentEnabled: b.config.SSMAgentEnabled(),
IsRestricted: b.config.IsChinaCloud(),
TemporaryIamInstanceProfilePolicyDocument: b.config.TemporaryIamInstanceProfilePolicyDocument,
},
instanceStep,
Expand Down

0 comments on commit 3563a72

Please sign in to comment.