Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion internal/notifier/bitbucket.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,18 @@ func NewBitbucket(commitStatus string, addr string, token string, tlsConfig *tls
owner := comp[0]
repo := comp[1]

client := bitbucket.NewBasicAuth(username, password)
// Support two authentication modes depending on secret content:
// - If username == "x-token-auth" or "x-bitbucket-api-token-auth" the
// password is treated as an OAuth bearer token and we should use
// NewOAuthbearerToken.
// - Otherwise use basic auth with <bitbucket_user_email>:<personal_api_token>.
var client *bitbucket.Client
if username == "x-token-auth" || username == "x-bitbucket-api-token-auth" {
// password in this case is the bearer token
client = bitbucket.NewOAuthbearerToken(password)
} else {
client = bitbucket.NewBasicAuth(username, password)
}
if tlsConfig != nil {
tr := &http.Transport{
TLSClientConfig: tlsConfig,
Expand Down
4 changes: 4 additions & 0 deletions internal/notifier/bitbucket_fuzz_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,11 @@ import (

func Fuzz_Bitbucket(f *testing.F) {
f.Add("kustomization/gitops-system/0c9c2e41", "user:pass", "org/repo", "revision/dsa123a", "info", []byte{}, []byte(`{"state":"SUCCESSFUL","description":"","key":"","name":"","url":""}`))
f.Add("kustomization/gitops-system/0c9c2e41", "x-token-auth:pass", "org/repo", "revision/dsa123a", "info", []byte{}, []byte(`{"state":"SUCCESSFUL","description":"","key":"","name":"","url":""}`))
f.Add("kustomization/gitops-system/0c9c2e41", "x-bitbucket-api-token-auth:pass", "org/repo", "revision/dsa123a", "info", []byte{}, []byte(`{"state":"SUCCESSFUL","description":"","key":"","name":"","url":""}`))
f.Add("kustomization/gitops-system/0c9c2e41", "user:pass", "org/repo", "revision/dsa123a", "error", []byte{}, []byte(`{}`))
f.Add("kustomization/gitops-system/0c9c2e41", "x-token-auth:pass", "org/repo", "revision/dsa123a", "error", []byte{}, []byte(`{}`))
f.Add("kustomization/gitops-system/0c9c2e41", "x-bitbucket-api-token-auth", "org/repo", "revision/dsa123a", "error", []byte{}, []byte(`{}`))

f.Fuzz(func(t *testing.T, commitStatus, token, urlSuffix, revision, severity string, seed, response []byte) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
Expand Down
20 changes: 19 additions & 1 deletion internal/notifier/bitbucket_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ import (
. "github.com/onsi/gomega"
)

func TestNewBitbucketBasic(t *testing.T) {
func TestNewBitbucketBasicAuth(t *testing.T) {
g := NewWithT(t)
b, err := NewBitbucket("kustomization/gitops-system/0c9c2e41", "https://bitbucket.org/foo/bar", "foo:bar", nil)
g.Expect(err).ToNot(HaveOccurred())
Expand All @@ -31,6 +31,24 @@ func TestNewBitbucketBasic(t *testing.T) {
g.Expect(b.CommitStatus).To(Equal("kustomization/gitops-system/0c9c2e41"))
}

func TestNewBitbucketOAuthRepositoryToken(t *testing.T) {
g := NewWithT(t)
b, err := NewBitbucket("kustomization/gitops-system/0c9c2e41", "https://bitbucket.org/foo/bar", "x-token-auth:bar", nil)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(b.Owner).To(Equal("foo"))
g.Expect(b.Repo).To(Equal("bar"))
g.Expect(b.CommitStatus).To(Equal("kustomization/gitops-system/0c9c2e41"))
}

func TestNewBitbucketOAuthPersonalToken(t *testing.T) {
g := NewWithT(t)
b, err := NewBitbucket("kustomization/gitops-system/0c9c2e41", "https://bitbucket.org/foo/bar", "x-bitbucket-api-token-auth:bar", nil)
g.Expect(err).ToNot(HaveOccurred())
g.Expect(b.Owner).To(Equal("foo"))
g.Expect(b.Repo).To(Equal("bar"))
g.Expect(b.CommitStatus).To(Equal("kustomization/gitops-system/0c9c2e41"))
}

func TestNewBitbucketEmptyCommitStatus(t *testing.T) {
g := NewWithT(t)
_, err := NewBitbucket("", "https://bitbucket.org/foo/bar", "foo:bar", nil)
Expand Down