terraform-provider-github: integration/terraform-provider-github auth doesn't work: "resource can only be used in the context of an organization"

Terraform Version

terraform -v
Terraform v1.0.4
on darwin_amd64

Affected Resource(s)

Please list the resources as a list, for example:

  • github_team

Terraform Configuration Files

terraform {
  required_providers {
    github = {
      source  = "hashicorp/github"
      version = "= 2.9.0"
    }
  }
}
# Exported GITHUB_TOKEN, GITHUB_OWNER, and GITHUB_ORGANIZATION to shell
provider "github" {}
resource "github_team" "some_team" {
  name        = "some-team"
  description = "Some cool team"
  privacy     = "closed"
}

Debug Output

Debug in gist: https://gist.github.com/KyMidd/12336386b5a65db6fac73c1f6bab29f6

Expected Behavior

Terraform should use PAT to make changes to enterprise org.

Actual Behavior

Terraform provider reports either empty user, user name same as org name (?), PAT user’s username, and reports “Error: This resource can only be used in the context of an organization, “KyMidd” is a user.”

Terraform will perform the following actions:

  # github_team.some_team will be created
  + resource "github_team" "some_team" {
      + create_default_maintainer = false
      + description               = "Some cool team"
      + etag                      = (known after apply)
      + id                        = (known after apply)
      + members_count             = (known after apply)
      + name                      = "some-team"
      + node_id                   = (known after apply)
      + privacy                   = "closed"
      + slug                      = (known after apply)
    }

Plan: 1 to add, 0 to change, 0 to destroy.
github_team.some_team: Creating...
╷
│ Error: This resource can only be used in the context of an organization, "KyMidd" is a user.
│ 
│   with github_team.some_team,
│   on main.tf line 17, in resource "github_team" "some_team":
│   17: resource "github_team" "some_team" {
│ 

Steps to Reproduce

  1. Create above config
  2. Exported GITHUB_TOKEN, GITHUB_OWNER, and GITHUB_ORGANIZATION to shell
  3. terraform init
  4. terraform apply

Important Factoids

  • Normal GitHub org within enterprise. PAT has full admin access to Org.
  • Have tested 2.9.0 provider, as well as newest version (4.13.0) of github/terraform-provider-github as well as integrations/terraform-provider-github
  • I’ve read about the bugs this provider has around reading env variables vs provider static info, Org vs Owner, and have tried lots of combinations. None has worked so far.

References

Are there any other GitHub issues (open or closed) or Pull Requests that should be linked here? For example:

  • GH-876
  • GH-655
  • GH-501

About this issue

  • Original URL
  • State: closed
  • Created 3 years ago
  • Reactions: 14
  • Comments: 18 (2 by maintainers)

Most upvoted comments

In my case the issue turned out to be an expired github token.

Not the error message I expected from an expired token.

I can confirm this is still the issue on 4.16.0 and 4.15.1

  • integrations/github broken
  • hashicorp/github works fine

I think closed is fine, Keegan is aggregating reports to the super-issue here: https://github.com/integrations/terraform-provider-github/issues/980

Thanks to Keegan and all folks who are supporting this software! We appreciate you ❤️

Thanks for the reports! We’re collecting information on how to best fix our authentication issues. Adding the authentication label to track and hope to bring a fix to our next major release.

Okay, weird. This works great on my local computer, but in GitHub Actions running on Ubuntu 20.04 container, it doesn’t. Is there any difference between the Mac version of this provider and Ubuntu linux 20.04 version? Didn’t test this, b/c I didn’t expect it. I’ll reopen this issue, as it still appears broken on some platforms.

On Ubuntu 20.04, working:

terraform {
  required_providers {
    github = {
      #source  = "integrations/github" - This new version doesn't work at all, totally broken
      source  = "hashicorp/github"
      version = "= 4.13.0"
    }
  }

Not working:

terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 4.15.1"
    }
  }

Provider block for both, and both export GITHUB_TOKEN into the shell as a variable.

provider "github" {
  #token = var.token # Provided via GITHUB_TOKEN ENV var
  owner = "org-name-goes-here"
}

Any thoughts?

Okay, through attempting about 100 things, found a combination that lets this work:

  1. github provider must be “hashicorp/github”, “integration/github” which is advised by a warn prompt if using old provider, doesn’t work.
terraform {
  required_providers {
    github = {
      #source  = "integrations/github" - This new version doesn't work at all, totally broken
      source  = "hashicorp/github"
      version = "= 4.13.0"
    }
  }
}
  1. GitHub provider block, “base_url” is not required, even for enterprise orgs
  2. The enterprise org name must be set within the github provider block:
provider "github" {
  owner = "github-enterprise-org-name-goes-here"
}
  1. GITHUB_TOKEN should be exported to the shell

Given those 4 things line up, this provider works to create and manage resources.