forked from gravitational/teleport
/
imds.go
132 lines (111 loc) · 4 KB
/
imds.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
/*
* 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 cloud
import (
"context"
"time"
"github.com/gravitational/trace"
"github.com/gravitational/teleport/api/types"
"github.com/gravitational/teleport/lib/cloud/aws"
"github.com/gravitational/teleport/lib/cloud/azure"
)
const (
// discoverInstanceMetadataTimeout is the maximum amount of time allowed
// to discover an instance metadata service. The timeout is short to
// minimize Teleport's startup time when it isn't running on any cloud
// instance. Checking for instance metadata typically takes less than 30ms.
discoverInstanceMetadataTimeout = 500 * time.Millisecond
)
// InstanceMetadata is an interface for fetching information from a cloud
// service's instance metadata.
type InstanceMetadata interface {
// IsAvailable checks if instance metadata is available.
IsAvailable(ctx context.Context) bool
// GetTags gets all of the instance's tags.
GetTags(ctx context.Context) (map[string]string, error)
// GetHostname gets the hostname set by the cloud instance that Teleport
// should use, if any.
GetHostname(ctx context.Context) (string, error)
// GetType gets the cloud instance type.
GetType() types.InstanceMetadataType
// GetID gets the cloud instance ID.
GetID(ctx context.Context) (string, error)
}
type imConstructor func(ctx context.Context) (InstanceMetadata, error)
var providers = map[types.InstanceMetadataType]imConstructor{
types.InstanceMetadataTypeEC2: initEC2,
types.InstanceMetadataTypeAzure: initAzure,
}
func initEC2(ctx context.Context) (InstanceMetadata, error) {
im, err := aws.NewInstanceMetadataClient(ctx)
return im, trace.Wrap(err)
}
func initAzure(ctx context.Context) (InstanceMetadata, error) {
return azure.NewInstanceMetadataClient(), nil
}
// DiscoverInstanceMetadata checks which cloud instance type Teleport is
// running on, if any.
func DiscoverInstanceMetadata(ctx context.Context) (InstanceMetadata, error) {
ctx, cancel := context.WithTimeout(ctx, discoverInstanceMetadataTimeout)
defer cancel()
c := make(chan InstanceMetadata)
clients := make([]InstanceMetadata, 0, len(providers))
for _, constructor := range providers {
im, err := constructor(ctx)
if err != nil {
return nil, trace.Wrap(err)
}
clients = append(clients, im)
}
for _, client := range clients {
client := client
go func() {
if client.IsAvailable(ctx) {
c <- client
}
}()
}
select {
case client := <-c:
return client, nil
case <-ctx.Done():
return nil, trace.NotFound("no instance metadata service found")
}
}
// DisabledIMDSClient is an EC2 instance metadata client that is always disabled. This is faster
// than the default client when not testing instance metadata behavior.
type DisabledIMDSClient struct{}
// NewDisabledIMDSClient creates a new DisabledIMDSClient.
func NewDisabledIMDSClient() InstanceMetadata {
return &DisabledIMDSClient{}
}
func (d *DisabledIMDSClient) IsAvailable(ctx context.Context) bool {
return false
}
func (d *DisabledIMDSClient) GetTags(ctx context.Context) (map[string]string, error) {
return nil, nil
}
func (d *DisabledIMDSClient) GetHostname(ctx context.Context) (string, error) {
return "", nil
}
func (d *DisabledIMDSClient) GetType() types.InstanceMetadataType {
return types.InstanceMetadataTypeDisabled
}
func (d *DisabledIMDSClient) GetID(ctx context.Context) (string, error) {
return "", nil
}