Yang Liu
Yang Liu

Reputation: 751

401 Bad credentials when try to hook code pipeline with GitHub

I got this error message when try to do cdk deploy PipelineStack:

Webhook could not be registered with GitHub. Error cause: Invalid credentials [StatusCode: 401, 
Body: {"message":"Bad credentials","documentation_url":"https://docs.github.com/rest"}] 
(Service: AWSCodePipeline; Status Code: 400; Error Code: ValidationException; 
Request ID: dbab7e3e-ed28-42b8-a2d5-7539be32776b; Proxy: null)

Why am I seeing above error?

I even tried verify the token stored in AWS secret manager directly

curl -H "Authorization: token token-stored-in-secretManager" https://api.github.com/users/my-user

and it return 200 response.

My issue is similar to How to connect github repo with aws using cdk?

BTW, my github repository is set as private not public. Not sure if that matters.

Context

Here is my CDK code for the source stage.

  pipeline.addStage({
      stageName: "Source",
      actions: [
        new codepipeline_actions.GitHubSourceAction({
          actionName: "Checkout",
          owner: "the-owner",
          repo: "the-repo",
          branch: "main",
          oauthToken: CDK.SecretValue.secretsManager(
            "website-GitHubToken"
          ),
          output: outputSources,
          trigger: codepipeline_actions.GitHubTrigger.WEBHOOK,
        }),
      ],

And this is my personal access token permissions:

enter image description here

Upvotes: 6

Views: 3049

Answers (2)

Anthony Bauer
Anthony Bauer

Reputation: 419

If you secret in AWS Secrets Manager is stored as a key/value pair, you'll have to use SecretsManagerSecretOptions with CDK.SecretValue.secretsManager. Below is an example, and here is the SecretsManagerSecretOptions documentation.

pipeline.addStage({
      stageName: "Source",
      actions: [
        new codepipeline_actions.GitHubSourceAction({
          actionName: "Checkout",
          owner: "the-owner",
          repo: "the-repo",
          branch: "main",
          oauthToken: CDK.SecretValue.secretsManager(
            "Secret name", { jsonField: "key" }
          ),
          output: outputSources,
          trigger: codepipeline_actions.GitHubTrigger.WEBHOOK,
        }),
      ],
      ...
})

Upvotes: 1

Yang Liu
Yang Liu

Reputation: 751

It's my fault.

I did not store the token properly: enter image description here

It should be stored like this:

enter image description here

Upvotes: 8

Related Questions