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

This resource can only be used in the context of an organization, "" is a user #830

Closed
MaXeraph opened this issue Jun 23, 2021 · 20 comments
Closed
Labels
Authentication r/membership Status: Stale Used by stalebot to clean house Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented

Comments

@MaXeraph
Copy link

MaXeraph commented Jun 23, 2021

Terraform Version

  • Terraform version: 1.0.0
  • Github version: 4.11.0

Affected Resource(s)

  • github_membership

Terraform Configuration Files

resource "github_membership" "configuration" {
  username = var.username
  role     = var.role
}

variable "username" {}

variable "role" {
  default = "member"
}

provider "github" {
  owner = "foo"
}

Debug Output

Debug/Trace doesn't show any new output that could specify the error.

Panic Output

N/A

Expected Behavior

Should be able to output a plan to remove github_membership resource belonging to an organization.

Actual Behavior

Errors:

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

  with module.users_foo["bar"].github_membership.configuration,
  on modules/user-memberships/main.tf line 1, in resource "github_membership" "configuration":
   1: resource "github_membership" "configuration" {

Steps to Reproduce

  1. terraform plan --refresh=false

Important Factoids

  • Errors and crashes when running plan on Jenkins. There were no changes on our Jenkins configuration, and we were able to run plan and apply with no errors prior to this config modification.
  • Locally running the same configuration with terraform plan --refresh=false successfully create a plan. However, it is run with my own Github token, supplied in the provider resource. Whereas Jenkins utilizes a global token (which has been working fine until now) in env GITHUB_TOKEN

References

Possibly related to #501

@martyca
Copy link

martyca commented Jun 24, 2021

I was experiencing this behaviour when updating the remote (terraform cloud) state file from my CLI using terraform import github_team.core 1234567.
I resolved the issue by locally setting the environment variable GITHUB_TOKEN to my personal access token (which has the same privileges as the token set on my terrraform cloud workspace)
In my case the issue was that I was trying to access resources locally that I was not authenticated to do.
see:
https://registry.terraform.io/providers/integrations/github/latest/docs#authentication

@roobert
Copy link

roobert commented Jun 28, 2021

Hey, I have also come across this same problem. The issue happens for me when using modules, it seems like the provider owner value is not inherited by modules and GITHUB_OWNER must be set in the environment in order to fix.

edited to add: Like @martyca, I have also found that I must have GITHUB_TOKEN set even though I have provided the token to the github provider block.

@alvarolorentedev
Copy link

still happening in 4.13.0

@roobert workaround of environment variables still works, but it relies now on a deprecated functionality.

╷
│ Warning: "organization": [DEPRECATED] Use owner (or GITHUB_OWNER) instead of organization (or GITHUB_ORGANIZATION)
│ 
│   with provider["registry.terraform.io/hashicorp/github"],
│   on <empty> line 0:
│   (source code not available)
│ 

@hazcod
Copy link

hazcod commented Oct 18, 2021

Encountering the same in v4.17.0.

@guillermotti
Copy link

Yes, as @hazcod said, with v4.17.0 I faced the same problem creating a new team

@lcaille
Copy link

lcaille commented Nov 18, 2021

Hi, same error with v4.18.0 too.

@hazcod
Copy link

hazcod commented Dec 27, 2021

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

@mwos-sl
Copy link

mwos-sl commented May 6, 2022

exporting GITHUB_OWNER does not solve the problem for me.
v4.24.1

@hazcod
Copy link

hazcod commented Jun 29, 2022

Still needing to do export GITHUB_TOKEN="${TF_VAR_github_token}" to workaround.

@kmdouglass
Copy link

kmdouglass commented Aug 15, 2022

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

This solution works for me with integrations/github provider version 4.28.0 so long as the provider configuration block is empty. In other words:

provider "github" {
  token = "var.token"
  owner = "my-org"
}

will fail even with the GITHUB_TOKEN and GITHUB_OWNER environment variables set, but

provider "github" {}

will work if the environment variables are set.

@alexlop29
Copy link

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

This solution works for me with integrations/github provider version 4.28.0 so long as the provider configuration block is empty. In other words:

provider "github" {
  token = "var.token"
  owner = "my-org"
}

will fail even with the GITHUB_TOKEN and GITHUB_OWNER environment variables set, but

provider "github" {}

will work if the environment variables are set.

+1 with v.5.7.0

@kfcampbell kfcampbell added Status: Up for grabs Issues that are ready to be worked on by anyone Priority: High labels Nov 10, 2022
@kfcampbell
Copy link
Member

it seems like the provider owner value is not inherited by modules and GITHUB_OWNER must be set in the environment in order to fix.

I wonder if that's a thing we can adjust in our code or a limitation of Terraform as a whole.

@sergio91zulu
Copy link

Work For me, Provider configure:

variable "GITHUB_TOKEN" {
  description = "Github Token Admin"
  type        = string
}

variable "GITHUB_OWNER" {
  description = "Github Owner Organization"
  type        = string
}
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 4.0"
    }
  }
provider "github" {
  token     = var.GITHUB_TOKEN
  owner     = var.GITHUB_OWNER
}

export TF_VAR_GITHUB_TOKEN={personal_token}
export TF_VAR_GITHUB_OWNER={org-name}
export GITHUB_OWNER="${TF_VAR_GITHUB_OWNER}"
export GITHUB_TOKEN="${TF_VAR_GITHUB_TOKEN}"

@shaun-rutherford
Copy link

I'm in the process of updating our TF code base from the 2.x version of github when I ran into this issue.

It looks like this issue starts in v3.0.0.
I tested on v2.9.1 and it works without issue.

@kfcampbell
Copy link
Member

Hmm...here's the diff between those two tags. There's some changes in github/config.go that could potentially be at fault.

@thetimbecker
Copy link

BEWARE: this error is very misleading. We thought the Go library needed because of #1391

For us, it turns out our Personal Access Token just stopped working for some reason. We made a new token and everything started working again.

Seems like this has come up a lot: https://github.com/integrations/terraform-provider-github/search?q=in+the+context+of+an+organization+is+a+user&type=issues

Maybe this can be added to the README or something as a troubleshooting step?

@kfcampbell
Copy link
Member

@thetimbecker agreed that usually the cause is an invalid PAT! Would you be interested in opening up a PR to update the docs accordingly?

@aiham
Copy link

aiham commented Feb 26, 2023

Same with v4.19.0... Workaround:

export GITHUB_OWNER="${TF_VAR_github_owner}"
export GITHUB_TOKEN="${TF_VAR_github_token}"

This solution works for me with integrations/github provider version 4.28.0 so long as the provider configuration block is empty. In other words:

provider "github" {
  token = "var.token"
  owner = "my-org"
}

will fail even with the GITHUB_TOKEN and GITHUB_OWNER environment variables set, but

provider "github" {}

will work if the environment variables are set.

+1 with v.5.7.0

Here's another confirmation 4 months later. This workaround still works in v5.18.0.

@thetimbecker
Copy link

@thetimbecker agreed that usually the cause is an invalid PAT! Would you be interested in opening up a PR to update the docs accordingly?

Clearly I never got around to this 😅

Unfortunately I can't take the time to do it well right now, especially since the issue it's fresh in my mind anymore.

Copy link

👋 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!

@github-actions github-actions bot added the Status: Stale Used by stalebot to clean house label May 28, 2024
@github-actions github-actions bot closed this as not planned Won't fix, can't repro, duplicate, stale Jun 4, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Authentication r/membership Status: Stale Used by stalebot to clean house Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented
Projects
None yet
Development

No branches or pull requests