Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support for data_masking in azurerm_api_management_api_diagnostic #12419

Merged
merged 31 commits into from
Jul 12, 2021
Merged
Show file tree
Hide file tree
Changes from 28 commits
Commits
Show all changes
31 commits
Select commit Hold shift + click to select a range
2cdcd9c
update
May 26, 2021
ba8f607
Merge branch 'master' of https://github.com/yupwei68/terraform-provid…
May 27, 2021
ad34b7b
Merge branch 'master' of https://github.com/terraform-providers/terra…
May 31, 2021
edf2cd8
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 1, 2021
420097a
revert
Jun 1, 2021
e3294fc
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 4, 2021
86da91a
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 7, 2021
09b9f8e
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 9, 2021
2f55300
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 10, 2021
c03c58d
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 10, 2021
57c031c
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 11, 2021
a6d7062
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 11, 2021
42b8b23
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 11, 2021
ef31b67
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 15, 2021
e054708
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 16, 2021
3f35b09
Merge branch 'master' of https://github.com/yupwei68/terraform-provid…
Jun 16, 2021
6dc973e
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 18, 2021
3363bb5
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 21, 2021
b9d7f9a
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 22, 2021
014c3bd
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 23, 2021
9223d60
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 25, 2021
788fecb
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 28, 2021
cf090fc
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jun 29, 2021
3ba7898
update
Jun 29, 2021
2232902
update
Jun 30, 2021
30f6e07
update
Jun 30, 2021
7eabf7c
Merge branch 'master' of https://github.com/terraform-providers/terra…
Jul 5, 2021
9a54b2c
update
Jul 9, 2021
1e70f99
Update azurerm/internal/services/apimanagement/api_management_api_dia…
katbyte Jul 9, 2021
77cdd91
Update website/docs/r/api_management_api_diagnostic.html.markdown
katbyte Jul 9, 2021
c58d0d9
fmt
Jul 12, 2021
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,18 @@ func resourceApiManagementApiDiagnosticAdditionalContentSchema() *pluginsdk.Sche
},
Set: pluginsdk.HashString,
},
"data_masking": {
Type: pluginsdk.TypeList,
Optional: true,
MaxItems: 1,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"query_params": schemaApiManagementDataMaskingEntityList(),

katbyte marked this conversation as resolved.
Show resolved Hide resolved
"headers": schemaApiManagementDataMaskingEntityList(),
},
},
},
},
},
}
Expand Down Expand Up @@ -329,6 +341,8 @@ func expandApiManagementApiDiagnosticHTTPMessageDiagnostic(input []interface{})
result.Headers = &headers
}

result.DataMasking = expandApiManagementDataMasking(v["data_masking"].([]interface{}))

return result
}

Expand All @@ -348,7 +362,105 @@ func flattenApiManagementApiDiagnosticHTTPMessageDiagnostic(input *apimanagement
if input.Headers != nil {
diagnostic["headers_to_log"] = set.FromStringSlice(*input.Headers)
}

diagnostic["data_masking"] = flattenApiManagementDataMasking(input.DataMasking)

result = append(result, diagnostic)

return result
}

func schemaApiManagementDataMaskingEntityList() *pluginsdk.Schema {
return &pluginsdk.Schema{
Type: pluginsdk.TypeList,
Optional: true,
Elem: &pluginsdk.Resource{
Schema: map[string]*pluginsdk.Schema{
"mode": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringInSlice([]string{
string(apimanagement.Hide),
string(apimanagement.Mask),
}, false),
},

"value": {
Type: pluginsdk.TypeString,
Required: true,
ValidateFunc: validation.StringIsNotEmpty,
},
},
},
}
}

func expandApiManagementDataMasking(input []interface{}) *apimanagement.DataMasking {
if len(input) == 0 || input[0] == nil {
return nil
}

inputRaw := input[0].(map[string]interface{})
return &apimanagement.DataMasking{
QueryParams: expandApiManagementDataMaskingEntityList(inputRaw["query_params"].([]interface{})),
Headers: expandApiManagementDataMaskingEntityList(inputRaw["headers"].([]interface{})),
}
}

func expandApiManagementDataMaskingEntityList(input []interface{}) *[]apimanagement.DataMaskingEntity {
if len(input) == 0 || input[0] == nil {
return nil
}

result := make([]apimanagement.DataMaskingEntity, 0)
for _, v := range input {
entity := v.(map[string]interface{})
result = append(result, apimanagement.DataMaskingEntity{
Mode: apimanagement.DataMaskingMode(entity["mode"].(string)),
Value: utils.String(entity["value"].(string)),
})
}
return &result
}

func flattenApiManagementDataMasking(dataMasking *apimanagement.DataMasking) []interface{} {
if dataMasking == nil {
return []interface{}{}
}

var queryParams, headers []interface{}
if dataMasking.QueryParams != nil {
queryParams = flattenApiManagementDataMaskingEntityList(dataMasking.QueryParams)
}
if dataMasking.Headers != nil {
headers = flattenApiManagementDataMaskingEntityList(dataMasking.Headers)
}

return []interface{}{
map[string]interface{}{
"query_params": queryParams,
"headers": headers,
},
}
}

func flattenApiManagementDataMaskingEntityList(dataMaskingList *[]apimanagement.DataMaskingEntity) []interface{} {
if dataMaskingList == nil || len(*dataMaskingList) == 0 {
return []interface{}{}
}

result := []interface{}{}

for _, entity := range *dataMaskingList {
var value string
if entity.Value != nil {
value = *entity.Value
}
result = append(result, map[string]interface{}{
"mode": string(entity.Mode),
"value": value,
})
}

return result
}
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,28 @@ func TestAccApiManagementApiDiagnostic_complete(t *testing.T) {
})
}

func TestAccApiManagementApiDiagnostic_dataMasking(t *testing.T) {
data := acceptance.BuildTestData(t, "azurerm_api_management_api_diagnostic", "test")
r := ApiManagementApiDiagnosticResource{}

data.ResourceTest(t, r, []acceptance.TestStep{
{
Config: r.complete(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
{
Config: r.dataMaskingUpdate(data),
Check: acceptance.ComposeTestCheckFunc(
check.That(data.ResourceName).ExistsInAzure(r),
),
},
data.ImportStep(),
})
}

func (ApiManagementApiDiagnosticResource) Exists(ctx context.Context, clients *clients.Client, state *pluginsdk.InstanceState) (*bool, error) {
id, err := parse.ApiDiagnosticID(state.ID)
if err != nil {
Expand Down Expand Up @@ -121,7 +143,7 @@ resource "azurerm_api_management" "test" {
resource_group_name = azurerm_resource_group.test.name
publisher_name = "pub1"
publisher_email = "[email protected]"
sku_name = "Developer_1"
sku_name = "Consumption_0"
}

resource "azurerm_api_management_logger" "test" {
Expand Down Expand Up @@ -214,6 +236,87 @@ func (r ApiManagementApiDiagnosticResource) complete(data acceptance.TestData) s
return fmt.Sprintf(`
%s

resource "azurerm_api_management_api_diagnostic" "test" {
identifier = "applicationinsights"
resource_group_name = azurerm_resource_group.test.name
api_management_name = azurerm_api_management.test.name
api_name = azurerm_api_management_api.test.name
api_management_logger_id = azurerm_api_management_logger.test.id
sampling_percentage = 1.0
always_log_errors = true
log_client_ip = true
http_correlation_protocol = "W3C"
verbosity = "verbose"

backend_request {
body_bytes = 1
headers_to_log = ["Host"]
data_masking {
query_params {
mode = "Hide"
value = "backend-Request-Test"
}
headers {
mode = "Mask"
value = "backend-Request-Header"
}
}
}

backend_response {
body_bytes = 2
headers_to_log = ["Content-Type"]
data_masking {
query_params {
mode = "Mask"
value = "backend-Resp-Test"
}
}
}

frontend_request {
body_bytes = 3
headers_to_log = ["Accept"]
data_masking {
headers {
mode = "Mask"
value = "frontend-Request-Header"
}
}
}

frontend_response {
body_bytes = 4
headers_to_log = ["Content-Length"]
data_masking {
query_params {
mode = "Hide"
value = "frontend-Response-Test"
}

query_params {
mode = "Mask"
value = "frontend-Response-Test-Alt"
}
headers {
mode = "Mask"
value = "frontend-Response-Header"
}

headers {
mode = "Mask"
value = "frontend-Response-Header-Alt"
}
}
}
}
`, r.template(data))
}

func (r ApiManagementApiDiagnosticResource) dataMaskingUpdate(data acceptance.TestData) string {
return fmt.Sprintf(`
%s

resource "azurerm_api_management_api_diagnostic" "test" {
identifier = "applicationinsights"
resource_group_name = azurerm_resource_group.test.name
Expand All @@ -234,16 +337,48 @@ resource "azurerm_api_management_api_diagnostic" "test" {
backend_response {
body_bytes = 2
headers_to_log = ["Content-Type"]
data_masking {
query_params {
mode = "Hide"
value = "backend-Resp-Test-Update"
}
}
}

frontend_request {
body_bytes = 3
headers_to_log = ["Accept"]
data_masking {
headers {
mode = "Mask"
value = "frontend-Request-Header-Update"
}
}
}

frontend_response {
body_bytes = 4
headers_to_log = ["Content-Length"]
data_masking {
query_params {
mode = "Hide"
value = "frontend-Response-Test-Update"
}

query_params {
mode = "Mask"
value = "frontend-Response-Test-Alt-Update"
}

query_params {
mode = "Mask"
value = "frontend-Response-Test-Alt2-Update"
}
headers {
mode = "Mask"
value = "frontend-Response-Header-Update"
}
}
}
}
`, r.template(data))
Expand Down
18 changes: 18 additions & 0 deletions website/docs/r/api_management_api_diagnostic.html.markdown
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,24 @@ A `backend_request`, `backend_response`, `frontend_request` or `frontend_respons

* `headers_to_log` - (Optional) Specifies a list of headers to log.

* `data_masking` - (Optional) A `data_masking` block as defined below.

---

A `data_masking` block supports the following:

* `query_params` - (Optional) A `query_params` block as defined below.

* `headers` - (Optional) A `headers` block as defined below.

---

A `query_params`, `headers` block supports the following:
katbyte marked this conversation as resolved.
Show resolved Hide resolved

* `mode` - (Required) The data masking mode. Possible values are `Mask` and `Hide` for `query_params`. The only possible value is `Mask` for `headers`.

* `value` - (Required) The name of the header or the uery parameter to mask.

## Attributes Reference

In addition to the Arguments listed above - the following Attributes are exported:
Expand Down