-
Notifications
You must be signed in to change notification settings - Fork 21
/
properties.go
304 lines (262 loc) · 8.78 KB
/
properties.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
package config
import (
"context"
"fmt"
"github.com/keikoproj/iam-manager/pkg/awsapi"
"github.com/keikoproj/iam-manager/pkg/k8s"
"github.com/keikoproj/iam-manager/pkg/log"
"k8s.io/api/core/v1"
"k8s.io/client-go/tools/cache"
"os"
"strconv"
"strings"
)
var (
Props *Properties
PolicyARNFormat = "arn:aws:iam::%s:policy/%s"
)
type Properties struct {
allowedPolicyAction []string
restrictedPolicyResources []string
restrictedS3Resources []string
awsAccountID string
managedPolicies []string
managedPermissionBoundaryPolicy string
awsRegion string
isWebhookEnabled string
maxRolesAllowed int
deriveNameFromNamespace string
controllerDesiredFrequency int
clusterName string
isIRSAEnabled string
clusterOIDCIssuerUrl string
defaultTrustPolicy string
}
func init() {
log := log.Logger(context.Background(), "internal.config.properties", "init")
if os.Getenv("LOCAL") != "" {
err := LoadProperties("LOCAL")
if err != nil {
log.Error(err, "failed to load properties for local environment")
return
}
log.Info("Loaded properties in init func for tests")
return
}
k8sClient, err := k8s.NewK8sClient()
if err != nil {
log.Error(err, "unable to create new k8s client")
panic(err)
}
res := k8sClient.GetConfigMap(context.Background(), IamManagerNamespaceName, IamManagerConfigMapName)
// load properties into a global variable
err = LoadProperties("", res)
if err != nil {
log.Error(err, "failed to load properties")
panic(err)
}
log.Info("Loaded properties in init func")
}
func LoadProperties(env string, cm ...*v1.ConfigMap) error {
log := log.Logger(context.Background(), "internal.config.properties", "LoadProperties")
// for local testing
if env != "" {
Props = &Properties{
allowedPolicyAction: strings.Split(os.Getenv("ALLOWED_POLICY_ACTION"), separator),
restrictedPolicyResources: strings.Split(os.Getenv("RESTRICTED_POLICY_RESOURCES"), separator),
restrictedS3Resources: strings.Split(os.Getenv("RESTRICTED_S3_RESOURCES"), separator),
awsAccountID: os.Getenv("AWS_ACCOUNT_ID"),
managedPolicies: strings.Split(os.Getenv("MANAGED_POLICIES"), separator),
managedPermissionBoundaryPolicy: os.Getenv("MANAGED_PERMISSION_BOUNDARY_POLICY"),
awsRegion: os.Getenv("AWS_REGION"),
isWebhookEnabled: os.Getenv("ENABLE_WEBHOOK"),
deriveNameFromNamespace: os.Getenv("DERIVE_NAME_FROM_NAMESPACE"),
clusterName: os.Getenv("CLUSTER_NAME"),
clusterOIDCIssuerUrl: os.Getenv("CLUSTER_OIDC_ISSUER_URL"),
defaultTrustPolicy: os.Getenv("DEFAULT_TRUST_POLICY"),
}
return nil
}
if len(cm) == 0 || cm[0] == nil {
log.Error(fmt.Errorf("config map cannot be nil"), "config map cannot be nil")
return fmt.Errorf("config map cannot be nil")
}
allowedPolicyAction := strings.Split(cm[0].Data[propertyIamPolicyWhitelist], separator)
restrictedPolicyResources := strings.Split(cm[0].Data[propertyIamPolicyBlacklist], separator)
restrictedS3Resources := strings.Split(cm[0].Data[propertyIamPolicyS3Restricted], separator)
clusterName := cm[0].Data[propertyClusterName]
defaultTrustPolicy := cm[0].Data[propertyDefaultTrustPolicy]
Props = &Properties{
allowedPolicyAction: allowedPolicyAction,
restrictedPolicyResources: restrictedPolicyResources,
restrictedS3Resources: restrictedS3Resources,
clusterName: clusterName,
defaultTrustPolicy: defaultTrustPolicy,
}
//Defaults
isWebhook := cm[0].Data[propertyWebhookEnabled]
if isWebhook == "true" {
Props.isWebhookEnabled = "true"
} else {
Props.isWebhookEnabled = "false"
}
deriveNameFromNS := cm[0].Data[propertyDeriveNameFromNameSpace]
if deriveNameFromNS == "true" {
Props.deriveNameFromNamespace = "true"
} else {
Props.deriveNameFromNamespace = "false"
}
awsRegion := cm[0].Data[propertyAwsRegion]
if awsRegion != "" {
Props.awsRegion = awsRegion
} else {
Props.awsRegion = "us-west-2"
}
maxRolesAllowed := cm[0].Data[propertyMaxIamRoles]
if maxRolesAllowed != "" {
maxRolesAllowed, err := strconv.Atoi(maxRolesAllowed)
if err != nil {
return err
}
Props.maxRolesAllowed = maxRolesAllowed
} else {
Props.maxRolesAllowed = 1
}
controllerDesiredFreq := cm[0].Data[propertyDesiredStateFrequency]
if controllerDesiredFreq != "" {
controllerDesiredFreq, err := strconv.Atoi(controllerDesiredFreq)
if err != nil {
return err
}
Props.controllerDesiredFrequency = controllerDesiredFreq
} else {
Props.controllerDesiredFrequency = 1800
}
awsAccountID := cm[0].Data[propertAWSAccountID]
// Load AWS account ID
if Props.awsAccountID == "" && awsAccountID == "" {
awsAccountID, err := awsapi.NewSTS(Props.awsRegion).GetAccountID(context.Background())
if err != nil {
return err
}
Props.awsAccountID = awsAccountID
} else {
Props.awsAccountID = awsAccountID
}
managedPermissionBoundaryPolicyArn := cm[0].Data[propertyPermissionBoundary]
if managedPermissionBoundaryPolicyArn == "" {
managedPermissionBoundaryPolicyArn = fmt.Sprintf(PolicyARNFormat, awsAccountID, "k8s-iam-manager-cluster-permission-boundary")
}
if !strings.HasPrefix(managedPermissionBoundaryPolicyArn, "arn:aws:iam::") {
managedPermissionBoundaryPolicyArn = fmt.Sprintf(PolicyARNFormat, awsAccountID, managedPermissionBoundaryPolicyArn)
}
Props.managedPermissionBoundaryPolicy = managedPermissionBoundaryPolicyArn
managedPolicies := strings.Split(cm[0].Data[propertyManagedPolicies], separator)
for i := range managedPolicies {
if !strings.HasPrefix(managedPolicies[i], "arn:aws:iam::") {
managedPolicies[i] = fmt.Sprintf(PolicyARNFormat, awsAccountID, managedPolicies[i])
}
}
Props.managedPolicies = managedPolicies
isIRSAEnabled := cm[0].Data[propertyIRSAEnabled]
if isIRSAEnabled == "true" {
Props.isIRSAEnabled = "true"
} else {
Props.isIRSAEnabled = "false"
}
oidcUrl := cm[0].Data[propertyK8sClusterOIDCIssuerUrl]
if isIRSAEnabled == "true" && oidcUrl == "" {
if clusterName == "" {
return fmt.Errorf("cluster name must be provided when IRSA is enabled to retrieve the OIDC url")
}
//call EKS describe cluster and get the OIDC URL
res, err := awsapi.NewEKS(Props.awsRegion).DescribeCluster(context.Background(), clusterName)
if err != nil {
return err
}
oidcUrl = *res.Cluster.Identity.Oidc.Issuer
}
Props.clusterOIDCIssuerUrl = oidcUrl
return nil
}
func (p *Properties) AllowedPolicyAction() []string {
return p.allowedPolicyAction
}
func (p *Properties) RestrictedPolicyResources() []string {
return p.restrictedPolicyResources
}
func (p *Properties) RestrictedS3Resources() []string {
return p.restrictedS3Resources
}
func (p *Properties) ManagedPolicies() []string {
return p.managedPolicies
}
func (p *Properties) AWSAccountID() string {
return p.awsAccountID
}
func (p *Properties) ManagedPermissionBoundaryPolicy() string {
return p.managedPermissionBoundaryPolicy
}
func (p *Properties) AWSRegion() string {
return p.awsRegion
}
func (p *Properties) IsWebHookEnabled() bool {
resp := false
if p.isWebhookEnabled == "true" {
resp = true
}
return resp
}
func (p *Properties) DeriveNameFromNamespace() bool {
resp := false
if p.deriveNameFromNamespace == "true" {
resp = true
}
return resp
}
func (p *Properties) MaxRolesAllowed() int {
return p.maxRolesAllowed
}
func (p *Properties) ControllerDesiredFrequency() int {
return p.controllerDesiredFrequency
}
func (p *Properties) IsIRSAEnabled() bool {
resp := false
if p.isIRSAEnabled == "true" {
resp = true
}
return resp
}
func (p *Properties) ClusterName() string {
return p.clusterName
}
func (p *Properties) OIDCIssuerUrl() string {
return p.clusterOIDCIssuerUrl
}
func (p *Properties) DefaultTrustPolicy() string {
return p.defaultTrustPolicy
}
func RunConfigMapInformer(ctx context.Context) {
log := log.Logger(context.Background(), "internal.config.properties", "RunConfigMapInformer")
cmInformer := k8s.GetConfigMapInformer(ctx, IamManagerNamespaceName, IamManagerConfigMapName)
cmInformer.AddEventHandler(cache.ResourceEventHandlerFuncs{
UpdateFunc: updateProperties,
},
)
log.Info("Starting config map informer")
cmInformer.Run(ctx.Done())
log.Info("Cancelling config map informer")
}
func updateProperties(old, new interface{}) {
log := log.Logger(context.Background(), "internal.config.properties", "updateProperties")
oldCM := old.(*v1.ConfigMap)
newCM := new.(*v1.ConfigMap)
if oldCM.ResourceVersion == newCM.ResourceVersion {
return
}
log.Info("Updating config map", "new revision ", newCM.ResourceVersion)
err := LoadProperties("", newCM)
if err != nil {
log.Error(err, "failed to update config map")
}
}