Skip to content

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

Closed as not planned

Description

@KyMidd

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:

Activity

  1. KyMidd commented on Aug 11, 2021

    @KyMidd
    Author

    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.

  2. KyMidd commented on Aug 12, 2021

    @KyMidd
    Author

    Now I'm building the provider locally from "integrations/terraform-provider-github" and it's failing to authenticate. Is the integration/ copy of this provider just flat broken auth-wise right now?

    terraform {
      required_providers {
        github = {
          # Testing with local
          source  = "terraform.example.com/local/github"
        }
      }
    }
    
    provider "github" {
      #token = var.token # Provided via GITHUB_TOKEN ENV var
      owner = "name-of-enterprise-org"
    }

    GITHUB_TOKEN is populated with a valid token.

    Init pulls in both the local provider that I've specified as well as the hashicorp/ version of the provider (rather than integrations/), likely due to me calling a module? Appears to be an open issue in another issue.

    rm -rf .terraform && rm .terraform.lock.hcl && terraform init
    
    Initializing the backend...
    
    Initializing provider plugins...
    - Finding latest version of terraform.example.com/local/github...
    - Finding latest version of hashicorp/github...
    - Installing hashicorp/github v4.13.0...
    - Installed hashicorp/github v4.13.0 (signed by HashiCorp)
    - Installing terraform.example.com/local/github v4.13.0...
    - Installed terraform.example.com/local/github v4.13.0 (unauthenticated)
    

    And here's the error message, same as saw earlier.

    terraform plan
    module.kyler_team.github_team_membership.members["kymidd"]: Refreshing state... [id=xxx:kymidd]
    module.kyler_team.github_team_membership.maintainers["kymidd"]: Refreshing state... [id=xxx:kymidd]
    module.kyler_team.github_team.team: Refreshing state... [id=xxx]
    ╷
    │ Error: This resource can only be used in the context of an organization, "" is a user.
    
  3. nicksantamaria commented on Aug 25, 2021

    @nicksantamaria

    Might be related to #876

    Looks like the fix over there is to set the GITHUB_OWNER env var.

  4. KyMidd commented on Aug 25, 2021

    @KyMidd
    Author

    Same behavior after exporting GITHUB_OWNER into shell:

    $ terraform apply -auto-approve
    (removed)
    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, "" is a user.
    │   with github_team.some_team,
    │   on main.tf line 1, in resource "github_team" "some_team":
    │    1: resource "github_team" "some_team" {
    
    $ GITHUB_OWNER=real_value_removed
    $ terraform apply -auto-approve
    (removed)
    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, "" is a user.
    │   with github_team.some_team,
    │   on main.tf line 1, in resource "github_team" "some_team":
    │    1: resource "github_team" "some_team" {
    

    I have written a GraphQL add-on to manage team auto-assignment, and I'd love for the provider to natively support it, but unable to develop in this state. Any ideas welcome.

  5. added
    Type: Bug
    on Aug 31, 2021
  6. KyMidd commented on Sep 3, 2021

    @KyMidd
    Author

    Saw v4.14.0 that PR #867 shipped and informs about authentication. I refreshed my copy of code, built local, and see the same issues.

    Two interesting things:

    • The provider says the "token" is not found regardless of whether GITHUB_TOKEN is set in the shell
    • The provider says the "owner" is blank (see: "owner from GITHUB_OWNER") regardless of whether GITHUB_OWNER is set in the shell or also if "owner" is set in the provider's github configuration terraform block.
    $ export TF_LOG=DEBUG; rm -rf .terraform; rm .terraform.lock.hcl; terraform init; terraform apply -auto-approve
    (removed)
    2021-09-03T12:10:32.370-0500 [DEBUG] provider.terraform-provider-github: 2021/09/03 12:10:32 [DEBUG] Selecting owner  from GITHUB_OWNER environment variable
    2021-09-03T12:10:32.370-0500 [DEBUG] provider.terraform-provider-github: 2021/09/03 12:10:32 [DEBUG] No token present; configuring anonymous owner.
    2021-09-03T12:10:32.374-0500 [WARN]  Provider "terraform.example.com/local/github" produced an invalid plan for github_team.some_team, but we are tolerating it because it is using the legacy plugin SDK.
        The following problems may be the cause of any confusing errors from downstream operations:
          - .create_default_maintainer: planned value cty.False for a non-computed attribute
    github_team.some_team: Creating...
    2021-09-03T12:10:32.374-0500 [INFO]  Starting apply for github_team.some_team
    2021-09-03T12:10:32.374-0500 [DEBUG] github_team.some_team: applying the planned Create change
    ╷
    │ Error: This resource can only be used in the context of an organization, "" is a user.
    │ 
    │   with github_team.some_team,
    │   on main.tf line 1, in resource "github_team" "some_team":
    │    1: resource "github_team" "some_team" {
    │ 
    ╵
    2021-09-03T12:10:32.394-0500 [DEBUG] provider: plugin process exited: path=.terraform/providers/terraform.example.com/local/github/4.14.0/darwin_amd64/terraform-provider-github pid=16622
    2021-09-03T12:10:32.394-0500 [DEBUG] provider: plugin exited 
    

    Unfortunately, auth still appears entirely broken. Any suggestions or testing you'd like to see I'm happy to do :) Thanks all!
    kyler

  7. changed the title [-]Create Team in Org within Enterprise Failing: "resource can only be used in the context of an organization"[/-] [+]integration/terraform-provider-github auth doesn't work: "resource can only be used in the context of an organization"[/+] on Sep 3, 2021
  8. KyMidd commented on Sep 24, 2021

    @KyMidd
    Author

    Hey team, after the recent releases I attempted this again. I built the provider:

    #/bin/bash
    
    echo "Setting gopath"
    GOPATH=/Users/username/go/
    
    echo "Building provider"
    make fmt
    make build
    
    echo #Checking output datestamps of output location
    echo 'ls -l $GOPATH/bin | grep terraform-provider-github'
    ls -l $GOPATH/bin | grep terraform-provider-github
    
    echo "making directory"
    mkdir -p /Users/username/.terraform.d/plugins/terraform.example.com/local/github/4.14.0/darwin_amd64
    
    echo "Staging provider"
    cp $GOPATH/bin/terraform-provider-github /Users/username/.terraform.d/plugins/terraform.example.com/local/github/4.14.0/darwin_amd64
    
    if [[ $? -eq 0 ]]; then
        echo "File built and staged"
    else
        echo "Didn't work, need to investigate"
    fi
    

    I exported the GITHUB_TOKEN in my terminal

    export GITHUB_TOKEN=ghp_Fixxxxxxxxx
    

    My github provider block specifies the org:

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

    And it worked 💥 💥 .

    Whatever ya'll have done to fix auth in the past few PRs, it appears to have worked. Since I can now compile and get a working copy, I can hopefully start developing and add some feature. Thanks all! I'll close this issue.

  9. KyMidd commented on Sep 24, 2021

    @KyMidd
    Author

    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?

  10. reopened this on Sep 24, 2021
  11. jcudit commented on Oct 5, 2021

    @jcudit
    Contributor

    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.

  12. ahmadnassri commented on Oct 11, 2021

    @ahmadnassri

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

    • integrations/github broken
    • hashicorp/github works fine
  13. kfcampbell commented on Nov 18, 2021

    @kfcampbell
    Contributor

    I've been playing around with this a little further and I don't believe this behavior is fundamentally broken. Here's a super-pared-down repro case that makes this evident:

    terraform {
      required_providers {
        github = {
          source = "integrations/github"
          version = "4.18.0"
        }
      }
    }
    
    provider "github" {}
    
    resource "github_team" "team-issue-repro" {
      name        = "878_repro_team"
      description = "Some cool issue reproduction team"
      privacy     = "closed"
    }

    I've exported my GITHUB_TOKEN and GITHUB_OWNER environment variables as such:

     sh$ env | grep GITHUB
    GITHUB_TOKEN=ghp_personal_token_redacted
    GITHUB_OWNER=kfcampbell-terraform-provider
    

    although it works the same if these variables are provided in the provider block of the above configuration as well.

    The team is successfully created in my organization:

    created_team

    Can you please try the above reproduction and let me know if it works for you?

  14. KyMidd commented on Dec 10, 2021

    @KyMidd
    Author

    Hey @kfcampbell : I no longer have access to this environment to test, but @asgr86 or @JordanKochVeradigm might be able to test this out, tagging in case they can.

    Based on your testing, it doesn't appear a mixed configuration vs environmental variables config is supported, and must fully use one or the other. And also the organization vs owners is sometimes broken and documentation isn't clear why.

    Thank you for taking this on, huge pain trying to scale our github horizontally without proper tooling that works reliably. Best of luck sorting this out!

  15. alexdepalex commented on Feb 9, 2022

    @alexdepalex

    Still seems to be broken on GH Enterprise 3.2.7 and integrations/github 4.20.0.

  16. paul-civitas commented on Mar 21, 2022

    @paul-civitas

    This was working for me before but I updated my provider and now it's broken :(

  17. bluehawk27 commented on Apr 4, 2022

    @bluehawk27

    @kfcampbell thank you for that comment. Your suggested course of action worked for me on 4.23.0

  18. paul-civitas commented on Apr 4, 2022

    @paul-civitas

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

    Not the error message I expected from an expired token.

  19. dmitry-mightydevops commented on Nov 19, 2022

    @dmitry-mightydevops

    on my side that error

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

    belongs to the use of github_team_repository resource

    so I had to

    • remove any mention of that resource from the state
    • comment tf code creating github_team_repository resources

    my provider is latest 5.9.0, terraform 1.3.4

    Terraform v1.3.4
    on linux_amd64
    + provider registry.terraform.io/cyrilgdn/postgresql v1.17.1
    + provider registry.terraform.io/gavinbunney/kubectl v1.14.0
    + provider registry.terraform.io/hashicorp/aws v4.40.0
    + provider registry.terraform.io/hashicorp/github v5.9.0
    + provider registry.terraform.io/hashicorp/kubernetes v2.16.0
    + provider registry.terraform.io/hashicorp/local v2.2.3
    + provider registry.terraform.io/hashicorp/null v3.2.1
    + provider registry.terraform.io/hashicorp/random v3.4.3
    + provider registry.terraform.io/hashicorp/tls v4.0.4
    + provider registry.terraform.io/integrations/github v5.9.0
    + provider registry.terraform.io/scottwinkler/shell v1.7.10
    + provider registry.terraform.io/winebarrel/mysql v1.10.6
    
    provider "github" {
    
      owner = var.github_organization
      app_auth {
        id              = var.github_app_auth_id
        installation_id = var.github_app_auth_installation_id
        pem_file        = file(var.github_app_auth_pem_file)
      }
    }

    All resources are authenticated just fine, except for the team permissions.

    I have selected the following Permissions for the github app:

    Repository permissions

    • Administration (read and write)
    • Checks (read and write)
    • Commit statuses (read and write)
    • Contents (read and write)
    • Discussions (read and write)
    • Issues (read and write)
    • Metadata (read-only)
    • Pages (read and write)
    • Projects (read and write)

    Organization permissions

    • Administration (read and write)
    • Members (read and write)
    • Team Discussions (read and write)

    Seems that should be a sufficient for github_team_repository, but it still fails.

    Interesting thing is that the association (i.e. team_repository) is created successfully, but then still fails with the error message,

    As reported here: #1373

  20. github-actions commented on Aug 17, 2023

    @github-actions

    👋 Hey Friends, this issue has been automatically marked as stale because it has no recent activity. It will be closed if no further activity occurs. Please add the Status: Pinned label if you feel that this issue needs to remain open/active. Thank you for your contributions and help in keeping things tidy!

  21. added
    Status: Stale
    on Aug 17, 2023
  22. KyMidd commented on Aug 24, 2023

    @KyMidd
    Author

    I think closed is fine, Keegan is aggregating reports to the super-issue here: #980

    Thanks to Keegan and all folks who are supporting this software! We appreciate you <3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    AuthenticationStatus: StaleType: Bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions