generated from davidji99/terraform-provider-scaffolding
/
resource_herokux_oauth_authorization.go
380 lines (316 loc) · 11 KB
/
resource_herokux_oauth_authorization.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
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
package herokux
import (
"context"
"fmt"
heroku "github.com/davidji99/heroku-go/v5"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/validation"
"log"
"net/http"
"os"
"regexp"
"strconv"
"strings"
)
const (
OneWeekInSeconds = 604800
)
func resourceHerokuxOauthAuthorization() *schema.Resource {
return &schema.Resource{
CreateContext: resourceHerokuxOauthAuthorizationCreate,
ReadContext: resourceHerokuxOauthAuthorizationRead,
UpdateContext: resourceHerokuxOauthAuthorizationUpdate,
DeleteContext: resourceHerokuxOauthAuthorizationDelete,
Importer: &schema.ResourceImporter{
StateContext: resourceHerokuxOauthAuthorizationImport,
},
Timeouts: resourceTimeouts(),
Schema: map[string]*schema.Schema{
"scope": {
Type: schema.TypeSet,
Required: true,
ForceNew: true,
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: validation.StringInSlice([]string{"global", "read",
"write", "read-protected", "write-protected", "identity"}, false),
},
Description: "Set custom OAuth scopes",
},
"auth_api_key_name": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Description: "Any word character (letter, number, underscore) string " +
"representing the API key used to create the new authorization",
ValidateFunc: validateAuthAPIKeyName,
},
"time_to_live": {
Type: schema.TypeInt,
Optional: true,
ForceNew: true,
Computed: true,
ValidateFunc: validation.IntAtLeast(1),
Description: "Set expiration in seconds. No expiration if not set.",
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Set a custom authorization description",
},
"access_token": {
Type: schema.TypeString,
Sensitive: true,
Computed: true,
Description: "The actual access token value",
},
"expires_in": {
Type: schema.TypeInt,
Computed: true,
Description: "How long in seconds before the token expires",
},
"token_id": {
Type: schema.TypeString,
Computed: true,
Description: "The token UUID",
},
},
}
}
func validateAuthAPIKeyName(v interface{}, k string) (ws []string, errors []error) {
name := v.(string)
if !regexp.MustCompile(`^\w{1,32}$`).MatchString(name) {
errors = append(errors, fmt.Errorf("auth_api_key_name may only include words, letters, or underscore "+
"with max length of 32 characters"))
}
return
}
func resourceHerokuxOauthAuthorizationImport(ctx context.Context, d *schema.ResourceData, meta interface{}) ([]*schema.ResourceData, error) {
result := strings.Split(d.Id(), ":")
var authID, ttl string
authAPIKeyName := ""
switch len(result) {
case 2:
authID = result[0]
ttl = result[1]
case 3:
authID = result[0]
ttl = result[1]
authAPIKeyName = result[2]
default:
return nil, fmt.Errorf("resource import ID should either be " +
"[<TOKEN_ID>:<TIME_TO_LIVE>:<AUTH_API_KEY_NAME>] or [<TOKEN_ID>:<TIME_TO_LIVE>]")
}
// Set attributes to state so the READ function will work properly
ttlInt, convErr := strconv.Atoi(ttl)
if convErr != nil {
return nil, convErr
}
d.Set("time_to_live", ttlInt)
d.SetId(authID)
if len(result) == 3 {
d.Set("auth_api_key_name", authAPIKeyName)
}
readErr := resourceHerokuxOauthAuthorizationRead(ctx, d, meta)
if readErr.HasError() {
return nil, fmt.Errorf(readErr[0].Detail)
}
return []*schema.ResourceData{d}, nil
}
func constructPlatformAPIClient(d *schema.ResourceData, meta interface{}) (*heroku.Service, diag.Diagnostics) {
var diags diag.Diagnostics
client := meta.(*Config).PlatformAPI
if v, ok := d.GetOk("auth_api_key_name"); ok {
// Check if the associated env variable is set representing the API key of the user account the new token
// will be created in Heroku. If no variable is set, then use the default PlatformAPI client created using
// the token sourced from the HEROKU_API_KEY env variable.
vs := v.(string)
// Construct the env variable name
enVarName := fmt.Sprintf("HEROKUX_%s_API_KEY", strings.ToUpper(vs))
log.Printf("[DEBUG] env variable to fetch custom Heroku API key: %v", enVarName)
apiKey, isFound := os.LookupEnv(enVarName)
// If env variable is not set, error out.
if !isFound {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("%s not found in the environment", enVarName),
Detail: fmt.Sprintf("Please define %s env variable with an API key authorized to create additional OAuth authorizations.", enVarName),
})
}
// Otherwise, initialize a PlatformAPI client with the new API key.
client = heroku.NewService(&http.Client{
Transport: &heroku.Transport{
Username: "", // Email is not required
Password: strings.TrimSpace(apiKey),
UserAgent: UserAgent,
Transport: heroku.RoundTripWithRetryBackoff{},
},
})
} else {
log.Printf("[DEBUG] auth_api_key_name not set. This resource will use the default env variable HEROKU_API_KEY for further actions.")
}
return client, diags
}
func resourceHerokuxOauthAuthorizationCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client, clientDiags := constructPlatformAPIClient(d, meta)
if clientDiags.HasError() {
return clientDiags
}
opts := heroku.OAuthAuthorizationCreateOpts{}
if v, ok := d.GetOk("scope"); ok {
vl := v.(*schema.Set).List()
scopes := make([]string, 0)
for _, l := range vl {
scopes = append(scopes, l.(string))
}
log.Printf("[DEBUG] oauth authorization scope is : %v", scopes)
opts.Scope = scopes
}
//if v, ok := d.GetOk("client"); ok {
// vs := v.(string)
// log.Printf("[DEBUG] oauth authorization client is : %v", vs)
// opts.Client = &vs
//}
if v, ok := d.GetOk("time_to_live"); ok {
vs := v.(int)
log.Printf("[DEBUG] oauth authorization time_to_live is : %v seconds", vs)
opts.ExpiresIn = &vs
}
if v, ok := d.GetOk("description"); ok {
vs := v.(string)
log.Printf("[DEBUG] oauth authorization description is : %v", vs)
opts.Description = &vs
}
log.Printf("[DEBUG] Creating new OAuth authorization")
newAuth, createErr := client.OAuthAuthorizationCreate(context.TODO(), opts)
if createErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "Unable to create new OAuth authorization",
Detail: createErr.Error(),
})
return diags
}
log.Printf("[DEBUG] Created new OAuth authorization")
d.SetId(newAuth.ID)
return resourceHerokuxOauthAuthorizationRead(ctx, d, meta)
}
func resourceHerokuxOauthAuthorizationRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client, clientDiags := constructPlatformAPIClient(d, meta)
if clientDiags.HasError() {
return clientDiags
}
hasCustomTTL := false
if _, ok := d.GetOk("time_to_live"); ok {
hasCustomTTL = true
}
t, getErr := client.OAuthAuthorizationInfo(context.TODO(), d.Id())
if getErr != nil {
// Handle when an existing oauth authorization has expired and is no longer available remotely.
// In this scenario, remove the resource from state so it can be recreated without a `terraform state rm`.
if strings.Contains(getErr.Error(), "Couldn't find that OAuth") && hasCustomTTL {
d.SetId("")
return diags
}
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Unable to retrieve info about OAuth authorization %s", d.Id()),
Detail: getErr.Error(),
})
return diags
}
// Validate expires_in is less than what is defined in the configuration
// as the Platform API does not return a field with the original specified TTL.
// We just want to make sure the TTL was applied correctly.
if v, ok := d.GetOk("time_to_live"); ok {
ttl := v.(int)
if ttl <= *t.AccessToken.ExpiresIn {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "OAuth authorization time-to-live/expiration duration not set properly",
Detail: fmt.Sprintf("The current expiration duration [%d] is greater than the specified [%d] "+
"one in your configuration. This should not be the case.", ttl, *t.AccessToken.ExpiresIn),
})
return diags
}
} else {
// If no time_to_live is specified, make sure the expires_in is `null` or `nil`.
if t.AccessToken.ExpiresIn != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: "OAuth authorization time-to-live/expiration duration not set properly",
Detail: fmt.Sprintf("Your configuration does not specify a time_to_live value "+
"but the authorization access token has an expiration duration of %d seconds. "+
"Please check and confirm.", *t.AccessToken.ExpiresIn),
})
return diags
}
}
var expiresIn int
if t.AccessToken.ExpiresIn == nil {
expiresIn = 0
} else {
expiresIn = *t.AccessToken.ExpiresIn
// Add a warning message to tell users their token will expire in given period
// This warning doesn't work due to a bug in Terraform core.
if *t.AccessToken.ExpiresIn <= OneWeekInSeconds {
diags = append(diags, diag.Diagnostic{
Severity: diag.Warning,
Summary: "[WARNING] Oauth authorization token is expiring soon",
Detail: fmt.Sprintf("Token %s is expiring in %d seconds", d.Id(), *t.AccessToken.ExpiresIn),
})
}
}
d.Set("expires_in", expiresIn)
d.Set("scope", t.Scope)
d.Set("access_token", t.AccessToken.Token)
d.Set("description", t.Description)
d.Set("token_id", t.AccessToken.ID)
return diags
}
func resourceHerokuxOauthAuthorizationUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
opts := heroku.OAuthAuthorizationUpdateOpts{}
client, clientDiags := constructPlatformAPIClient(d, meta)
if clientDiags.HasError() {
return clientDiags
}
if d.HasChange("description") {
vs := d.Get("description").(string)
opts.Description = &vs
}
log.Printf("[DEBUG] Updating OAuth authorization %s", d.Id())
_, updateErr := client.OAuthAuthorizationUpdate(context.TODO(), d.Id(), opts)
if updateErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Unable to update OAuth authorization %s", d.Id()),
Detail: updateErr.Error(),
})
return diags
}
log.Printf("[DEBUG] Updated OAuth authorization %s", d.Id())
return resourceHerokuxOauthAuthorizationRead(ctx, d, meta)
}
func resourceHerokuxOauthAuthorizationDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
var diags diag.Diagnostics
client, clientDiags := constructPlatformAPIClient(d, meta)
if clientDiags.HasError() {
return clientDiags
}
_, deleteErr := client.OAuthAuthorizationDelete(context.TODO(), d.Id())
if deleteErr != nil {
diags = append(diags, diag.Diagnostic{
Severity: diag.Error,
Summary: fmt.Sprintf("Unable to delete token %s", d.Id()),
Detail: deleteErr.Error(),
})
return diags
}
d.SetId("")
return nil
}