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

xray_ignore_rule doesnt have an update resource function, causes error #156

Closed
gschaffer-cxn opened this issue Jan 16, 2024 · 3 comments · Fixed by #162
Closed

xray_ignore_rule doesnt have an update resource function, causes error #156

gschaffer-cxn opened this issue Jan 16, 2024 · 3 comments · Fixed by #162
Assignees
Labels
bug Something isn't working

Comments

@gschaffer-cxn
Copy link

Describe the bug
We wanted to change and xray ignore rule to edit the component being edited. The change is very basic just remove the component version

Requirements for and issue
Perform a basic update on a resource

     -  component_name  = "gav://org.yaml:snakeyaml:1.33"
    +  component_name  = "gav://org.yaml:snakeyaml
  # xray_ignore_rule.compontent["IR44"] will be updated in-place
  ~ resource "xray_ignore_rule" "compontent" {
        id         = "doesnt matter"
        # (6 unchanged attributes hidden)
      - component {
          - name = "gav://org.yaml:snakeyaml:1.33" -> null
        }
      + component {
          + name = "gav://org.yaml:snakeyaml"
        }
    }

Expected behavior
Terraform updates the resource

Additional context
Terraform produces an error like this

│ Error: doesn't support update
│ 
│   with xray_ignore_rule.compontent["IR44"],
│   on ignore_rules.tf line 1, in resource "xray_ignore_rule" "compontent":
│    1: resource "xray_ignore_rule" "compontent" {
│ 
╵

My quick investigation produces that ignore_rule doesnt have an update function specified, so if my understanding is correct it causes the error.

The error message is generated when terraform tries to call the update function of that schema https://github.com/hashicorp/terraform/blob/main/internal/legacy/helper/schema/resource.go#L314

@gschaffer-cxn gschaffer-cxn added the bug Something isn't working label Jan 16, 2024
@alexhung
Copy link
Member

@gschaffer-cxn This is because the Xray API for Ignore Rules do not support update.

I will need to mark the name attribute with ForceNew: true so that when it is changed, Terraform will know to delete the original resource and create a new one.

@gschaffer-cxn
Copy link
Author

@alexhung figured its something like this. I was able to use null resource and lfecycle rules to set up a parameterzed ForceNew like behavior, but hope this gets implemented soon.

resource "null_resource" "ignore_rule_trigger" {
  for_each = local.xray_component_ignore_rules
  # Changes to any input variable would require a retrigger
  triggers = {
    notes = each.value.notes
    # trigger requires everything to be string
    cves = join(",", each.value.vulnerabilities)
    component_name = each.value.component_name
  }
}

resource "xray_ignore_rule" "compontent" {
  for_each = local.xray_component_ignore_rules
  # note can be max 255 characters, trimming
  notes           = substr("${each.key}-${each.value.notes}", 0, 255)
  cves = each.value.vulnerabilities
  component {
    name = each.value.component_name
  }


  lifecycle {
    replace_triggered_by = [null_resource.ignore_rule_trigger[each.key]]
  }
}

This causes the null resource to count as trigger for replacement, and replace_triggered_by can act on this. Not the prettiest workaround but its better than manual tainting.

alexhung added a commit that referenced this issue Jan 31, 2024
Add 'ForceNew' setting to all nested attributes
@alexhung alexhung linked a pull request Feb 2, 2024 that will close this issue
@alexhung
Copy link
Member

alexhung commented Feb 2, 2024

@gschaffer-cxn I've released a new version which should trigger Terraform to re-create the resource if name, version, etc. is changed.

@alexhung alexhung closed this as completed Feb 2, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants