@@ -239,9 +239,8 @@ export class Easy_EKS{ //purposefully don't extend stack, to implement builder p
239239 // this.config.kmsKeyAlias, {description: "Easy EKS generated kms key, used to encrypt etcd and ebs-csi-driver provisioned volumes"}
240240 // ));}
241241 // else { eksBlueprint.resourceProvider(blueprints.GlobalResources.KmsKey, new blueprints.LookupKmsKeyProvider(this.config.kmsKeyAlias)); }
242- ensure_existance_of_aliased_kms_key ( this . config . kmsKeyAlias ) ;
243- const kms_key = kms . Key . fromLookup ( this . stack , 'pre-existing-kms-key' , { aliasName : this . config . kmsKeyAlias } ) ;
244-
242+ ensure_existance_of_aliased_kms_key ( this . config . kmsKeyAlias , this . stack . stackName , this . stack . region ) ;
243+ const kms_key = this . config . kmsKey ;
245244 this . cluster = new eks . Cluster ( this . stack , this . config . id , {
246245 clusterName : this . config . id ,
247246 version : this . config . kubernetesVersion ,
@@ -632,7 +631,7 @@ const enhanced_viewer_cr = {
632631}
633632///////////////////////////////////////////////////////////////////////////////////////////////////
634633
635- function ensure_existance_of_aliased_kms_key ( kmsKeyAlias : string ) {
634+ function ensure_existance_of_aliased_kms_key ( kmsKeyAlias : string , stackName : string , region : string ) {
636635 /*UX Improvement: By default EKS Blueprint will make new KMS key everytime you make a cluster.
637636 This logic checks for pre-existing keys, and prefers to reuse them. Else create if needed, reuse next time.
638637 The intent is to achieve the following EasyEKS default: (which is overrideable):
@@ -641,15 +640,69 @@ function ensure_existance_of_aliased_kms_key(kmsKeyAlias: string){
641640 * prod envs share a kms key: "alias/eks/prod"
642641 */
643642 let kms_key :kms . Key ;
644- const cmd = `aws kms list-aliases | jq '.Aliases[] | select(.AliasName == "${ kmsKeyAlias } ") | .TargetKeyId'`
643+ const cmd = `aws kms list-aliases --region ${ region } | jq '.Aliases[] | select(.AliasName == "${ kmsKeyAlias } ") | .TargetKeyId'`
645644 const cmd_results = execSync ( cmd ) . toString ( ) ;
645+ let key_id = "" ;
646646 if ( cmd_results === "" ) { //if alias not found, then make a kms key with the alias
647- const create_key_cmd = `aws kms create-key --description="Easy EKS generated kms key, used to encrypt etcd and ebs-csi-driver provisioned volumes"`
647+ const create_key_cmd = `aws kms create-key --region ${ region } -- description="Easy EKS generated kms key, used to encrypt etcd and ebs-csi-driver provisioned volumes"`
648648 const results = JSON . parse ( execSync ( create_key_cmd ) . toString ( ) ) ;
649- const key_id = results . KeyMetadata . KeyId ;
650- const add_alias_cmd = `aws kms create-alias --alias-name ${ kmsKeyAlias } --target-key-id ${ key_id } ` ;
649+ key_id = results . KeyMetadata . KeyId ;
650+ const add_alias_cmd = `aws kms create-alias --alias-name ${ kmsKeyAlias } --target-key-id ${ key_id } --region ${ region } ` ;
651651 execSync ( add_alias_cmd ) ;
652+ //get the ebs csi role, so it can be used to add permissions to the new key
652653 }
654+ // disabled for now, as we need to test that it assigns the permissions correctly before enable customer eks
655+ // for encription.
656+ //else { //if alias found, then get the key id
657+ // key_id = cmd_results.replace(/"/g, ''); //remove quotes from string
658+ //}
659+ //give_kms_access_to_ebs_csi_role(stackName, region, key_id);
660+
653661}
654662
663+
664+ /*function give_kms_access_to_ebs_csi_role(stackName: string, region: string, KeyId: string){
665+ const roleName = stackName + '-awsebscsidriveriamrole';
666+ const cdm_list_ebs_csi_role = `aws iam list-roles --query "Roles[?contains(RoleName, '${roleName}')].Arn" --output text`;
667+ const list_roles = execSync(cdm_list_ebs_csi_role);
668+ if (list_roles.toString() !== '') {
669+ const policy = `{
670+ "Version": "2012-10-17",
671+ "Id": "key-default-1",
672+ "Statement": [
673+ {
674+ "Sid": "Enable IAM User Permissions",
675+ "Effect": "Allow",
676+ "Principal": {
677+ "AWS": "arn:aws:iam::381492072749:root"
678+ },
679+ "Action": "kms:*",
680+ "Resource": "*"
681+ },
682+ {
683+ "Sid": "Enable IAM User Permissions",
684+ "Effect": "Allow",
685+ "Principal": {
686+ "AWS": "${list_roles.toString().trim()}"
687+ },
688+ "Action": [
689+ "kms:Encrypt",
690+ "kms:Decrypt",
691+ "kms:ReEncrypt*",
692+ "kms:GenerateDataKey*",
693+ "kms:DescribeKey",
694+ "kms:CreateGrant",
695+ "kms:ListGrants",
696+ "kms:RevokeGrant"
697+ ],
698+ "Resource": "*"
699+ }
700+ ]
701+ }`;
702+ const cmp_policy = `aws kms put-key-policy --policy-name default --key-id ${KeyId.trim()} --region ${region} --policy '${policy}'`;
703+ execSync(cmp_policy);
704+ } else {
705+ console.log(`EBS CSI Role with name: ${roleName} already exists.`);
706+ }
707+ }*/
655708///////////////////////////////////////////////////////////////////////////////////////////////////
0 commit comments