-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
rampolicy.go
183 lines (146 loc) · 4.83 KB
/
rampolicy.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
/*
Copyright 2019 The Kubernetes Authors.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package alitasks
import (
"fmt"
"net/url"
"github.com/denverdino/aliyungo/common"
"github.com/denverdino/aliyungo/ram"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/aliup"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
)
// +kops:fitask
type RAMPolicy struct {
Lifecycle *fi.Lifecycle
Name *string
RamRole *RAMRole
PolicyType *string
PolicyDocument fi.Resource
}
var _ fi.CompareWithID = &RAMPolicy{}
func (r *RAMPolicy) CompareWithID() *string {
return r.Name
}
func (r *RAMPolicy) Find(c *fi.Context) (*RAMPolicy, error) {
cloud := c.Cloud.(aliup.ALICloud)
policyRequest := ram.PolicyRequest{
PolicyName: fi.StringValue(r.Name),
PolicyType: ram.Type(fi.StringValue(r.PolicyType)),
}
policyResp, err := cloud.RamClient().GetPolicy(policyRequest)
if err != nil {
if e, ok := err.(*common.Error); ok && e.StatusCode == 404 {
klog.V(2).Infof("no RamPolicy with name: %q", *r.Name)
return nil, nil
}
return nil, fmt.Errorf("error get RamPolicy %s: %v", *r.Name, err)
}
klog.V(2).Infof("found matching RamPolicy with name: %q", *r.Name)
policy := policyResp.Policy
defaultPolicy, err := url.QueryUnescape(policyResp.DefaultPolicyVersion.PolicyDocument)
if err != nil {
return nil, fmt.Errorf("error parsing PolicyDocument for RAMPolicy %q: %v", fi.StringValue(r.Name), err)
}
actual := &RAMPolicy{
Name: fi.String(policy.PolicyName),
PolicyType: fi.String(string(policy.PolicyType)),
PolicyDocument: fi.NewStringResource(defaultPolicy),
}
// Avoid spurious changes
actual.RamRole = r.RamRole
actual.Lifecycle = r.Lifecycle
return actual, nil
}
func (r *RAMPolicy) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(r, c)
}
func (_ *RAMPolicy) CheckChanges(a, e, changes *RAMPolicy) error {
if e.PolicyDocument == nil {
return fi.RequiredField("PolicyDocument")
}
if e.Name == nil {
return fi.RequiredField("Name")
}
return nil
}
func (_ *RAMPolicy) RenderALI(t *aliup.ALIAPITarget, a, e, changes *RAMPolicy) error {
policy, err := e.policyDocumentString()
if err != nil {
return fmt.Errorf("error rendering PolicyDocument: %v", err)
}
policyRequest := ram.PolicyRequest{}
if a == nil {
klog.V(2).Infof("Creating RAMPolicy with Name:%q", fi.StringValue(e.Name))
policyRequest = ram.PolicyRequest{
PolicyName: fi.StringValue(e.Name),
PolicyDocument: policy,
PolicyType: ram.Type(fi.StringValue(e.PolicyType)),
}
_, err := t.Cloud.RamClient().CreatePolicy(policyRequest)
if err != nil {
return fmt.Errorf("error creating RAMPolicy: %v", err)
}
attachPolicyRequest := ram.AttachPolicyToRoleRequest{
PolicyRequest: policyRequest,
RoleName: fi.StringValue(e.RamRole.Name),
}
_, err = t.Cloud.RamClient().AttachPolicyToRole(attachPolicyRequest)
if err != nil {
return fmt.Errorf("error attaching RAMPolicy to RAMRole: %v", err)
}
return nil
}
return nil
}
func (r *RAMPolicy) policyDocumentString() (string, error) {
if r.PolicyDocument == nil {
return "", nil
}
return fi.ResourceAsString(r.PolicyDocument)
}
type terraformRAMPolicy struct {
Name *string `json:"name,omitempty" cty:"name"`
Document *string `json:"document,omitempty" cty:"document"`
}
type terraformRAMPolicyAttach struct {
PolicyName *terraform.Literal `json:"policy_name,omitempty" cty:"policy_name"`
PolicyType *string `json:"policy_type,omitempty" cty:"policy_type"`
RoleName *terraform.Literal `json:"role_name,omitempty" cty:"role_name"`
}
func (_ *RAMPolicy) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *RAMPolicy) error {
policyString, err := e.policyDocumentString()
if err != nil {
return fmt.Errorf("error rendering PolicyDocument: %v", err)
}
tf := &terraformRAMPolicy{
Name: e.Name,
Document: fi.String(policyString),
}
err = t.RenderResource("alicloud_ram_policy", *e.Name, tf)
if err != nil {
return err
}
policyType := "Custom"
tfAttach := &terraformRAMPolicyAttach{
PolicyName: e.TerraformLink(),
RoleName: e.RamRole.TerraformLink(),
PolicyType: &policyType,
}
err = t.RenderResource("alicloud_ram_role_policy_attachment", *e.Name, tfAttach)
return err
}
func (s *RAMPolicy) TerraformLink() *terraform.Literal {
return terraform.LiteralProperty("alicloud_ram_policy", *s.Name, "id")
}