Skip to content

Conversation

vjeffrey
Copy link
Contributor

@vjeffrey vjeffrey commented Jul 12, 2025

fixed bug with creating exceptions

to test this, set jsonbase64 service account to your local dev and run:

terraform {
  required_providers {
    mondoo = {
      source  = "mondoohq/mondoo"
      version = ">= 0.21"
    }
  }
}
provider "mondoo" {
  endpoint = "http://localhost:8989"
    space = "test-infallible-taussig-796596"
}



# Define the domains
resource "mondoo_integration_domain" "mondoo_com" {
  host  = "mondoo.com"
  http  = false
  https = true
}

resource "mondoo_integration_domain" "releases_mondoo_com" {
  host  = "releases.mondoo.com"
  http  = false
  https = true
}

resource "mondoo_integration_domain" "lunalectric_com" {
  host  = "lunalectric.com"
  http  = false
  https = true
}

resource "mondoo_integration_domain" "gitlab_lunalectric_com" {
  host  = "gitlab.lunalectric.com"
  http  = false
  https = true
}

# Define the policies and domains for assignment
resource "mondoo_policy_assignment" "domain_policies" {
  policies = [
    "//policy.api.mondoo.app/policies/mondoo-tls-security",
    "//policy.api.mondoo.app/policies/mondoo-dns-security",
    "//policy.api.mondoo.app/policies/mondoo-email-security",
    "//policy.api.mondoo.app/policies/mondoo-http-security"
  ]
}


resource "mondoo_policy_assignment" "cis_policy_assignment_enabled" {

  policies = [
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-10-l1-ce",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-10-l1-bl",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-11-l1-ce",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-11-l1-bl",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-server-2016-dc-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-server-2016-ms-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-server-2019-dc-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-server-2019-ms-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-server-2022-dc-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-windows-server-2022-ms-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-azure-windows-server-2019-dc-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-azure-windows-server-2019-ms-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-azure-windows-server-2022-dc-level-1",
    "//policy.api.mondoo.app/policies/cis-microsoft-azure-windows-server-2022-ms-level-1",
    "//policy.api.mondoo.app/policies/mondoo-edr-policy",
  ]

  state = "enabled"
}

# Set exceptions for Windows policies in the space
resource "mondoo_exception" "windows_defender_exception" {
  justification = "Windows Defender is disabled. Other EDR is used/configured instead."
  action        = "RISK_ACCEPTED"
  valid_until = "2025-09-09"
  check_mrns = [
    "//policy.api.mondoo.app/queries/cis-microsoft-windows-10--18.10.42.5.1",
    "//policy.api.mondoo.app/queries/cis-microsoft-windows-11--18.10.42.5.1",
    "//policy.api.mondoo.app/queries/cis-microsoft-windows-server-2016--18.10.42.5.1",
    "//policy.api.mondoo.app/queries/cis-microsoft-windows-server-2019--18.10.42.5.1",
    "//policy.api.mondoo.app/queries/cis-microsoft-windows-server-2022--18.10.42.5.1",
  ]
  depends_on = [
    mondoo_policy_assignment.cis_policy_assignment_enabled
  ]
}

you'll see it fail. then checkout this branch, run make install, make dev/enter, and remove any old tf state files and run this tf again, itll work

added import

Screenshot 2025-07-17 at 13 10 23

more details

the update path is kinda wild. we didnt have exception ids exposed before, so it was search based. now that we have an id, that's much more ideal. but it means we have to ensure we account for that during the update path (which is why we check for exception id, then search for the exception and get the id if it's not present). the fallback there is that if we cant find the exception the user will need to import it.
since exception id is optional, the tf wont care about this as long as there r no changes to apply to the exception resource

i tested updating from older tf manually by installing version 0.23.0 locally, running the tf, and then getting on this branch version of the plugin and re-applying from there

@vjeffrey vjeffrey force-pushed the vj/exception-deletion branch 5 times, most recently from 7871358 to b6fcccb Compare July 12, 2025 22:15
@vjeffrey vjeffrey changed the title 🧹 add createException api, add test 🧹 add createException api, deleteException api, add test Jul 12, 2025
@vjeffrey vjeffrey marked this pull request as ready for review July 12, 2025 22:17
@vjeffrey vjeffrey requested a review from imilchev July 12, 2025 22:35
Action types.String `tfsdk:"action"`
CheckMrns types.List `tfsdk:"check_mrns"`
VulnerabilityMrns types.List `tfsdk:"vulnerability_mrns"`
ExceptionId types.String `tfsdk:"exception_id"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

what happens to existing state files where this doesnt exist

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm. good question. ill check

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

good call, i tested and that then would fail to delete the exception on update.
im implementing a change that will searchf or the exception if we dont have the exception id. if we search for it and find it, great.
if not, then my thought was we return an error to the user and then allow them to import the exception resource

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ok. i tested this all out.

  • if a user has an exception in their state file and there are no changes between the rest of the data in the exception and the state file, the tf wont complain because the exception id is optional
  • if a user has an exception in their state file and there are changes between what exists and the state file, then the tf will trigger an update, which will now search for the exception, if found delete and recreate, if not found return error and instruct to import

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

working on import resource now

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

import resource should be working now as well

@vjeffrey vjeffrey force-pushed the vj/exception-deletion branch 2 times, most recently from f62c206 to 45b8110 Compare July 17, 2025 19:51
i tried debugging it but got nowhere. it fails when i include the id
in the query for the import test. but when i run import manually everything
works. and the import test works if i just query for space exceptions without the id.
@vjeffrey vjeffrey force-pushed the vj/exception-deletion branch from 45b8110 to 43e0434 Compare August 1, 2025 21:55
@imilchev imilchev requested review from kkereziev and removed request for imilchev August 4, 2025 07:56
resource.TestCheckResourceAttr("mondoo_exception.windows_defender_exception", "action", "FALSE_POSITIVE"),
),
},
// // import testing
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leftover ?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it is, but i want to try to get it working in the future. we should have the imports tested. this one is being real tricky on me so i was hoping to address the test itself in a followup

@vjeffrey
Copy link
Contributor Author

vjeffrey commented Aug 4, 2025

oh nice thanks for the review @kkereziev ill address your feedback this afternoon

@vjeffrey vjeffrey requested a review from kkereziev August 5, 2025 13:49
@vjeffrey vjeffrey merged commit 49a0b5b into main Aug 5, 2025
27 of 28 checks passed
@vjeffrey vjeffrey deleted the vj/exception-deletion branch August 5, 2025 16:46
@github-actions github-actions bot locked and limited conversation to collaborators Aug 5, 2025
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants