generated from hashicorp/terraform-provider-scaffolding
-
Notifications
You must be signed in to change notification settings - Fork 3
/
idp.go
203 lines (188 loc) · 5.41 KB
/
idp.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
package client
import (
"context"
"encoding/json"
"fmt"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
u "net/url"
)
const idpEndpoint = "v1/settings/idps"
type IdpSamlConfig struct {
Certificate string `json:"certificate,omitempty"`
Issuer string `json:"issuer,omitempty"`
SsoUrl string `json:"sso_url,omitempty"`
AuthnContextClass string `json:"authn_context_class,omitempty"`
JitEnabled bool `json:"jit_enabled,omitempty"`
}
type IdpOidcConfig struct {
Issuer string `json:"issuer,omitempty"`
ClientId string `json:"client_id,omitempty"`
ClientSecret string `json:"client_secret"`
JitEnabled bool `json:"jit_enabled,omitempty"`
}
type IdpScimConfig struct {
ApiKeyId string `json:"api_key_id,omitempty"`
AssumeOwnership bool `json:"assume_ownership,omitempty"`
}
type Idp struct {
ID string `json:"id,omitempty"`
Name string `json:"name,omitempty"`
Description *string `json:"description"`
Enabled bool `json:"enabled"`
Hidden bool `json:"hidden"`
Icon *string `json:"icon"`
MappedAttributes *[]string `json:"mapped_attributes"`
SamlConfig *IdpSamlConfig `json:"saml_config,omitempty"`
OidcConfig *IdpOidcConfig `json:"oidc_config,omitempty"`
ScimConfig *IdpScimConfig `json:"scim_config,omitempty"`
}
func NewIdp(d *schema.ResourceData) *Idp {
res := &Idp{}
if d.HasChange("name") {
res.Name = d.Get("name").(string)
}
res.Enabled = d.Get("enabled").(bool)
res.Hidden = d.Get("hidden").(bool)
icon := d.Get("icon").(string)
if icon != "" {
res.Icon = &icon
} else {
res.Icon = nil
}
dsc := d.Get("description").(string)
if dsc != "" {
res.Description = &dsc
} else {
res.Description = nil
}
ma := ResourceTypeSetToStringSlice(d.Get("mapped_attributes").(*schema.Set))
if len(ma) > 0 {
res.MappedAttributes = &ma
} else {
res.MappedAttributes = nil
}
res.SamlConfig = NewIdpSamlConfig(d)
res.OidcConfig = NewIdpOidcConfig(d)
res.ScimConfig = NewIdpScimConfig(d)
return res
}
func NewIdpSamlConfig(d *schema.ResourceData) *IdpSamlConfig {
res := &IdpSamlConfig{}
sc, exists := d.GetOk("saml_config")
if !exists {
return nil
}
saml := sc.([]interface{})
if len(saml) != 1 {
return nil
}
saml_conf := saml[0].(map[string]interface{})
if _, ok := saml_conf["issuer"]; ok {
res.Issuer = saml_conf["issuer"].(string)
}
if _, ok := saml_conf["certificate"]; ok {
res.Certificate = saml_conf["certificate"].(string)
}
if _, ok := saml_conf["sso_url"]; ok {
res.SsoUrl = saml_conf["sso_url"].(string)
}
if _, ok := saml_conf["authn_context_class"]; ok {
res.AuthnContextClass = saml_conf["authn_context_class"].(string)
}
if _, ok := saml_conf["jit_enabled"]; ok {
res.JitEnabled = saml_conf["jit_enabled"].(bool)
}
return res
}
func NewIdpOidcConfig(d *schema.ResourceData) *IdpOidcConfig {
res := &IdpOidcConfig{}
oc, exists := d.GetOk("oidc_config")
if !exists {
return nil
}
oidc := oc.([]interface{})
if len(oidc) != 1 {
return nil
}
oidc_conf := oidc[0].(map[string]interface{})
if _, ok := oidc_conf["issuer"]; ok {
res.Issuer = oidc_conf["issuer"].(string)
}
if _, ok := oidc_conf["client_id"]; ok {
res.ClientId = oidc_conf["client_id"].(string)
}
if _, ok := oidc_conf["client_secret"]; ok {
res.ClientSecret = oidc_conf["client_secret"].(string)
}
if _, ok := oidc_conf["jit_enabled"]; ok {
res.JitEnabled = oidc_conf["jit_enabled"].(bool)
}
return res
}
func NewIdpScimConfig(d *schema.ResourceData) *IdpScimConfig {
res := &IdpScimConfig{}
sc, exists := d.GetOk("scim_config")
if !exists {
return nil
}
scim := sc.([]interface{})
if len(scim) != 1 {
return nil
}
scim_conf := scim[0].(map[string]interface{})
if _, ok := scim_conf["api_key_id"]; ok {
res.ApiKeyId = scim_conf["api_key_id"].(string)
}
if _, ok := scim_conf["assume_ownership"]; ok {
res.AssumeOwnership = scim_conf["assume_ownership"].(bool)
}
return res
}
func parseIdp(resp []byte) (*Idp, error) {
idp := &Idp{}
err := json.Unmarshal(resp, idp)
if err != nil {
return nil, fmt.Errorf("could not parse idp response: %v", err)
}
return idp, nil
}
func CreateIdp(ctx context.Context, c *Client, idp *Idp) (*Idp, error) {
body, err := json.Marshal(idp)
if err != nil {
return nil, fmt.Errorf("could not convert idp to json: %v", err)
}
idpUrl := fmt.Sprintf("%s/%s", c.BaseURL, idpEndpoint)
resp, err := c.Post(ctx, idpUrl, body)
if err != nil {
return nil, err
}
return parseIdp(resp)
}
func UpdateIdp(ctx context.Context, c *Client, idpID string, idp *Idp) (*Idp, error) {
body, err := json.Marshal(idp)
if err != nil {
return nil, fmt.Errorf("could not convert idp to json: %v", err)
}
idpUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, idpEndpoint, idpID)
resp, err := c.Patch(ctx, idpUrl, body)
if err != nil {
return nil, err
}
return parseIdp(resp)
}
func GetIdp(ctx context.Context, c *Client, idpID string) (*Idp, error) {
idpUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, idpEndpoint, idpID)
resp, err := c.Get(ctx, idpUrl, u.Values{"expand": {"true"}})
if err != nil {
return nil, err
}
return parseIdp(resp)
}
func DeleteIdp(ctx context.Context, c *Client, idpID string) (*Idp, error) {
idpUrl := fmt.Sprintf("%s/%s/%s", c.BaseURL, idpEndpoint, idpID)
resp, err := c.Delete(ctx, idpUrl, nil)
if err != nil {
return nil, err
}
return parseIdp(resp)
}