-
Notifications
You must be signed in to change notification settings - Fork 27
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
In order to access the codecommit repository via SSH, we must upload a SSH key. We add a required variable for the concourse terraform: `git_rsa_id_pub`, which must have the public SSH key to add, and a new output `git_ssh_key_id` which is the key id of the ssh key and the user that must be used when connecting to the codecommit git repo. But terraform does not currently support upload ssh keys, although the PR is in master and will be released soon: hashicorp/terraform#5744 hashicorp/terraform#5774 To workaround this, we implemented the following workaround: 1. One template_file which contains the public key. If it changes, it will run a script `aws-upload-aws-key.sh` to upload the SSH key to the user. The script takes care of duplicates. 2. A template_file resource will read from a `id_rsa_key_id`, and execute a script to query the ID if the file has changed since last execution. This ensures that the file is updated. We must commit a empty file to avoid terraform fail the first run. 3. Another template_file which reads the previous populated `id_rsa_key_id` file. This allows read the file content and expose it as a terraform output. This workaround can be removed and replaced as the resource `aws_iam_user_ssh_key` is supported and released in the official terraform release.
- Loading branch information
Showing
6 changed files
with
91 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,15 @@ | ||
#!/bin/sh | ||
set -e | ||
|
||
username="$1" | ||
id_rsa_pub=$(echo $2 | awk '{print $2}') | ||
|
||
for key_id in $(aws iam list-ssh-public-keys --user-name "${username}" --query 'SSHPublicKeys[*].SSHPublicKeyId' | sed -n 's/.*\(AP.*\)".*/\1/p'); do | ||
key=$(aws iam get-ssh-public-key --encoding SSH --user-name "${username}" --ssh-public-key-id "${key_id}" --query 'SSHPublicKey.SSHPublicKeyBody') | ||
if echo "${key}" | grep -q "${id_rsa_pub}"; then | ||
echo $key_id | ||
exit 0 | ||
fi | ||
done | ||
echo "Not found" | ||
exit 1 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
#!/bin/sh | ||
output=$(mktemp) | ||
trap 'rm -f "${output}"' EXIT | ||
|
||
aws iam upload-ssh-public-key --user-name $1 --ssh-public-key-body "$2" > "${output}" 2>&1 | ||
RET="$?" | ||
cat "${output}" | ||
|
||
if [ "${RET}" != "0" ]; then | ||
if grep -q "Duplicate SSH public key uploaded" "${output}"; then | ||
echo "Key is already uploaded." | ||
# Try to find out the key id | ||
exit 0 | ||
else | ||
echo "Error uploading key" | ||
exit "${RET}" | ||
fi | ||
fi | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
Empty file git_ssh_key_id to avoid terraform fail during the first run. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters