-
Notifications
You must be signed in to change notification settings - Fork 213
/
Copy pathsdkutil.go
206 lines (170 loc) · 5.19 KB
/
sdkutil.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
// Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved.
// SPDX-License-Identifier: MIT
package util
import (
"fmt"
"os"
"os/user"
"path/filepath"
"runtime"
"github.com/aws/aws-sdk-go/aws/session"
"github.com/aws/amazon-cloudwatch-agent/cfg/commonconfig"
"github.com/aws/amazon-cloudwatch-agent/translator"
"github.com/aws/amazon-cloudwatch-agent/translator/config"
"github.com/aws/amazon-cloudwatch-agent/translator/util/ec2util"
"github.com/aws/amazon-cloudwatch-agent/translator/util/ecsutil"
"github.com/aws/amazon-cloudwatch-agent/translator/util/eksdetector"
)
const (
DEFAULT_PROFILE = "AmazonCloudWatchAgent"
)
var DetectRegion = detectRegion
var DetectCredentialsPath = detectCredentialsPath
var DefaultEC2Region = defaultEC2Region
var DefaultECSRegion = defaultECSRegion
var IsEKS = isEKS
var runInAws = os.Getenv(config.RUN_IN_AWS)
var runWithIrsa = os.Getenv(config.RUN_WITH_IRSA)
func DetectAgentMode(configuredMode string) string {
if configuredMode != "auto" {
return configuredMode
}
if runInAws == config.RUN_IN_AWS_TRUE {
fmt.Println("I! Detected from ENV instance is EC2")
return config.ModeEC2
}
if runWithIrsa == config.RUN_WITH_IRSA_TRUE {
fmt.Println("I! Detected from ENV RUN_WITH_IRSA is True")
return config.ModeWithIRSA
}
if DefaultEC2Region() != "" {
fmt.Println("I! Detected the instance is EC2")
return config.ModeEC2
}
if DefaultECSRegion() != "" {
fmt.Println("I! Detected the instance is ECS")
return config.ModeEC2
}
fmt.Println("I! Detected the instance is OnPremise")
return config.ModeOnPrem
}
func DetectKubernetesMode(configuredMode string) string {
isEKS := IsEKS()
if isEKS.Err != nil {
return "" // not kubernetes
}
if isEKS.Value {
return config.ModeEKS
}
if configuredMode == config.ModeEC2 {
return config.ModeK8sEC2
}
return config.ModeK8sOnPrem
}
func SDKRegionWithCredsMap(mode string, credsConfig map[string]string) (region string) {
credsMap := GetCredentials(mode, credsConfig)
profile, profile_ok := credsMap[commonconfig.CredentialProfile]
sharedConfigFile, sharedConfigFile_ok := credsMap[commonconfig.CredentialFile]
if !profile_ok && !sharedConfigFile_ok {
return ""
}
opts := session.Options{}
if profile_ok {
opts.Profile = profile
}
if sharedConfigFile_ok {
exPath := filepath.Dir(sharedConfigFile)
opts.SharedConfigFiles = []string{sharedConfigFile, exPath + "/config"}
}
CheckAndSetHomeDir()
opts.SharedConfigState = session.SharedConfigEnable
ses, err := session.NewSessionWithOptions(opts)
if err != nil {
return ""
}
if ses.Config != nil && ses.Config.Region != nil {
region = *ses.Config.Region
fmt.Println("I! SDKRegionWithCredsMap region: ", region)
}
return region
}
func defaultEC2Region() string {
return ec2util.GetEC2UtilSingleton().Region
}
func defaultECSRegion() string {
return ecsutil.GetECSUtilSingleton().Region
}
func isEKS() eksdetector.IsEKSCache {
return eksdetector.IsEKS()
}
func detectRegion(mode string, credsConfig map[string]string) (region string, regionType string) {
region = SDKRegionWithCredsMap(mode, credsConfig)
regionType = config.RegionTypeNotFound
if region != "" {
regionType = config.RegionTypeCredsMap
}
// For ec2, fallback to metadata when no region info found in credential profile.
if region == "" && mode == config.ModeEC2 {
fmt.Println("I! Trying to detect region from ec2")
region = DefaultEC2Region()
regionType = config.RegionTypeEC2Metadata
}
// try to get region from ecs metadata
if region == "" && mode == config.ModeEC2 {
fmt.Println("I! Trying to detect region from ecs")
region = DefaultECSRegion()
regionType = config.RegionTypeECSMetadata
}
return
}
func CheckAndSetHomeDir() {
homeDir := detectHomeDirectory()
if runtime.GOOS == config.OS_TYPE_WINDOWS {
os.Setenv("USERPROFILE", homeDir)
fmt.Println("I! Set home dir windows: " + homeDir)
} else {
os.Setenv("HOME", homeDir)
fmt.Println("I! Set home dir Linux: " + homeDir)
}
}
func detectCredentialsPath() (credentialsPath string) {
homeDir := detectHomeDirectory()
return filepath.Join(homeDir, ".aws", "credentials")
}
func detectHomeDirectory() string {
var homeDir string
if runtime.GOOS == config.OS_TYPE_WINDOWS {
// the cwagent process is always running under user "System"
systemDrivePath := GetWindowsSystemDrivePath() // C:
homeDir = systemDrivePath + "\\Users\\Administrator"
} else {
if usr, err := user.Current(); err == nil {
homeDir = usr.HomeDir
}
if homeDir == "" {
if runtime.GOOS == config.OS_TYPE_DARWIN {
homeDir = "/var/root"
} else {
homeDir = "/root"
}
}
}
fmt.Println("Got Home directory: " + homeDir)
if homeDir == "" {
translator.AddErrorMessages("/translator/util/sdkutil", "Can not get the correct Home directory")
}
return homeDir
}
func GetCredentials(mode string, credsConfig map[string]string) (result map[string]string) {
result = map[string]string{}
for k, v := range credsConfig {
result[k] = v
}
profile, hasProfile := credsConfig[commonconfig.CredentialProfile]
if hasProfile {
result[commonconfig.CredentialProfile] = profile
} else if (mode == config.ModeOnPrem) || (mode == config.ModeOnPremise) {
result[commonconfig.CredentialProfile] = DEFAULT_PROFILE
}
return
}