Skip to content
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

Add Nexus webhook receiver #126

Merged
merged 4 commits into from
Jan 20, 2021
Merged
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
2 changes: 2 additions & 0 deletions api/v1beta1/receiver_types.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ type ReceiverSpec struct {
// Type of webhook sender, used to determine
// the validation procedure and payload deserialization.
// +kubebuilder:validation:Enum=generic;github;gitlab;bitbucket;harbor;dockerhub;quay;gcr
// +kubebuilder:validation:Enum=generic;github;gitlab;bitbucket;harbor;dockerhub;quay;gcr;nexus
// +required
Type string `json:"type"`

Expand Down Expand Up @@ -71,6 +72,7 @@ const (
DockerHubReceiver string = "dockerhub"
QuayReceiver string = "quay"
GCRReceiver string = "gcr"
NexusReceiver string = "nexus"
)

func ReceiverReady(receiver Receiver, reason, message, url string) Receiver {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,7 @@ spec:
- dockerhub
- quay
- gcr
- nexus
type: string
required:
- resources
Expand Down
22 changes: 22 additions & 0 deletions docs/spec/v1beta1/receiver.md
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ const (
DockerHubReceiver string = "dockerhub"
QuayReceiver string = "quay"
GCRReceiver string = "gcr"
NexusReceiver string = "nexus"
)
```

Expand Down Expand Up @@ -229,6 +230,27 @@ spec:
name: webapp
```

### Nexus receiver

```yaml
apiVersion: notification.toolkit.fluxcd.io/v1beta1
kind: Receiver
metadata:
name: nexus-receiver
namespace: default
spec:
type: nexus
secretRef:
name: webhook-token
resources:
- kind: ImageRepository
name: webapp
```
somtochiama marked this conversation as resolved.
Show resolved Hide resolved

Note that you have to fill in the generated token as the secret key when creating the Nexus Webhook Capability.
See [Nexus Webhook Capability](https://help.sonatype.com/repomanager3/webhooks/enabling-a-repository-webhook-capability)
The controller uses the `X-Nexus-Webhook-Signature` HTTP header to verify that the request is legitimate.

### GCR receiver

```yaml
Expand Down
39 changes: 39 additions & 0 deletions internal/server/receiver_handlers.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,9 +18,13 @@ package server

import (
"context"
"crypto/hmac"
"crypto/sha1"
"encoding/base64"
"encoding/hex"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"net/url"
"strings"
Expand Down Expand Up @@ -265,6 +269,34 @@ func (s *ReceiverServer) validate(ctx context.Context, receiver v1beta1.Receiver
fmt.Sprintf("handling event from %s for tag %s", d.Digest, d.Tag),
"receiver", receiver.Name)
return nil
case v1beta1.NexusReceiver:
signature := r.Header.Get("X-Nexus-Webhook-Signature")
if len(signature) == 0 {
return fmt.Errorf("Signature is missing from header")
}

b, err := ioutil.ReadAll(r.Body)
if err != nil {
return fmt.Errorf("cannot read payload. error: %s", err)
}

if !verifyHmacSignature([]byte(token), signature, b) {
return fmt.Errorf("invalid nexus signature")
}
type payload struct {
Action string `json:"action"`
RepositoryName string `json:"repositoryName"`
}
var p payload

if err := json.Unmarshal(b, &p); err != nil {
return fmt.Errorf("cannot decode Nexus webhook payload: %s", err)
}

s.logger.Info(
fmt.Sprintf("handling event from %s", p.RepositoryName),
"receiver", receiver.Name)
return nil
}

return fmt.Errorf("recevier type '%s' not supported", receiver.Spec.Type)
Expand Down Expand Up @@ -382,3 +414,10 @@ func authenticateGCRRequest(c *http.Client, bearer string, tokenIndex int) (err

return nil
}

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))
}