Skip to content

Conversation

@jamestran201
Copy link

@jamestran201 jamestran201 commented Sep 20, 2024

The resourceGithubRepositoryUpdate function currently passes the current state of security_and_analysis to the PATCH /repo request. If the repo has an enforced security configuration, this can result in the API returning a 422 response because changing security settings is not allowed in this case. Even though the security_and_analysis settings remain the same, and only some other settings are changed, the request will still fail.

To avoid the request failing when security_and_analysis remains the same, we will only pass it in the request payload when changes have been made.

Resolves #2383


Before the change?

Currently, if a repository has Secret Scanning enabled. The payload in the PATCH /repository request will include this data, thus attempts to enable Secret Scanning on the repository again. This can happen even with this template that doesn't modify Secret Scanning

resource "github_repository" "allow_rebase_merge" {
  name               = "test-tf-changes"
  allow_rebase_merge = false
}

After the change?

The request payload for PATCH /repository will only contain the security_and_analysis object when security features are being modified. This means that the value for at least one of the features is being updated: advanced_security, secret_scanning, secret_scanning_push_protection, secret_scanning_non_provider_patterns, secret_scanning_validity_checks. By doing this, we will only attempt to modify these settings when a change is specified by the template.

Pull request checklist

  • Tests for the changes have been added (for bug fixes / features)
  • Docs have been reviewed and added / updated if needed (for bug fixes / features)

I didn't write an acceptance test for this change because the setup requires applying a security configuration to a repo. I tried the following manual test:

  • Create main.tf that looks like
provider "github" {
  token = "<your API token here>"
  owner = "<your org here>"
}

terraform {
  required_providers {
    github = {
      source = "integrations/github"
    }
  }
}
resource "github_repository" "allow_rebase_merge" {
  name               = "manual-pr-test"
  allow_rebase_merge = false
  ignore_vulnerability_alerts_during_read = true
}
  • Run terraform apply
  • In the GitHub UI, create a security configuration that enforces all security features. Then apply it to the repo above
  • Change allow_rebase_merge from false to true in the template
  • Run terraform apply again
  • The command should complete successfully

ignore_vulnerability_alerts_during_read is set for this test because the terraform state stored locally can be different from the repo's state after a security configuration is applied. This causes the update command to attempt changing Dependabot alerts enablement state, which will fail if the state is enforced by a security configuration.

Does this introduce a breaking change?

Please see our docs on breaking changes to help!

  • Yes
  • No

The `resourceGithubRepositoryUpdate` function currently passes
the current state of security_and_analysis to the PATCH /repo
request. If the repo has an enforced security configuration,
this can result in the API returning a 422 response because
changing security settings is not allowed in this case.
Even though the security_and_analysis settings remain the same,
and only some other settings are changed, the request will still
fail.

To avoid the request failing when security_and_analysis remains
the same, we will only pass it in the request payload when changes
have been made.
@jamestran201 jamestran201 marked this pull request as ready for review September 20, 2024 21:00
@jamestran201 jamestran201 marked this pull request as draft September 20, 2024 21:28
@jamestran201 jamestran201 marked this pull request as ready for review September 23, 2024 14:58
@arkadiyt
Copy link

arkadiyt commented Oct 9, 2024

Would love to see this shipped, it is a very annoying issue for our org

@kfcampbell kfcampbell merged commit ab6c7e6 into integrations:main Oct 17, 2024
3 checks passed
ihor-hrytskiv pushed a commit to ihor-hrytskiv/terraform-provider-github that referenced this pull request Oct 28, 2024
…tegrations#2397)

The `resourceGithubRepositoryUpdate` function currently passes
the current state of security_and_analysis to the PATCH /repo
request. If the repo has an enforced security configuration,
this can result in the API returning a 422 response because
changing security settings is not allowed in this case.
Even though the security_and_analysis settings remain the same,
and only some other settings are changed, the request will still
fail.

To avoid the request failing when security_and_analysis remains
the same, we will only pass it in the request payload when changes
have been made.

Co-authored-by: Keegan Campbell <[email protected]>
niklasfaschingmoia added a commit to moia-oss/terraform-provider-github that referenced this pull request Jul 7, 2025
Same issue as
integrations#2397.

Alternatively we could also just send only archived=true and not all
fields (since we just ended management of the repo via terraform and thus care
whether it matches the settings).
niklasfaschingmoia added a commit to moia-oss/terraform-provider-github that referenced this pull request Jul 8, 2025
See integrations#2644

archive_on_destroy currently fails when the security_and_analysis repository
configuration is controlled by an enterprise advanced security configuration
[1].

    │ Error: PATCH https://api.github.com/repos/foo/bar: 422 An enforced
    security configuration prevented modifying secret scanning
    enablement. Contact your organization owner for details. []

Since archive_on_destroy is only meant to archive the repository on
destroy we don't actually need to send a patch request to sync the repository
configuration to match terraform and we could just send a request that does
that

    repoReq := &github.Repository{Archived: github.Bool(true)}

but I guess that's a personal preference and for consistency let's just do what
we already do in `resourceGithubRepositoryUpdate` [2] and filter out the
SecurityAndAnalysis patches from the request when they didn't change.

[1]
https://docs.github.com/en/enterprise-cloud@latest/admin/managing-code-security/securing-your-enterprise/creating-a-custom-security-configuration-for-your-enterprise
[2]
integrations#2252
integrations#2397
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

[BUG]: github_repository: 422 An enforced security configuration prevented modifying advanced security enablement.

3 participants