-
Notifications
You must be signed in to change notification settings - Fork 117
/
resource_bigip_ltm_profile_http.go
513 lines (481 loc) · 18.1 KB
/
resource_bigip_ltm_profile_http.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
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
/*
Copyright 2019 F5 Networks Inc.
This Source Code Form is subject to the terms of the Mozilla Public License, v. 2.0.
If a copy of the MPL was not distributed with this file, You can obtain one at https://mozilla.org/MPL/2.0/.
*/
package bigip
import (
"context"
"log"
"os"
"strings"
bigip "github.com/f5devcentral/go-bigip"
"github.com/f5devcentral/go-bigip/f5teem"
"github.com/google/uuid"
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
)
func resourceBigipLtmProfileHttp() *schema.Resource {
return &schema.Resource{
CreateContext: resourceBigipLtmProfileHttpCreate,
ReadContext: resourceBigipLtmProfileHttpRead,
UpdateContext: resourceBigipLtmProfileHttpUpdate,
DeleteContext: resourceBigipLtmProfileHttpDelete,
Importer: &schema.ResourceImporter{
StateContext: schema.ImportStatePassthroughContext,
},
Schema: map[string]*schema.Schema{
"name": {
Type: schema.TypeString,
Required: true,
ForceNew: true,
Description: "Name of the profile",
ValidateFunc: validateF5NameWithDirectory,
},
"proxy_type": {
Type: schema.TypeString,
Optional: true,
ForceNew: true,
Computed: true,
Description: "Specifies the proxy mode for this profile: reverse, explicit, or transparent. The default is Reverse.",
},
"defaults_from": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Inherit defaults from parent profile",
ValidateFunc: validateF5Name,
},
"app_service": {
Type: schema.TypeString,
Optional: true,
Description: "The application service to which the object belongs.",
},
"basic_auth_realm": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies a quoted string for the basic authentication realm. The system sends this string to a client whenever authorization fails. The default value is none",
},
"description": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "User defined description",
},
"encrypt_cookies": {
Type: schema.TypeSet,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Encrypts specified cookies that the BIG-IP system sends to a client system",
},
"encrypt_cookie_secret": {
Type: schema.TypeString,
Optional: true,
Description: "Specifies a passphrase for the cookie encryption. Note: Since it's a sensitive entity idempotency will fail for it in the update call.",
},
"fallback_host": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies an HTTP fallback host. HTTP redirection allows you to redirect HTTP traffic to another protocol identifier, host name, port number, or URI path.",
},
"fallback_status_codes": {
Type: schema.TypeSet,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Specifies one or more three-digit status codes that can be returned by an HTTP server,that should trigger a redirection to the fallback host",
},
"head_erase": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies the header string that you want to erase from an HTTP request. Default is none",
},
"head_insert": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies a quoted header string that you want to insert into an HTTP request. Default is none",
},
"insert_xforwarded_for": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies, when enabled, that the system inserts an X-Forwarded-For header in an HTTP request with the client IP address, to use with connection pooling. The default is Disabled.",
},
"lws_width": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Specifies the maximum column width for any given line, when inserting an HTTP header in an HTTP request. The default is 80",
},
"lws_separator": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies the linear white space (LWS) separator that the system inserts when a header exceeds the maximum width you specify in the LWS Maximum Columns setting.",
},
"accept_xff": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Enables or disables trusting the client IP address, and statistics from the client IP address, based on the request's XFF (X-forwarded-for) headers, if they exist.",
},
"oneconnect_transformations": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Enables the system to perform HTTP header transformations for the purpose of keeping server-side connections open. This feature requires configuration of a OneConnect profile.",
},
"tm_partition": {
Type: schema.TypeString,
Optional: true,
Default: "",
Description: "Displays the administrative partition within which this profile resides. ",
},
"redirect_rewrite": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether the system rewrites the URIs that are part of HTTP redirect (3XX) responses. The default is None",
},
"response_headers_permitted": {
Type: schema.TypeSet,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
Description: "Specifies headers that the BIG-IP system allows in an HTTP response.If you are specifying more than one header, separate the headers with a blank space",
},
"request_chunking": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies how the system handles HTTP content that is chunked by a client. The default is Preserve",
},
"response_chunking": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies how the system handles HTTP content that is chunked by a server. The default is Selective",
},
"server_agent_name": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies the value of the Server header in responses that the BIG-IP itself generates. The default is BigIP. If no string is specified, then no Server header will be added to such responses",
},
"via_host_name": {
Type: schema.TypeString,
Computed: true,
Optional: true,
Description: "Specifies the hostname to include into Via header",
},
"via_request": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether to append, remove, or preserve a Via header in an HTTP request",
},
"via_response": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether to append, remove, or preserve a Via header in an HTTP request",
},
"xff_alternative_names": {
Type: schema.TypeSet,
Set: schema.HashString,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Computed: true,
Description: "Specifies alternative XFF headers instead of the default X-forwarded-for header",
},
"http_strict_transport_security": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"include_subdomains": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether to include the includeSubdomains directive in the HSTS header.",
},
"maximum_age": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Specifies the maximum age to assume the connection should remain secure.",
},
"mode": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether to include the HSTS response header.",
},
"preload": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether to include the preload directive in the HSTS header.",
},
},
},
},
"enforcement": {
Type: schema.TypeSet,
Optional: true,
Computed: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"known_methods": {
Type: schema.TypeList,
Computed: true,
Elem: &schema.Schema{Type: schema.TypeString},
Optional: true,
Description: "Specifies which HTTP methods count as being known. Removing RFC-defined methods from this list will cause the HTTP filter to not recognize them.",
},
"max_header_count": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Specifies the maximum number of headers allowed in HTTP request/response.",
},
"max_header_size": {
Type: schema.TypeInt,
Optional: true,
Computed: true,
Description: "Specifies the maximum header size.",
},
"unknown_method": {
Type: schema.TypeString,
Optional: true,
Computed: true,
Description: "Specifies whether to allow, reject or switch to pass-through mode when an unknown HTTP method is parsed.",
},
},
},
},
},
}
}
func resourceBigipLtmProfileHttpCreate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Get("name").(string)
log.Printf("[INFO] Creating HTTP Profile:%+v ", name)
pss := &bigip.HttpProfile{
Name: name,
}
config := getHttpProfileConfig(d, pss)
err := client.AddHttpProfile(config)
if err != nil {
return diag.FromErr(err)
}
d.SetId(name)
if !client.Teem {
id := uuid.New()
uniqueID := id.String()
assetInfo := f5teem.AssetInfo{
Name: "Terraform-provider-bigip",
Version: client.UserAgent,
Id: uniqueID,
}
apiKey := os.Getenv("TEEM_API_KEY")
teemDevice := f5teem.AnonymousClient(assetInfo, apiKey)
f := map[string]interface{}{
"Terraform Version": client.UserAgent,
}
tsVer := strings.Split(client.UserAgent, "/")
err = teemDevice.Report(f, "bigip_ltm_profile_http", tsVer[3])
if err != nil {
log.Printf("[ERROR]Sending Telemetry data failed:%v", err)
}
}
return resourceBigipLtmProfileHttpRead(ctx, d, meta)
}
func resourceBigipLtmProfileHttpRead(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Fetching HTTP Profile " + name)
pp, err := client.GetHttpProfile(name)
if err != nil {
log.Printf("[ERROR] Unable to retrieve HTTP Profile (%s) ", err)
return diag.FromErr(err)
}
if pp == nil {
log.Printf("[WARN] HTTP Profile (%s) not found, removing from state", name)
d.SetId("")
return nil
}
_ = d.Set("name", name)
_ = d.Set("defaults_from", pp.DefaultsFrom)
_ = d.Set("proxy_type", pp.ProxyType)
if _, ok := d.GetOk("accept_xff"); ok {
_ = d.Set("accept_xff", pp.AcceptXff)
}
if _, ok := d.GetOk("basic_auth_realm"); ok {
_ = d.Set("basic_auth_realm", pp.BasicAuthRealm)
}
if _, ok := d.GetOk("description"); ok {
_ = d.Set("description", pp.Description)
}
if _, ok := d.GetOk("encrypt_cookie_secret"); ok {
_ = d.Set("encrypt_cookie_secret", pp.EncryptCookieSecret)
}
if _, ok := d.GetOk("encrypt_cookies"); ok {
_ = d.Set("encrypt_cookies", pp.EncryptCookies)
}
if _, ok := d.GetOk("fallback_host"); ok {
_ = d.Set("fallback_host", pp.FallbackHost)
}
if _, ok := d.GetOk("fallback_status_codes"); ok {
_ = d.Set("fallback_status_codes", pp.FallbackStatusCodes)
}
if _, ok := d.GetOk("head_erase"); ok {
_ = d.Set("head_erase", pp.HeaderErase)
}
if _, ok := d.GetOk("head_insert"); ok {
_ = d.Set("head_insert", pp.HeaderInsert)
}
if _, ok := d.GetOk("insert_xforwarded_for"); ok {
_ = d.Set("insert_xforwarded_for", pp.InsertXforwardedFor)
}
if _, ok := d.GetOk("lws_separator"); ok {
_ = d.Set("lws_separator", pp.LwsSeparator)
}
if _, ok := d.GetOk("oneconnect_transformations"); ok {
_ = d.Set("oneconnect_transformations", pp.OneconnectTransformations)
}
if _, ok := d.GetOk("tm_partition"); ok {
_ = d.Set("tm_partition", pp.TmPartition)
}
if _, ok := d.GetOk("redirect_rewrite"); ok {
_ = d.Set("redirect_rewrite", pp.RedirectRewrite)
}
if _, ok := d.GetOk("request_chunking"); ok {
_ = d.Set("request_chunking", pp.RequestChunking)
}
if _, ok := d.GetOk("response_chunking"); ok {
_ = d.Set("response_chunking", pp.ResponseChunking)
}
_ = d.Set("response_headers_permitted", pp.ResponseHeadersPermitted)
if _, ok := d.GetOk("server_agent_name"); ok {
_ = d.Set("server_agent_name", pp.ServerAgentName)
}
if _, ok := d.GetOk("via_host_name"); ok {
_ = d.Set("via_host_name", pp.ViaHostName)
}
if _, ok := d.GetOk("via_request"); ok {
_ = d.Set("via_request", pp.ViaRequest)
}
if _, ok := d.GetOk("via_response"); ok {
_ = d.Set("via_response", pp.ViaResponse)
}
_ = d.Set("xff_alternative_names", pp.XffAlternativeNames)
var enforcementList []interface{}
enforcement := make(map[string]interface{})
enforcement["max_header_count"] = pp.Enforcement.MaxHeaderCount
enforcement["max_header_size"] = pp.Enforcement.MaxHeaderSize
enforcement["unknown_method"] = pp.Enforcement.UnknownMethod
if p, ok := d.GetOk("enforcement"); ok {
for _, r := range p.(*schema.Set).List() {
if len(r.(map[string]interface{})["known_methods"].([]interface{})) != 0 {
enforcement["known_methods"] = pp.Enforcement.KnownMethods
}
}
}
enforcementList = append(enforcementList, enforcement)
if _, ok := d.GetOk("enforcement"); ok {
_ = d.Set("enforcement", enforcementList)
}
var hstsList []interface{}
hsts := make(map[string]interface{})
hsts["include_subdomains"] = pp.Hsts.IncludeSubdomains
hsts["maximum_age"] = pp.Hsts.MaximumAge
hsts["mode"] = pp.Hsts.Mode
hsts["preload"] = pp.Hsts.Preload
hstsList = append(hstsList, hsts)
if _, ok := d.GetOk("http_strict_transport_security"); ok {
_ = d.Set("http_strict_transport_security", hstsList)
}
return nil
}
func resourceBigipLtmProfileHttpUpdate(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Printf("[INFO] Updating HTTP Profile Profile:%+v ", name)
pss := &bigip.HttpProfile{
Name: name,
}
config := getHttpProfileConfig(d, pss)
err := client.ModifyHttpProfile(name, config)
if err != nil {
log.Printf("[ERROR] Unable to Modify HTTP Profile (%s) (%v)", name, err)
return diag.FromErr(err)
}
return resourceBigipLtmProfileHttpRead(ctx, d, meta)
}
func resourceBigipLtmProfileHttpDelete(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
client := meta.(*bigip.BigIP)
name := d.Id()
log.Println("[INFO] Deleting HTTPProfile " + name)
err := client.DeleteHttpProfile(name)
if err != nil {
log.Printf("[ERROR] Unable to Delete HTTPProfile (%s) (%v) ", name, err)
return diag.FromErr(err)
}
d.SetId("")
return nil
}
func getHttpProfileConfig(d *schema.ResourceData, config *bigip.HttpProfile) *bigip.HttpProfile {
config.AppService = d.Get("app_service").(string)
config.DefaultsFrom = d.Get("defaults_from").(string)
config.AcceptXff = d.Get("accept_xff").(string)
config.BasicAuthRealm = d.Get("basic_auth_realm").(string)
config.Description = d.Get("description").(string)
config.EncryptCookieSecret = d.Get("encrypt_cookie_secret").(string)
config.EncryptCookies = setToStringSlice(d.Get("encrypt_cookies").(*schema.Set))
config.FallbackHost = d.Get("fallback_host").(string)
config.FallbackStatusCodes = setToStringSlice(d.Get("fallback_status_codes").(*schema.Set))
config.HeaderErase = d.Get("head_erase").(string)
config.HeaderInsert = d.Get("head_insert").(string)
config.InsertXforwardedFor = d.Get("insert_xforwarded_for").(string)
config.LwsSeparator = d.Get("lws_separator").(string)
config.OneconnectTransformations = d.Get("oneconnect_transformations").(string)
config.TmPartition = d.Get("tm_partition").(string)
config.ProxyType = d.Get("proxy_type").(string)
config.RedirectRewrite = d.Get("redirect_rewrite").(string)
config.RequestChunking = d.Get("request_chunking").(string)
config.ResponseChunking = d.Get("response_chunking").(string)
config.ResponseHeadersPermitted = setToInterfaceSlice(d.Get("response_headers_permitted").(*schema.Set))
config.ServerAgentName = d.Get("server_agent_name").(string)
config.ViaHostName = d.Get("via_host_name").(string)
config.ViaRequest = d.Get("via_request").(string)
config.ViaResponse = d.Get("via_response").(string)
config.XffAlternativeNames = setToInterfaceSlice(d.Get("xff_alternative_names").(*schema.Set))
config.LwsWidth = d.Get("lws_width").(int)
p := d.Get("http_strict_transport_security")
for _, r := range p.(*schema.Set).List() {
config.Hsts.IncludeSubdomains = r.(map[string]interface{})["include_subdomains"].(string)
config.Hsts.Mode = r.(map[string]interface{})["mode"].(string)
config.Hsts.Preload = r.(map[string]interface{})["preload"].(string)
config.Hsts.MaximumAge = r.(map[string]interface{})["maximum_age"].(int)
}
v := d.Get("enforcement")
for _, r := range v.(*schema.Set).List() {
var knownMethods []string
for _, val := range r.(map[string]interface{})["known_methods"].([]interface{}) {
knownMethods = append(knownMethods, val.(string))
}
config.Enforcement.KnownMethods = knownMethods
config.Enforcement.UnknownMethod = r.(map[string]interface{})["unknown_method"].(string)
config.Enforcement.MaxHeaderCount = r.(map[string]interface{})["max_header_count"].(int)
config.Enforcement.MaxHeaderSize = r.(map[string]interface{})["max_header_size"].(int)
}
return config
}