Skip to content

Commit

Permalink
Merge pull request #1366 from buildkite/feat_add_kms_signing_support
Browse files Browse the repository at this point in the history
Add the option to create a KMS key for signing of pipelines
  • Loading branch information
wolfeidau authored Sep 11, 2024
2 parents 45f566a + 466633d commit 92a527b
Show file tree
Hide file tree
Showing 3 changed files with 100 additions and 3 deletions.
1 change: 1 addition & 0 deletions packer/linux/conf/bin/bk-install-elastic-stack.sh
Original file line number Diff line number Diff line change
Expand Up @@ -291,6 +291,7 @@ disconnect-after-idle-timeout=${BUILDKITE_SCALE_IN_IDLE_PERIOD}
disconnect-after-job=${BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB}
tracing-backend=${BUILDKITE_AGENT_TRACING_BACKEND}
cancel-grace-period=${BUILDKITE_AGENT_CANCEL_GRACE_PERIOD}
signing-aws-kms-key=${BUILDKITE_AGENT_SIGNING_KMS_KEY}
EOF

if [[ "${BUILDKITE_ENV_FILE_URL}" != "" ]]; then
Expand Down
1 change: 1 addition & 0 deletions packer/windows/conf/bin/bk-install-elastic-stack.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,7 @@ shell=powershell
disconnect-after-idle-timeout=${Env:BUILDKITE_SCALE_IN_IDLE_PERIOD}
disconnect-after-job=${Env:BUILDKITE_TERMINATE_INSTANCE_AFTER_JOB}
tracing-backend=${Env:BUILDKITE_AGENT_TRACING_BACKEND}
signing-jwks-key-id=${Env:BUILDKITE_AGENT_SIGNING_KMS_KEY}
"@
$OFS=" "

Expand Down
101 changes: 98 additions & 3 deletions templates/aws-stack.yml
Original file line number Diff line number Diff line change
Expand Up @@ -27,15 +27,22 @@ Metadata:
AWS::CloudFormation::Interface:
ParameterGroups:
- Label:
default: Buildkite Configuration
default: Base Configuration
Parameters:
- BuildkiteAgentToken
- BuildkiteAgentTokenParameterStorePath
- BuildkiteAgentTokenParameterStoreKMSKey
- BuildkiteAgentToken
- BuildkiteQueue

- Label:
default: Advanced Buildkite Configuration
default: Signed Pipelines Configuration
Parameters:
- PipelineSigningKMSKeyId
- PipelineSigningKMSKeySpec
- PipelineSigningKMSAccess

- Label:
default: Advanced Configuration
Parameters:
- BuildkiteAgentRelease
- BuildkiteAgentTags
Expand Down Expand Up @@ -568,6 +575,27 @@ Parameters:
Description: Optional - Customise the EC2 instance Name tag
Default: ""

PipelineSigningKMSKeyId:
Type: String
Description: Optional - Identifier of the KMS key used to sign and verify pipelines (Created if left blank and PipelineSigningKMSKeySpec is selected)
Default: ""

PipelineSigningKMSKeySpec:
Type: String
Description: The key spec for the KMS key used to sign and verify pipelines
AllowedValues:
- "ECC_NIST_P256"
- "none"
Default: "none"

PipelineSigningKMSAccess:
Type: String
Description: The access level for the KMS key used to sign and verify pipelines
AllowedValues:
- "sign-and-verify"
- "verify"
Default: "sign-and-verify"

Rules:
HasToken:
Assertions:
Expand All @@ -582,6 +610,17 @@ Rules:
- !Ref BuildkiteAgentTokenParameterStorePath
- ""
AssertDescription: "You must provide BuildkiteAgentToken or BuildkiteAgentTokenParameterStorePath"
HasPipelineSigningKMSKey:
Assertions:
- Assert:
!Or
- !Equals
- !Ref PipelineSigningKMSKeyId
- ""
- !Equals
- !Ref PipelineSigningKMSKeySpec
- "none"
AssertDescription: "You must provide either provide a PipelineSigningKMSKeyId or select a PipelineSigningKMSKeySpec but not both"

Outputs:
VpcId:
Expand All @@ -602,6 +641,12 @@ Outputs:
Export:
Name: !Sub '${AWS::StackName}-ManagedSecretsLoggingBucket'

PipelineSigningKMSKey:
Value:
!If [ CreatePipelineSigningKMSKey, !Ref PipelineSigningKMSKey, "none" ]
Export:
Name: !Sub '${AWS::StackName}-PipelineSigningKMSKey'

AutoScalingGroupName:
Value: !Ref AgentAutoScaleGroup
Export:
Expand Down Expand Up @@ -685,6 +730,20 @@ Conditions:

UseCostAllocationTags:
!Equals [ !Ref EnableCostAllocationTags, "true" ]

UsePipelineSigningKMSKey:
!Not [ !Equals [ !Ref PipelineSigningKMSKeyId, "" ] ]

CreatePipelineSigningKMSKey:
!And
- !Equals [ !Ref PipelineSigningKMSKeyId, "" ]
- !Not [ !Equals [ !Ref PipelineSigningKMSKeySpec, "none" ] ]

HasPipelineSigningKMSKey:
!Or [ !Condition CreatePipelineSigningKMSKey, !Condition UsePipelineSigningKMSKey ]

HasSigningKMSAccessSignAndVerify:
!Equals [ !Ref PipelineSigningKMSAccess, "sign-and-verify" ]

HasKeyName:
!Not [ !Equals [ !Ref KeyName, "" ] ]
Expand Down Expand Up @@ -871,6 +930,18 @@ Resources:
Name: !Sub "/${AWS::StackName}/buildkite/agent-token"
Type: String
Value: !Ref BuildkiteAgentToken

PipelineSigningKMSKey:
Type: AWS::KMS::Key
Condition: CreatePipelineSigningKMSKey
DeletionPolicy: Retain
Properties:
Description: Key used to sign and verify pipelines
KeySpec: !Ref PipelineSigningKMSKeySpec
KeyUsage: SIGN_VERIFY
Tags:
- Key: Name
Value: !Sub '${AWS::StackName}-PipelineSigningKey'

# Allow ec2 instances to assume a role and be granted the IAMPolicies
IAMInstanceProfile:
Expand Down Expand Up @@ -903,6 +974,26 @@ Resources:
- !Ref 'AWS::NoValue'
- !Ref 'AWS::NoValue'
Policies:
- !If
- HasPipelineSigningKMSKey
- PolicyName: PipelineSigningKMSKeyAccess
PolicyDocument:
Version: '2012-10-17'
Statement:
- Effect: Allow
Action:
!If
- HasSigningKMSAccessSignAndVerify
- - kms:Sign
- kms:Verify
- kms:GetPublicKey
- - kms:Verify
- kms:GetPublicKey
Resource: !If
- CreatePipelineSigningKMSKey
- !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${PipelineSigningKMSKey}
- !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${PipelineSigningKMSKeyId}
- !Ref 'AWS::NoValue'
- !If
- UseCustomerManagedKeyForParameterStore
- PolicyName: DecryptAgentToken
Expand Down Expand Up @@ -1231,6 +1322,7 @@ Resources:
$Env:BUILDKITE_QUEUE="${BuildkiteQueue}"
$Env:BUILDKITE_AGENT_ENABLE_GIT_MIRRORS="${BuildkiteAgentEnableGitMirrors}"
$Env:BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT="${BootstrapScriptUrl}"
$Env:BUILDKITE_AGENT_SIGNING_KMS_KEY="${PipelineSigningKMSKey}"
$Env:BUILDKITE_ENV_FILE_URL="${AgentEnvFileUrl}"
$Env:BUILDKITE_AUTHORIZED_USERS_URL="${AuthorizedUsersUrl}"
$Env:BUILDKITE_ECR_POLICY="${ECRAccessPolicy}"
Expand All @@ -1248,6 +1340,7 @@ Resources:
LocalSecretsBucket: !If [ CreateSecretsBucket, !Ref ManagedSecretsBucket, !Ref SecretsBucket ],
LocalSecretsBucketRegion: !If [ CreateSecretsBucket, !Ref "AWS::Region", !Ref SecretsBucketRegion ],
AgentTokenPath: !If [ UseCustomerManagedParameterPath, !Ref BuildkiteAgentTokenParameterStorePath, !Ref BuildkiteAgentTokenParameter ],
PipelineSigningKMSKey: !If [ CreatePipelineSigningKMSKey, !Ref PipelineSigningKMSKey, !Ref PipelineSigningKMSKeyId ],
}
- !Sub
- |
Expand Down Expand Up @@ -1288,6 +1381,7 @@ Resources:
BUILDKITE_AGENT_TRACING_BACKEND="${BuildkiteAgentTracingBackend}" \
BUILDKITE_AGENT_RELEASE="${BuildkiteAgentRelease}" \
BUILDKITE_AGENT_CANCEL_GRACE_PERIOD="${BuildkiteAgentCancelGracePeriod}" \
BUILDKITE_AGENT_SIGNING_KMS_KEY="${PipelineSigningKMSKey}" \
BUILDKITE_QUEUE="${BuildkiteQueue}" \
BUILDKITE_AGENT_ENABLE_GIT_MIRRORS="${BuildkiteAgentEnableGitMirrors}" \
BUILDKITE_ELASTIC_BOOTSTRAP_SCRIPT="${BootstrapScriptUrl}" \
Expand All @@ -1310,6 +1404,7 @@ Resources:
LocalSecretsBucket: !If [ CreateSecretsBucket, !Ref ManagedSecretsBucket, !Ref SecretsBucket ],
LocalSecretsBucketRegion: !If [ CreateSecretsBucket, !Ref "AWS::Region", !Ref SecretsBucketRegion ],
AgentTokenPath: !If [ UseCustomerManagedParameterPath, !Ref BuildkiteAgentTokenParameterStorePath, !Ref BuildkiteAgentTokenParameter ],
PipelineSigningKMSKey: !If [ CreatePipelineSigningKMSKey, !Ref PipelineSigningKMSKey, !Ref PipelineSigningKMSKeyId ],
}

AgentAutoScaleGroup:
Expand Down

0 comments on commit 92a527b

Please sign in to comment.