Skip to content

Commit

Permalink
Upload git ssh public key to git user
Browse files Browse the repository at this point in the history
We want to upload the generated SSH key to the created git user.

We use the resource `aws_iam_user_ssh_key` from terraform, implemented
in hashicorp/terraform#5744 so this requires
an updated version of terraform.

We retrieve the generated key as a s3 resource and pass it to terraform
as a TF_VAR_ variable.

After the key is uploaded, AWS assigns it a unique id which must be used
as user for SSH when connecting to the codecommit git repositories.

We render and output a full url with that ssh_key_id and the ssh url of the
repository, using scp like connect strings.
  • Loading branch information
keymon committed Mar 23, 2016
1 parent b09ef37 commit 5434142
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 0 deletions.
10 changes: 10 additions & 0 deletions concourse/pipelines/create-deployer.yml
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,13 @@ resources:
versioned_file: id_rsa
region_name: {{aws_region}}

- name: git-ssh-public-key
type: s3-iam
source:
bucket: {{state_bucket}}
versioned_file: git_id_rsa.pub
region_name: {{aws_region}}

- name: concourse-cert
type: s3-iam
source:
Expand Down Expand Up @@ -217,6 +224,7 @@ jobs:
passed: [vpc]
- get: concourse-terraform-state
- get: concourse-cert
- get: git-ssh-public-key

- task: vpc-terraform-outputs-to-sh
config:
Expand Down Expand Up @@ -270,6 +278,7 @@ jobs:
- name: vpc-terraform-outputs
- name: concourse-terraform-state
- name: generate-concourse-cert
- name: git-ssh-public-key
params:
VAGRANT_IP: {{vagrant_ip}}
TF_VAR_env: {{deploy_env}}
Expand All @@ -283,6 +292,7 @@ jobs:
- |
cp generate-concourse-cert/concourse.crt generate-concourse-cert/concourse.key .
. vpc-terraform-outputs/tfvars.sh
export TF_VAR_git_rsa_id_pub=$(<git-ssh-public-key/git_id_rsa.pub)
terraform_params=${VAGRANT_IP:+-var vagrant_cidr=$VAGRANT_IP/32}
terraform apply ${terraform_params} \
-var-file=paas-cf/terraform/{{aws_account}}.tfvars \
Expand Down
6 changes: 6 additions & 0 deletions terraform/concourse/codecommit.tf
Original file line number Diff line number Diff line change
Expand Up @@ -30,3 +30,9 @@ resource "aws_iam_user" "git" {
# ]
# append = true
#}

resource "aws_iam_user_ssh_key" "git" {
username = "${aws_iam_user.git.name}"
encoding = "PEM"
public_key = "${var.git_rsa_id_pub}"
}
1 change: 1 addition & 0 deletions terraform/concourse/git_ssh_key_id
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.
5 changes: 5 additions & 0 deletions terraform/concourse/outputs.tf
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,8 @@ output "concourse_elb_name" {
output "concourse_dns_name" {
value = "${aws_route53_record.deployer-concourse.fqdn}"
}

output "git_concourse_pool_clone_full_url_ssh" {
# convert the ssh:// url to a scp like connect string and add the git user
value = "${aws_iam_user_ssh_key.git.ssh_public_key_id}@${replace(aws_codecommit_repository.concourse-pool.clone_url_ssh, "/^ssh://([^/]+)//", "$1:")}"
}
4 changes: 4 additions & 0 deletions terraform/concourse/variables.tf
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,7 @@ variable "system_dns_zone_id" {
variable "system_dns_zone_name" {
description = "Amazon Route53 DNS zone name for the provisioned environment."
}

variable "git_rsa_id_pub" {
description = "Public SSH key for the git user"
}

0 comments on commit 5434142

Please sign in to comment.