forked from hashicorp/terraform-provider-vault
/
resource_okta_auth_backend_user.go
157 lines (128 loc) · 3.94 KB
/
resource_okta_auth_backend_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
package vault
import (
"errors"
"fmt"
"github.com/hashicorp/terraform/helper/schema"
"github.com/hashicorp/vault/api"
"log"
"strings"
)
func oktaAuthBackendUserResource() *schema.Resource {
return &schema.Resource{
Create: oktaAuthBackendUserWrite,
Read: oktaAuthBackendUserRead,
Update: oktaAuthBackendUserWrite,
Delete: oktaAuthBackendUserDelete,
Schema: map[string]*schema.Schema{
"path": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Path to the Okta auth backend",
},
"username": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the user within Okta",
},
"groups": {
Type: schema.TypeSet,
Required: false,
Optional: true,
Description: "Groups within the Okta auth backend to associate with this user",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: func(v interface{}, k string) (ws []string, errs []error) {
value := v.(string)
// No comma as it'll become part of a comma separate list
if strings.Contains(value, ",") || strings.Contains(value, "/") {
errs = append(errs, errors.New("group cannot contain ',' or '/'"))
}
return
},
},
Set: schema.HashString,
},
"policies": {
Type: schema.TypeSet,
Required: false,
Optional: true,
Description: "Policies to associate with this user",
Elem: &schema.Schema{
Type: schema.TypeString,
ValidateFunc: func(v interface{}, k string) (ws []string, errs []error) {
value := v.(string)
// No comma as it'll become part of a comma separate list
if strings.Contains(value, ",") {
errs = append(errs, errors.New("policy cannot contain ','"))
}
return
},
},
Set: schema.HashString,
},
},
}
}
func oktaAuthBackendUserWrite(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
username := d.Get("username").(string)
path := d.Get("path").(string)
log.Printf("[DEBUG] Writing user %s to Okta auth backend %s", username, path)
var groupsString []string
if groups, ok := d.GetOk("groups"); ok {
groupsString = toStringArray(groups.(*schema.Set).List())
} else {
groupsString = []string{}
}
var policiesString []string
if policies, ok := d.GetOk("policies"); ok {
policiesString = toStringArray(policies.(*schema.Set).List())
} else {
policiesString = []string{}
}
user := oktaUser{
Username: username,
Groups: groupsString,
Policies: policiesString,
}
if err := updateOktaUser(client, path, user); err != nil {
return fmt.Errorf("unable to update user %s in Vault: %s", username, err)
}
d.SetId(fmt.Sprintf("%s/%s", path, username))
return oktaAuthBackendUserRead(d, meta)
}
func oktaAuthBackendUserRead(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Get("path").(string)
username := d.Get("username").(string)
log.Printf("[DEBUG] Reading user %s from Okta auth backend %s", username, path)
present, err := isOktaUserPresent(client, path, username)
if err != nil {
return fmt.Errorf("unable to read user %s in Vault: %s", username, err)
}
if !present {
// User not found, so remove this resource
d.SetId("")
return nil
}
user, err := readOktaUser(client, path, username)
if err != nil {
return fmt.Errorf("unable to update user %s from Vault: %s", username, err)
}
d.Set("groups", user.Groups)
d.Set("policies", user.Policies)
return nil
}
func oktaAuthBackendUserDelete(d *schema.ResourceData, meta interface{}) error {
client := meta.(*api.Client)
path := d.Get("path").(string)
username := d.Get("username").(string)
log.Printf("[DEBUG] Deleting user %s from Okta auth backend %s", username, path)
if err := deleteOktaUser(client, path, username); err != nil {
return fmt.Errorf("unable to delete user %s from Vault: %s", username, path)
}
d.SetId("")
return nil
}