-
Notifications
You must be signed in to change notification settings - Fork 1.7k
/
agentless-installer.sh.tmpl
219 lines (185 loc) · 7.58 KB
/
agentless-installer.sh.tmpl
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
#!/usr/bin/env bash
set -o errexit
set -o pipefail
set -o nounset
upgrade_endpoint="{{ .PublicProxyAddr }}/v1/webapi/automaticupgrades/channel/default"
# upgrade_endpoint_fetch loads the specified value from the upgrade endpoint. the only
# currently supported values are 'version' and 'critical'.
upgrade_endpoint_fetch() {
host_path="${upgrade_endpoint}/${1}"
if sf_output="$(curl --proto '=https' --tlsv1.2 -sSf "https://${host_path}")"; then
# emit output with empty lines and extra whitespace removed
echo "$sf_output" | grep -v -e '^[[:space:]]*$' | awk '{$1=$1};1'
return 0
else
return 1
fi
}
# get_target_version loads the current value of the /version endpoint.
get_target_version() {
if tv_output="$(upgrade_endpoint_fetch version)"; then
# emit version string with leading 'v' removed if one is present
echo "${tv_output#v}"
return 0
fi
return 1
}
run_teleport() {
TOKEN="$1"
PRINCIPALS="$2"
LABELS="$3"
ADDRESS="$4"
sudo /usr/local/bin/teleport join openssh \
--openssh-config="${SSHD_CONFIG}" \
--join-method=iam \
--token="$TOKEN" \
--proxy-server="{{ .PublicProxyAddr }}" \
--additional-principals="$PRINCIPALS" \
--labels="$LABELS" \
--address="$ADDRESS":22 \
--restart-sshd
}
get_metadata_item() {
IMDS_TOKEN="$1"
ENDPOINT="$2"
curl -m5 -sS -H "X-aws-ec2-metadata-token: ${IMDS_TOKEN}" "http://169.254.169.254/latest/meta-data/$ENDPOINT"
}
get_principals() {
IMDS_TOKEN="$1"
LOCAL_IP="$(get_metadata_item "$IMDS_TOKEN" local-ipv4)"
PUBLIC_IP="$(get_metadata_item "$IMDS_TOKEN" public-ipv4 || echo "")"
PRINCIPALS=""
if [ ! "$LOCAL_IP" = "" ]; then
PRINCIPALS="$LOCAL_IP,$PRINCIPALS"
fi
if [ ! "$PUBLIC_IP" = "" ]; then
PRINCIPALS="$PUBLIC_IP,$PRINCIPALS"
fi
echo "$PRINCIPALS"
}
get_address() {
IMDS_TOKEN="$1"
PUBLIC_IP=$(get_metadata_item "$IMDS_TOKEN" public-ipv4 || echo "")
if [ ! "$PUBLIC_IP" = "" ]; then
echo "$PUBLIC_IP"
return 0
fi
LOCAL_IP="$(get_metadata_item "$IMDS_TOKEN" local-ipv4)"
if [ ! "$LOCAL_IP" = "" ]; then
echo "$LOCAL_IP"
return 0
fi
echo "Failed to retreive an IP address to connect to, which is a required parameter"
return 1
}
get_labels() {
IMDS_TOKEN="$1"
INSTANCE_INFO=$(curl -m5 -sS -H "X-aws-ec2-metadata-token: ${IMDS_TOKEN}" http://169.254.169.254/latest/dynamic/instance-identity/document)
ACCOUNT_ID="$(echo "$INSTANCE_INFO" | jq -r .accountId)"
INSTANCE_ID="$(echo "$INSTANCE_INFO" | jq -r .instanceId)"
REGION="$(echo "$INSTANCE_INFO" | jq -r .region)"
LABELS="teleport.dev/instance-id=${INSTANCE_ID},teleport.dev/account-id=${ACCOUNT_ID},teleport.dev/aws-region=${REGION}"
echo "$LABELS"
}
install_teleport() {
# shellcheck disable=SC1091
. /etc/os-release
TELEPORT_PACKAGE="{{ .TeleportPackage }}"
TELEPORT_UPDATER_PACKAGE="{{ .TeleportPackage }}-updater"
if [ "$ID" = "debian" ] || [ "$ID" = "ubuntu" ]; then
# old versions of ubuntu require that keys get added by `apt-key add`, without
# adding the key apt shows a key signing error when installing teleport.
if [ "$VERSION_CODENAME" = "xenial" ] || [ "$VERSION_CODENAME" = "trusty" ]; then
curl -o /tmp/teleport-pubkey.asc https://apt.releases.teleport.dev/gpg
sudo apt-key add /tmp/teleport-pubkey.asc
echo "deb https://apt.releases.teleport.dev/ubuntu ${VERSION_CODENAME?} {{ .RepoChannel }}" | sudo tee /etc/apt/sources.list.d/teleport.list
rm /tmp/teleport-pubkey.asc
else
curl https://apt.releases.teleport.dev/gpg | sudo tee /usr/share/keyrings/teleport-archive-keyring.asc
echo "deb [signed-by=/usr/share/keyrings/teleport-archive-keyring.asc] https://apt.releases.teleport.dev/${ID?} ${VERSION_CODENAME?} {{ .RepoChannel }}" | sudo tee /etc/apt/sources.list.d/teleport.list >/dev/null
fi
sudo apt-get update
# shellcheck disable=SC2050
if [ "{{ .AutomaticUpgrades }}" = "true" ]; then
# automatic upgrades
if ! target_version="$(get_target_version)"; then
# error getting the target version
sudo apt-get install -y "$TELEPORT_PACKAGE" jq "$TELEPORT_UPDATER_PACKAGE"
elif [ "$target_version" == "none" ]; then
# no target version advertised
sudo apt-get install -y "$TELEPORT_PACKAGE" jq "$TELEPORT_UPDATER_PACKAGE"
else
# successfully retrieved target version
sudo apt-get install -y "$TELEPORT_PACKAGE=$target_version" jq "$TELEPORT_UPDATER_PACKAGE=$target_version"
fi
else
# no automatic upgrades
sudo apt-get install -y "$TELEPORT_PACKAGE"
fi
elif [ "$ID" = "amzn" ] || [ "$ID" = "rhel" ]; then
if [ "$ID" = "rhel" ]; then
VERSION_ID=${VERSION_ID//\.*/} # convert version numbers like '7.2' to only include the major version
fi
sudo yum install -y yum-utils
sudo yum-config-manager --add-repo \
"$(rpm --eval "https://yum.releases.teleport.dev/$ID/$VERSION_ID/Teleport/%{_arch}/{{ .RepoChannel }}/teleport.repo")"
# shellcheck disable=SC2050
if [ "{{ .AutomaticUpgrades }}" = "true" ]; then
# automatic upgrades
if ! target_version="$(get_target_version)"; then
# error getting the target version
sudo yum install -y "$TELEPORT_PACKAGE" jq "$TELEPORT_UPDATER_PACKAGE"
elif [ "$target_version" == "none" ]; then
# no target version advertised
sudo yum install -y "$TELEPORT_PACKAGE" jq "$TELEPORT_UPDATER_PACKAGE"
else
# successfully retrieved target version
sudo yum install -y "$TELEPORT_PACKAGE-$target_version" jq "$TELEPORT_UPDATER_PACKAGE-$target_version"
fi
else
# no automatic upgrades
sudo yum install -y "$TELEPORT_PACKAGE"
fi
elif [ "$ID" = "sles" ] || [ "$ID" = "opensuse-tumbleweed" ] || [ "$ID" = "opensuse-leap" ]; then
if [ "$ID" = "opensuse-tumbleweed" ]; then
VERSION_ID="15" # tumbleweed uses dated VERSION_IDs like 20230702
else
VERSION_ID="${VERSION_ID//.*/}" # convert version numbers like '7.2' to only include the major version
fi
sudo rpm --import "https://zypper.releases.teleport.dev/gpg"
sudo zypper --non-interactive addrepo "$(rpm --eval "https://yum.releases.teleport.dev/sles/$VERSION_ID/Teleport/%{_arch}/{{ .RepoChannel }}/teleport.repo")"
sudo zypper --gpg-auto-import-keys refresh
# shellcheck disable=SC2050
if [ "{{ .AutomaticUpgrades }}" = "true" ]; then
# automatic upgrades
if ! target_version="$(get_target_version)"; then
# error getting the target version
sudo zypper --non-interactive install -y "$TELEPORT_PACKAGE" jq "$TELEPORT_UPDATER_PACKAGE"
elif [ "$target_version" == "none" ]; then
# no target version advertised
sudo zypper --non-interactive install -y "$TELEPORT_PACKAGE" jq "$TELEPORT_UPDATER_PACKAGE"
else
# successfully retrieved target version
sudo zypper --non-interactive install -y "$TELEPORT_PACKAGE-$target_version" jq "$TELEPORT_UPDATER_PACKAGE-$target_version"
fi
else
# no automatic upgrades
sudo zypper --non-interactive install -y "$TELEPORT_PACKAGE"
fi
else
echo "Unsupported distro: $ID"
exit 1
fi
}
(
flock -n 9 || exit 1
TOKEN="$1"
if ! test -f /usr/local/bin/teleport; then
install_teleport
fi
IMDS_TOKEN=$(curl -m5 -sS -X PUT "http://169.254.169.254/latest/api/token" -H "X-aws-ec2-metadata-token-ttl-seconds: 300")
PRINCIPALS="$(get_principals "$IMDS_TOKEN")"
LABELS="$(get_labels "$IMDS_TOKEN")"
ADDRESS="$(get_address "$IMDS_TOKEN")"
run_teleport "$TOKEN" "$PRINCIPALS" "$LABELS" "$ADDRESS"
) 9>/var/lock/teleport_install.lock