Skip to content

Commit 6b3a742

Browse files
committed
modify ebs encryption logic
1 parent 537c346 commit 6b3a742

File tree

4 files changed

+31
-32
lines changed

4 files changed

+31
-32
lines changed

deploy/Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ RUN yum update -y && \
2828
git \
2929
wget \
3030
openssl \
31-
java-1.8.0-openjdk-devel.x86_64
31+
java-1.8.0-amazon-corretto-devel
3232

3333
COPY --from=docker:27.2.0 /usr/local/bin/docker /usr/local/bin/
3434
COPY --from=download-goployer goployer /usr/local/bin/

pkg/aws/ec2.go

Lines changed: 23 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -485,15 +485,22 @@ func (e EC2Client) MakeLaunchTemplateBlockDeviceMappings(blocks []schemas.BlockD
485485
}
486486

487487
enabledEBSEncrypted := block.Encrypted
488-
kmsKeyArn := e.getKMSKeyArn(block.KmsKeyId)
488+
keyId, err := e.getKmsKeyIdByAlias(block.KmsAlias)
489+
if err != nil {
490+
Logger.Fatalf("Error: %v", err)
491+
}
492+
493+
fmt.Printf("KMS Key ID for alias %s: %s\n", block.KmsAlias, keyId)
489494
LaunchTemplateEbsBlockDevice := &ec2.LaunchTemplateEbsBlockDeviceRequest{}
490495

496+
Logger.Debugf("Encrypted KMS Arn : %s", keyId)
497+
491498
if enabledEBSEncrypted {
492499
LaunchTemplateEbsBlockDevice = &ec2.LaunchTemplateEbsBlockDeviceRequest{
493500
VolumeSize: aws.Int64(bSize),
494501
VolumeType: aws.String(bType),
495502
Encrypted: aws.Bool(enabledEBSEncrypted),
496-
KmsKeyId: aws.String(kmsKeyArn),
503+
KmsKeyId: aws.String(keyId),
497504
}
498505
} else {
499506
LaunchTemplateEbsBlockDevice = &ec2.LaunchTemplateEbsBlockDeviceRequest{
@@ -1296,42 +1303,29 @@ func (e EC2Client) DescribeAMIArchitecture(amiID string) (string, error) {
12961303
return amiArchitecture, nil
12971304
}
12981305

1299-
func (e EC2Client) getKMSKeyArn(kmsKeyId string) string {
1300-
1301-
kmsAlias := kmsKeyId
1306+
func (e EC2Client) getKmsKeyIdByAlias(alias string) (string, error) {
13021307

1303-
if kmsAlias == "" {
1308+
if len(alias) == 0 {
13041309
Logger.Info("Volume Encrypt default KMS Key(aws/ebs)")
1305-
kmsAlias = "alias/aws/ebs"
1306-
} else if !strings.HasPrefix(kmsAlias, "alias") {
1310+
alias = "alias/aws/ebs"
1311+
} else if !strings.HasPrefix(alias, "alias") {
13071312
var sb strings.Builder
13081313
sb.WriteString("alias/")
1309-
sb.WriteString(kmsAlias)
1310-
kmsAlias = sb.String()
1314+
sb.WriteString(alias)
1315+
alias = sb.String()
13111316
}
13121317

1313-
input := &kms.DescribeKeyInput{
1314-
KeyId: aws.String(kmsAlias),
1318+
result, err := e.KMSClient.ListAliases(&kms.ListAliasesInput{})
1319+
if err != nil {
1320+
return "", fmt.Errorf("failed to list aliases, %v", err)
13151321
}
13161322

1317-
result, err := e.KMSClient.DescribeKey(input)
1318-
if err != nil {
1319-
var aerr awserr.Error
1320-
if errors.As(err, &aerr) {
1321-
switch aerr.Code() {
1322-
case kms.ErrCodeNotFoundException:
1323-
Logger.Println(kms.ErrCodeNotFoundException, aerr.Error())
1324-
case kms.ErrCodeInvalidArnException:
1325-
Logger.Println(kms.ErrCodeInvalidArnException, aerr.Error())
1326-
case kms.ErrCodeDependencyTimeoutException:
1327-
Logger.Println(kms.ErrCodeDependencyTimeoutException, aerr.Error())
1328-
case kms.ErrCodeInternalException:
1329-
Logger.Println(kms.ErrCodeInternalException, aerr.Error())
1330-
default:
1331-
Logger.Println(aerr.Error())
1323+
for _, aliasEntry := range result.Aliases {
1324+
if aliasEntry.AliasName != nil && *aliasEntry.AliasName == alias {
1325+
if aliasEntry.TargetKeyId != nil {
1326+
return *aliasEntry.TargetKeyId, nil
13321327
}
13331328
}
1334-
return ""
13351329
}
1336-
return *result.KeyMetadata.Arn
1330+
return "", fmt.Errorf("alias %s not found", alias)
13371331
}

pkg/deployer/deployer.go

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -596,6 +596,8 @@ func (d *Deployer) Deploy(config schemas.Config, region schemas.RegionConfig) er
596596
}
597597

598598
blockDevices := client.EC2Service.MakeLaunchTemplateBlockDeviceMappings(d.Stack.BlockDevices)
599+
d.Logger.Debugf("additional blokcDevice infomation %s", blockDevices[0].Ebs.String())
600+
599601
ebsOptimized := d.Stack.EbsOptimized
600602

601603
// Instance Type Override
@@ -645,7 +647,10 @@ func (d *Deployer) Deploy(config schemas.Config, region schemas.RegionConfig) er
645647
return err
646648
}
647649

648-
subnets := region.SubnetIDs
650+
subnets := make([]string, 0)
651+
if len(region.SubnetIDs) != 0 {
652+
subnets = region.SubnetIDs
653+
}
649654
if len(subnets) == 0 {
650655
Logger.Info("Not Subnet ID Specific")
651656
subnets, err = client.EC2Service.GetSubnets(region.VPC, region.UsePublicSubnets, availabilityZones)

pkg/schemas/config.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -249,7 +249,7 @@ type BlockDevice struct {
249249
Encrypted bool `yaml:"encrypted"`
250250

251251
// KMS key
252-
KmsKeyId string `yaml:"kmsKeyId"`
252+
KmsAlias string `yaml:"kmsAlias"`
253253
}
254254

255255
// Lifecycle Callback configuration

0 commit comments

Comments
 (0)