Skip to content

Commit

Permalink
Merge 6f7a522 into 2e60b22
Browse files Browse the repository at this point in the history
  • Loading branch information
mtrinh11 authored Sep 21, 2021
2 parents 2e60b22 + 6f7a522 commit 1f11c14
Show file tree
Hide file tree
Showing 12 changed files with 579 additions and 217 deletions.
3 changes: 2 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/)
and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.html).

## [Unreleased]
- Changed webhooks to internalWebhooks to enable the storing of partnerIDs. [#79](https:/xmidt-org/ancla/pull/79)

## [v0.2.4]
- Update webhookValidator builder to fix http issue. [#77](https:/xmidt-org/ancla/pull/77)
Expand Down Expand Up @@ -74,4 +75,4 @@ and this project adheres to [Semantic Versioning](http://semver.org/spec/v2.0.0.
[v0.1.3]: https:/xmidt-org/ancla/compare/0.1.2...v0.1.3
[v0.1.2]: https:/xmidt-org/ancla/compare/0.1.1...v0.1.2
[v0.1.1]: https:/xmidt-org/ancla/compare/0.1.0...v0.1.1
[v0.1.0]: https:/xmidt-org/ancla/compare/0.0.0...v0.1.0
[v0.1.0]: https:/xmidt-org/ancla/compare/0.0.0...v0.1.0
4 changes: 2 additions & 2 deletions endpoint.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ import (
func newAddWebhookEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, request interface{}) (interface{}, error) {
r := request.(*addWebhookRequest)
return nil, s.Add(ctx, r.owner, r.webhook)
return nil, s.Add(ctx, r.owner, r.internalWebook)
}
}

func newGetAllWebhooksEndpoint(s Service) endpoint.Endpoint {
return func(ctx context.Context, _ interface{}) (interface{}, error) {
return s.AllWebhooks(ctx)
return s.GetAll(ctx)
}
}
27 changes: 22 additions & 5 deletions endpoint_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package ancla

import (
Expand All @@ -13,12 +30,12 @@ func TestNewAddWebhookEndpoint(t *testing.T) {
m := new(mockService)
endpoint := newAddWebhookEndpoint(m)
input := &addWebhookRequest{
owner: "owner-val",
webhook: Webhook{},
owner: "owner-val",
internalWebook: InternalWebhook{},
}

errFake := errors.New("failed")
m.On("Add", context.TODO(), "owner-val", input.webhook).Return(errFake)
m.On("Add", context.TODO(), "owner-val", input.internalWebook).Return(errFake)
resp, err := endpoint(context.Background(), input)
assert.Nil(resp)
assert.Equal(errFake, err)
Expand All @@ -30,8 +47,8 @@ func TestGetAllWebhooksEndpoint(t *testing.T) {
m := new(mockService)
endpoint := newGetAllWebhooksEndpoint(m)

respFake := []Webhook{}
m.On("AllWebhooks", context.TODO()).Return(respFake, nil)
respFake := []InternalWebhook{}
m.On("GetAll", context.TODO()).Return(respFake, nil)
resp, err := endpoint(context.Background(), nil)
assert.Nil(err)
assert.Equal(respFake, resp)
Expand Down
4 changes: 4 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -274,6 +274,7 @@ github.com/googleapis/gax-go/v2 v2.0.4/go.mod h1:0Wqv26UfaUD9n4G6kQubkQ+KchISgw+
github.com/googleapis/gax-go/v2 v2.0.5/go.mod h1:DWXyrwAJ9X0FpwwEdw+IPEYBICEFu5mhpdKc/us6bOk=
github.com/googleapis/gnostic v0.2.0/go.mod h1:sJBsCZ4ayReDTBIg8b9dl28c5xFWyhBTVRp3pOg5EKY=
github.com/goph/emperror v0.17.1/go.mod h1:+ZbQ+fUNO/6FNiUo0ujtMjhgad9Xa6fQL9KhH4LNHic=
github.com/goph/emperror v0.17.3-0.20190703203600-60a8d9faa17b h1:3/cwc6wu5QADzKEW2HP7+kZpKgm7OHysQ3ULVVQzQhs=
github.com/goph/emperror v0.17.3-0.20190703203600-60a8d9faa17b/go.mod h1:+ZbQ+fUNO/6FNiUo0ujtMjhgad9Xa6fQL9KhH4LNHic=
github.com/gophercloud/gophercloud v0.0.0-20180828235145-f29afc2cceca/go.mod h1:3WdhXV3rUYy9p6AUW8d94kr+HS62Y4VL9mBnFxsD8q4=
github.com/gopherjs/gopherjs v0.0.0-20180825215210-0210a2f0f73c/go.mod h1:wJfORRmW1u3UXTncJ5qlYoELFm8eSnnEO6hX4iZ3EWY=
Expand Down Expand Up @@ -451,6 +452,7 @@ github.com/mattn/go-runewidth v0.0.2/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzp
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
github.com/matttproud/golang_protobuf_extensions v1.0.1/go.mod h1:D8He9yQNgCq6Z5Ld7szi9bcBfOoFv/3dc6xSMkL2PC0=
github.com/miekg/dns v1.0.14/go.mod h1:W1PPwlIAgtquWBMBEV9nkV9Cazfe8ScdGz/Lj7v3Nrg=
github.com/miekg/dns v1.1.26 h1:gPxPSwALAeHJSjarOs00QjVdV9QoBvc1D2ujQUr5BzU=
github.com/miekg/dns v1.1.26/go.mod h1:bPDLeHnStXmXAq1m/Ch/hvfNHr14JKNPMBo3VZKjuso=
github.com/mitchellh/cli v1.0.0/go.mod h1:hNIlj7HEI86fIcpObd7a0FcrxTWetlwJDGcceTlRvqc=
github.com/mitchellh/cli v1.1.0/go.mod h1:xcISNoH86gajksDmfB23e/pu+B+GeFRMYmoHXxx3xhI=
Expand Down Expand Up @@ -769,6 +771,7 @@ golang.org/x/crypto v0.0.0-20190923035154-9ee001bba392/go.mod h1:/lpIB1dKB+9EgE3
golang.org/x/crypto v0.0.0-20191011191535-87dc89f01550/go.mod h1:yigFU9vqHzYiE8UmvKecakEJjdnWj3jj499lnFckfCI=
golang.org/x/crypto v0.0.0-20191106202628-ed6320f186d4/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200622213623-75b288015ac9/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a h1:vclmkQCjlDX5OydZ9wv8rBCcS0QyQY66Mpf/7BZbInM=
golang.org/x/crypto v0.0.0-20200820211705-5c72a883971a/go.mod h1:LzIPMQfyMNhhGPhUkYOs5KpL4U8rLKemX1yGLhDgUto=
golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
golang.org/x/exp v0.0.0-20190306152737-a1d7652674e8/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA=
Expand Down Expand Up @@ -869,6 +872,7 @@ golang.org/x/sync v0.0.0-20200317015054-43a5402ce75a/go.mod h1:RxMgew5VJxzue5/jJ
golang.org/x/sync v0.0.0-20200625203802-6e8e738ad208/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201020160332-67f06af15bc9/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20201207232520-09787c993a3a/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c h1:5KslGYwFpkhGh+Q16bwMP3cOontH8FOep7tGV86Y7SQ=
golang.org/x/sync v0.0.0-20210220032951-036812b2e83c/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180823144017-11551d06cbcc/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
Expand Down
17 changes: 17 additions & 0 deletions jwtAcquireParser.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package ancla

import (
Expand Down
17 changes: 17 additions & 0 deletions jwtAcquireParser_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package ancla

import (
Expand Down
25 changes: 21 additions & 4 deletions mocks_test.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
/**
* Copyright 2021 Comcast Cable Communications Management, LLC
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
*/

package ancla

import (
Expand Down Expand Up @@ -48,14 +65,14 @@ type mockService struct {
mock.Mock
}

func (m *mockService) Add(ctx context.Context, owner string, w Webhook) error {
args := m.Called(ctx, owner, w)
func (m *mockService) Add(ctx context.Context, owner string, iw InternalWebhook) error {
args := m.Called(ctx, owner, iw)
return args.Error(0)
}

func (m *mockService) AllWebhooks(ctx context.Context) ([]Webhook, error) {
func (m *mockService) GetAll(ctx context.Context) ([]InternalWebhook, error) {
args := m.Called(ctx)
return args.Get(0).([]Webhook), args.Error(1)
return args.Get(0).([]InternalWebhook), args.Error(1)
}

type mockCounter struct {
Expand Down
71 changes: 43 additions & 28 deletions service.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,10 +50,10 @@ var (
type Service interface {
// Add adds the given owned webhook to the current list of webhooks. If the operation
// succeeds, a non-nil error is returned.
Add(ctx context.Context, owner string, w Webhook) error
Add(ctx context.Context, owner string, iw InternalWebhook) error

// AllWebhooks lists all the current registered webhooks.
AllWebhooks(ctx context.Context) ([]Webhook, error)
// GetAll lists all the current registered webhooks.
GetAll(ctx context.Context) ([]InternalWebhook, error)
}

// Config contains information needed to initialize the webhook service.
Expand Down Expand Up @@ -85,8 +85,13 @@ type service struct {
now func() time.Time
}

func (s *service) Add(ctx context.Context, owner string, w Webhook) error {
item, err := webhookToItem(s.now, w)
type InternalWebhook struct {
PartnerIDs []string
Webhook Webhook
}

func (s *service) Add(ctx context.Context, owner string, iw InternalWebhook) error {
item, err := internalWebhookToItem(s.now, iw)
if err != nil {
return fmt.Errorf(errFmt, errFailedWebhookConversion, err)
}
Expand All @@ -101,42 +106,43 @@ func (s *service) Add(ctx context.Context, owner string, w Webhook) error {
return fmt.Errorf("%w: %s", errNonSuccessPushResult, result)
}

// AllWebhooks returns all webhooks found on the configured webhooks partition
// GetAll returns all webhooks found on the configured webhooks partition
// of Argus.
func (s *service) AllWebhooks(ctx context.Context) ([]Webhook, error) {
func (s *service) GetAll(ctx context.Context) ([]InternalWebhook, error) {
items, err := s.argus.GetItems(ctx, "")
if err != nil {
return nil, fmt.Errorf(errFmt, errFailedWebhooksFetch, err)
}

webhooks := make([]Webhook, len(items))
iws := make([]InternalWebhook, len(items))

for i, item := range items {
webhook, err := itemToWebhook(item)
webhook, err := itemToInternalWebhook(item)
if err != nil {
return nil, fmt.Errorf(errFmt, errFailedItemConversion, err)
}
webhooks[i] = webhook
iws[i] = webhook
}

return webhooks, nil
return iws, nil
}

func webhookToItem(now func() time.Time, w Webhook) (model.Item, error) {
encodedWebhook, err := json.Marshal(w)
func internalWebhookToItem(now func() time.Time, iw InternalWebhook) (model.Item, error) {
encodedWebhook, err := json.Marshal(iw)
if err != nil {
return model.Item{}, err
}
var data map[string]interface{}

err = json.Unmarshal(encodedWebhook, &data)
if err != nil {
return model.Item{}, err
}

SecondsToExpiry := w.Until.Sub(now()).Seconds()
SecondsToExpiry := iw.Webhook.Until.Sub(now()).Seconds()
TTLSeconds := int64(math.Max(0, SecondsToExpiry))

checksum := fmt.Sprintf("%x", sha256.Sum256([]byte(w.Config.URL)))
checksum := fmt.Sprintf("%x", sha256.Sum256([]byte(iw.Webhook.Config.URL)))

return model.Item{
Data: data,
Expand All @@ -145,17 +151,17 @@ func webhookToItem(now func() time.Time, w Webhook) (model.Item, error) {
}, nil
}

func itemToWebhook(i model.Item) (Webhook, error) {
func itemToInternalWebhook(i model.Item) (InternalWebhook, error) {
encodedWebhook, err := json.Marshal(i.Data)
if err != nil {
return Webhook{}, err
return InternalWebhook{}, err
}
var w Webhook
err = json.Unmarshal(encodedWebhook, &w)
var iw InternalWebhook
err = json.Unmarshal(encodedWebhook, &iw)
if err != nil {
return Webhook{}, err
return InternalWebhook{}, err
}
return w, nil
return iw, nil
}

func validateConfig(cfg *Config) {
Expand Down Expand Up @@ -208,25 +214,34 @@ func prepArgusConfig(cfg *Config, watches ...Watch) error {

func createArgusListener(logger log.Logger, watches ...Watch) chrysom.Listener {
return chrysom.ListenerFunc(func(items chrysom.Items) {
webhooks, err := itemsToWebhooks(items)
iws, err := itemsToInternalWebhooks(items)
if err != nil {
level.Error(logger).Log(logging.MessageKey(), "Failed to convert items to webhooks", "err", err)
return
}
ws := internalWebhooksToWebhooks(iws)
for _, watch := range watches {
watch.Update(webhooks)
watch.Update(ws)
}
})
}

func itemsToWebhooks(items []model.Item) ([]Webhook, error) {
webhooks := []Webhook{}
func itemsToInternalWebhooks(items []model.Item) ([]InternalWebhook, error) {
iws := []InternalWebhook{}
for _, item := range items {
webhook, err := itemToWebhook(item)
iw, err := itemToInternalWebhook(item)
if err != nil {
return nil, err
}
webhooks = append(webhooks, webhook)
iws = append(iws, iw)
}
return iws, nil
}

func internalWebhooksToWebhooks(iws []InternalWebhook) []Webhook {
w := make([]Webhook, 0, len(iws))
for _, iw := range iws {
w = append(w, iw.Webhook)
}
return webhooks, nil
return w
}
Loading

0 comments on commit 1f11c14

Please sign in to comment.