Skip to content

Commit 905f62c

Browse files
authored
Merge pull request #414 from ComputeCanada/fix_authorized_keys
Limit where user tf is defined
2 parents fb4a000 + 185d97c commit 905f62c

3 files changed

Lines changed: 23 additions & 9 deletions

File tree

common/configuration/main.tf

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,7 @@ locals {
9999
cloud_provider = var.cloud_provider
100100
cloud_region = var.cloud_region
101101
tags = values.tags
102+
bastion_tags = var.bastion_tags
102103
node_name = key,
103104
node_prefix = values.prefix,
104105
domain_name = var.domain_name
@@ -108,7 +109,7 @@ locals {
108109
puppetserver_password = local.puppet_passwd,
109110
sudoer_username = var.sudoer_username,
110111
ssh_authorized_keys = local.public_keys
111-
tf_ssh_public_key = tls_private_key.ssh.public_key_openssh
112+
tf_ssh_public_key = chomp(tls_private_key.ssh.public_key_openssh)
112113
terraform_facts = local.terraform_facts
113114
skip_upgrade = var.skip_upgrade
114115
puppetfile = var.puppetfile
@@ -141,7 +142,7 @@ output "terraform_facts" {
141142
}
142143

143144
output "puppetservers" {
144-
value = { for host, values in local.final_inventory : host => values.local_ip if contains(values.tags, "puppet") }
145+
value = { for host, values in local.final_inventory : host => { local_ip = values.local_ip, public_ip = values.public_ip } if contains(values.tags, "puppet") }
145146
}
146147

147148
output "guest_passwd" {

common/configuration/puppet.yaml.tftpl

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,15 @@ mounts:
66
- [ ephemeral0, /mnt/ephemeral0 ]
77

88
users:
9+
%{ if contains(tags, "puppet") || length(setintersection(tags, bastion_tags)) > 0 ~}
910
- name: tf
1011
system: true
1112
passwd: '*'
1213
no_user_group: true
1314
homedir: /tmp
1415
%{ if contains(tags, "puppet") }
1516
sudo: "ALL = NOPASSWD: /usr/sbin/update_etc_puppetlabs.sh *.zip"
17+
%{ endif ~}
1618
%{ endif ~}
1719
- name: ${sudoer_username}
1820
groups: adm, wheel, systemd-journal
@@ -164,16 +166,26 @@ runcmd:
164166
- test -f /run/cloud-init-failed && echo 'WARNING - some steps cloud-init runcmd failed, listed in /run/cloud-init-failed. Manual fixing and rebooting required. ' | tee /etc/motd || true
165167

166168
write_files:
167-
# If the ip addresses of the puppet servers are not known in advance, we cannot restrict the ssh connection to them.
168-
- content: restrict,%{ if contains(tags, "puppet") }pty%{ else }%{if length(compact(values(puppetservers))) == length(keys(puppetservers)) }%{ for host, ip in puppetservers }permitopen="${ip}:22"%{ endfor }%{ else }pty%{ endif },port-forwarding,command="/sbin/nologin"%{ endif } ${tf_ssh_public_key}
169-
path: /etc/ssh/authorized_keys.tf
170-
permissions: "0644"
169+
%{ if contains(tags, "puppet") || length(setintersection(tags, bastion_tags)) > 0 ~}
171170
- content: |
172171
Match User tf
173172
AuthorizedKeysFile /etc/ssh/authorized_keys.%u
174173
AuthenticationMethods publickey
175174
path: /etc/ssh/sshd_config.d/50-authenticationmethods.conf
176175
permissions: "0600"
176+
%{ endif ~}
177+
%{ if contains(tags, "puppet") ~}
178+
- content: restrict,pty ${tf_ssh_public_key}
179+
path: /etc/ssh/authorized_keys.tf
180+
permissions: "0644"
181+
%{ else ~}
182+
%{ if length(setintersection(tags, bastion_tags)) > 0 ~}
183+
# If the ip addresses of the puppet servers are not known in advance, we cannot restrict the ssh connection to them.
184+
- content: restrict,%{if length(compact(values(puppetservers))) == length(keys(puppetservers)) }%{ for host, ip in puppetservers }permitopen="${ip}:22"%{ endfor }%{ else }pty%{ endif },port-forwarding,command="/sbin/nologin" ${tf_ssh_public_key}
185+
path: /etc/ssh/authorized_keys.tf
186+
permissions: "0644"
187+
%{ endif ~}
188+
%{ endif ~}
177189
- content: |
178190
facts : {
179191
blocklist : [

common/provision/main.tf

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,8 @@ data "archive_file" "puppetserver_files" {
6060
}
6161

6262
locals {
63-
bastion_host = length(var.configuration.bastions) > 0 ? var.configuration.bastions[keys(var.configuration.bastions)[0]] : null
63+
puppetservers_are_bastion = length(setintersection(keys(var.configuration.bastions), keys(var.configuration.puppetservers))) == length(var.configuration.puppetservers)
64+
bastion_host = length(var.configuration.bastions) > 0 ? var.configuration.bastions[keys(var.configuration.bastions)[0]] : null
6465
}
6566

6667
resource "terraform_data" "deploy_puppetserver_files" {
@@ -69,11 +70,11 @@ resource "terraform_data" "deploy_puppetserver_files" {
6970
connection {
7071
type = "ssh"
7172
agent = false
72-
bastion_host = contains(local.bastion_host.tags, "public") ? local.bastion_host.public_ip : local.bastion_host.local_ip
73+
bastion_host = local.puppetservers_are_bastion ? null : (contains(local.bastion_host.tags, "public") ? local.bastion_host.public_ip : local.bastion_host.local_ip)
7374
bastion_user = "tf"
7475
bastion_private_key = var.configuration.ssh_key.private
7576
user = "tf"
76-
host = each.value
77+
host = local.puppetservers_are_bastion && each.value.public_ip != "" ? each.value.public_ip : each.value.local_ip
7778
private_key = var.configuration.ssh_key.private
7879
}
7980

0 commit comments

Comments
 (0)