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

[Feature Request] Authoritative repository access management #395

Closed
eriksw opened this issue Mar 26, 2020 · 8 comments · Fixed by #1121
Closed

[Feature Request] Authoritative repository access management #395

eriksw opened this issue Mar 26, 2020 · 8 comments · Fixed by #1121
Labels
New resource Type: Feature New feature or request

Comments

@eriksw
Copy link

eriksw commented Mar 26, 2020

Currently, there appears to be no way to authoritatively manage repository permissions using the GitHub terraform provider.

My use case is that there are repositories that have been created over the years, that have been imported into management by terraform, but these repositories have miscellaneous grants of read/admin/write permissions to arbitrary teams and users. (Most commonly, 'admin' to the user that initially created the repo, but there's also cases like read access being granted to a contract pentester that nobody remembered to revoke, etc...)

github_team_repository and github_repository_collaborator exist, but they are only additive: the absence of a resource setting the access for a team/user will not remove that user's access.

It would be great if there was an equivalent to google_project_iam_policy — something where I can declare what users and teams should have what levels of access, and that upon apply will remove all other access.

@eriksw eriksw changed the title Authoritative repository access management [Feature Request] Authoritative repository access management Mar 26, 2020
@jcudit jcudit added New resource Type: Feature New feature or request labels Dec 3, 2020
@j-martin
Copy link

j-martin commented Oct 28, 2021

In case it may help somebody we ended with this hack on top of a hack.

locals {
  members = toset([
    "userA",
  ])

  owners = toset([
    "userB",
  ])

  all_members       = setunion(local.owners, local.members)
  existing_members  = toset(data.github_organization.main.members)
  unmanaged_members = setsubtract(local.existing_members, local.all_members)
}

data "github_organization" "main" {
  name = "<your-org>"
}

resource "github_membership" "main" {
  for_each = setunion(local.members, local.owners)
  username = each.value
  role     = contains(local.owners, each.value) ? "admin" : "member"
}

// The GitHub provider is not authoritative for the members of the organization
// and Terraform does not support raising exceptions so we have to hack this check.
// https://github.com/integrations/terraform-provider-github/issues/395
// https://github.com/hashicorp/terraform/issues/15469#issuecomment-814789329

resource "github_membership" "unmanaged_members_this_should_be_empty" {
  for_each = local.unmanaged_members
  username = each.value
  role     = "member"
}

resource "null_resource" "unmanaged_members" {
  count = length(local.unmanaged_members) == 0 ? 0 : "There are members who are not managed by Terraform. Comment out this resource to find who. The last one will surprise you!"
}

It can be refatored/modified into a module to manage team memberships.

@maroux
Copy link
Contributor

maroux commented Apr 20, 2022

Just opened a PR for this issue, still working on tests.

@maroux
Copy link
Contributor

maroux commented May 18, 2022

@jcudit could someone take a look at the linked PR and let me know if this is something you'd be open to merging?

@maroux
Copy link
Contributor

maroux commented May 19, 2022

cc @kfcampbell maybe?

The PR is now complete with tests.

maroux added a commit to maroux/terraform-provider-github that referenced this issue May 19, 2022
@eriksw
Copy link
Author

eriksw commented May 19, 2022

@maroux Is your PR intended to cover team access as well, or just individual users?

What I'm hoping will eventually be part of this provider is a resource where there's one instance of that resource per repository and it covers both team and user(/collaborator) access in an authoritative manner. (Everything that's not a GitHub App and not inherited from the organization level.)

@maroux
Copy link
Contributor

maroux commented May 19, 2022

@eriksw both teams and individual users. Example config:

resource "github_repository_collaborators" "a_repo_collaborators" {
  repository = "our-cool-repo"

  user {
    permission = "admin"
    username  = "SomeUser"
  }
  
  team {
    permission = "pull"
    team_id = "SomeTeam"
  }
}

maroux added a commit to maroux/terraform-provider-github that referenced this issue Oct 21, 2022
maroux added a commit to maroux/terraform-provider-github that referenced this issue Oct 31, 2022
@github-actions
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 Feb 14, 2023
@eriksw
Copy link
Author

eriksw commented Feb 14, 2023

Please don't close this until an authoritative resource is available.

@github-actions github-actions bot removed the Status: Stale Used by stalebot to clean house label Feb 15, 2023
kfcampbell added a commit that referenced this issue Apr 4, 2023
* Add resource for authoritative repo access management

Fixes #395

* fixes after testing

* Tests

* Bump to v48

* docs

* review comments

* also add line to github_team_repository

* env

* support for custom role and move to using slug instead of team id

* v50

* more v50

* inverse

* Vague function rename

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
avidspartan1 pushed a commit to avidspartan1/terraform-provider-github that referenced this issue Feb 5, 2024
)

* Add resource for authoritative repo access management

Fixes integrations#395

* fixes after testing

* Tests

* Bump to v48

* docs

* review comments

* also add line to github_team_repository

* env

* support for custom role and move to using slug instead of team id

* v50

* more v50

* inverse

* Vague function rename

---------

Co-authored-by: Keegan Campbell <me@kfcampbell.com>
Co-authored-by: Nick Floyd <139819+nickfloyd@users.noreply.github.com>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
New resource Type: Feature New feature or request
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants