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

Create data source for organization ip allow list #1275

Merged
merged 7 commits into from
Sep 20, 2022
Merged

Create data source for organization ip allow list #1275

merged 7 commits into from
Sep 20, 2022

Conversation

douglascayers
Copy link
Contributor

Summary

Allow terraform to retrieve an enterprise cloud organization's IP allow list.

This data source is a precursor to a future PR to add a resource to allow terraform to manage IP allow list entries as requested in #1067.

Changes

  • Add data source to retrieve an organization's IP allow list via APIv4 (graphql)
  • Paginates 100 at a time to retrieve all entries
  • Add unit test and documentation

Tests

  • github/data_source_github_organization_ip_allow_list_test.go

Usage

Example configuration:

data "github_organization_ip_allow_list" "all" {}

Example output:

{
  "resources": [
    {
      "mode": "data",
      "type": "github_organization_ip_allow_list",
      "name": "all",
      "instances": [
        {
          "schema_version": 0,
          "attributes": {
            "id": "MDEyOk9yZ2FuaXphdGlvbjg0NDkwOTE4",
            "ip_allow_list": [
              {
                "allow_list_value": "192.30.252.0/22",
                "created_at": "2022-06-10T22:58:22Z",
                "id": "IALE_kwHOBQk6ps4ACyRH",
                "is_active": true,
                "name": "GitHub Hooks",
                "updated_at": "2022-06-10T22:58:22Z"
              },
              {
                "allow_list_value": "185.199.108.0/22",
                "created_at": "2022-06-10T22:58:22Z",
                "id": "IALE_kwHOBQk6ps4ACyRH",
                "is_active": true,
                "name": "GitHub Hooks",
                "updated_at": "2022-06-10T22:58:22Z"
              }
            ]
          },
          "sensitive_attributes": []
        }
      ]
    }
  ]
}

@douglascayers
Copy link
Contributor Author

Hi @kfcampbell, would appreciate your review of this PR to add data source to retrieve an org's IP allow list. Thanks!

"github_actions_secrets": dataSourceGithubActionsSecrets(),
"github_actions_organization_secrets": dataSourceGithubActionsOrganizationSecrets(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorted the resources

Comment on lines -148 to +149
"github_actions_organization_secrets": dataSourceGithubActionsOrganizationSecrets(),
"github_organization_ip_allow_list": dataSourceGithubOrganizationIpAllowList(),
Copy link
Contributor Author

Choose a reason for hiding this comment

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

sorted the resources by moving actions resource to top; then added new the ip allow list resource

@kfcampbell
Copy link
Member

I'm seeing the following error when running the integration test locally:

    testing.go:705: Step 0 error: Check failed: 6 errors occurred:
        	* Check 1/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.id' expected to be set
        	* Check 2/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.name' expected to be set
        	* Check 3/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.allow_list_value' expected to be set
        	* Check 4/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.is_active' expected to be set
        	* Check 5/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.created_at' expected to be set
        	* Check 6/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.updated_at' expected to be set
```

and the test suite doesn't pass. Is it succeeding for you locally? 

@douglascayers
Copy link
Contributor Author

Hrmm, will take a look. It was passing for sure before I back merged with main branch today. I saw the commit check pass so thought that meant the back merge went well.

@douglascayers
Copy link
Contributor Author

douglascayers commented Sep 19, 2022

@kfcampbell The test succeeds locally for me. How are you running the test?

This is what I do from the root of the project:

$ make build
==> Checking that code complies with gofmt requirements...
go build ./...

$ go test -v -run TestAccGithubOrganizationIpAllowListDataSource ./github
=== RUN   TestAccGithubOrganizationIpAllowListDataSource
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_anonymous_account
    data_source_github_organization_ip_allow_list_test.go:40: anonymous account not supported for this operation
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_individual_account
    data_source_github_organization_ip_allow_list_test.go:44: individual account not supported for this operation
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_organization_account
    testing.go:567: Acceptance tests skipped unless env 'TF_ACC' set
--- PASS: TestAccGithubOrganizationIpAllowListDataSource (0.00s)
    --- PASS: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error (0.00s)
        --- SKIP: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_anonymous_account (0.00s)
        --- SKIP: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_individual_account (0.00s)
        --- SKIP: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_organization_account (0.00s)
PASS
ok      github.com/integrations/terraform-provider-github/v5/github     0.462s

@douglascayers
Copy link
Contributor Author

Oh, I just noticed the key part in that test result:

testing.go:567: Acceptance tests skipped unless env 'TF_ACC' set

Stand by, let me set some vars and retest to confirm ⏳

@douglascayers
Copy link
Contributor Author

douglascayers commented Sep 19, 2022

Yes, that test passes for me locally.

$ make build
==> Checking that code complies with gofmt requirements...
go build ./...

$ export GITHUB_TOKEN="ghp_<my_token>"
$ export GITHUB_ORGANIZATION="<my_org>"

$ TF_ACC=1 go test -v -run TestAccGithubOrganizationIpAllowListDataSource ./github
=== RUN   TestAccGithubOrganizationIpAllowListDataSource
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_anonymous_account
    data_source_github_organization_ip_allow_list_test.go:40: anonymous account not supported for this operation
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_individual_account
    data_source_github_organization_ip_allow_list_test.go:44: individual account not supported for this operation
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_organization_account
--- PASS: TestAccGithubOrganizationIpAllowListDataSource (17.58s)
    --- PASS: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error (17.58s)
        --- SKIP: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_anonymous_account (0.00s)
        --- SKIP: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_individual_account (0.00s)
        --- PASS: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_organization_account (17.58s)
PASS
ok      github.com/integrations/terraform-provider-github/v5/github     18.058s

@kfcampbell
Copy link
Member

Unfortunately, our integration tests are being run manually at this stage and are not automatically run on PR checks.

This is weird...I have different results. When I'm up-to-date with your branch, I set the following environment variables:

 sh$ env | grep GITHUB
GITHUB_TOKEN=REDACTED
GITHUB_ORGANIZATION=kfcampbell-terraform-provider

 sh$ env | grep TF_
TF_ACC=1

Then I'll run the test using your same command:

 sh$ TF_ACC=1 go test -v -run TestAccGithubOrganizationIpAllowListDataSource ./github
=== RUN   TestAccGithubOrganizationIpAllowListDataSource
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_anonymous_account
    data_source_github_organization_ip_allow_list_test.go:40: anonymous account not supported for this operation
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_individual_account
    data_source_github_organization_ip_allow_list_test.go:44: individual account not supported for this operation
=== RUN   TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_organization_account
    testing.go:705: Step 0 error: Check failed: 6 errors occurred:
        	* Check 1/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.id' expected to be set
        	* Check 2/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.name' expected to be set
        	* Check 3/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.allow_list_value' expected to be set
        	* Check 4/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.is_active' expected to be set
        	* Check 5/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.created_at' expected to be set
        	* Check 6/6 error: data.github_organization_ip_allow_list.all: Attribute 'ip_allow_list.0.updated_at' expected to be set


--- FAIL: TestAccGithubOrganizationIpAllowListDataSource (2.32s)
    --- FAIL: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error (2.32s)
        --- SKIP: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_anonymous_account (0.00s)
        --- SKIP: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_individual_account (0.00s)
        --- FAIL: TestAccGithubOrganizationIpAllowListDataSource/queries_without_error/with_an_organization_account (2.32s)
FAIL
FAIL	github.com/integrations/terraform-provider-github/v5/github	2.333s
FAIL

FWIW, I have never set or modified IP allowlists for my test organization, which may be why this is failing.

@kfcampbell
Copy link
Member

Yep, manually setting an IP and enabling an allowlist organization makes the test pass.

What would you prefer to do here? We could merge as-is with the idea that once #1067 is closed, we could come back and fix the test.

@douglascayers
Copy link
Contributor Author

douglascayers commented Sep 20, 2022

@kfcampbell Thanks for confirming.

Hrm, I don't have an Enterprise Org that I can test where it doesn't have IP allow lists added but the feature is enabled.

❓ Does the test fail for you if the feature is enabled but no IPs allowed?
❓ Or fail only if the feature isn't enabled?

If it's only failing when the feature isn't enabled, I can try to make the test smarter to check whether it's enabled or not and skip with a message to the user.

Or if the test is simply failing because in either case (feature disabled or no IPs allowed) then the result is empty and so the assertion can't be satisified as written, I can see if I can update the assertion to check for empty list.

If the feature isn't enabled, I would have expected this query to have failed further upstream before the test has a chance to assert the output.

What would you prefer to do here? We could merge as-is with the idea that once #1067 is closed, we could come back and fix the test.

Although I'd love to get this merged now, it's probably best to wait until the test can pass when the feature is enabled with zero allowed IPs (which could very well be a scenario a customer has, not sure why they'd be using this if that were the case, but best not to break their terraform build just because they have zero IPs).

@douglascayers
Copy link
Contributor Author

douglascayers commented Sep 20, 2022

@kfcampbell you know what, I don't think an org can both have IP allow list on AND have no IPs listed as that would prevent the users from accessing it entirely. So there likely has to be at least one IP.

In that case, I think this is good to merge as-is. With the known room for improvement that the test should skip if the feature isn't enabled to explain why the test didn't run.

Which essentially me finally coming back around to your original suggestion 😁

We could merge as-is with the idea that once #1067 is closed, we could come back and fix the test.

@kfcampbell
Copy link
Member

😄 That works for me! I'll merge and release this, and I'm looking forward to checking out any progress on #1067.

Copy link
Member

@kfcampbell kfcampbell left a comment

Choose a reason for hiding this comment

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

After #1067 is closed, the test TestAccGithubOrganizationIpAllowListDataSource should be amended to ensure that the organization being tested has the IP allowlist feature enabled.

@kfcampbell kfcampbell merged commit aaf1a88 into integrations:main Sep 20, 2022
@douglascayers douglascayers deleted the data-source-github-organization-ip-allow-list branch September 20, 2022 19:28
kazaker pushed a commit to auto1-oss/terraform-provider-github that referenced this pull request Dec 28, 2022
* feat: add data source to get org's ip allow list

* test: add test for org ip allow list data source

* doc: document org ip allow list data source

* chore: backmerge from main

* chore: sort resources
avidspartan1 pushed a commit to avidspartan1/terraform-provider-github that referenced this pull request Feb 5, 2024
* feat: add data source to get org's ip allow list

* test: add test for org ip allow list data source

* doc: document org ip allow list data source

* chore: backmerge from main

* chore: sort resources
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.

None yet

2 participants