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

feat: WebhookConfig #4

Open
wants to merge 52 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
52 commits
Select commit Hold shift + click to select a range
bc4c4db
feat: introduce `WebhookConfig`
denopink Jan 11, 2024
41b7f59
chore: update to WebhookConfig
denopink Jan 11, 2024
c40ff21
Update webhook.go
denopink Jan 11, 2024
ec3e73a
Update webhook.go
denopink Jan 12, 2024
604fa5a
Update webhook.go
denopink Jan 16, 2024
375a0a7
Update webhook.go
denopink Jan 16, 2024
4a4875f
Update webhook.go
denopink Jan 17, 2024
879a57e
Update webhook.go
denopink Jan 26, 2024
aea7b5b
chore(deps): bump xmidt-org/shared-go from 4.1.0 to 4.2.0 (#5)
dependabot[bot] Jan 18, 2024
4cf5ed4
chore(deps): bump xmidt-org/shared-go from 4.2.0 to 4.2.2 (#6)
dependabot[bot] Jan 19, 2024
99b9e0a
feat(deps): bump github.com/xmidt-org/urlegit from 0.1.0 to 0.1.1 (#7)
dependabot[bot] Jan 19, 2024
c213980
added structure for retry hints
maurafortino Feb 6, 2024
3285ed0
removed the retryhint struct from registration v2 and updated a comment
maurafortino Feb 6, 2024
43b25b1
Merge pull request #9 from xmidt-org/maurafortino/feat/retryhint
denopink Feb 8, 2024
d0621ca
Update webhook.go
denopink Feb 20, 2024
e6d8933
added register interface and updated validate function
maurafortino May 2, 2024
209f020
moved validation from ancla to webhook and added multi-err functionality
maurafortino May 16, 2024
1085b01
commenting out nowFunc for now
maurafortino May 16, 2024
47ea530
removed GetPartnerIds from register and added alwaysvalid as an optio…
maurafortino May 16, 2024
58b841f
added validation file and moved build validators from ancla to this p…
maurafortino May 28, 2024
1bca26e
added validation for until
maurafortino May 29, 2024
d30c20f
added back in nowFunc to registrationv1 and added validation for it
maurafortino May 29, 2024
8cd8302
updatedthe until option validation
maurafortino May 29, 2024
424691f
Merge pull request #16 from xmidt-org/edits-v2
maurafortino Jun 27, 2024
9b8a7ac
validation remaining the same for RegistrationV1; adding in validatio…
maurafortino Jun 28, 2024
b11e74f
added in more validation for RegistrationV2 and updated run_tests
maurafortino Jun 28, 2024
56c9dd9
added test cases for registrationV2 and default cases
maurafortino Jul 1, 2024
9564e84
Revert "Update webhook.go"
maurafortino Jul 1, 2024
dda637f
Revert "Revert "Update webhook.go""
maurafortino Jul 1, 2024
2e1fc04
Revert "updatedthe until option validation"
maurafortino Jul 1, 2024
491ef4b
Revert "added back in nowFunc to registrationv1 and added validation …
maurafortino Jul 1, 2024
101193c
fixed merge issues
maurafortino Jul 1, 2024
5a7e41c
added tests for AlwaysValid option
maurafortino Jul 1, 2024
90baaab
removing the register interface from webhook-schema and moving it to …
maurafortino Jul 2, 2024
e317ca8
added back in the url validation logic that was originally in ancla
maurafortino Jul 3, 2024
316cfea
this is an option for how we can set up the Validation Options
maurafortino Jul 3, 2024
d343c39
added back in the struct methods to clean up code; removed the generi…
maurafortino Jul 8, 2024
84187f4
added back in BuildValidators as I am going to be editing in a future PR
maurafortino Jul 8, 2024
c1be827
moved specialuseips back to original spot
maurafortino Jul 8, 2024
2eb87c9
fixed spacing
maurafortino Jul 8, 2024
2d66130
fixed merge conflicts
maurafortino Jul 8, 2024
6ecbde7
fixed merge conflicts i missed
maurafortino Jul 8, 2024
638f3d4
fixed spacing
maurafortino Jul 9, 2024
358b295
Merge branch 'validation-v3' into draft-options-config
maurafortino Jul 9, 2024
9947a7d
updated tests
maurafortino Jul 9, 2024
9e6e62d
returning error for v2 deviceidregexmustcompile and updated unit test
maurafortino Jul 9, 2024
556f139
removed validation config as that
maurafortino Jul 10, 2024
8d55e4c
fixed formatting issues
maurafortino Jul 11, 2024
0ee06ff
Merge pull request #36 from xmidt-org/validation-v3
maurafortino Jul 11, 2024
ffe3451
added the CheckUntil function from ancla
maurafortino Jul 17, 2024
b8c1ba1
Merge pull request #47 from xmidt-org/draft-options-config
maurafortino Jul 18, 2024
5b48a48
chore: update based on pr feedback
denopink Oct 29, 2024
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: 1 addition & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ on:

jobs:
ci:
uses: xmidt-org/shared-go/.github/workflows/ci.yml@5bc4b83f25ff4c944cd6253ba189e50d1997ab3c # v4.1.0
uses: xmidt-org/shared-go/.github/workflows/ci.yml@9e191cedb6f62d364dc8706c07107c8ef612c346 # v4.2.2
with:
release-type: library
secrets: inherit
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -29,3 +29,4 @@ report.json
# VSCode
*.code-workspace
.vscode/*
.dev/*
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ go 1.20

require (
github.com/stretchr/testify v1.8.4
github.com/xmidt-org/urlegit v0.1.0
github.com/xmidt-org/urlegit v0.1.1
)

require (
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,8 @@ github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZb
github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4=
github.com/stretchr/testify v1.8.4 h1:CcVxjf3Q8PM0mHUKJCdn+eZZtm5yQwehR5yeSVQQcUk=
github.com/stretchr/testify v1.8.4/go.mod h1:sz/lmYIOXD/1dqDmKjjqLyZ2RngseejIcXlSw2iwfAo=
github.com/xmidt-org/urlegit v0.1.0 h1:WZLlWo0e5JNZabLEi7/1+sK/np9qrH9XnoB+ZdsHieM=
github.com/xmidt-org/urlegit v0.1.0/go.mod h1:ih/VtgW3xfpV7FNIrHUpNdP0GapcfLOND8y0JwH51vA=
github.com/xmidt-org/urlegit v0.1.1 h1:sjFlckD7Okql7gQACX5hpicqiqD/kIs1hhG7a623dJQ=
github.com/xmidt-org/urlegit v0.1.1/go.mod h1:ih/VtgW3xfpV7FNIrHUpNdP0GapcfLOND8y0JwH51vA=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM=
gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0=
gopkg.in/yaml.v3 v3.0.1 h1:fxVm/GzAzEWqLHuvctI91KS9hhNmmWOoWu0XTYJS7CA=
Expand Down
195 changes: 120 additions & 75 deletions options.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,6 @@ package webhook

import (
"fmt"
"regexp"
"time"

"github.com/xmidt-org/urlegit"
Expand All @@ -20,7 +19,7 @@ type errorOption struct {
err error
}

func (e errorOption) Validate(*Registration) error {
func (e errorOption) Validate(any) error {
return error(e.err)
}

Expand All @@ -31,6 +30,20 @@ func (e errorOption) String() string {
return "Error('" + e.err.Error() + "')"
}

func AlwaysValid() Option {
return AlwaysValidOption{}
}

type AlwaysValidOption struct{}

func (a AlwaysValidOption) Validate(any) error {
return nil
}

func (a AlwaysValidOption) String() string {
return "alwaysValidOption"
}

// AtLeastOneEvent makes sure there is at least one value in Events and ensures
// that all values should parse into regex.
func AtLeastOneEvent() Option {
Expand All @@ -39,12 +52,15 @@ func AtLeastOneEvent() Option {

type atLeastOneEventOption struct{}

func (atLeastOneEventOption) Validate(r *Registration) error {
if len(r.Events) == 0 {
return fmt.Errorf("%w: cannot have zero events", ErrInvalidInput)
func (atLeastOneEventOption) Validate(i any) error {
switch r := i.(type) {
case *RegistrationV1:
return r.ValidateOneEvent()
case *RegistrationV2:
return fmt.Errorf("%w: RegistrationV2 does not have an events field to validate", ErrInvalidType)
default:
return ErrUknownType
}

return nil
}

func (atLeastOneEventOption) String() string {
Expand All @@ -58,14 +74,15 @@ func EventRegexMustCompile() Option {

type eventRegexMustCompileOption struct{}

func (eventRegexMustCompileOption) Validate(r *Registration) error {
for _, e := range r.Events {
_, err := regexp.Compile(e)
if err != nil {
return fmt.Errorf("%w: unable to compile matching", ErrInvalidInput)
}
func (eventRegexMustCompileOption) Validate(i any) error {
switch r := i.(type) {
case *RegistrationV1:
return r.ValidateEventRegex()
case *RegistrationV2:
return r.ValidateEventRegex()
default:
return ErrUknownType
}
return nil
}

func (eventRegexMustCompileOption) String() string {
Expand All @@ -80,14 +97,15 @@ func DeviceIDRegexMustCompile() Option {

type deviceIDRegexMustCompileOption struct{}

func (deviceIDRegexMustCompileOption) Validate(r *Registration) error {
for _, e := range r.Matcher.DeviceID {
_, err := regexp.Compile(e)
if err != nil {
return fmt.Errorf("%w: unable to compile matching", ErrInvalidInput)
}
func (deviceIDRegexMustCompileOption) Validate(i any) error {
switch r := i.(type) {
case *RegistrationV1:
return r.ValidateDeviceId()
case *RegistrationV2:
return fmt.Errorf("%w: RegistrationV2 does not use DeviceID directly, use `FieldRegex` instead", ErrInvalidType)
default:
return ErrUknownType
}
return nil
}

func (deviceIDRegexMustCompileOption) String() string {
Expand All @@ -107,40 +125,15 @@ type validateRegistrationDurationOption struct {
ttl time.Duration
}

func (v validateRegistrationDurationOption) Validate(r *Registration) error {
if v.ttl <= 0 {
v.ttl = time.Duration(0)
}

if v.ttl != 0 && v.ttl < time.Duration(r.Duration) {
return fmt.Errorf("%w: the registration is for too long", ErrInvalidInput)
}

if r.Until.IsZero() && r.Duration == 0 {
return fmt.Errorf("%w: either Duration or Until must be set", ErrInvalidInput)
}

if !r.Until.IsZero() && r.Duration != 0 {
return fmt.Errorf("%w: only one of Duration or Until may be set", ErrInvalidInput)
}

if !r.Until.IsZero() {
nowFunc := time.Now
if r.nowFunc != nil {
nowFunc = r.nowFunc
}

now := nowFunc()
if v.ttl != 0 && r.Until.After(now.Add(v.ttl)) {
return fmt.Errorf("%w: the registration is for too long", ErrInvalidInput)
}

if r.Until.Before(now) {
return fmt.Errorf("%w: the registration has already expired", ErrInvalidInput)
}
func (v validateRegistrationDurationOption) Validate(i any) error {
switch r := i.(type) {
case *RegistrationV1:
return r.ValidateDuration(v.ttl)
case *RegistrationV2:
return r.ValidateDuration()
default:
return ErrUknownType
}

return nil
}

func (v validateRegistrationDurationOption) String() string {
Expand All @@ -157,8 +150,12 @@ type provideTimeNowFuncOption struct {
nowFunc func() time.Time
}

func (p provideTimeNowFuncOption) Validate(r *Registration) error {
r.nowFunc = p.nowFunc
func (p provideTimeNowFuncOption) Validate(i any) error {
switch r := i.(type) {
case *RegistrationV1:
r.SetNowFunc(p.nowFunc)
}

return nil
}

Expand All @@ -179,13 +176,24 @@ type provideFailureURLValidatorOption struct {
checker *urlegit.Checker
}

func (p provideFailureURLValidatorOption) Validate(r *Registration) error {
func (p provideFailureURLValidatorOption) Validate(i any) error {
var failureURL string
//TODO: do we want to move this check to be inside each case statement?
if p.checker == nil {
return nil
}

if r.FailureURL != "" {
if err := p.checker.Text(r.FailureURL); err != nil {
switch r := i.(type) {
case *RegistrationV1:
failureURL = r.FailureURL
case *RegistrationV2:
failureURL = r.FailureURL
default:
return ErrUknownType
}

if failureURL != "" {
if err := p.checker.Text(failureURL); err != nil {
return fmt.Errorf("%w: failure url is invalid", ErrInvalidInput)
}
}
Expand All @@ -209,17 +217,19 @@ type provideReceiverURLValidatorOption struct {
checker *urlegit.Checker
}

func (p provideReceiverURLValidatorOption) Validate(r *Registration) error {
func (p provideReceiverURLValidatorOption) Validate(i any) error {
if p.checker == nil {
return nil
}

if r.Config.ReceiverURL != "" {
if err := p.checker.Text(r.Config.ReceiverURL); err != nil {
return fmt.Errorf("%w: failure url is invalid", ErrInvalidInput)
}
switch r := i.(type) {
case *RegistrationV1:
return r.ValidateReceiverURL(p.checker)
case *RegistrationV2:
return r.ValidateReceiverURL(p.checker)
default:
return ErrUknownType
}
return nil
}

func (p provideReceiverURLValidatorOption) String() string {
Expand All @@ -239,17 +249,19 @@ type provideAlternativeURLValidatorOption struct {
checker *urlegit.Checker
}

func (p provideAlternativeURLValidatorOption) Validate(r *Registration) error {
func (p provideAlternativeURLValidatorOption) Validate(i any) error {
if p.checker == nil {
return nil
}

for _, url := range r.Config.AlternativeURLs {
if err := p.checker.Text(url); err != nil {
return fmt.Errorf("%w: failure url is invalid", ErrInvalidInput)
}
switch r := i.(type) {
case *RegistrationV1:
return r.ValidateAltURL(p.checker)
case *RegistrationV2:
return fmt.Errorf("%w: RegistrationV2 does not have an alternative urls field. Use ProvideReceiverURLValidator() to validate all non-failure urls", ErrInvalidType)
default:
return ErrUknownType
}
return nil
}

func (p provideAlternativeURLValidatorOption) String() string {
Expand All @@ -266,13 +278,46 @@ func NoUntil() Option {

type noUntilOption struct{}

func (noUntilOption) Validate(r *Registration) error {
if !r.Until.IsZero() {
return fmt.Errorf("%w: Until is not allowed", ErrInvalidInput)
func (noUntilOption) Validate(i any) error {
switch r := i.(type) {
case *RegistrationV1:
return r.ValidateNoUntil()
case *RegistrationV2:
return fmt.Errorf("%w: RegistrationV2 does not use an Until field", ErrInvalidType)
default:
return ErrUknownType
}
return nil
}

func (noUntilOption) String() string {
return "NoUntil()"
}

func Until(now func() time.Time, jitter, max time.Duration) Option {
return untilOption{
now: now,
jitter: jitter,
max: max,
}
}

type untilOption struct {
now func() time.Time
jitter time.Duration
max time.Duration
}

func (u untilOption) Validate(i any) error {
switch r := i.(type) {
case *RegistrationV1:
return r.CheckUntil(u.now, u.jitter, u.max)
case *RegistrationV2:
return fmt.Errorf("%w: RegistrationV2 does not use an Until field", ErrInvalidType)
default:
return ErrUknownType
}
}

func (u untilOption) String() string {
return fmt.Sprintf("untilOption(%v, %v, %v)", u.jitter.String(), u.max.String(), u.now().String())
}
Loading
Loading