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

[BUG]: github_repository_deploy_key fails when creating: POST returns 404 #1971

Open
1 task done
Richard87 opened this issue Oct 22, 2023 · 10 comments
Open
1 task done
Labels
Status: Up for grabs Issues that are ready to be worked on by anyone Type: Bug Something isn't working as documented

Comments

@Richard87
Copy link

Expected Behavior

The deploy key should be created

Actual Behavior

github_repository_deploy_key.this: Creating...
╷
│ Error: POST https://api.github.com/repos//infrastructure/keys: 404 Not Found []
│ 
│   with github_repository_deploy_key.this,
│   on main.tf line 44, in resource "github_repository_deploy_key" "this":
│   44: resource "github_repository_deploy_key" "this" {
│ 
╵

Terraform Version

terraform -v
Terraform v1.5.7
on darwin_arm64

  • provider registry.terraform.io/fluxcd/flux v1.1.2
  • provider registry.terraform.io/hashicorp/azurerm v3.74.0
  • provider registry.terraform.io/hashicorp/tls v4.0.4
  • provider registry.terraform.io/integrations/github v5.40.0

Your version of Terraform is out of date! The latest version
is 1.6.2. You can update by downloading from https://www.terraform.io/downloads.html

Affected Resource(s)

  • github_repository_deploy_key

Terraform Configuration Files

# We strongly recommend using the required_providers block to set the
# Azure Provider source and version being used
terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 5.0"
    }
  }
}

provider "github" {
  owner        = var.GITHUB_ORG
  organization = var.GITHUB_ORG
}

resource "tls_private_key" "flux" {
  algorithm   = "ECDSA"
  ecdsa_curve = "P256"
}

resource "github_repository_deploy_key" "this" {
  title      = "Flux"
  repository = var.GITHUB_REPO
  key        = tls_private_key.flux.public_key_openssh
  read_only  = "false"
}

Steps to Reproduce

I have tried these variants:

terraform apply --var-file ../.env
// Tried setting `organization` instead of `owner` in the `provider` settings with same results
GITHUB_OWNER=org-name terraform apply --var-file ../.env
// Tried setting `repository` name to `var.GITHUB_ORG` to verify that the variable is set correctly... and it was :) 

Debug Output

// TF_LOG=DEBUG terraform apply --var-file ../.env 2> log.txt

https://gist.github.com/Richard87/3123b78f9caaeb763fe49ce5a612cee6

2023-10-22T12:08:11.602+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: 2023/10/22 12:08:11 [INFO] Selecting owner  from GITHUB_OWNER environment variable
2023-10-22T12:08:11.602+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: 2023/10/22 12:08:11 [INFO] Selecting organization attribute as owner: happydogsas

...

2023-10-22T12:08:11.615+0200 [INFO]  Starting apply for github_repository_deploy_key.this
2023-10-22T12:08:11.615+0200 [DEBUG] github_repository_deploy_key.this: applying the planned Create change
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: 2023/10/22 12:08:11 [DEBUG] GitHub API Request Details:
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: ---[ REQUEST ]---------------------------------------
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: POST /repos//infrastructure/keys HTTP/1.1
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Host: api.github.com
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: User-Agent: go-github/v55.0.0
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Content-Length: 206
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Accept: application/vnd.github.v3+json,application/vnd.github.stone-crop-preview+json
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Content-Type: application/json
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Github-Api-Version: 2022-11-28
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Accept-Encoding: gzip
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: 
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: {
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0:  "key": "ecdsa-sha2-nistp256 AAAAE2VjZHNhLXNoYTItbmlzdHAyNTYAAAAIbmlzdHAyNTYAAABBBE97tjVQGT1MurcZMMvkyNpkBbTgyTgXrcoMN/aOdSAcuns5UOATfiJFeCT7PHvRPJqRBnFXkb4F4erBTs5qEtI=\n",
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0:  "title": "Flux",
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0:  "read_only": false
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: }
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: 
2023-10-22T12:08:11.616+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: -----------------------------------------------------
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: 2023/10/22 12:08:11 [DEBUG] GitHub API Response Details:
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: ---[ RESPONSE ]--------------------------------------
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: HTTP/2.0 404 Not Found
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Access-Control-Allow-Origin: *
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Access-Control-Expose-Headers: ETag, Link, Location, Retry-After, X-GitHub-OTP, X-RateLimit-Limit, X-RateLimit-Remaining, X-RateLimit-Used, X-RateLimit-Resource, X-RateLimit-Reset, X-OAuth-Scopes, X-Accepted-OAuth-Scopes, X-Poll-Interval, X-GitHub-Media-Type, X-GitHub-SSO, X-GitHub-Request-Id, Deprecation, Sunset
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Content-Security-Policy: default-src 'none'
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Content-Type: application/json; charset=utf-8
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Date: Sun, 22 Oct 2023 10:08:11 GMT
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Referrer-Policy: origin-when-cross-origin, strict-origin-when-cross-origin
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Server: GitHub.com
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Strict-Transport-Security: max-age=31536000; includeSubdomains; preload
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: Vary: Accept-Encoding, Accept, X-Requested-With
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Content-Type-Options: nosniff
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Frame-Options: deny
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Github-Api-Version-Selected: 2022-11-28
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Github-Media-Type: github.v3; format=json, github.stone-crop-preview; format=json
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Github-Request-Id: C0FD:C891:2019CC9:209190A:6534F48B
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Ratelimit-Limit: 60
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Ratelimit-Remaining: 55
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Ratelimit-Reset: 1697971509
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Ratelimit-Resource: core
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Ratelimit-Used: 5
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: X-Xss-Protection: 0
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: 
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: {
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0:  "message": "Not Found",
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0:  "documentation_url": "https://docs.github.com/rest"
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: }
2023-10-22T12:08:11.864+0200 [DEBUG] provider.terraform-provider-github_v5.40.0: -----------------------------------------------------
2023-10-22T12:08:11.870+0200 [DEBUG] State storage *statemgr.Filesystem declined to persist a state snapshot
2023-10-22T12:08:11.870+0200 [ERROR] vertex "github_repository_deploy_key.this" error: POST https://api.github.com/repos//infrastructure/keys: 404 Not Found []
�[31m╷�[0m�[0m
�[31m│�[0m �[0m�[1m�[31mError: �[0m�[0m�[1mPOST https://api.github.com/repos//infrastructure/keys: 404 Not Found []�[0m
�[31m│�[0m �[0m
�[31m│�[0m �[0m�[0m  with github_repository_deploy_key.this,
�[31m│�[0m �[0m  on main.tf line 22, in resource "github_repository_deploy_key" "this":
�[31m│�[0m �[0m  22: resource "github_repository_deploy_key" "this" �[4m{�[0m�[0m
�[31m│�[0m �[0m
�[31m╵�[0m�[0m
2023-10-22T12:08:11.878+0200 [DEBUG] provider.stdio: received EOF, stopping recv loop: err="rpc error: code = Unavailable desc = error reading from server: EOF"
2023-10-22T12:08:11.880+0200 [DEBUG] provider: plugin process exited: path=.terraform/providers/registry.terraform.io/integrations/github/5.40.0/darwin_arm64/terraform-provider-github_v5.40.0 pid=69177
2023-10-22T12:08:11.885+0200 [DEBUG] provider: plugin exited

Panic Output

No response

Code of Conduct

  • I agree to follow this project's Code of Conduct
@Richard87 Richard87 added Status: Triage This is being looked at and prioritized Type: Bug Something isn't working as documented labels Oct 22, 2023
@kfcampbell kfcampbell added Status: Up for grabs Issues that are ready to be worked on by anyone and removed Status: Triage This is being looked at and prioritized labels Oct 23, 2023
@automaticgiant
Copy link

automaticgiant commented Nov 3, 2023

seems like a similar issue to what i'm encountering

data "github_repository" "repo" {
  full_name = "org/repo"
}

resource "github_repository_environment" "stage" {
  environment  = "stage"
  repository   = data.github_repository.repo.id
}

repo is private in org

i can't tell if it's a github_repository_environment issue or a data source issue (probably the former - i can put the result of the data source in an output and see the right repo)

it will only ever try to manage a nonexistent repo in my user's namespace

@rkulinski
Copy link

Same issue.

@rishabh-spaceiq
Copy link

rishabh-spaceiq commented Nov 28, 2023

I am also facing similar issue while creating deploy key in github.

github_repository_deploy_key.main: Creating...

│ Error: POST https://api.github.com/repos/ORG/REPO/keys: 404 Not Found []

│ with github_repository_deploy_key.main,
│ on main.tf line 67, in resource "github_repository_deploy_key" "main":
│ 67: resource "github_repository_deploy_key" "main" {

@rishabh-spaceiq
Copy link

PAT token was expired, that's why I was getting error.
Once I used token from new PAT token then it started working

@squat
Copy link

squat commented Jan 12, 2024

In case this helps anyone: we faced the same issue today and the fix was to ensure that the user associated with the token had the admin role on the repository. This is documented in: https://docs.github.com/en/organizations/managing-user-access-to-your-organizations-repositories/managing-repository-roles/repository-roles-for-an-organization

@BuckinghamIO
Copy link

Facing the same issue also:

PAT token hasn't expired.
PAT token user has admin role on repository too.

@BuckinghamIO
Copy link

BuckinghamIO commented Mar 22, 2024

Just an update this can occur if you forgot to add the Github Provider block.

Could we add a dependency check for another provider to prevent this?

@linton-galloway-bloom
Copy link

Just an update this can occur if you forgot to add the Github Provider block.

Could we add a dependency check for another provider to prevent this?

Thanks, this fixed it for me. I'd missed the injection of the custom GitHub provider. I'm using fine grained access tokens generated via a GitHub App with the repo constraints managed in a terraform submodule.

@Sebelino
Copy link
Contributor

In my case, I had forgotten to add a a required_providers block inside one of my sub-modules. Once I added it and reran terraform init, the resource was created successfully.

$ tree
.
├── main.tf
├── modules
│   └── deploy-key
│       ├── main.tf
│       └── versions.tf
└── versions.tf
$ cat ./modules/deploy-key/versions.tf
terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "6.2.2"
    }
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
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

9 participants