-
Notifications
You must be signed in to change notification settings - Fork 4.7k
/
Copy pathiamoidcprovider.go
253 lines (216 loc) · 7.3 KB
/
iamoidcprovider.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
/*
Copyright 2020 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 awstasks
import (
"errors"
"fmt"
"strings"
"github.com/aws/aws-sdk-go/aws"
"github.com/aws/aws-sdk-go/service/iam"
"k8s.io/apimachinery/pkg/util/sets"
"k8s.io/klog/v2"
"k8s.io/kops/upup/pkg/fi"
"k8s.io/kops/upup/pkg/fi/cloudup/awsup"
"k8s.io/kops/upup/pkg/fi/cloudup/cloudformation"
"k8s.io/kops/upup/pkg/fi/cloudup/terraform"
"k8s.io/kops/upup/pkg/fi/cloudup/terraformWriter"
)
// +kops:fitask
type IAMOIDCProvider struct {
Lifecycle fi.Lifecycle
ClientIDs []*string
Thumbprints []*string
URL *string
Name *string
Tags map[string]string
arn *string
}
var _ fi.CompareWithID = &IAMOIDCProvider{}
func (e *IAMOIDCProvider) CompareWithID() *string {
return e.Name
}
func (e *IAMOIDCProvider) Find(c *fi.Context) (*IAMOIDCProvider, error) {
cloud := c.Cloud.(awsup.AWSCloud)
response, err := cloud.IAM().ListOpenIDConnectProviders(&iam.ListOpenIDConnectProvidersInput{})
if err != nil {
return nil, fmt.Errorf("error listing oidc providers: %v", err)
}
providers := response.OpenIDConnectProviderList
for _, provider := range providers {
arn := provider.Arn
descResp, err := cloud.IAM().GetOpenIDConnectProvider(&iam.GetOpenIDConnectProviderInput{
OpenIDConnectProviderArn: arn,
})
if err != nil {
return nil, fmt.Errorf("error describing oidc provider: %v", err)
}
// AWS does not return the https:// in the url
actualURL := aws.StringValue(descResp.Url)
if !strings.Contains(actualURL, "://") {
actualURL = "https://" + actualURL
}
if actualURL == fi.StringValue(e.URL) {
actual := &IAMOIDCProvider{
ClientIDs: descResp.ClientIDList,
Thumbprints: descResp.ThumbprintList,
URL: &actualURL,
Tags: mapIAMTagsToMap(descResp.Tags),
arn: arn,
}
actual.Lifecycle = e.Lifecycle
actual.Name = e.Name
klog.V(2).Infof("found matching IAMOIDCProvider %q", aws.StringValue(arn))
return actual, nil
}
}
return nil, nil
}
func (e *IAMOIDCProvider) Run(c *fi.Context) error {
return fi.DefaultDeltaRunMethod(e, c)
}
func (s *IAMOIDCProvider) CheckChanges(a, e, changes *IAMOIDCProvider) error {
if e.URL == nil {
return fi.RequiredField("URL")
}
if e.ClientIDs == nil {
return fi.RequiredField("ClientIDs")
}
if len(e.Thumbprints) == 0 {
return fi.RequiredField("Thumbprints")
}
if a != nil {
if changes.URL != nil {
return fi.CannotChangeField("URL")
}
}
return nil
}
func (p *IAMOIDCProvider) RenderAWS(t *awsup.AWSAPITarget, a, e, changes *IAMOIDCProvider) error {
thumbprints := e.Thumbprints
if a == nil {
klog.V(2).Infof("Creating IAMOIDCProvider with Name:%q", *e.Name)
request := &iam.CreateOpenIDConnectProviderInput{
ClientIDList: e.ClientIDs,
ThumbprintList: thumbprints,
Url: e.URL,
Tags: mapToIAMTags(e.Tags),
}
response, err := t.Cloud.IAM().CreateOpenIDConnectProvider(request)
if err != nil {
return fmt.Errorf("error creating IAMOIDCProvider: %v", err)
}
e.arn = response.OpenIDConnectProviderArn
} else {
if changes.Thumbprints != nil {
klog.V(2).Infof("Updating IAMOIDCProvider Thumbprints %q", fi.StringValue(e.arn))
request := &iam.UpdateOpenIDConnectProviderThumbprintInput{}
request.OpenIDConnectProviderArn = a.arn
request.ThumbprintList = thumbprints
_, err := t.Cloud.IAM().UpdateOpenIDConnectProviderThumbprint(request)
if err != nil {
return fmt.Errorf("error updating IAMOIDCProvider Thumbprints: %v", err)
}
}
if changes.Tags != nil {
if len(a.Tags) > 0 {
existingTagKeys := make([]*string, 0)
for k := range a.Tags {
existingTagKeys = append(existingTagKeys, &k)
}
untagRequest := &iam.UntagOpenIDConnectProviderInput{
OpenIDConnectProviderArn: a.arn,
TagKeys: existingTagKeys,
}
_, err := t.Cloud.IAM().UntagOpenIDConnectProvider(untagRequest)
if err != nil {
return fmt.Errorf("error untagging IAMOIDCProvider: %v", err)
}
}
if len(e.Tags) > 0 {
tagRequest := &iam.TagOpenIDConnectProviderInput{
OpenIDConnectProviderArn: a.arn,
Tags: mapToIAMTags(e.Tags),
}
_, err := t.Cloud.IAM().TagOpenIDConnectProvider(tagRequest)
if err != nil {
return fmt.Errorf("error tagging IAMOIDCProvider: %v", err)
}
}
}
if changes.ClientIDs != nil {
actual := sets.NewString()
for _, aud := range a.ClientIDs {
actual.Insert(*aud)
}
expected := sets.NewString()
for _, aud := range e.ClientIDs {
expected.Insert(*aud)
}
toRemove := actual.Difference(expected)
for _, elem := range toRemove.List() {
request := &iam.RemoveClientIDFromOpenIDConnectProviderInput{
OpenIDConnectProviderArn: a.arn,
ClientID: &elem,
}
_, err := t.Cloud.IAM().RemoveClientIDFromOpenIDConnectProvider(request)
if err != nil {
return fmt.Errorf("error removing audience %s to IAMOIDCProvider: %v", elem, err)
}
}
toAdd := expected.Difference(actual)
for _, elem := range toAdd.List() {
request := &iam.AddClientIDToOpenIDConnectProviderInput{
OpenIDConnectProviderArn: a.arn,
ClientID: &elem,
}
_, err := t.Cloud.IAM().AddClientIDToOpenIDConnectProvider(request)
if err != nil {
return fmt.Errorf("error adding audience %s to IAMOIDCProvider: %v", elem, err)
}
}
}
}
return nil
}
type terraformIAMOIDCProvider struct {
URL *string `json:"url" cty:"url"`
ClientIDList []*string `json:"client_id_list" cty:"client_id_list"`
ThumbprintList []*string `json:"thumbprint_list" cty:"thumbprint_list"`
AssumeRolePolicy *terraformWriter.Literal `json:"assume_role_policy" cty:"assume_role_policy"`
Tags map[string]string `json:"tags,omitempty" cty:"tags"`
}
func (p *IAMOIDCProvider) RenderTerraform(t *terraform.TerraformTarget, a, e, changes *IAMOIDCProvider) error {
err := t.AddOutputVariable("iam_openid_connect_provider_arn", e.TerraformLink())
if err != nil {
return err
}
issuerSubs := strings.SplitAfter(aws.StringValue(e.URL), "://")
issuer := issuerSubs[len(issuerSubs)-1]
err = t.AddOutputVariable("iam_openid_connect_provider_issuer", terraformWriter.LiteralFromStringValue(issuer))
if err != nil {
return err
}
tf := &terraformIAMOIDCProvider{
URL: e.URL,
ClientIDList: e.ClientIDs,
ThumbprintList: e.Thumbprints,
Tags: e.Tags,
}
return t.RenderResource("aws_iam_openid_connect_provider", *e.Name, tf)
}
func (e *IAMOIDCProvider) TerraformLink() *terraformWriter.Literal {
return terraformWriter.LiteralProperty("aws_iam_openid_connect_provider", *e.Name, "arn")
}
func (_ *IAMOIDCProvider) RenderCloudformation(t *cloudformation.CloudformationTarget, a, e, changes *IAMOIDCProvider) error {
return errors.New("cloudformation does not support IAM OIDC Provider")
}