-
Notifications
You must be signed in to change notification settings - Fork 283
/
import.go
231 lines (223 loc) · 6.02 KB
/
import.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
// Copyright 2016 Yahoo Inc.
// Licensed under the terms of the Apache version 2.0 license. See LICENSE file for terms.
package zmscli
import (
"fmt"
"os"
"strconv"
"strings"
"github.com/ardielle/ardielle-go/rdl"
"github.com/yahoo/athenz/clients/go/zms"
)
func parseRoleMember(memberLine string) *zms.RoleMember {
memberFields := strings.Split(memberLine, " ")
roleMember := zms.NewRoleMember()
roleMember.MemberName = zms.ResourceName(memberFields[0])
if len(memberFields) > 1 {
expiration, err := rdl.TimestampParse(memberFields[1])
if err != nil {
fmt.Println("***", err)
} else {
roleMember.Expiration = &expiration
}
}
return roleMember
}
func (cli Zms) importRoles(dn string, lstRoles []interface{}, validatedAdmins []string, skipErrors bool) error {
for _, role := range lstRoles {
roleMap := role.(map[interface{}]interface{})
rn := roleMap["name"].(string)
fmt.Fprintf(os.Stdout, "Processing role "+rn+"...\n")
if val, ok := roleMap["members"]; ok {
mem := val.([]interface{})
roleMembers := make([]*zms.RoleMember, 0)
var err error
if rn == "admin" && validatedAdmins != nil {
// need to retrieve the current admin role
// and make sure to remove any existing admin
role, err := cli.Zms.GetRole(zms.DomainName(dn), "admin", nil, nil)
if err != nil {
return err
}
for _, mbr := range mem {
roleMember := parseRoleMember(mbr.(string))
if !cli.containsMember(role.RoleMembers, string(roleMember.MemberName)) {
roleMembers = append(roleMembers, roleMember)
}
}
for _, admin := range validatedAdmins {
roleMember := zms.NewRoleMember()
roleMember.MemberName = zms.ResourceName(admin)
if !cli.containsMember(roleMembers, admin) && !cli.containsMember(role.RoleMembers, admin) {
roleMembers = append(roleMembers, roleMember)
}
}
_, err = cli.AddRoleMembers(dn, rn, roleMembers)
} else {
for _, m := range mem {
roleMember := parseRoleMember(m.(string))
roleMembers = append(roleMembers, roleMember)
}
b := cli.Verbose
cli.Verbose = true
_, err = cli.AddGroupRole(dn, rn, roleMembers)
cli.Verbose = b
}
if err != nil {
if skipErrors {
fmt.Println("***", err)
} else {
return err
}
}
} else if val, ok := roleMap["trust"]; ok {
trust := val.(string)
_, err := cli.AddDelegatedRole(dn, rn, trust)
if err != nil {
if skipErrors {
fmt.Println("***", err)
} else {
return err
}
}
} else {
roleMembers := make([]*zms.RoleMember, 0)
b := cli.Verbose
cli.Verbose = true
_, err := cli.AddGroupRole(dn, rn, roleMembers)
cli.Verbose = b
if err != nil {
if skipErrors {
fmt.Println("***", err)
} else {
return err
}
}
}
}
return nil
}
func (cli Zms) importPolicies(dn string, lstPolicies []interface{}, skipErrors bool) error {
for _, policy := range lstPolicies {
policyMap := policy.(map[interface{}]interface{})
name := policyMap["name"].(string)
fmt.Fprintf(os.Stdout, "Processing policy "+name+"...\n")
assertions := make([]*zms.Assertion, 0)
if val, ok := policyMap["assertions"]; ok {
if val == nil {
fmt.Fprintf(os.Stdout, "Skipping empty policy: "+name+"\n")
continue
}
lst := val.([]interface{})
if len(lst) > 0 {
for _, a := range lst {
if name == "admin" && a.(string) == "grant * to admin on *" {
continue
}
assertion := strings.Split(a.(string), " ")
newAssertion, err := parseAssertion(dn, assertion)
if err != nil {
if skipErrors {
fmt.Println("***", err)
} else {
return err
}
}
assertions = append(assertions, newAssertion)
}
}
}
if name != "admin" {
_, err := cli.AddPolicyWithAssertions(dn, name, assertions)
if err != nil {
if skipErrors {
fmt.Println("***", err)
} else {
return err
}
}
}
}
return nil
}
func (cli Zms) generatePublicKeys(lstPublicKeys []interface{}) []*zms.PublicKeyEntry {
publicKeys := make([]*zms.PublicKeyEntry, 0)
if lstPublicKeys != nil {
for _, pubKey := range lstPublicKeys {
publicKeyMap := pubKey.(map[interface{}]interface{})
// if we're using just version numbers then yaml
// will interpret the key id as integer
var keyId string
switch v := publicKeyMap["keyId"].(type) {
case int:
keyId = strconv.Itoa(v)
case string:
keyId = v
default:
panic("Unknown data type for keyid")
}
value := publicKeyMap["value"].(string)
publicKey := zms.PublicKeyEntry{
Key: value,
Id: keyId,
}
publicKeys = append(publicKeys, &publicKey)
}
}
return publicKeys
}
func (cli Zms) importServices(dn string, lstServices []interface{}, skipErrors bool) error {
for _, service := range lstServices {
serviceMap := service.(map[interface{}]interface{})
name := serviceMap["name"].(string)
fmt.Fprintf(os.Stdout, "Processing service "+name+"...\n")
var lstPublicKeys []interface{}
if val, ok := serviceMap["publicKeys"]; ok {
lstPublicKeys = val.([]interface{})
}
publicKeys := cli.generatePublicKeys(lstPublicKeys)
_, err := cli.AddServiceWithKeys(dn, name, publicKeys)
if err != nil {
return err
}
if val, ok := serviceMap["providerEndpoint"]; ok {
endpoint := val.(string)
if endpoint != "" {
_, err = cli.SetServiceEndpoint(dn, name, endpoint)
if err != nil {
return err
}
}
}
user := ""
if val, ok := serviceMap["user"]; ok {
user = val.(string)
}
group := ""
if val, ok := serviceMap["group"]; ok {
group = val.(string)
}
exe := ""
if val, ok := serviceMap["executable"]; ok {
exe = val.(string)
}
if user != "" || group != "" || exe != "" {
_, err = cli.SetServiceExe(dn, name, exe, user, group)
if err != nil {
return err
}
}
if val, ok := serviceMap["hosts"]; ok {
hostList := val.([]interface{})
hosts := make([]string, 0)
for _, host := range hostList {
hosts = append(hosts, host.(string))
}
_, err = cli.AddServiceHost(dn, name, hosts)
if err != nil {
return err
}
}
}
return nil
}