/
resource_okta_app_user.go
229 lines (214 loc) · 7.61 KB
/
resource_okta_app_user.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
package okta
import (
"context"
"encoding/json"
"errors"
"strings"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"github.com/okta/terraform-provider-okta/sdk"
)
func resourceAppUser() *schema.Resource {
return &schema.Resource{
CreateContext: resourceAppUserCreate,
ReadContext: resourceAppUserRead,
UpdateContext: resourceAppUserUpdate,
DeleteContext: resourceAppUserDelete,
Importer: &schema.ResourceImporter{
StateContext: func(ctx context.Context, d *schema.ResourceData, m interface{}) ([]*schema.ResourceData, error) {
parts := strings.Split(d.Id(), "/")
if len(parts) != 2 {
return nil, errors.New("invalid resource import specifier. Use: terraform import <app_id>/<user_id>")
}
_ = d.Set("app_id", parts[0])
_ = d.Set("user_id", parts[1])
_ = d.Set("retain_assignment", false)
assignment, _, err := getOktaClientFromMetadata(m).Application.
GetApplicationUser(ctx, parts[0], parts[1], nil)
if err != nil {
return nil, err
}
d.SetId(assignment.Id)
return []*schema.ResourceData{d}, nil
},
},
Description: `Creates an Application User.
This resource allows you to create and configure an Application User.
~> **IMPORTANT:** When the 'okta_app_user' is retained, by setting 'retain_assignment' to 'true', it is no longer managed by Terraform after it is destroyed. To truly delete the assignment, you will need to remove it either through the Okta Console or API. This argument exists for the use case where the same user is assigned in multiple places in order to prevent a single destruction removing all of them.`,
Schema: map[string]*schema.Schema{
"app_id": {
Type: schema.TypeString,
Required: true,
Description: "App to associate user with",
},
"user_id": {
Type: schema.TypeString,
Required: true,
Description: "User associated with the application",
},
"username": {
Type: schema.TypeString,
Optional: true,
DiffSuppressFunc: func(k, old, new string, d *schema.ResourceData) bool {
return d.Get("has_shared_username").(bool)
},
Description: "The username to use for the app user. In case the user is assigned to the app with `SHARED_USERNAME_AND_PASSWORD` credentials scheme, this field will be computed and should not be set.",
},
"has_shared_username": {
Type: schema.TypeBool,
Computed: true,
},
"password": {
Type: schema.TypeString,
Sensitive: true,
Optional: true,
Description: "The password to use.",
},
"profile": {
Type: schema.TypeString,
ValidateDiagFunc: stringIsJSON,
StateFunc: normalizeDataJSON,
Optional: true,
DiffSuppressFunc: noChangeInObjectFromUnmarshaledJSON,
Description: "The JSON profile of the App User.",
},
"retain_assignment": {
Type: schema.TypeBool,
Optional: true,
Default: false,
Description: "Retain the user assignment on destroy. If set to true, the resource will be removed from state but not from the Okta app.",
},
},
}
}
func resourceAppUserCreate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var app *sdk.AutoLoginApplication
respApp, _, err := getOktaClientFromMetadata(m).Application.GetApplication(ctx, d.Get("app_id").(string), sdk.NewAutoLoginApplication(), nil)
if err != nil {
return diag.Errorf("failed to get application by ID: %v", err)
}
app = respApp.(*sdk.AutoLoginApplication)
un := d.Get("username").(string)
if app.Credentials != nil && app.Credentials.Scheme == "SHARED_USERNAME_AND_PASSWORD" {
if un != "" {
return diag.Errorf("'username' should not be set if it is assigned to the app with 'SHARED_USERNAME_AND_PASSWORD' credentials scheme")
}
_ = d.Set("has_shared_username", true)
} else {
if un == "" {
return diag.Errorf("'username' is required (the only exception is when the assigned app has 'SHARED_USERNAME_AND_PASSWORD' credentials scheme)")
}
_ = d.Set("has_shared_username", false)
}
u, _, err := getOktaClientFromMetadata(m).Application.AssignUserToApplication(
ctx,
d.Get("app_id").(string),
*getAppUser(d),
)
if err != nil {
return diag.Errorf("failed to assign user to application: %v", err)
}
d.SetId(u.Id)
return resourceAppUserRead(ctx, d, m)
}
func resourceAppUserUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var app *sdk.AutoLoginApplication
respApp, _, err := getOktaClientFromMetadata(m).Application.GetApplication(ctx, d.Get("app_id").(string), sdk.NewAutoLoginApplication(), nil)
if err != nil {
return diag.Errorf("failed to get application by ID: %v", err)
}
app = respApp.(*sdk.AutoLoginApplication)
un := d.Get("username").(string)
if app.Credentials != nil && app.Credentials.Scheme == "SHARED_USERNAME_AND_PASSWORD" {
if un != "" {
return diag.Errorf("'username' should not be set if it is assigned to the app with 'SHARED_USERNAME_AND_PASSWORD' credentials scheme")
}
_ = d.Set("has_shared_username", true)
} else {
if un == "" {
return diag.Errorf("'username' is required (the only exception is when the assigned app has 'SHARED_USERNAME_AND_PASSWORD' credentials scheme)")
}
_ = d.Set("has_shared_username", false)
}
_, _, err = getOktaClientFromMetadata(m).Application.UpdateApplicationUser(
ctx,
d.Get("app_id").(string),
d.Get("user_id").(string),
*getAppUser(d),
)
if err != nil {
return diag.Errorf("failed to update application's user: %v", err)
}
return resourceAppUserRead(ctx, d, m)
}
func resourceAppUserRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
var app *sdk.AutoLoginApplication
respApp, resp, err := getOktaClientFromMetadata(m).Application.GetApplication(ctx, d.Get("app_id").(string), sdk.NewAutoLoginApplication(), nil)
if is404(resp) {
d.SetId("")
return nil
}
if err != nil {
return diag.Errorf("failed to get application by ID: %v", err)
}
app = respApp.(*sdk.AutoLoginApplication)
if app.Credentials != nil && app.Credentials.Scheme == "SHARED_USERNAME_AND_PASSWORD" {
_ = d.Set("has_shared_username", true)
} else {
_ = d.Set("has_shared_username", false)
}
u, resp, err := getOktaClientFromMetadata(m).Application.GetApplicationUser(
ctx,
d.Get("app_id").(string),
d.Get("user_id").(string),
nil,
)
if is404(resp) {
d.SetId("")
return nil
}
if err != nil {
return diag.Errorf("failed to get application's user: %v", err)
}
var rawProfile string
if u.Profile != nil {
p, _ := json.Marshal(u.Profile)
rawProfile = string(p)
}
_ = d.Set("profile", rawProfile)
_ = d.Set("username", u.Credentials.UserName)
return nil
}
func resourceAppUserDelete(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
retain := d.Get("retain_assignment").(bool)
if retain {
// The assignment should be retained, bail before DeleteApplicationUser is called
return nil
}
_, err := getOktaClientFromMetadata(m).Application.DeleteApplicationUser(
ctx,
d.Get("app_id").(string),
d.Get("user_id").(string),
nil,
)
if err != nil {
return diag.Errorf("failed to delete application's user: %v", err)
}
return nil
}
func getAppUser(d *schema.ResourceData) *sdk.AppUser {
var profile interface{}
rawProfile := d.Get("profile").(string)
// JSON is already validated
_ = json.Unmarshal([]byte(rawProfile), &profile)
return &sdk.AppUser{
Id: d.Get("user_id").(string),
Credentials: &sdk.AppUserCredentials{
UserName: d.Get("username").(string),
Password: &sdk.AppUserPasswordCredential{
Value: d.Get("password").(string),
},
},
Profile: profile,
}
}