-
Notifications
You must be signed in to change notification settings - Fork 100
/
resource_artifactory_oauth_settings.go
250 lines (213 loc) · 7.08 KB
/
resource_artifactory_oauth_settings.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
package artifactory
import (
"context"
"github.com/go-resty/resty/v2"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
"gopkg.in/yaml.v2"
)
type OauthSecurity struct {
Oauth OauthSettingsWrapper `yaml:"security"`
}
type OauthSettingsWrapper struct {
Settings OauthSettings `yaml:"oauthSettings"`
}
type OauthSettings struct {
EnableIntegration bool `yaml:"enableIntegration" json:"enabled"`
PersistUsers bool `yaml:"persistUsers" json:"persistUsers"`
AllowUserToAccessProfile bool `yaml:"allowUserToAccessProfile" json:"allowUserToAccessProfile"`
OauthProvidersSettings map[string]OauthProviderSettings `yaml:"oauthProvidersSettings"`
AvailableTypes []OauthType `json:"availableTypes"`
Providers []OauthProviderSettings `json:"providers"`
}
type OauthProviderSettings struct {
Name string `json:"name"`
Enabled bool `yaml:"enabled" json:"enabled"`
Type string `yaml:"providerType" json:"providerType"`
ClientId string `yaml:"id" json:"id"`
ClientSecret string `yaml:"secret" json:"secret"`
ApiUrl string `yaml:"apiUrl" json:"apiUrl"`
AuthUrl string `yaml:"authUrl" json:"authUrl"`
TokenUrl string `yaml:"tokenUrl" json:"tokenUrl"`
}
type OauthType struct {
DisplayName string
Type string
MandatoryFields []string
FieldHolders []string
FieldValues []string
}
func resourceArtifactoryOauthSettings() *schema.Resource {
return &schema.Resource{
UpdateContext: resourceOauthSettingsUpdate,
CreateContext: resourceOauthSettingsUpdate,
DeleteContext: resourceOauthSettingsDelete,
ReadContext: resourceOauthSettingsRead,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Schema: map[string]*schema.Schema{
"enable": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"persist_users": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"allow_user_to_access_profile": {
Type: schema.TypeBool,
Optional: true,
Default: false,
},
"oauth_provider": {
Type: schema.TypeSet,
Required: true,
MinItems: 1,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
},
"enabled": {
Type: schema.TypeBool,
Optional: true,
Default: true,
},
"type": {
Type: schema.TypeString,
Required: true,
},
"client_id": {
Type: schema.TypeString,
Required: true,
},
"client_secret": {
Type: schema.TypeString,
Required: true,
Sensitive: true,
},
"api_url": {
Type: schema.TypeString,
Required: true,
},
"auth_url": {
Type: schema.TypeString,
Required: true,
},
"token_url": {
Type: schema.TypeString,
Required: true,
},
},
},
},
},
}
}
func resourceOauthSettingsRead(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
c := m.(*resty.Client)
oauthSettings := OauthSettings{}
_, err := c.R().SetResult(&oauthSettings).Get("artifactory/api/oauth")
if err != nil {
return diag.Errorf("failed to retrieve data from <base_url>/artifactory/api/oauth during Read")
}
s := OauthSecurity{OauthSettingsWrapper{Settings: oauthSettings}}
s.Oauth.Settings.OauthProvidersSettings = make(map[string]OauthProviderSettings)
for _, provider := range s.Oauth.Settings.Providers {
s.Oauth.Settings.OauthProvidersSettings[provider.Name] = provider
}
// remove resource from state if no providers are found
if len(s.Oauth.Settings.Providers) == 0 {
d.SetId("")
}
packDiag := packOauthSecurity(&s, d)
if packDiag != nil {
return packDiag
}
return diag.Diagnostics{{
Severity: diag.Warning,
Summary: "Usage of Undocumented Artifactory API Endpoints",
Detail: "The artifactory_oauth_settings resource uses endpoints that are undocumented and may not work with SaaS environments, or may change without notice.",
}}
}
func resourceOauthSettingsUpdate(ctx context.Context, d *schema.ResourceData, m interface{}) diag.Diagnostics {
unpacked := unpackOauthSecurity(d)
content, err := yaml.Marshal(&unpacked)
if err != nil {
return diag.Errorf("failed to marshal oauth settings during Update")
}
err = sendConfigurationPatch(content, m)
if err != nil {
return diag.Errorf("failed to send PATCH request to Artifactory during Update")
}
// we should only have one oauth settings resource, using same id
d.SetId("oauth_settings")
return resourceOauthSettingsRead(ctx, d, m)
}
func resourceOauthSettingsDelete(_ context.Context, _ *schema.ResourceData, m interface{}) diag.Diagnostics {
var content = `
security:
oauthSettings: ~
`
err := sendConfigurationPatch([]byte(content), m)
if err != nil {
return diag.Errorf("failed to send PATCH request to Artifactory during Delete")
}
return nil
}
func unpackOauthSecurity(s *schema.ResourceData) *OauthSecurity {
d := &ResourceData{s}
security := new(OauthSecurity)
settings := OauthSettings{
EnableIntegration: d.getBool("enable", false),
PersistUsers: d.getBool("persist_users", false),
AllowUserToAccessProfile: d.getBool("allow_user_to_access_profile", false),
}
if v, ok := d.GetOkExists("oauth_provider"); ok {
oauthProviderSettings := map[string]OauthProviderSettings{}
for _, m := range v.(*schema.Set).List() {
o := m.(map[string]interface{})
oauthProviderSettings[o["name"].(string)] = OauthProviderSettings{
Name: o["name"].(string),
Enabled: o["enabled"].(bool),
Type: o["type"].(string),
ClientId: o["client_id"].(string),
ClientSecret: o["client_secret"].(string),
ApiUrl: o["api_url"].(string),
AuthUrl: o["auth_url"].(string),
TokenUrl: o["token_url"].(string),
}
}
settings.OauthProvidersSettings = oauthProviderSettings
security.Oauth.Settings = settings
}
return security
}
func packOauthSecurity(s *OauthSecurity, d *schema.ResourceData) diag.Diagnostics {
setValue := mkLens(d)
setValue("enable", s.Oauth.Settings.EnableIntegration)
setValue("persist_users", s.Oauth.Settings.PersistUsers)
errors := setValue("allow_user_to_access_profile", s.Oauth.Settings.AllowUserToAccessProfile)
settings := make([]interface{}, 0)
for name, setting := range s.Oauth.Settings.OauthProvidersSettings {
providerSetting := map[string]interface{}{
"name": name,
"enabled": setting.Enabled,
"type": setting.Type,
"client_id": setting.ClientId,
"client_secret": setting.ClientSecret,
"api_url": setting.ApiUrl,
"auth_url": setting.AuthUrl,
"token_url": setting.TokenUrl,
}
settings = append(settings, providerSetting)
}
if errors != nil && len(errors) > 0 {
return diag.Errorf("failed to pack oauth settings %q", errors)
}
return nil
}