-
Notifications
You must be signed in to change notification settings - Fork 1.2k
feat: BitBucket Cloud: add support for webhook secrets #4275
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
Merged
X-Guardian
merged 12 commits into
runatlantis:main
from
almightyfoon:add-support-for-bitbucket-cloud-webhooks
Jan 25, 2025
Merged
Changes from 2 commits
Commits
Show all changes
12 commits
Select commit
Hold shift + click to select a range
663aaa5
adding support for bitbucket cloud webhook secrets
399e4af
updating docs
c03f6a8
Merge branch 'main' into add-support-for-bitbucket-cloud-webhooks
jamengual e838aa8
Merge branch 'main' into add-support-for-bitbucket-cloud-webhooks
almightyfoon adb5875
Merge branch 'main' into add-support-for-bitbucket-cloud-webhooks
X-Guardian 36bf58a
Merge branch 'main' into add-support-for-bitbucket-cloud-webhooks
jamengual ba9e0ea
fixing a linting issue and adding a suggestion from the PR
03975aa
Merge branch 'main' into add-support-for-bitbucket-cloud-webhooks
jamengual 392af3e
Merge branch 'main' into add-support-for-bitbucket-cloud-webhooks
X-Guardian b59cb60
Update webhook-secrets.md
X-Guardian 0f02566
Merge branch 'main' into add-support-for-bitbucket-cloud-webhooks
X-Guardian 66c7569
Fix linting error
X-Guardian File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,72 @@ | ||
| package bitbucketcloud | ||
|
|
||
| import ( | ||
| "crypto/hmac" | ||
| "crypto/sha1" // nolint: gosec | ||
| "crypto/sha256" | ||
| "crypto/sha512" | ||
| "encoding/hex" | ||
| "fmt" | ||
| "hash" | ||
| "strings" | ||
|
|
||
| "github.com/pkg/errors" | ||
| ) | ||
|
|
||
| // Attribution: This code is taken from https://github.com/google/go-github. | ||
|
|
||
| func ValidateSignature(payload []byte, signature string, secretKey []byte) error { | ||
| messageMAC, hashFunc, err := messageMAC(signature) | ||
| if err != nil { | ||
| return err | ||
| } | ||
| if !checkMAC(payload, messageMAC, secretKey, hashFunc) { | ||
| return errors.New("payload signature check failed") | ||
| } | ||
| return nil | ||
| } | ||
|
|
||
| // genMAC generates the HMAC signature for a message provided the secret key | ||
| // and hashFunc. | ||
| func genMAC(message, key []byte, hashFunc func() hash.Hash) []byte { | ||
| mac := hmac.New(hashFunc, key) | ||
| // nolint: errcheck | ||
| mac.Write(message) | ||
| return mac.Sum(nil) | ||
| } | ||
|
|
||
| // checkMAC reports whether messageMAC is a valid HMAC tag for message. | ||
| func checkMAC(message, messageMAC, key []byte, hashFunc func() hash.Hash) bool { | ||
| expectedMAC := genMAC(message, key, hashFunc) | ||
| return hmac.Equal(messageMAC, expectedMAC) | ||
| } | ||
|
|
||
| // messageMAC returns the hex-decoded HMAC tag from the signature and its | ||
| // corresponding hash function. | ||
| func messageMAC(signature string) ([]byte, func() hash.Hash, error) { | ||
| if signature == "" { | ||
| return nil, nil, errors.New("missing signature") | ||
| } | ||
| sigParts := strings.SplitN(signature, "=", 2) | ||
| if len(sigParts) != 2 { | ||
| return nil, nil, fmt.Errorf("error parsing signature %q", signature) | ||
| } | ||
|
|
||
| var hashFunc func() hash.Hash | ||
| switch sigParts[0] { | ||
| case "sha1": | ||
| hashFunc = sha1.New | ||
| case "sha256": | ||
| hashFunc = sha256.New | ||
| case "sha512": | ||
| hashFunc = sha512.New | ||
| default: | ||
| return nil, nil, fmt.Errorf("unknown hash type prefix: %q", sigParts[0]) | ||
| } | ||
|
|
||
| buf, err := hex.DecodeString(sigParts[1]) | ||
| if err != nil { | ||
| return nil, nil, fmt.Errorf("error decoding signature %q: %v", signature, err) | ||
| } | ||
| return buf, hashFunc, nil | ||
| } |
24 changes: 24 additions & 0 deletions
24
server/events/vcs/bitbucketcloud/request_validation_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,24 @@ | ||
| package bitbucketcloud_test | ||
|
|
||
| import ( | ||
| "testing" | ||
|
|
||
| "github.com/runatlantis/atlantis/server/events/vcs/bitbucketcloud" | ||
| . "github.com/runatlantis/atlantis/testing" | ||
| ) | ||
|
|
||
| func TestValidateSignature(t *testing.T) { | ||
| body := `{"eventKey":"pr:comment:added","date":"2018-07-24T15:10:05+0200","actor":{"name":"lkysow","emailAddress":"lkysow@gmail.com","id":1,"displayName":"Luke Kysow","active":true,"slug":"lkysow","type":"NORMAL"},"pullRequest":{"id":5,"version":0,"title":"another.tf edited online with Bitbucket","state":"OPEN","open":true,"closed":false,"createdDate":1532365427513,"updatedDate":1532365427513,"fromRef":{"id":"refs/heads/lkysow/anothertf-1532365422773","displayId":"lkysow/anothertf-1532365422773","latestCommit":"b52b8e254e956654dcdb394d0ccba9199f420427","repository":{"slug":"atlantis-example","id":1,"name":"atlantis-example","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"AT","id":1,"name":"atlantis","public":false,"type":"NORMAL"},"public":false}},"toRef":{"id":"refs/heads/master","displayId":"master","latestCommit":"0a338874369017deba7c22e99e6000932724282f","repository":{"slug":"atlantis-example","id":1,"name":"atlantis-example","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"AT","id":1,"name":"atlantis","public":false,"type":"NORMAL"},"public":false}},"locked":false,"author":{"user":{"name":"lkysow","emailAddress":"lkysow@gmail.com","id":1,"displayName":"Luke Kysow","active":true,"slug":"lkysow","type":"NORMAL"},"role":"AUTHOR","approved":false,"status":"UNAPPROVED"},"reviewers":[],"participants":[]},"comment":{"properties":{"repositoryId":1},"id":65,"version":0,"text":"comment","author":{"name":"lkysow","emailAddress":"lkysow@gmail.com","id":1,"displayName":"Luke Kysow","active":true,"slug":"lkysow","type":"NORMAL"},"createdDate":1532437805137,"updatedDate":1532437805137,"comments":[],"tasks":[]}}` | ||
| secret := "mysecret" | ||
| sig := `sha256=ed11f92d1565a4de586727fe8260558277d58009e8957a79eb4749a7009ce083` | ||
| err := bitbucketcloud.ValidateSignature([]byte(body), sig, []byte(secret)) | ||
| Ok(t, err) | ||
| } | ||
|
|
||
| func TestValidateSignature_Invalid(t *testing.T) { | ||
| body := `"eventKey":"pr:comment:added","date":"2018-07-24T15:10:05+0200","actor":{"name":"lkysow","emailAddress":"lkysow@gmail.com","id":1,"displayName":"Luke Kysow","active":true,"slug":"lkysow","type":"NORMAL"},"pullRequest":{"id":5,"version":0,"title":"another.tf edited online with Bitbucket","state":"OPEN","open":true,"closed":false,"createdDate":1532365427513,"updatedDate":1532365427513,"fromRef":{"id":"refs/heads/lkysow/anothertf-1532365422773","displayId":"lkysow/anothertf-1532365422773","latestCommit":"b52b8e254e956654dcdb394d0ccba9199f420427","repository":{"slug":"atlantis-example","id":1,"name":"atlantis-example","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"AT","id":1,"name":"atlantis","public":false,"type":"NORMAL"},"public":false}},"toRef":{"id":"refs/heads/main","displayId":"main","latestCommit":"0a338874369017deba7c22e99e6000932724282f","repository":{"slug":"atlantis-example","id":1,"name":"atlantis-example","scmId":"git","state":"AVAILABLE","statusMessage":"Available","forkable":true,"project":{"key":"AT","id":1,"name":"atlantis","public":false,"type":"NORMAL"},"public":false}},"locked":false,"author":{"user":{"name":"lkysow","emailAddress":"lkysow@gmail.com","id":1,"displayName":"Luke Kysow","active":true,"slug":"lkysow","type":"NORMAL"},"role":"AUTHOR","approved":false,"status":"UNAPPROVED"},"reviewers":[],"participants":[]},"comment":{"properties":{"repositoryId":1},"id":65,"version":0,"text":"comment","author":{"name":"lkysow","emailAddress":"lkysow@gmail.com","id":1,"displayName":"Luke Kysow","active":true,"slug":"lkysow","type":"NORMAL"},"createdDate":1532437805137,"updatedDate":1532437805137,"comments":[],"tasks":[]}}` | ||
| secret := "mysecret" | ||
| sig := `sha256=ed11f92d1565a4de586727fe8260558277d58009e8957a79eb4749a7009ce083` | ||
| err := bitbucketcloud.ValidateSignature([]byte(body), sig, []byte(secret)) | ||
| ErrEquals(t, "payload signature check failed", err) | ||
| } |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.