forked from gravitational/teleport
-
Notifications
You must be signed in to change notification settings - Fork 0
/
metadata.go
425 lines (376 loc) · 12.9 KB
/
metadata.go
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
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
/*
* Teleport
* Copyright (C) 2023 Gravitational, Inc.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU Affero General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU Affero General Public License for more details.
*
* You should have received a copy of the GNU Affero General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
package metadata
import (
"context"
"crypto/tls"
"encoding/json"
"fmt"
"net/http"
"os"
"os/exec"
"runtime"
"strings"
"time"
"github.com/gravitational/trace"
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/api/types"
apiutils "github.com/gravitational/teleport/api/utils"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/utils"
)
// Metadata contains the instance "system" metadata.
// We expect each of these values to not change for the lifetime of the instance.
type Metadata struct {
// OS advertises the instance OS ("darwin" or "linux").
OS string
// OSVersion advertises the instance OS version (e.g. "ubuntu 22.04").
OSVersion string
// HostArchitecture advertises the instance host architecture (e.g. "x86_64" or "arm64").
HostArchitecture string
// GlibcVersion advertises the instance glibc version of linux instances (e.g. "2.35").
GlibcVersion string
// InstallMethods advertises the install methods used for the instance (e.g. "dockerfile").
InstallMethods []string
// ContainerRuntime advertises the container runtime for the instance, if any (e.g. "docker").
ContainerRuntime string
// ContainerOrchestrator advertises the container orchestrator for the instance, if any
// (e.g. "kubernetes-v1.24.8-eks-ffeb93d").
ContainerOrchestrator string
// CloudEnvironment advertises the cloud environment for the instance, if any (e.g. "aws").
CloudEnvironment string
// CloudMetadata contains extra metadata about the instance's cloud environment, if any.
CloudMetadata *types.CloudMetadata
}
// fetchConfig contains the configuration used by the FetchMetadata method.
type fetchConfig struct {
// getenv is the method called to retrieve an environment
// variable.
// It is configurable so that it can be mocked in tests.
getenv func(name string) string
// readFile is the method called to read a file.
// It is configurable so that it can be mocked in tests.
readFile func(name string) ([]byte, error)
// execCommand is the method called to execute a command.
// It is configurable so that it can be mocked in tests.
execCommand func(name string, args ...string) ([]byte, error)
// httpDo is the method called to perform an http request.
// It is configurable so that it can be mocked in tests.
httpDo func(req *http.Request, insecureSkipVerify bool) (*http.Response, error)
// getCloudMetadata is the method called to get additional info about an
// instance running in a cloud environment.
// It is configurable so that it can be mocked in tests.
fetchCloudMetadata func(ctx context.Context, cloudEnvironment string) *types.CloudMetadata
}
// setDefaults sets the values of several methods used to read files, execute
// commands, performing http requests, etc.
// Having these methods configurable allows us to mock them in tests.
func (c *fetchConfig) setDefaults() {
if c.getenv == nil {
c.getenv = os.Getenv
}
if c.readFile == nil {
c.readFile = os.ReadFile
}
if c.execCommand == nil {
c.execCommand = func(name string, args ...string) ([]byte, error) {
return exec.Command(name, args...).Output()
}
}
if c.httpDo == nil {
c.httpDo = func(req *http.Request, insecureSkipVerify bool) (*http.Response, error) {
transport, err := defaults.Transport()
if err != nil {
return nil, trace.Wrap(err)
}
// Initialize transport.TLSClientConfig if defaults.Transport() returns a nil one
if transport.TLSClientConfig == nil {
transport.TLSClientConfig = &tls.Config{}
}
transport.TLSClientConfig.InsecureSkipVerify = insecureSkipVerify
client := &http.Client{
Transport: transport,
Timeout: 5 * time.Second,
}
return client.Do(req)
}
}
if c.fetchCloudMetadata == nil {
c.fetchCloudMetadata = func(ctx context.Context, cloudEnvironment string) *types.CloudMetadata {
switch cloudEnvironment {
case "aws":
iid, err := utils.GetEC2InstanceIdentityDocument(ctx)
if err != nil {
return nil
}
return &types.CloudMetadata{
AWS: &types.AWSInfo{
AccountID: iid.AccountID,
InstanceID: iid.InstanceID,
},
}
default:
return nil
}
}
}
}
// fetch fetches all metadata.
func (c *fetchConfig) fetch(ctx context.Context) *Metadata {
metadata := &Metadata{
OS: c.fetchOS(),
OSVersion: c.fetchOSVersion(),
HostArchitecture: c.fetchHostArchitecture(),
GlibcVersion: c.fetchGlibcVersion(),
InstallMethods: c.fetchInstallMethods(),
ContainerRuntime: c.fetchContainerRuntime(),
ContainerOrchestrator: c.fetchContainerOrchestrator(ctx),
CloudEnvironment: c.fetchCloudEnvironment(ctx),
}
metadata.CloudMetadata = c.fetchCloudMetadata(ctx, metadata.CloudEnvironment)
return metadata
}
// fetchOS returns the value of GOOS.
func (c *fetchConfig) fetchOS() string {
return runtime.GOOS
}
// fetchHostArchitecture returns the value of GOARCH.
func (c *fetchConfig) fetchHostArchitecture() string {
return runtime.GOARCH
}
// fetchInstallMethods returns the list of methods used to install the instance.
func (c *fetchConfig) fetchInstallMethods() []string {
installMethods := []string{}
if c.dockerfileInstallMethod() {
installMethods = append(installMethods, "dockerfile")
}
if c.helmKubeAgentInstallMethod() {
installMethods = append(installMethods, "helm_kube_agent")
}
if c.nodeScriptInstallMethod() {
installMethods = append(installMethods, "node_script")
}
if c.systemctlInstallMethod() {
installMethods = append(installMethods, "systemctl")
}
if AWSOIDCDeployServiceInstallMethod() {
installMethods = append(installMethods, "awsoidc_deployservice")
}
return installMethods
}
// dockerfileInstallMethod returns true if the instance was installed using our
// Dockerfile.
func (c *fetchConfig) dockerfileInstallMethod() bool {
return c.boolEnvIsTrue("TELEPORT_INSTALL_METHOD_DOCKERFILE")
}
// helmKubeAgentInstallMethod returns true if the instance was installed using our
// Helm chart.
func (c *fetchConfig) helmKubeAgentInstallMethod() bool {
return c.boolEnvIsTrue("TELEPORT_INSTALL_METHOD_HELM_KUBE_AGENT")
}
// nodeScriptInstallMethod returns true if the instance was installed using our
// install-node.sh script.
func (c *fetchConfig) nodeScriptInstallMethod() bool {
return c.boolEnvIsTrue("TELEPORT_INSTALL_METHOD_NODE_SCRIPT")
}
// AWSOIDCDeployServiceInstallMethod returns true if the instance was installed using
// the DeployService action of the AWS OIDC integration.
// This install method uses Amazon ECS with Fargate deployment method.
func AWSOIDCDeployServiceInstallMethod() bool {
b, err := apiutils.ParseBool(os.Getenv(types.InstallMethodAWSOIDCDeployServiceEnvVar))
if err != nil {
return false
}
return b
}
// systemctlInstallMethod returns true if the instance is running using systemctl.
func (c *fetchConfig) systemctlInstallMethod() bool {
out, err := c.exec("systemctl", "status", "teleport.service")
if err != nil {
return false
}
return strings.Contains(out, "active (running)")
}
// fetchContainerRuntime returns "docker" if the file "/.dockerenv" exists.
func (c *fetchConfig) fetchContainerRuntime() string {
_, err := c.read("/.dockerenv")
if err != nil {
return ""
}
// If the file exists, we should be running on Docker.
return "docker"
}
// fetchContainerOrchestrator returns kubernetes-${GIT_VERSION} if the instance is
// running on kubernetes.
// This function performs the equivalent of the following:
// curl -k https://$KUBERNETES_SERVICE_HOST:$KUBERNETES_SERVICE_PORT/version | jq .gitVersion
func (c *fetchConfig) fetchContainerOrchestrator(ctx context.Context) string {
host := c.getenv("KUBERNETES_SERVICE_HOST")
port := c.getenv("KUBERNETES_SERVICE_PORT")
if host == "" || port == "" {
return ""
}
url := fmt.Sprintf("https://%s:%s/version", host, port)
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return ""
}
const insecureSkipVerify = true
resp, err := c.httpDo(req, insecureSkipVerify)
if err != nil {
return ""
}
defer resp.Body.Close()
body, err := utils.ReadAtMost(resp.Body, teleport.MaxHTTPResponseSize)
if err != nil {
return ""
}
var version struct {
GitVersion string `json:"gitVersion"`
}
if err := json.Unmarshal(body, &version); err != nil {
return ""
}
if version.GitVersion == "" {
return ""
}
return fmt.Sprintf("kubernetes-%s", version.GitVersion)
}
// fetchCloudEnvironment returns aws, gpc or azure if the instance is running on
// such cloud environments.
func (c *fetchConfig) fetchCloudEnvironment(ctx context.Context) string {
ctx, cancel := context.WithTimeout(ctx, 3*time.Second)
defer cancel()
// kick off 3 checks in parallel, at most 1 will succeed
checks := []struct {
env string
f func(context.Context) bool
}{
{"aws", c.awsHTTPGetSuccess},
{"gcp", c.gcpHTTPGetSuccess},
{"azure", c.azureHTTPGetSuccess},
}
cloudEnv := make(chan string, len(checks))
for _, check := range checks {
check := check
go func() {
if check.f(ctx) {
cloudEnv <- check.env
} else {
cloudEnv <- ""
}
}()
}
for range checks {
if env := <-cloudEnv; env != "" {
return env
}
}
return ""
}
// awsHTTPGetSuccess hits the AWS metadata endpoint in order to detect whether
// the instance is running on AWS.
// https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html
func (c *fetchConfig) awsHTTPGetSuccess(ctx context.Context) bool {
url := "http://169.254.169.254/latest/api/token"
req, err := http.NewRequestWithContext(ctx, http.MethodPut, url, nil)
if err != nil {
return false
}
req.Header.Add("X-aws-ec2-metadata-token-ttl-seconds", "300")
const insecureSkipVerify = false
resp, err := c.httpDo(req, insecureSkipVerify)
if err != nil {
return false
}
defer resp.Body.Close()
imdsToken, err := utils.ReadAtMost(resp.Body, teleport.MaxHTTPResponseSize)
if err != nil {
return false
}
if resp.StatusCode != http.StatusOK || imdsToken == nil {
return false
}
url = "http://169.254.169.254/latest/meta-data/"
req, err = http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return false
}
req.Header.Add("X-aws-ec2-metadata-token", string(imdsToken))
return c.httpReqSuccess(req)
}
// gcpHTTPGetSuccess hits the GCP metadata endpoint in order to detect whether
// the instance is running on GCP.
// https://cloud.google.com/compute/docs/metadata/overview#parts-of-a-request
func (c *fetchConfig) gcpHTTPGetSuccess(ctx context.Context) bool {
url := "http://metadata.google.internal/computeMetadata/v1"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return false
}
req.Header.Add("Metadata-Flavor", "Google")
return c.httpReqSuccess(req)
}
// azureHTTPGetSuccess hits the Azure metadata endpoint in order to detect whether
// the instance is running on Azure.
// https://learn.microsoft.com/en-us/azure/virtual-machines/instance-metadata-service
func (c *fetchConfig) azureHTTPGetSuccess(ctx context.Context) bool {
url := "http://169.254.169.254/metadata/instance?api-version=2021-02-01"
req, err := http.NewRequestWithContext(ctx, http.MethodGet, url, nil)
if err != nil {
return false
}
req.Header.Add("Metadata", "true")
return c.httpReqSuccess(req)
}
// exec runs a command and returns its output.
func (c *fetchConfig) exec(name string, args ...string) (string, error) {
out, err := c.execCommand(name, args...)
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// read reads a file and returns its content.
func (c *fetchConfig) read(name string) (string, error) {
out, err := c.readFile(name)
if err != nil {
return "", err
}
return strings.TrimSpace(string(out)), nil
}
// httpReqSuccess performs an http request, returning true if the status code
// is 200.
func (c *fetchConfig) httpReqSuccess(req *http.Request) bool {
const insecureSkipVerify = false
resp, err := c.httpDo(req, insecureSkipVerify)
if err != nil {
return false
}
defer resp.Body.Close()
return resp.StatusCode == http.StatusOK
}
// boolEnvIsTrue returns true if the environment variable is set to a value
// that represent true (e.g. true, yes, y, ...).
func (c *fetchConfig) boolEnvIsTrue(name string) bool {
b, err := apiutils.ParseBool(c.getenv(name))
if err != nil {
return false
}
return b
}