Skip to content

Commit 0a8fa8f

Browse files
author
Lachlan Donald
authored
Merge pull request #601 from jradtilbrook/ssm
Add support for loading BuildkiteAgentTokenPath from Parameter Store
2 parents 5408db9 + b0d0f2a commit 0a8fa8f

File tree

2 files changed

+48
-3
lines changed

2 files changed

+48
-3
lines changed

packer/linux/conf/bin/bk-install-elastic-stack.sh

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ if [[ "${BUILDKITE_AGENT_ENABLE_GIT_MIRRORS_EXPERIMENT}" == "true" ]] ; then
100100
fi
101101
fi
102102

103+
# If the agent token path is set, use that instead of BUILDKITE_AGENT_TOKEN
104+
if [[ -n "${BUILDKITE_AGENT_TOKEN_PATH}" ]] ; then
105+
BUILDKITE_AGENT_TOKEN="$(aws ssm get-parameter --name "${BUILDKITE_AGENT_TOKEN_PATH}" --with-decryption --query Parameter.Value)"
106+
fi
107+
103108
cat << EOF > /etc/buildkite-agent/buildkite-agent.cfg
104109
name="${BUILDKITE_STACK_NAME}-${INSTANCE_ID}-%n"
105110
token="${BUILDKITE_AGENT_TOKEN}"

templates/aws-stack.yml

Lines changed: 43 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,8 @@ Metadata:
99
default: Buildkite Configuration
1010
Parameters:
1111
- BuildkiteAgentToken
12+
- BuildkiteAgentTokenParameterStorePath
13+
- BuildkiteAgentTokenParameterStoreKMSKey
1214
- BuildkiteQueue
1315

1416
- Label:
@@ -102,7 +104,17 @@ Parameters:
102104
Description: Buildkite agent registration token
103105
Type: String
104106
NoEcho: true
105-
MinLength: 1
107+
Default: ""
108+
109+
BuildkiteAgentTokenParameterStorePath:
110+
Description: AWS SSM path to the Buildkite agent registration token (this takes precedence over BuildkiteAgentToken)
111+
Type: String
112+
Default: ""
113+
114+
BuildkiteAgentTokenParameterStoreKMSKey:
115+
Description: AWS KMS key ID used to encrypt the SSM parameter (if encrypted)
116+
Type: String
117+
Default: ""
106118

107119
BuildkiteAgentTags:
108120
Description: Additional tags seperated by commas to provide to the agent. E.g os=linux,llamas=always
@@ -418,6 +430,12 @@ Conditions:
418430
UseECR:
419431
!Not [ !Equals [ !Ref ECRAccessPolicy, "none" ] ]
420432

433+
UseSSMAgentToken:
434+
!Not [ !Equals [ !Ref BuildkiteAgentTokenParameterStorePath, "" ] ]
435+
436+
UseCustomerManagedKeyForParameterStore:
437+
!Not [ !Equals [ !Ref BuildkiteAgentTokenParameterStoreKMSKey, "" ] ]
438+
421439
HasVariableSize:
422440
!Not [ !Equals [ !Ref MaxSize, !Ref MinSize ] ]
423441

@@ -771,6 +789,7 @@ Resources:
771789
$Env:BUILDKITE_SCALE_IN_IDLE_PERIOD="${ScaleInIdlePeriod}"
772790
$Env:BUILDKITE_SECRETS_BUCKET="${LocalSecretsBucket}"
773791
$Env:BUILDKITE_AGENT_TOKEN="${BuildkiteAgentToken}"
792+
$Env:BUILDKITE_AGENT_TOKEN_PATH="${BuildkiteAgentTokenParameterStorePath}"
774793
$Env:BUILDKITE_AGENTS_PER_INSTANCE="${AgentsPerInstance}"
775794
$Env:BUILDKITE_AGENT_TAGS="${BuildkiteAgentTags}"
776795
$Env:BUILDKITE_AGENT_TIMESTAMP_LINES="${BuildkiteAgentTimestampLines}"
@@ -811,6 +830,7 @@ Resources:
811830
BUILDKITE_SCALE_IN_IDLE_PERIOD=${ScaleInIdlePeriod} \
812831
BUILDKITE_SECRETS_BUCKET="${LocalSecretsBucket}" \
813832
BUILDKITE_AGENT_TOKEN="${BuildkiteAgentToken}" \
833+
BUILDKITE_AGENT_TOKEN_PATH="${BuildkiteAgentTokenParameterStorePath}" \
814834
BUILDKITE_AGENTS_PER_INSTANCE="${AgentsPerInstance}" \
815835
BUILDKITE_AGENT_TAGS="${BuildkiteAgentTags}" \
816836
BUILDKITE_AGENT_TIMESTAMP_LINES="${BuildkiteAgentTimestampLines}" \
@@ -897,7 +917,7 @@ Resources:
897917
- lambda.amazonaws.com
898918
Action:
899919
- sts:AssumeRole
900-
ManagedPolicyArns:
920+
ManagedPolicyArns: !Split
901921
- arn:aws:iam::aws:policy/service-role/AWSLambdaBasicExecutionRole
902922
Policies:
903923
- PolicyName: AutoScalingGroups
@@ -909,6 +929,25 @@ Resources:
909929
- autoscaling:DescribeAutoScalingGroups
910930
- autoscaling:SetDesiredCapacity
911931
Resource: '*'
932+
- !If
933+
- UseCustomerManagedKeyForParameterStore
934+
- - PolicyName: DecryptAgentToken
935+
PolicyDocument:
936+
Version: '2012-10-17'
937+
Statement:
938+
- Effect: Allow
939+
Action:
940+
- kms:Decrypt
941+
Resource: !Sub arn:aws:kms:${AWS::Region}:${AWS::AccountId}:key/${BuildkiteAgentTokenParameterStoreKMSKey}
942+
- PolicyName: ReadAgentToken
943+
PolicyDocument:
944+
Version: '2012-10-17'
945+
Statement:
946+
- Effect: Allow
947+
Action:
948+
- ssm:GetParameter
949+
Resource: !Sub arn:aws:ssm:${AWS::Region}:${AWS::AccountId}:parameter/${BuildkiteAgentTokenParameterStorePath}
950+
- !Ref 'AWS::NoValue'
912951
- PolicyName: WriteCloudwatchMetrics
913952
PolicyDocument:
914953
Version: '2012-10-17'
@@ -941,7 +980,8 @@ Resources:
941980
MemorySize: 128
942981
Environment:
943982
Variables:
944-
BUILDKITE_AGENT_TOKEN: !Ref BuildkiteAgentToken
983+
BUILDKITE_AGENT_TOKEN: !If [ UseSSMAgentToken, !Ref 'AWS::NoValue', !Ref BuildkiteAgentToken ]
984+
BUILDKITE_AGENT_TOKEN_SSM_KEY: !Ref BuildkiteAgentTokenParameterStorePath
945985
BUILDKITE_QUEUE: !Ref BuildkiteQueue
946986
AGENTS_PER_INSTANCE: !Ref AgentsPerInstance
947987
CLOUDWATCH_METRICS: "1"

0 commit comments

Comments
 (0)