This repository has been archived by the owner on Feb 24, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 14
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #26 from fluxcd/nexus
Nexus Repository Manager support
- Loading branch information
Showing
9 changed files
with
137 additions
and
15 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,84 @@ | ||
package main | ||
|
||
import ( | ||
"crypto/hmac" | ||
"crypto/sha1" | ||
"encoding/hex" | ||
"encoding/json" | ||
"fmt" | ||
"io/ioutil" | ||
"net/http" | ||
"strings" | ||
|
||
fluxapi "github.com/fluxcd/flux/pkg/api" | ||
) | ||
|
||
const Nexus = "Nexus" | ||
|
||
func init() { | ||
Sources[Nexus] = handleNexus | ||
} | ||
|
||
func handleNexus(s fluxapi.Server, key []byte, w http.ResponseWriter, r *http.Request, e Endpoint) { | ||
if webhookID := r.Header.Get("X-Nexus-Webhook-Id"); webhookID != "rm:repository:component" { | ||
http.Error(w, "Unsupported webhook ID", http.StatusBadRequest) | ||
log(Nexus, "unsupported webhook ID:", webhookID) | ||
return | ||
} | ||
|
||
signature := r.Header.Get("X-Nexus-Webhook-Signature") | ||
if len(signature) == 0 { | ||
http.Error(w, "Signature is missing from header", http.StatusUnauthorized) | ||
log(Nexus, "missing X-Nexus-Webhook-Signature header") | ||
return | ||
} | ||
|
||
b, err := ioutil.ReadAll(r.Body) | ||
if err != nil { | ||
http.Error(w, "Cannot read payload", http.StatusBadRequest) | ||
log(Nexus, fmt.Errorf("could not read payload: %v", err)) | ||
return | ||
} | ||
|
||
if !verifyHmacSignature(key, signature, b) { | ||
http.Error(w, "Invalid signature", http.StatusUnauthorized) | ||
log(Nexus, "invalid X-Nexus-Webhook-Signature") | ||
return | ||
} | ||
|
||
type payload struct { | ||
Action string `json:"action"` | ||
Component struct { | ||
Format string `json:"format"` | ||
Name string `json:"name"` | ||
} `json:"component"` | ||
} | ||
|
||
var p payload | ||
if err := json.Unmarshal(b, &p); err != nil { | ||
http.Error(w, "Cannot decode webhook payload", http.StatusBadRequest) | ||
log(Nexus, err.Error()) | ||
return | ||
} | ||
|
||
if p.Component.Format != "docker" || p.Action != "CREATED" { | ||
http.Error(w, "Ignoring component format", http.StatusBadRequest) | ||
log(Nexus, "ignoring action:", p.Action, "for asset format:", p.Component.Format) | ||
return | ||
} | ||
|
||
// The request Nexus makes contains no information about the | ||
// hostname of the Docker registry. | ||
img := p.Component.Name | ||
if e.RegistryHost != "" { | ||
img = strings.TrimRight(e.RegistryHost, "/") + "/" + img | ||
} | ||
doImageNotify(s, w, r, img) | ||
} | ||
|
||
func verifyHmacSignature(key []byte, signature string, payload []byte) bool { | ||
mac := hmac.New(sha1.New, key) | ||
_, _ = mac.Write(payload) | ||
expectedMAC := hex.EncodeToString(mac.Sum(nil)) | ||
return hmac.Equal([]byte(signature), []byte(expectedMAC)) | ||
} |
This file contains 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 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 |
---|---|---|
|
@@ -243,6 +243,47 @@ func Test_Harbor(t *testing.T) { | |
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) { | ||
var called bool | ||
downstream := newDownstream(t, expectedNexus, &called) | ||
defer downstream.Close() | ||
|
||
endpoint := Endpoint{Source: Nexus, KeyPath: "nexus_key", RegistryHost: "container.example.com"} | ||
fp, handler, err := HandlerFromEndpoint("test/fixtures", downstream.URL, endpoint) | ||
assert.NoError(t, err) | ||
|
||
hookServer := httptest.NewTLSServer(handler) | ||
defer hookServer.Close() | ||
|
||
payload := loadFixture(t, "nexus_payload") | ||
|
||
c := hookServer.Client() | ||
req, err := http.NewRequest("POST", hookServer.URL+"/hook/"+fp, bytes.NewReader(payload)) | ||
assert.NoError(t, err) | ||
req.Header.Set("X-Nexus-Webhook-Id", "rm:repository:component") | ||
req.Header.Set("X-Nexus-Webhook-Delivery", "bd9e6aef-0e27-4570-980d-f639c49ab5ed") | ||
req.Header.Set("X-Nexus-Webhook-Signature", "d06eea380d4631e8c1180b689d10d9ba83ab68f6") | ||
|
||
res, err := c.Do(req) | ||
assert.NoError(t, err) | ||
assert.True(t, called) | ||
assert.Equal(t, 200, res.StatusCode) | ||
|
||
// check that bogus signature is rejected | ||
called = false | ||
req, err = http.NewRequest("POST", hookServer.URL+"/hook/"+fp, bytes.NewReader(payload)) | ||
assert.NoError(t, err) | ||
req.Header.Set("X-Nexus-Webhook-Id", "rm:repository:component") | ||
req.Header.Set("X-Nexus-Webhook-Delivery", "bd9e6aef-0e27-4570-980d-f639c49ab5ed") | ||
req.Header.Set("X-Nexus-Webhook-Signature", "BOGUS") | ||
res, err = c.Do(req) | ||
assert.NoError(t, err) | ||
assert.False(t, called) | ||
assert.Equal(t, 401, res.StatusCode) | ||
} | ||
|
||
const expectedBitbucketCloud = `{"Kind":"git","Source":{"URL":"[email protected]:mbridgen/dummy.git","Branch":"master"}}` | ||
|
||
func Test_BitbucketCloud(t *testing.T) { | ||
|
This file contains 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 @@ | ||
gk0qp4u98xy0vntojv81i8zcjr4zw7xd6c4ugzad |
This file contains 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 @@ | ||
{"timestamp":"2020-04-05T21:49:44.219+0000","nodeId":"8C4F3E91-CD46AFE0-61B8403D-94526884-19AC4E06","initiator":"admin/127.0.0.1","repositoryName":"app1","action":"CREATED","component":{"id":"c770169c0b2e3ed8357368b43d4da3d8","componentId":"YXBwMTpjNzcwMTY5YzBiMmUzZWQ4MzU3MzY4YjQzZDRkYTNkOA","format":"docker","name":"app1/alpine","version":"3.4"}} |