terraform-provider-github icon indicating copy to clipboard operation
terraform-provider-github copied to clipboard

Possible bug: provider fails to recognize the targeted owner org when authenticated with GitHub App installation

Open mdb opened this issue 2 years ago • 0 comments

Hello! I believe terraform-provider-github fails to recognize the targeted organization when authenticated with a GitHub App installation, causing:

Error: this resource can only be used in the context of an organization, "" is a user

...despite that earlier logs report:

2022-09-01T19:27:23.160Z [DEBUG] provider.terraform-provider-github_v4.30.0: 2022/09/01 19:27:23 [INFO] Selecting organization attribute as owner: <the-targeted-org>

Terraform Version

1.2.7

Affected Resource(s)

Terraform Configuration Files

Note that I'm also using the following GitHub App environment variables to configure the provider, as purportedly supported by the provider:

GITHUB_APP_ID
GITHUB_APP_INSTALLATION_ID
GITHUB_APP_PEM_FILE
terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 4.0"
    }
  }
}

provider "github" {
  organization = "<the-targeted-org>"
}

resource "github_team" "foo" {
  name        = "foo"
  description = "test team"
}

Expected Behavior

The provider should be correctly configured to target the org specified by the owner = <the-targeted-org> argument in the provider "github" {}.

Actual Behavior

2022-09-01T19:27:23.160Z [DEBUG] provider.terraform-provider-github_v4.30.0: 2022/09/01 19:27:23 [INFO] Selecting organization attribute as owner: <the-targeted-org>
2022-09-01T19:27:23.160Z [DEBUG] provider.terraform-provider-github_v4.30.0: 2022/09/01 19:27:23 [INFO] Setting write_delay_ms to 1000
2022-09-01T19:27:23.160Z [DEBUG] provider.terraform-provider-github_v4.30.0: 2022/09/01 19:27:23 [DEBUG] Setting read_delay_ms to 0
2022-09-01T19:27:23.160Z [DEBUG] provider.terraform-provider-github_v4.30.0: 2022/09/01 19:27:23 [INFO] No token present; configuring anonymous owner.
...
Error: this resource can only be used in the context of an organization, "" is a user

Important Factoids

When a GitHub App installation is used to authenticate the provider, it seems we enter this code path and error, for example: https://github.com/integrations/terraform-provider-github/blob/main/github/resource_github_team.go#L82

...from https://github.com/integrations/terraform-provider-github/blob/main/github/util.go#L23

...presumably because the use of a GitHub App causes us to never enter this codepath: https://github.com/integrations/terraform-provider-github/blob/main/github/config.go#L116

...which results in this incorrect error:

Error: this resource can only be used in the context of an organization, "" is a user

Note that the use of an oauth/personal access token to authenticate the provider does not encounter the above-cited error and works as expected, presumably because we enter this code path, which in turn sends down this codepath, if I understand correctly.

Note that it's possible I'm mistaken about this purported bug's validity altogether, as I would think the setting of a token when a GitHub App installation is used would ensure Config#Anonymous returns false, thus sending us into this codepath where I'd think we should eventually land here.

mdb avatar Sep 01 '22 20:09 mdb