badarsebard / terraform-provider-xsoar

Apache License 2.0
9 stars 3 forks source link

xsoar_mapper provider bug (only when setting mapping = ) #12

Open Xboarder56 opened 1 year ago

Xboarder56 commented 1 year ago

@badarsebard I was testing mapper creation with the provider today and noticed no matter what I did it would fail with a 400 error on a terraform apply.

2023-02-23T02:51:40.179Z [DEBUG] provider.terraform-provider-xsoar_v0.3.61: 2023/02/23 02:51:40 code: 400 status: 400 Bad Request headers: map[Content-Type:[application/json] Date:[Thu, 23 Feb 2023 02:51:40 GMT] Server-Timing:[332] Strict-Transport-Security:[max-age=10886400000000000; includeSubDomains] Vary:[Accept-Encoding Accept-Encoding] X-Content-Type-Options:[nosniff] X-Frame-Options:[DENY] X-Xss-Protection:[1; mode=block]] body: {"id":"bad_request","status":400,"title":"Bad request","detail":"Request body is not well-formed. It must be JSON.","error":"json: cannot unmarshal object into Go struct field InstanceClassifier.keyTypeMap of type string","encrypted":false,"multires":null}

This wouldn't occur when not populating the variable mapping in the terraform resource. Setting the mapping to a single value in the XSOAR gui and copying this value into the mapping portion allowed the plan to successfully detect that no changes are observed.

I went ahead and inspected this via proxy to see how the request appeared going from terraform to the XSOAR server and right away I noticed the provider is appending the value of mapper into keyTypeMap when making the post request. The post request payload looks like this when the provider applies the change:

{"keyTypeMap":{"Phishing":{"dontMapEventToLabels":false}},"name":"foo1","propagationLabels":null,"type":"mapping-incoming"}

When I believe it should be like this:

{"mapping":{"Phishing":{"dontMapEventToLabels":false}},"name":"foo1","propagationLabels":null,"type":"mapping-incoming"}

The plan always runs fine so I think this is only on the creation portion and not the data resource. When making a terraform output using your data resource in the provider it returned the mapping value correctly as well.

My test terraform code to validate this and gather the attached info:

resource "xsoar_mapper" "example1" {
  name               = "foo1"
  direction          = "incoming"
  propagation_labels = ["all"]
  mapping = jsonencode({
    Phishing = {
      dontMapEventToLabels = true
      internalMapping = {
        "Log Source" = {
          simple = "mailbox"
        }
      }
    }
  })
}

Proxy (Post / Response)

image

Terraform Apply

image

Xboarder56 commented 1 year ago

@badarsebard I think this is all that is needed to fix this. Changing the following lines to

SetMapping instead of SetKeyTypeMap

https://github.com/badarsebard/terraform-provider-xsoar/blob/3e21859cd488ee45ce09dbfbef58b2a5c5ae271f/xsoar/resource_mapper.go#L130

https://github.com/badarsebard/terraform-provider-xsoar/blob/3e21859cd488ee45ce09dbfbef58b2a5c5ae271f/xsoar/resource_mapper.go#L301