Skip to content

armgit / zulip

Public Imported from GitHub
0

Zulip server and web application. Open-source team chat that helps teams stay productive and focused.

puppet: Use IAM Roles Anywhere to get AWS credentials outside EC2.

Alex Vandiver69ef808d7b5f
diff --git a/puppet/zulip/manifests/common.pp b/puppet/zulip/manifests/common.pp
index 5768f4734b1..4351aafc1e9 100644
--- a/puppet/zulip/manifests/common.pp
+++ b/puppet/zulip/manifests/common.pp
@@ -85,6 +85,16 @@ class zulip::common {
### zulip_ops packages
+ # https://docs.aws.amazon.com/rolesanywhere/latest/userguide/credential-helper.html
+ 'aws_signing_helper' => {
+ 'version' => '1.1.1',
+ 'sha256' => {
+ 'amd64' => '3761071497510ae1bde82aa31e34bbb63b9701deb932434e786a8479062b2b9b',
+ # aarch64 would need to compile from source:
+ # https://github.com/aws/rolesanywhere-credential-helper/tree/main
+ },
+ },
+
# https://release-registry.services.sentry.io/apps/sentry-cli/latest
'sentry-cli' => {
'version' => '2.23.0',
diff --git a/puppet/zulip_ops/files/teleport-aws-credentials b/puppet/zulip_ops/files/teleport-aws-credentials
new file mode 100644
index 00000000000..a90a4be22a6
--- /dev/null
+++ b/puppet/zulip_ops/files/teleport-aws-credentials
@@ -0,0 +1,42 @@
+#!/usr/bin/env bash
+
+# This uses AWS IAM Roles Anywhere[1] to acquire the NonAWSInstance
+# role for the whole server. It does this by having configured the
+# Teleport CA to be a "trust anchor," allowing the Teleport host
+# certificate to be used for authentication; the NonAWSInstance role
+# itself has a trust relationship which lists the CA as sufficient to
+# authorize for the role.
+#
+# [1]: https://docs.aws.amazon.com/rolesanywhere/latest/userguide
+
+set -eu
+
+# Check if we have cached credentials; they're good for 60 minutes, so
+# check within 55min.
+cache="/etc/aws-credential-cache.json"
+if [ -s "$cache" ] && [ "$(find "$cache" -mmin -55)" ]; then
+ cat "$cache"
+ exit 0
+fi
+
+# Extract the current host cert and key from the Teleport database.
+# These are nominally static for the lifetime of the host, but there's
+# little reason to not refresh them on every run, in case they get
+# rotated.
+
+teleport_json=$(
+ sqlite3 /var/lib/teleport/proc/sqlite.db "select value from kv where key = '/ids/node/current'"
+)
+
+# $teleport_json is a secret (it has the host key) so extract it
+# carefully (i.e. not using `echo $teleport_json"`")
+jq -r .spec.tls_cert <<<"$teleport_json" | base64 -d >/var/lib/teleport/host.crt
+jq -r .spec.key <<<"$teleport_json" | base64 -d >/var/lib/teleport/host.key
+
+# Write the cache out
+/srv/zulip-aws-tools/bin/aws_signing_helper credential-process \
+ --certificate /var/lib/teleport/host.crt \
+ --private-key /var/lib/teleport/host.key \
+ "$@" >"$cache"
+
+cat "$cache"
diff --git a/puppet/zulip_ops/manifests/aws_tools.pp b/puppet/zulip_ops/manifests/aws_tools.pp
index 95dcc0ffc63..908db24d201 100644
--- a/puppet/zulip_ops/manifests/aws_tools.pp
+++ b/puppet/zulip_ops/manifests/aws_tools.pp
@@ -1,6 +1,8 @@
# @summary Installs the AWS CLI
#
class zulip_ops::aws_tools {
+ $is_ec2 = zulipconf('machine', 'hosting_provider', 'ec2') == 'ec2'
+
file { '/usr/local/bin/install-aws-cli':
ensure => file,
mode => '0755',
@@ -14,4 +16,56 @@ class zulip_ops::aws_tools {
# exist yet. Count this as needing to run.
unless => '[ -f /usr/local/bin/install-aws-cli ] && /usr/local/bin/install-aws-cli check',
}
+
+ if ! $is_ec2 {
+ if $::os['architecture'] != 'amd64' {
+ # We would need to build aws_signing_helper from source
+ fail('Only amd64 hosts supported on non-EC2')
+ }
+ $helper_version = $zulip::common::versions['aws_signing_helper']['version']
+ zulip::external_dep { 'aws_signing_helper':
+ version => $helper_version,
+ url => "https://rolesanywhere.amazonaws.com/releases/${helper_version}/X86_64/Linux/aws_signing_helper",
+ before => File['/root/.aws/config'],
+ }
+ file { '/srv/zulip-aws-tools/bin/aws_signing_helper':
+ ensure => link,
+ target => "/srv/zulip-aws_signing_helper-${helper_version}",
+ require => [
+ Zulip::External_Dep['aws_signing_helper'],
+ Exec['install-aws-cli'],
+ ],
+ }
+ package { 'sqlite3': ensure => installed }
+ file { '/usr/local/bin/teleport-aws-credentials':
+ ensure => file,
+ require => [
+ Package['sqlite3'],
+ Service['teleport_node'],
+ ],
+ before => [
+ File['/root/.aws/config'],
+ ],
+ mode => '0755',
+ owner => 'root',
+ group => 'root',
+ source => 'puppet:///modules/zulip_ops/teleport-aws-credentials',
+ }
+ }
+ file { '/root/.aws':
+ ensure => directory,
+ mode => '0755',
+ owner => 'root',
+ group => 'root',
+ }
+ $aws_trust_arn = zulipsecret('secrets','aws_trust_arn','')
+ $aws_profile_arn = zulipsecret('secrets','aws_profile_arn','')
+ $aws_role_arn = zulipsecret('secrets','aws_role_arn','')
+ file { '/root/.aws/config':
+ ensure => file,
+ mode => '0644',
+ owner => 'root',
+ group => 'root',
+ content => template('zulip_ops/dotfiles/aws_config.erb'),
+ }
}
diff --git a/puppet/zulip_ops/manifests/profile/base.pp b/puppet/zulip_ops/manifests/profile/base.pp
index a8fcee0afdf..33ceaef0aef 100644
--- a/puppet/zulip_ops/manifests/profile/base.pp
+++ b/puppet/zulip_ops/manifests/profile/base.pp
@@ -117,10 +117,9 @@ class zulip_ops::profile::base {
require => User['zulip'],
}
- if $is_ec2 {
- # The AWS tools are not useful unless the host can auth to AWS.
- include zulip_ops::aws_tools
+ include zulip_ops::aws_tools
+ if $is_ec2 {
# Non-EC2 (e.g. CZO) don't have the private commit that adds these
# zulip_ops files.
file { '/root/.ssh/authorized_keys':
diff --git a/puppet/zulip_ops/templates/dotfiles/aws_config.erb b/puppet/zulip_ops/templates/dotfiles/aws_config.erb
new file mode 100644
index 00000000000..ff5bd57c6a6
--- /dev/null
+++ b/puppet/zulip_ops/templates/dotfiles/aws_config.erb
@@ -0,0 +1,10 @@
+[default]
+region = us-east-1
+output = text
+<% if @is_ec2 -%>
+# Credentials are from the IAM role attached to the EC2 instance
+<% else -%>
+# We pull the Teleport host certificate and use that to auth to AWS
+# using IAM Roles Anywhere
+credential_process = /usr/local/bin/teleport-aws-credentials --trust-anchor-arn <%= @aws_trust_arn %> --profile-arn <%= @aws_profile_arn %> --role-arn <%= @aws_role_arn %>
+<% end %>