Skip to content
This repository has been archived by the owner on Feb 24, 2024. It is now read-only.

Commit

Permalink
Merge pull request #33 from B-Souty/harbor-v2-support
Browse files Browse the repository at this point in the history
Harbor v2 support
  • Loading branch information
squaremo authored Sep 29, 2020
2 parents 86635da + e9ef4e9 commit 50473d9
Show file tree
Hide file tree
Showing 4 changed files with 58 additions and 1 deletion.
2 changes: 1 addition & 1 deletion harbor.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ func handleHarbor(s fluxapi.Server, key []byte, w http.ResponseWriter, r *http.R
return
}

if p.Type != "pushImage" {
if p.Type != "pushImage" && p.Type != "PUSH_ARTIFACT" {
http.Error(w, "Unexpected event type", http.StatusBadRequest)
log(Harbor, "unexpected event type:", p.Type)
return
Expand Down
37 changes: 37 additions & 0 deletions sources_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,43 @@ func Test_Harbor(t *testing.T) {
assert.Equal(t, 401, res.StatusCode)
}

const expectedHarborV2 = `{"Kind":"image","Source":{"Name":{"Domain":"hub.harbor.com","Image":"test-webhook/debian"}}}`

func Test_HarborV2(t *testing.T) {
var called bool
downstream := newDownstream(t, expectedHarborV2, &called)
defer downstream.Close()

endpoint := Endpoint{Source: Harbor, KeyPath: "harborV2_key"}
fp, handler, err := HandlerFromEndpoint("test/fixtures", downstream.URL, endpoint)
assert.NoError(t, err)

hookServer := httptest.NewTLSServer(handler)
defer hookServer.Close()

payload := loadFixture(t, "harborV2_payload")

c := hookServer.Client()
req, err := http.NewRequest("POST", hookServer.URL+"/hook/"+fp, bytes.NewReader(payload))
assert.NoError(t, err)
req.Header.Set("Authorization", string(loadFixture(t, "harborV2_key")))

res, err := c.Do(req)
assert.NoError(t, err)
assert.True(t, called)
assert.Equal(t, 200, res.StatusCode)

// Check that bogus token is rejected
called = false
req, err = http.NewRequest("POST", hookServer.URL+"/hook/"+fp, bytes.NewReader(payload))
assert.NoError(t, err)
req.Header.Set("Authorization", "BOGUS")
res, err = c.Do(req)
assert.NoError(t, err)
assert.False(t, called)
assert.Equal(t, 401, res.StatusCode)
}

const expectedNexus = `{"Kind":"image","Source":{"Name":{"Domain":"container.example.com","Image":"app1/alpine"}}}`

func Test_Nexus(t *testing.T) {
Expand Down
1 change: 1 addition & 0 deletions test/fixtures/harborV2_key
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
f4f1e8fcd09bc07a397df58b3a2af9ed9414e39b
19 changes: 19 additions & 0 deletions test/fixtures/harborV2_payload
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"type": "PUSH_ARTIFACT",
"occur_at": 1586922308,
"operator": "admin",
"event_data": {
"resources": [{
"digest": "sha256:8a9e9863dbb6e10edb5adfe917c00da84e1700fa76e7ed02476aa6e6fb8ee0d8",
"tag": "latest",
"resource_url": "hub.harbor.com/test-webhook/debian:latest"
}],
"repository": {
"date_created": 1586922308,
"name": "debian",
"namespace": "test-webhook",
"repo_full_name": "test-webhook/debian",
"repo_type": "private"
}
}
}

0 comments on commit 50473d9

Please sign in to comment.