-
Notifications
You must be signed in to change notification settings - Fork 10
/
resource_appgate_identity_provider_radius.go
357 lines (330 loc) · 11 KB
/
resource_appgate_identity_provider_radius.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
package appgate
import (
"context"
"fmt"
"log"
"time"
"github.com/appgate/sdp-api-client-go/api/v18/openapi"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceAppgateRadiusProvider() *schema.Resource {
return &schema.Resource{
Create: resourceAppgateRadiusProviderRuleCreate,
Read: resourceAppgateRadiusProviderRuleRead,
Update: resourceAppgateRadiusProviderRuleUpdate,
Delete: identityProviderDelete,
Importer: &schema.ResourceImporter{
State: schema.ImportStatePassthrough,
},
Timeouts: &schema.ResourceTimeout{
Create: schema.DefaultTimeout(10 * time.Minute),
Update: schema.DefaultTimeout(10 * time.Minute),
Delete: schema.DefaultTimeout(10 * time.Minute),
},
Schema: func() map[string]*schema.Schema {
s := identityProviderSchema()
s["type"].Default = identityProviderRadius
s["hostnames"] = &schema.Schema{
Type: schema.TypeList,
Required: true,
Elem: &schema.Schema{Type: schema.TypeString},
}
s["port"] = &schema.Schema{
Type: schema.TypeInt,
Required: true,
}
s["shared_secret"] = &schema.Schema{
Type: schema.TypeString,
Required: true,
Sensitive: true,
}
s["authentication_protocol"] = &schema.Schema{
Type: schema.TypeString,
Optional: true,
Default: "CHAP",
ValidateFunc: func(v interface{}, name string) (warns []string, errs []error) {
s := v.(string)
list := []string{"PAP", "CHAP"}
for _, x := range list {
if s == x {
return
}
}
errs = append(errs, fmt.Errorf("authentication_protocol must be on of %v, got %s", list, s))
return
},
}
return s
}(),
}
}
func resourceAppgateRadiusProviderRuleCreate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Creating RadiusProvider: %s", d.Get("name").(string))
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.RadiusIdentityProvidersApi
ctx := context.TODO()
currentVersion := meta.(*Client).ApplianceVersion
provider := &openapi.ConfigurableIdentityProvider{}
provider.Type = identityProviderRadius
provider, err = readProviderFromConfig(d, *provider, currentVersion)
if err != nil {
return fmt.Errorf("Failed to read and create basic identity provider for %s %w", identityProviderRadius, err)
}
args := openapi.RadiusProvider{}
// base
if currentVersion.LessThan(Appliance55Version) {
args.DeviceLimitPerUser = nil
}
args.SetType(provider.GetType())
args.SetId(provider.GetId())
args.SetName(provider.GetName())
args.SetNotes(provider.GetNotes())
args.SetTags(provider.GetTags())
// identity provider
if provider.AdminProvider != nil {
args.SetAdminProvider(*provider.AdminProvider)
}
if provider.DeviceLimitPerUser != nil {
args.SetDeviceLimitPerUser(*provider.DeviceLimitPerUser)
}
if provider.OnBoarding2FA != nil {
args.SetOnBoarding2FA(*provider.OnBoarding2FA)
}
if provider.InactivityTimeoutMinutes != nil {
args.SetInactivityTimeoutMinutes(*provider.InactivityTimeoutMinutes)
}
if provider.NetworkInactivityTimeoutEnabled != nil {
if currentVersion.LessThan(Appliance61Version) {
return ErrNetworkInactivityTimeoutEnabled
}
args.SetNetworkInactivityTimeoutEnabled(provider.GetNetworkInactivityTimeoutEnabled())
}
if provider.IpPoolV4 != nil {
args.SetIpPoolV4(*provider.IpPoolV4)
}
if provider.IpPoolV6 != nil {
args.SetIpPoolV6(*provider.IpPoolV6)
}
if provider.UserScripts != nil {
args.SetUserScripts(provider.GetUserScripts())
}
if provider.DnsServers != nil {
args.SetDnsServers(provider.GetDnsServers())
}
if provider.DnsSearchDomains != nil {
args.SetDnsSearchDomains(provider.GetDnsSearchDomains())
}
if provider.BlockLocalDnsRequests != nil {
args.SetBlockLocalDnsRequests(*provider.BlockLocalDnsRequests)
}
if provider.ClaimMappings != nil {
args.SetClaimMappings(provider.GetClaimMappings())
}
if provider.OnDemandClaimMappings != nil {
args.SetOnDemandClaimMappings(provider.GetOnDemandClaimMappings())
}
// radius
if v, ok := d.GetOk("hostnames"); ok {
hostnames, err := readArrayOfStringsFromConfig(v.([]interface{}))
if err != nil {
return err
}
args.SetHostnames(hostnames)
}
if v, ok := d.GetOk("port"); ok {
args.SetPort(int32(v.(int)))
}
if v, ok := d.GetOk("shared_secret"); ok {
args.SetSharedSecret(v.(string))
}
if v, ok := d.GetOk("authentication_protocol"); ok {
args.SetAuthenticationProtocol(v.(string))
}
request := api.IdentityProvidersPost(ctx)
p, _, err := request.Body(args).Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not create %s provider %w", identityProviderRadius, prettyPrintAPIError(err))
}
d.SetId(p.GetId())
return resourceAppgateRadiusProviderRuleRead(d, meta)
}
func resourceAppgateRadiusProviderRuleRead(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Reading radius identity provider id: %+v", d.Id())
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.RadiusIdentityProvidersApi
ctx := context.TODO()
currentVersion := meta.(*Client).ApplianceVersion
request := api.IdentityProvidersIdGet(ctx, d.Id())
radius, _, err := request.Authorization(token).Execute()
if err != nil {
d.SetId("")
return fmt.Errorf("Failed to read LDAP Identity provider, %w", err)
}
d.Set("type", identityProviderRadius)
// base attributes
d.Set("name", radius.Name)
d.Set("notes", radius.Notes)
d.Set("tags", radius.Tags)
// identity provider attributes
d.Set("admin_provider", radius.GetAdminProvider())
if v, ok := radius.GetDeviceLimitPerUserOk(); ok {
d.Set("device_limit_per_user", *v)
}
if v, ok := radius.GetOnBoarding2FAOk(); ok {
if err := d.Set("on_boarding_two_factor", flattenIdentityProviderOnboarding2fa(*v, currentVersion)); err != nil {
return err
}
}
d.Set("inactivity_timeout_minutes", radius.GetInactivityTimeoutMinutes())
d.Set("network_inactivity_timeout_enabled", radius.GetNetworkInactivityTimeoutEnabled())
if v, ok := radius.GetIpPoolV4Ok(); ok {
d.Set("ip_pool_v4", *v)
}
if v, ok := radius.GetIpPoolV6Ok(); ok {
d.Set("ip_pool_v6", *v)
}
d.Set("user_scripts", radius.GetUserScripts())
d.Set("dns_servers", radius.GetDnsServers())
d.Set("dns_search_domains", radius.GetDnsSearchDomains())
d.Set("block_local_dns_requests", radius.GetBlockLocalDnsRequests())
if v, ok := radius.GetClaimMappingsOk(); ok {
if err := d.Set("claim_mappings", flattenIdentityProviderClaimsMappning(v)); err != nil {
return err
}
}
if v, ok := radius.GetOnDemandClaimMappingsOk(); ok {
if err := d.Set("on_demand_claim_mappings", flattenIdentityProviderOnDemandClaimsMappning(v)); err != nil {
return err
}
}
// radius attributes
if v, ok := radius.GetHostnamesOk(); ok {
d.Set("hostnames", v)
}
if v, ok := radius.GetPortOk(); ok {
d.Set("port", *v)
}
if val, ok := d.GetOk("shared_secret"); ok {
d.Set("shared_secret", val)
}
if v, ok := radius.GetAuthenticationProtocolOk(); ok {
d.Set("authentication_protocol", *v)
}
return nil
}
func resourceAppgateRadiusProviderRuleUpdate(d *schema.ResourceData, meta interface{}) error {
log.Printf("[DEBUG] Updating radius identity provider id: %+v", d.Id())
token, err := meta.(*Client).GetToken()
if err != nil {
return err
}
api := meta.(*Client).API.RadiusIdentityProvidersApi
ctx := context.TODO()
currentVersion := meta.(*Client).ApplianceVersion
request := api.IdentityProvidersIdGet(ctx, d.Id())
originalRadiusProvider, _, err := request.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Failed to read LDAP Identity provider, %w", err)
}
// base attributes
if d.HasChange("name") {
originalRadiusProvider.SetName(d.Get("name").(string))
}
if d.HasChange("notes") {
originalRadiusProvider.SetNotes(d.Get("notes").(string))
}
if d.HasChange("tags") {
originalRadiusProvider.SetTags(schemaExtractTags(d))
}
// identity provider attributes
if d.HasChange("admin_provider") {
originalRadiusProvider.SetAdminProvider(d.Get("admin_provider").(bool))
}
if d.HasChange("device_limit_per_user") {
originalRadiusProvider.SetDeviceLimitPerUser(int32(d.Get("device_limit_per_user").(int)))
}
if d.HasChange("on_boarding_two_factor") {
_, v := d.GetChange("on_boarding_two_factor")
onboarding, err := readOnBoardingTwoFactorFromConfig(v.([]interface{}), currentVersion)
if err != nil {
return err
}
originalRadiusProvider.SetOnBoarding2FA(onboarding)
}
if d.HasChange("inactivity_timeout_minutes") {
originalRadiusProvider.SetInactivityTimeoutMinutes(int32(d.Get("inactivity_timeout_minutes").(int)))
}
if d.HasChange("network_inactivity_timeout_enabled") {
originalRadiusProvider.SetNetworkInactivityTimeoutEnabled(d.Get("network_inactivity_timeout_enabled").(bool))
}
if d.HasChange("ip_pool_v4") {
originalRadiusProvider.SetIpPoolV4(d.Get("ip_pool_v4").(string))
}
if d.HasChange("ip_pool_v6") {
originalRadiusProvider.SetIpPoolV6(d.Get("ip_pool_v6").(string))
}
if d.HasChange("user_scripts") {
_, v := d.GetChange("user_scripts")
us, err := readArrayOfStringsFromConfig(v.([]interface{}))
if err != nil {
return fmt.Errorf("Failed to read user_scripts %w", err)
}
originalRadiusProvider.SetUserScripts(us)
}
if d.HasChange("dns_servers") {
_, v := d.GetChange("dns_servers")
servers, err := readArrayOfStringsFromConfig(v.([]interface{}))
if err != nil {
return fmt.Errorf("Failed to read dns servers %w", err)
}
originalRadiusProvider.SetDnsServers(servers)
}
if d.HasChange("dns_search_domains") {
_, v := d.GetChange("dns_search_domains")
servers, err := readArrayOfStringsFromConfig(v.([]interface{}))
if err != nil {
return fmt.Errorf("Failed to read dns search domains %w", err)
}
originalRadiusProvider.SetDnsSearchDomains(servers)
}
if d.HasChange("block_local_dns_requests") {
originalRadiusProvider.SetBlockLocalDnsRequests(d.Get("block_local_dns_requests").(bool))
}
if d.HasChange("claim_mappings") {
_, v := d.GetChange("claim_mappings")
claims := readIdentityProviderClaimMappingFromConfig(v.([]interface{}))
originalRadiusProvider.SetClaimMappings(claims)
}
if d.HasChange("on_demand_claim_mappings") {
_, v := d.GetChange("on_demand_claim_mappings")
claims := readIdentityProviderOnDemandClaimMappingFromConfig(v.([]interface{}))
originalRadiusProvider.SetOnDemandClaimMappings(claims)
}
// radius provider attributes
if d.HasChange("hostnames") {
_, v := d.GetChange("hostnames")
servers, err := readArrayOfStringsFromConfig(v.([]interface{}))
if err != nil {
return fmt.Errorf("Failed to read hostnames %w", err)
}
originalRadiusProvider.SetHostnames(servers)
}
if d.HasChange("port") {
_, v := d.GetChange("port")
originalRadiusProvider.SetPort(int32(v.(int)))
}
originalRadiusProvider.SetSharedSecret(d.Get("shared_secret").(string))
req := api.IdentityProvidersIdPut(ctx, d.Id())
req = req.Body(*originalRadiusProvider)
_, _, err = req.Authorization(token).Execute()
if err != nil {
return fmt.Errorf("Could not update %s provider %w", identityProviderRadius, prettyPrintAPIError(err))
}
return resourceAppgateRadiusProviderRuleRead(d, meta)
}