forked from openshift/cloud-credential-operator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
create-ram-users.go
352 lines (304 loc) · 12.6 KB
/
create-ram-users.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
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
package alibabacloud
import (
"encoding/json"
"fmt"
"io/ioutil"
"log"
"os"
"path/filepath"
"github.com/pkg/errors"
"github.com/spf13/cobra"
alibabaerrors "github.com/aliyun/alibaba-cloud-sdk-go/sdk/errors"
"github.com/aliyun/alibaba-cloud-sdk-go/services/ram"
"github.com/openshift/cloud-credential-operator/pkg/alibabacloud"
credreqv1 "github.com/openshift/cloud-credential-operator/pkg/apis/cloudcredential/v1"
"github.com/openshift/cloud-credential-operator/pkg/cmd/provisioning"
)
const (
secretManifestsTemplate = `apiVersion: v1
stringData:
credentials: |-
[default]
type = access_key
access_key_id = %s
access_key_secret = %s
kind: Secret
metadata:
name: %s
namespace: %s
type: Opaque`
// ramPolicyType is the default policy type used for created Policies
ramPolicyType = "Custom"
// autoRotateStrategy will delete the earliest inactive policy version
autoRotateStrategy = "DeleteOldestNonDefaultVersionWhenLimitExceeded"
// ccoctlResourcePrefix is the prefix of the tag key applied to the Alibaba Cloud ram user created by ccoctl
ccoctlResourcePrefix = "openshift.io/ccoctl"
)
var (
// CreateRAMUsersOpts captures the options that affect creation/updating
// of the RAM Uers/Policies.
CreateRAMUsersOpts = options{
Region: "",
TargetDir: "",
Name: "",
CredRequestDir: "",
}
)
func createRAMUsersCmd(cmd *cobra.Command, args []string) {
client, err := alibabacloud.NewClient(CreateRAMUsersOpts.Region)
if err != nil {
log.Fatalf("Failed to create a client: %v", err)
}
err = createRAMUsers(client, CreateRAMUsersOpts.Name, CreateRAMUsersOpts.CredRequestDir,
CreateRAMUsersOpts.TargetDir, CreateRAMUsersOpts.EnableTechPreview)
if err != nil {
log.Fatalf(err.Error())
}
}
// createRAMUsers will create a ram user for the given credenital request and attach the specific ram policy
func createRAMUsers(client alibabacloud.Client, name, credReqDir, targetDir string, enableTechPreview bool) error {
// Process directory
credRequests, err := provisioning.GetListOfCredentialsRequests(credReqDir, enableTechPreview)
if err != nil {
return errors.Wrap(err, "Failed to process files containing CredentialsRequests")
}
// Create RAM Roles (with policies)
if err := processNewCredentialsRequests(client, credRequests, name, targetDir); err != nil {
return errors.Wrap(err, "Failed while processing each CredentialsRequest")
}
return nil
}
// StatementEntry is a simple type used to serialize to Alibaba Cloud' PolicyDocument format.
type StatementEntry struct {
Effect string
Action []string
Resource string
}
// PolicyDocument is a simple type used to serialize to Alibaba Cloud' PolicyDocument format.
type PolicyDocument struct {
Version string
Statement []StatementEntry
}
func createComponentPolicy(client alibabacloud.Client, policyName string, statements []credreqv1.AlibabaStatementEntry) error {
policyDocument := PolicyDocument{
Version: "1",
Statement: []StatementEntry{},
}
for _, entry := range statements {
policyDocument.Statement = append(policyDocument.Statement,
StatementEntry{
Effect: entry.Effect,
Action: entry.Action,
Resource: entry.Resource,
})
}
policyBytes, err := json.Marshal(&policyDocument)
if err != nil {
log.Fatalf("Failed to marshal the policy to JSON: %s", err)
}
req := ram.CreateGetPolicyRequest()
req.PolicyName = policyName
req.PolicyType = ramPolicyType
_, err = client.GetPolicy(req)
if err != nil {
aErr, ok := err.(*alibabaerrors.ServerError)
if ok && aErr.ErrorCode() == errorPolicyNotExists {
//create new policy
log.Printf("Ready for creating new ram policy %s", policyName)
preq := ram.CreateCreatePolicyRequest()
preq.PolicyName = policyName
preq.PolicyDocument = string(policyBytes)
preq.Description = "Created by OpenShift ccoctl"
_, err = client.CreatePolicy(preq)
if err == nil {
return nil
}
}
log.Fatalf("Failed to create new ram policy %s, err is %v", policyName, err)
}
//create new policy with new version and set it to the default one
vreq := ram.CreateCreatePolicyVersionRequest()
vreq.PolicyName = policyName
vreq.PolicyDocument = string(policyBytes)
vreq.RotateStrategy = autoRotateStrategy
vreq.SetAsDefault = "true"
_, err = client.CreatePolicyVersion(vreq)
return err
}
// attachComponentPolicy will attach the given policy to the preset component user
func attachComponentPolicy(client alibabacloud.Client, user, policyName string) error {
req := ram.CreateAttachPolicyToUserRequest()
req.PolicyName = policyName
req.UserName = user
req.PolicyType = ramPolicyType
_, err := client.AttachPolicyToUser(req)
if err != nil {
aErr, ok := err.(*alibabaerrors.ServerError)
if ok && aErr.ErrorCode() == errorUserAleadyAttachedPolicy {
return nil
}
}
return err
}
func processNewCredentialsRequests(client alibabacloud.Client, credReqs []*credreqv1.CredentialsRequest, name, targetDir string) error {
for _, cr := range credReqs {
err := createUserAndAttachPolicy(client, name, targetDir, cr)
if err != nil {
return errors.Wrap(err, fmt.Sprintf("Failed to create user and attach policy for CredentialsRequest %s", cr.Name))
}
}
return nil
}
func createUser(client alibabacloud.Client, name string, credReq *credreqv1.CredentialsRequest) (string, error) {
userName := fmt.Sprintf("%s-%s-%s", name, credReq.Spec.SecretRef.Namespace, credReq.Spec.SecretRef.Name)
shortName, displayName := generateRAMUserName(userName)
userReq := ram.CreateCreateUserRequest()
userReq.UserName = shortName
userReq.DisplayName = displayName
userReq.Comments = fmt.Sprintf("%s/%s", ccoctlResourcePrefix, name)
user, err := client.CreateUser(userReq)
if err != nil {
aErr, ok := err.(*alibabaerrors.ServerError)
if ok && aErr.ErrorCode() == errorUserAlreadyExists {
log.Printf("RAM User %s already exists, continuing", shortName)
return shortName, nil
} else {
return "", errors.Wrap(err, "Failed to create RAM user")
}
}
log.Printf("Created RAM User: %s", user.User.UserName)
return user.User.UserName, nil
}
func generateUserAccessKeys(client alibabacloud.Client, userName string) (*ram.CreateAccessKeyResponse, error) {
accessKeyReq := ram.CreateCreateAccessKeyRequest()
accessKeyReq.UserName = userName
accessKeyResp, err := client.CreateAccessKey(accessKeyReq)
if err != nil {
aErr, ok := err.(*alibabaerrors.ServerError)
if ok && aErr.ErrorCode() == errorAKLimitExceeded {
log.Printf("RAM User %s's accesskey number limit exceeded, will delete original accesskeys", userName)
listKeyReq := ram.CreateListAccessKeysRequest()
listKeyReq.UserName = userName
listKeyRes, err := client.ListAccessKeys(listKeyReq)
if err != nil {
return nil, errors.Wrap(err, "Failed to list accesskeys")
}
//get the older one or the keys with in-active status to delete
deleteKeys, err := getDeleteAccessKeys(listKeyRes.AccessKeys.AccessKey)
if err != nil {
return nil, errors.Wrap(err, "Failed to get the older ram accesskey")
}
for _, oneAK := range deleteKeys {
log.Printf("Ready to delete user %s accesskey %s", userName, oneAK.AccessKeyId)
deleteKeyReq := ram.CreateDeleteAccessKeyRequest()
deleteKeyReq.UserName = userName
deleteKeyReq.UserAccessKeyId = oneAK.AccessKeyId
_, err := client.DeleteAccessKey(deleteKeyReq)
if err != nil {
return nil, err
}
}
accessKeyResp, err = client.CreateAccessKey(accessKeyReq)
if err != nil {
return nil, err
}
log.Printf("Created access keys for RAM User: %s", userName)
return accessKeyResp, nil
}
return nil, errors.Wrap(err, "Failed to create RAM user access keys")
}
log.Printf("Created access keys for RAM User: %s", userName)
return accessKeyResp, nil
}
func createUserAndAttachPolicy(client alibabacloud.Client, name, targetDir string, credReq *credreqv1.CredentialsRequest) error {
policyName := generatePolicyName(fmt.Sprintf("%s-%s-%s-policy", name, credReq.Spec.SecretRef.Namespace, credReq.Spec.SecretRef.Name))
// Decode Alibaba CloudProviderSpec
codec, err := credreqv1.NewCodec()
if err != nil {
return errors.Wrap(err, "Failed to create credReq codec")
}
alibabaProviderSpec := credreqv1.AlibabaCloudProviderSpec{}
if err := codec.DecodeProviderSpec(credReq.Spec.ProviderSpec, &alibabaProviderSpec); err != nil {
return errors.Wrap(err, "Failed to decode the provider spec")
}
if alibabaProviderSpec.Kind != "AlibabaCloudProviderSpec" {
return fmt.Errorf("CredentialsRequest %s/%s is not of type Alibaba Cloud", credReq.Namespace, credReq.Name)
}
ramUserName, err := createUser(client, name, credReq)
if err != nil {
return errors.Wrap(err, "Failed while creating RAM User")
}
err = createComponentPolicy(client, policyName, alibabaProviderSpec.StatementEntries)
if err != nil {
return refineMissingRegionIdErr(err)
}
log.Printf("RAM policy %s has created", policyName)
err = attachComponentPolicy(client, ramUserName, policyName)
if err != nil {
return refineMissingRegionIdErr(err)
}
log.Printf("Policy %s has attached on user %s", policyName, ramUserName)
accessKeys, err := generateUserAccessKeys(client, ramUserName)
if err != nil {
return errors.Wrap(err, "Failed to generate RAM User access keys")
}
if err := writeCredReqSecret(credReq, targetDir, accessKeys.AccessKey.AccessKeyId, accessKeys.AccessKey.AccessKeySecret); err != nil {
return errors.Wrap(err, "Failed to save Secret for install manifests")
}
return nil
}
// writeCredReqSecret will take a CredentialsRequest and return
// a Secret with the AK/SK of a ram user who has grant component permission defined in CredentialsRequest.
func writeCredReqSecret(cr *credreqv1.CredentialsRequest, targetDir, accessKeyId, accessKeySecret string) error {
manifestsDir := filepath.Join(targetDir, provisioning.ManifestsDirName)
fileName := fmt.Sprintf("%s-%s-credentials.yaml", cr.Spec.SecretRef.Namespace, cr.Spec.SecretRef.Name)
filePath := filepath.Join(manifestsDir, fileName)
fileData := fmt.Sprintf(secretManifestsTemplate, accessKeyId, accessKeySecret, cr.Spec.SecretRef.Name, cr.Spec.SecretRef.Namespace)
if err := ioutil.WriteFile(filePath, []byte(fileData), 0600); err != nil {
return errors.Wrap(err, "Failed to save Secret file")
}
log.Printf("Saved credentials configuration to: %s", filePath)
return nil
}
// initEnvForCreateRAMUsersCmd ensure destination directory is ready to receive the generated files, and will create the directory if necessary.
func initEnvForCreateRAMUsersCmd(cmd *cobra.Command, args []string) {
if CreateRAMUsersOpts.TargetDir == "" {
pwd, err := os.Getwd()
if err != nil {
log.Fatalf("Failed to get current directory: %s", err)
}
CreateRAMUsersOpts.TargetDir = pwd
}
fPath, err := filepath.Abs(CreateRAMUsersOpts.TargetDir)
if err != nil {
log.Fatalf("Failed to resolve full path: %s", err)
}
// create target dir if necessary
err = provisioning.EnsureDir(fPath)
if err != nil {
log.Fatalf("Failed to create target directory at %s", fPath)
}
// create manifests dir if necessary
manifestsDir := filepath.Join(fPath, provisioning.ManifestsDirName)
err = provisioning.EnsureDir(manifestsDir)
if err != nil {
log.Fatalf("Failed to create manifests directory at %s", manifestsDir)
}
}
// NewCreateRAMUsersCmd provides the "create-ram-users" subcommand
func NewCreateRAMUsersCmd() *cobra.Command {
createRAMUsersCmd := &cobra.Command{
Use: "create-ram-users",
Short: "Create RAM Users and policies",
Run: createRAMUsersCmd,
PersistentPreRun: initEnvForCreateRAMUsersCmd,
}
createRAMUsersCmd.PersistentFlags().StringVar(&CreateRAMUsersOpts.Name, "name", "", "User-defined name for all created Alibaba Cloud resources (can be separate from the cluster's infra-id)")
createRAMUsersCmd.MarkPersistentFlagRequired("name")
createRAMUsersCmd.PersistentFlags().StringVar(&CreateRAMUsersOpts.CredRequestDir, "credentials-requests-dir", "", "Directory containing files of CredentialsRequests to create RAM AK for (can be created by running 'oc adm release extract --credentials-requests --cloud=alibabacloud' against an OpenShift release image)")
createRAMUsersCmd.MarkPersistentFlagRequired("credentials-requests-dir")
createRAMUsersCmd.PersistentFlags().StringVar(&CreateRAMUsersOpts.Region, "region", "", "Alibaba Cloud region endpoint only required for GovCloud")
createRAMUsersCmd.PersistentFlags().StringVar(&CreateRAMUsersOpts.TargetDir, "output-dir", "", "Directory to place generated files (defaults to current directory)")
createRAMUsersCmd.PersistentFlags().BoolVar(&CreateRAMUsersOpts.EnableTechPreview, "enable-tech-preview", false, "Opt into processing CredentialsRequests marked as tech-preview")
return createRAMUsersCmd
}