-
Notifications
You must be signed in to change notification settings - Fork 9
/
ops.go
142 lines (116 loc) · 4.35 KB
/
ops.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
// Copyright 2022 Namespace Labs Inc; All rights reserved.
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
package iam
import (
"context"
"errors"
"github.com/aws/aws-sdk-go-v2/service/iam"
"github.com/aws/aws-sdk-go-v2/service/iam/types"
"namespacelabs.dev/foundation/internal/fnerrors"
awsprovider "namespacelabs.dev/foundation/internal/providers/aws"
"namespacelabs.dev/foundation/schema"
"namespacelabs.dev/foundation/std/execution"
)
func RegisterGraphHandlers() {
execution.RegisterFuncs(execution.Funcs[*OpEnsureRole]{
Aliases: []string{"type.googleapis.com/foundation.providers.aws.iam.OpEnsureRole"},
Handle: func(ctx context.Context, def *schema.SerializedInvocation, m *OpEnsureRole) (*execution.HandleResult, error) {
if m.AssumeRolePolicyJson == "" || m.RoleName == "" {
return nil, fnerrors.BadInputError("both role_name and assume_role_policy_json are required")
}
config, err := execution.Get(ctx, execution.ConfigurationInjection)
if err != nil {
return nil, err
}
sesh, err := awsprovider.MustConfiguredSession(ctx, config)
if err != nil {
return nil, err
}
input := &iam.CreateRoleInput{
RoleName: &m.RoleName,
AssumeRolePolicyDocument: &m.AssumeRolePolicyJson,
}
if m.Description != "" {
input.Description = &m.Description
}
addTags(&input.Tags, m.Tag, m.ForServer)
iamcli := iam.NewFromConfig(sesh.Config())
if _, err := iamcli.CreateRole(ctx, input); err != nil {
var e *types.EntityAlreadyExistsException
if errors.As(err, &e) {
if _, err := iamcli.TagRole(ctx, &iam.TagRoleInput{Tags: input.Tags, RoleName: input.RoleName}); err != nil {
return nil, fnerrors.InvocationError("aws", "IAM role already existed, and failed to update its tags: %w", err)
}
if _, err := iamcli.UpdateAssumeRolePolicy(ctx, &iam.UpdateAssumeRolePolicyInput{
RoleName: &m.RoleName,
PolicyDocument: &m.AssumeRolePolicyJson,
}); err != nil {
return nil, fnerrors.InvocationError("aws", "IAM role already existed, and failed to update its policy: %w", err)
}
} else {
return nil, fnerrors.InvocationError("aws", "failed to create IAM role: %w", err)
}
}
return nil, nil
},
PlanOrder: func(context.Context, *OpEnsureRole) (*schema.ScheduleOrder, error) {
return &schema.ScheduleOrder{
SchedCategory: []string{"aws:iam:ensure-role"},
}, nil
},
})
execution.RegisterFuncs(execution.Funcs[*OpAssociatePolicy]{
Aliases: []string{"type.googleapis.com/foundation.providers.aws.iam.OpAssociatePolicy"},
Handle: func(ctx context.Context, def *schema.SerializedInvocation, m *OpAssociatePolicy) (*execution.HandleResult, error) {
if m.PolicyJson == "" || m.PolicyName == "" || m.RoleName == "" {
return nil, fnerrors.BadInputError("all of `role_name` and `policy_name` and `policy_json` are required")
}
config, err := execution.Get(ctx, execution.ConfigurationInjection)
if err != nil {
return nil, err
}
sesh, err := awsprovider.MustConfiguredSession(ctx, config)
if err != nil {
return nil, err
}
input := &iam.PutRolePolicyInput{
PolicyName: &m.PolicyName,
PolicyDocument: &m.PolicyJson,
RoleName: &m.RoleName,
}
iamcli := iam.NewFromConfig(sesh.Config())
if _, err := iamcli.PutRolePolicy(ctx, input); err != nil {
var e *types.EntityAlreadyExistsException
if !errors.As(err, &e) {
return nil, fnerrors.InvocationError("aws", "failed to attach policy to role: %w", err)
}
}
return nil, nil
},
PlanOrder: func(context.Context, *OpAssociatePolicy) (*schema.ScheduleOrder, error) {
// Run policy associations after we create roles.
return &schema.ScheduleOrder{
SchedAfterCategory: []string{"aws:iam:ensure-role"},
}, nil
},
})
}
type tagLike interface {
GetKey() string
GetValue() string
}
func addTags[V tagLike](tags *[]types.Tag, userSpecified []V, srv *schema.Server) {
for _, t := range userSpecified {
*tags = append(*tags, tag(t.GetKey(), t.GetValue()))
}
if srv != nil {
*tags = append(*tags,
tag("alpha.foundation.namespacelabs.com/server-id", srv.Id),
tag("alpha.foundation.namespacelabs.com/server-package-name", srv.PackageName),
)
}
}
func tag(k, v string) types.Tag {
return types.Tag{Key: &v, Value: &v}
}