-
-
Notifications
You must be signed in to change notification settings - Fork 153
Expand file tree
/
Copy pathyaml_func_aws.go
More file actions
151 lines (131 loc) · 4.38 KB
/
yaml_func_aws.go
File metadata and controls
151 lines (131 loc) · 4.38 KB
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
package exec
import (
"context"
errUtils "github.com/cloudposse/atmos/errors"
log "github.com/cloudposse/atmos/pkg/logger"
"github.com/cloudposse/atmos/pkg/perf"
"github.com/cloudposse/atmos/pkg/schema"
u "github.com/cloudposse/atmos/pkg/utils"
)
const (
execAWSYAMLFunction = "Executing Atmos YAML function"
invalidYAMLFunction = "Invalid YAML function"
failedGetIdentity = "Failed to get AWS caller identity"
functionKey = "function"
)
// processTagAwsValue is a shared helper for AWS YAML functions.
// It validates the input tag, retrieves AWS caller identity, and returns the requested value.
func processTagAwsValue(
atmosConfig *schema.AtmosConfiguration,
input string,
expectedTag string,
stackInfo *schema.ConfigAndStacksInfo,
extractor func(*AWSCallerIdentity) string,
) any {
log.Debug(execAWSYAMLFunction, functionKey, input)
// Validate the tag matches expected.
if input != expectedTag {
log.Error(invalidYAMLFunction, functionKey, input, "expected", expectedTag)
errUtils.CheckErrorPrintAndExit(errUtils.ErrYamlFuncInvalidArguments, "", "")
return nil
}
// Get auth context from stack info if available.
var authContext *schema.AWSAuthContext
if stackInfo != nil && stackInfo.AuthContext != nil && stackInfo.AuthContext.AWS != nil {
authContext = stackInfo.AuthContext.AWS
}
// Get the AWS caller identity (cached).
ctx := context.Background()
identity, err := getAWSCallerIdentityCached(ctx, atmosConfig, authContext)
if err != nil {
log.Error(failedGetIdentity, "error", err)
errUtils.CheckErrorPrintAndExit(err, "", "")
return nil
}
// Extract the requested value.
return extractor(identity)
}
// processTagAwsAccountID processes the !aws.account_id YAML function.
// It returns the AWS account ID of the current caller identity.
// The function takes no parameters.
//
// Usage in YAML:
//
// account_id: !aws.account_id
func processTagAwsAccountID(
atmosConfig *schema.AtmosConfiguration,
input string,
stackInfo *schema.ConfigAndStacksInfo,
) any {
defer perf.Track(atmosConfig, "exec.processTagAwsAccountID")()
result := processTagAwsValue(atmosConfig, input, u.AtmosYamlFuncAwsAccountID, stackInfo, func(id *AWSCallerIdentity) string {
return id.Account
})
if result != nil {
log.Debug("Resolved !aws.account_id", "account_id", result)
}
return result
}
// processTagAwsCallerIdentityArn processes the !aws.caller_identity_arn YAML function.
// It returns the ARN of the current AWS caller identity.
// The function takes no parameters.
//
// Usage in YAML:
//
// caller_arn: !aws.caller_identity_arn
func processTagAwsCallerIdentityArn(
atmosConfig *schema.AtmosConfiguration,
input string,
stackInfo *schema.ConfigAndStacksInfo,
) any {
defer perf.Track(atmosConfig, "exec.processTagAwsCallerIdentityArn")()
result := processTagAwsValue(atmosConfig, input, u.AtmosYamlFuncAwsCallerIdentityArn, stackInfo, func(id *AWSCallerIdentity) string {
return id.Arn
})
if result != nil {
log.Debug("Resolved !aws.caller_identity_arn", "arn", result)
}
return result
}
// processTagAwsCallerIdentityUserID processes the !aws.caller_identity_user_id YAML function.
// It returns the unique user ID of the current AWS caller identity.
// The function takes no parameters.
//
// Usage in YAML:
//
// user_id: !aws.caller_identity_user_id
func processTagAwsCallerIdentityUserID(
atmosConfig *schema.AtmosConfiguration,
input string,
stackInfo *schema.ConfigAndStacksInfo,
) any {
defer perf.Track(atmosConfig, "exec.processTagAwsCallerIdentityUserID")()
result := processTagAwsValue(atmosConfig, input, u.AtmosYamlFuncAwsCallerIdentityUserID, stackInfo, func(id *AWSCallerIdentity) string {
return id.UserID
})
if result != nil {
log.Debug("Resolved !aws.caller_identity_user_id", "user_id", result)
}
return result
}
// processTagAwsRegion processes the !aws.region YAML function.
// It returns the AWS region from the current configuration.
// The function takes no parameters.
//
// Usage in YAML:
//
// region: !aws.region
func processTagAwsRegion(
atmosConfig *schema.AtmosConfiguration,
input string,
stackInfo *schema.ConfigAndStacksInfo,
) any {
defer perf.Track(atmosConfig, "exec.processTagAwsRegion")()
result := processTagAwsValue(atmosConfig, input, u.AtmosYamlFuncAwsRegion, stackInfo, func(id *AWSCallerIdentity) string {
return id.Region
})
if result != nil {
log.Debug("Resolved !aws.region", "region", result)
}
return result
}