-
Notifications
You must be signed in to change notification settings - Fork 311
/
Copy pathcredentials.go
251 lines (209 loc) · 6.44 KB
/
credentials.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
package graph
import (
"fmt"
"strings"
"time"
"github.com/Azure/azure-sdk-for-go/services/graphrbac/1.6/graphrbac"
"github.com/Azure/go-autorest/autorest/date"
"github.com/hashicorp/go-uuid"
"github.com/hashicorp/terraform-plugin-sdk/helper/resource"
"github.com/hashicorp/terraform-plugin-sdk/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/helper/validation"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/ar"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/p"
"github.com/terraform-providers/terraform-provider-azuread/azuread/helpers/validate"
)
// valid types are `application` and `service_principal`
func PasswordResourceSchema(object_type string) map[string]*schema.Schema {
return map[string]*schema.Schema{
object_type + "_id": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
ValidateFunc: validate.UUID,
},
"key_id": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validate.UUID,
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
},
"value": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Sensitive: true,
ValidateFunc: validation.StringLenBetween(1, 863), // Encrypted secret cannot be empty and can be at most 1024 bytes.
},
"start_date": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ValidateFunc: validation.IsRFC3339Time,
},
"end_date": {
Type: schema.TypeString,
Optional: true,
Computed: true,
ForceNew: true,
ExactlyOneOf: []string{"end_date_relative"},
ValidateFunc: validation.IsRFC3339Time,
},
"end_date_relative": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
ExactlyOneOf: []string{"end_date"},
ValidateFunc: validate.NoEmptyStrings,
},
}
}
type PasswordCredentialId struct {
ObjectId string
KeyId string
}
func (id PasswordCredentialId) String() string {
return id.ObjectId + "/" + id.KeyId
}
func ParsePasswordCredentialId(id string) (PasswordCredentialId, error) {
parts := strings.Split(id, "/")
if len(parts) != 2 {
return PasswordCredentialId{}, fmt.Errorf("Password Credential ID should be in the format {objectId}/{keyId} - but got %q", id)
}
if _, err := uuid.ParseUUID(parts[0]); err != nil {
return PasswordCredentialId{}, fmt.Errorf("Object ID isn't a valid UUID (%q): %+v", id[0], err)
}
if _, err := uuid.ParseUUID(parts[1]); err != nil {
return PasswordCredentialId{}, fmt.Errorf("Credential ID isn't a valid UUID (%q): %+v", id[1], err)
}
return PasswordCredentialId{
ObjectId: parts[0],
KeyId: parts[1],
}, nil
}
func PasswordCredentialIdFrom(objectId, keyId string) PasswordCredentialId {
return PasswordCredentialId{
ObjectId: objectId,
KeyId: keyId,
}
}
func PasswordCredentialForResource(d *schema.ResourceData) (*graphrbac.PasswordCredential, error) {
value := d.Get("value").(string)
// errors should be handled by the validation
var keyId string
if v, ok := d.GetOk("key_id"); ok {
keyId = v.(string)
} else {
kid, err := uuid.GenerateUUID()
if err != nil {
return nil, err
}
keyId = kid
}
var endDate time.Time
if v := d.Get("end_date").(string); v != "" {
endDate, _ = time.Parse(time.RFC3339, v)
} else if v := d.Get("end_date_relative").(string); v != "" {
d, err := time.ParseDuration(v)
if err != nil {
return nil, fmt.Errorf("unable to parse `end_date_relative` (%s) as a duration", v)
}
endDate = time.Now().Add(d)
} else {
return nil, fmt.Errorf("one of `end_date` or `end_date_relative` must be specified")
}
credential := graphrbac.PasswordCredential{
KeyID: p.String(keyId),
Value: p.String(value),
EndDate: &date.Time{Time: endDate},
}
if v, ok := d.GetOk("description"); ok {
customIdentifier := []byte(v.(string))
credential.CustomKeyIdentifier = &customIdentifier
}
if v, ok := d.GetOk("start_date"); ok {
// errors will be handled by the validation
startDate, _ := time.Parse(time.RFC3339, v.(string))
credential.StartDate = &date.Time{Time: startDate}
}
return &credential, nil
}
func PasswordCredentialResultFindByKeyId(creds graphrbac.PasswordCredentialListResult, keyId string) *graphrbac.PasswordCredential {
var cred *graphrbac.PasswordCredential
if creds.Value != nil {
for _, c := range *creds.Value {
if c.KeyID == nil {
continue
}
if *c.KeyID == keyId {
cred = &c
break
}
}
}
return cred
}
func PasswordCredentialResultAdd(existing graphrbac.PasswordCredentialListResult, cred *graphrbac.PasswordCredential, errorOnDuplicate bool) (*[]graphrbac.PasswordCredential, error) {
newCreds := make([]graphrbac.PasswordCredential, 0)
if existing.Value != nil {
if errorOnDuplicate {
for _, v := range *existing.Value {
if v.KeyID == nil {
continue
}
if *v.KeyID == *cred.KeyID {
return nil, fmt.Errorf("credential already exists found")
}
}
}
newCreds = *existing.Value
}
newCreds = append(newCreds, *cred)
return &newCreds, nil
}
func PasswordCredentialResultRemoveByKeyId(existing graphrbac.PasswordCredentialListResult, keyId string) *[]graphrbac.PasswordCredential {
newCreds := make([]graphrbac.PasswordCredential, 0)
if existing.Value != nil {
for _, v := range *existing.Value {
if v.KeyID == nil {
continue
}
if *v.KeyID == keyId {
continue
}
newCreds = append(newCreds, v)
}
}
return &newCreds
}
func WaitForPasswordCredentialReplication(keyId string, f func() (graphrbac.PasswordCredentialListResult, error)) (interface{}, error) {
return (&resource.StateChangeConf{
Pending: []string{"404", "BadCast", "NotFound"},
Target: []string{"Found"},
Timeout: 5 * time.Minute,
MinTimeout: 1 * time.Second,
ContinuousTargetOccurence: 10,
Refresh: func() (interface{}, string, error) {
creds, err := f()
if err != nil {
if ar.ResponseWasNotFound(creds.Response) {
return creds, "404", nil
}
return creds, "Error", fmt.Errorf("Error calling f, response was not 404 (%d): %v", creds.Response.StatusCode, err)
}
credential := PasswordCredentialResultFindByKeyId(creds, keyId)
if credential == nil {
return creds, "NotFound", nil
}
return creds, "Found", nil
},
}).WaitForState()
}