Skip to content

Commit

Permalink
Merge pull request #121 from xmidt-org/switch-to-events
Browse files Browse the repository at this point in the history
Use events and listeners instead of the metrics/logging approach befo…
  • Loading branch information
schmidtw authored Sep 1, 2023
2 parents 4e01d87 + e3d40f4 commit 7560704
Show file tree
Hide file tree
Showing 14 changed files with 605 additions and 746 deletions.
20 changes: 20 additions & 0 deletions .codecov.yml
Original file line number Diff line number Diff line change
@@ -1,10 +1,30 @@
# SPDX-FileCopyrightText: 2019 Comcast Cable Communications Management, LLC
# SPDX-License-Identifier: Apache-2.0
---

coverage:
range: 50..80
round: down
precision: 2
status:
project:
default:
target: auto
threshold: 2%
paths:
- "github.com/xmidt-org/wrp-listener/..."
patch:
default:
target: auto
threshold: 2%
paths:
- "github.com/xmidt-org/wrp-listener/..."
changes:
default:
target: auto
threshold: 2%
paths:
- "github.com/xmidt-org/wrp-listener/..."

ignore:
- "*_test.go"
Expand Down
7 changes: 0 additions & 7 deletions cmd/bearerListener/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ import (

"github.com/xmidt-org/webhook-schema"
listener "github.com/xmidt-org/wrp-listener"
"go.uber.org/zap"
)

type eventListener struct {
Expand Down Expand Up @@ -85,15 +84,9 @@ func main() {
sharedSecrets[i] = strings.TrimSpace(sharedSecrets[i])
}

logger, err := zap.NewDevelopment()
if err != nil {
panic(err)
}

whl, err := listener.New(&r, webhookURL,
listener.AuthBearer(os.Getenv("WEBHOOK_BEARER_TOKEN")),
listener.AcceptSHA1(),
listener.Logger(logger),
listener.Once(),
listener.AcceptedSecrets(sharedSecrets...),
)
Expand Down
43 changes: 38 additions & 5 deletions errors.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,43 @@ import (
)

var (
ErrInput = errors.New("invalid input")
ErrInvalidAuth = errors.New("invalid auth")
ErrInvalidRegistration = errors.New("invalid registration")
ErrRegistrationFailed = errors.New("registration failed")
// ErrInput is returned when an invalid input is provided.
ErrInput = errors.New("invalid input")

// ErrInvalidTokenHeader is returned when the token header is invalid.
ErrInvalidTokenHeader = errors.New("invalid token header")

// ErrRegistrationFailed is returned when the webhook registration fails.
ErrRegistrationFailed = errors.New("registration failed")

// ErrRegistrationNotAttempted is returned when the webhook registration
// was not attempted.
ErrRegistrationNotAttempted = errors.New("registration not attempted")
ErrNotAcceptedHash = errors.New("not accepted hash")

// ErrNotAcceptedHash is returned when the hash is not accepted, because it
// is not in the list of accepted hashes.
ErrNotAcceptedHash = errors.New("not accepted hash")

// ErrAuthFetchFailed is returned when the auth fetch fails and returns an
// error.
ErrAuthFetchFailed = errors.New("auth fetch failed")

// ErrNewRequestFailed is returned when the request cannot be created.
ErrNewRequestFailed = errors.New("new request failed")

// ErrInvalidHeaderFormat is returned when the header is not in the correct
// format.
ErrInvalidHeaderFormat = errors.New("invalid header format")

// ErrInvalidAlgorithm is returned when the algorithm is not supported.
ErrAlgorithmNotFound = errors.New("algorithm not found")

// ErrNoToken is returned when the token is not found.
ErrNoToken = errors.New("no token")

// ErrInvalidSignature is returned when the signature is invalid.
ErrInvalidSignature = errors.New("invalid signature")

// ErrUnableToReadBody is returned when the body cannot be read.
ErrUnableToReadBody = errors.New("unable to read body")
)
84 changes: 84 additions & 0 deletions events.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,84 @@
// SPDX-FileCopyrightText: 2023 Comcast Cable Communications Management, LLC
// SPDX-License-Identifier: Apache-2.0

package listener

import "time"

// EventRegistration is an event that occurs during webhook registration.
//
// The time the registration is attempted may be quite different from the time
// the event is created. Therefore it is recorded in the event as At when it
// occurs.
//
// The duration of the registration may be of interest so it is captured in the
// event as Duration when it occurs.
//
// The body of the request or response may be of interest so it is captured in
// the event as Body when an error occurs and it is available.
//
// The status code of the response may be of interest so it is captured in the
// event as StatusCode when it occurs.
//
// Any error that occurs during the registration is captured in the event as Err
// when it occurs. Multiple error may be included for each event.
type EventRegistration struct {
// At holds the starting time of the event if applicable.
At time.Time

// Duration holds the duration of the event if applicable.
Duration time.Duration

// The body of the request or response if applicable.
Body []byte

// StatusCode holds the HTTP status code returned by the webhook registration.
StatusCode int

// Err holds any error that occurred while performing the registration.
Err error
}

// RegistrationListener is a sink for registration events.
type RegistrationEventListener interface {
OnRegistrationEvent(EventRegistration)
}

// TokenizeEvent is an event that occurs during Tokenize() call.
//
// When available the header, algorithms, and algorithm used are included.
// Any error that occurs during tokenization is included.
type TokenizeEvent struct {
// Header holds the header that was used to tokenize the request.
Header string

// Algorithms holds the algorithms that were offered to tokenize the request.
Algorithms []string

// Algorithm holds the algorithm that was used to tokenize the request.
Algorithm string

// Err holds any error that occurred while tokenizing the request.
Err error
}

// RegistrationListener is a sink for registration events.
type TokenizeEventListener interface {
OnTokenizeEvent(TokenizeEvent)
}

// AuthorizeEvent is an event that occurs during the Authorize() call.
// When available the algorithm used is included.
// Any error that occurs during authorization is included.
type AuthorizeEvent struct {
// Algorithm holds the algorithm that was used for authorization.
Algorithm string

// Err holds any error that occurred while tokenizing the request.
Err error
}

// RegistrationListener is a sink for registration events.
type AuthorizeEventListener interface {
OnAuthorizeEvent(AuthorizeEvent)
}
Loading

0 comments on commit 7560704

Please sign in to comment.