diff --git a/harbor.go b/harbor.go index 8e70ba5..00e4933 100644 --- a/harbor.go +++ b/harbor.go @@ -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 diff --git a/sources_test.go b/sources_test.go index 4dd61de..78d818b 100644 --- a/sources_test.go +++ b/sources_test.go @@ -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) { diff --git a/test/fixtures/harborV2_key b/test/fixtures/harborV2_key new file mode 100644 index 0000000..1cc58d8 --- /dev/null +++ b/test/fixtures/harborV2_key @@ -0,0 +1 @@ +f4f1e8fcd09bc07a397df58b3a2af9ed9414e39b \ No newline at end of file diff --git a/test/fixtures/harborV2_payload b/test/fixtures/harborV2_payload new file mode 100644 index 0000000..36f1e3f --- /dev/null +++ b/test/fixtures/harborV2_payload @@ -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" + } + } +} \ No newline at end of file