Skip to content

Commit

Permalink
Move acl to weaver package
Browse files Browse the repository at this point in the history
  • Loading branch information
spy16 committed Feb 1, 2019
1 parent eb472d5 commit 389701c
Show file tree
Hide file tree
Showing 8 changed files with 42 additions and 41 deletions.
12 changes: 5 additions & 7 deletions server/acl.go → acl.go
Original file line number Diff line number Diff line change
@@ -1,19 +1,17 @@
package server
package weaver

import (
"encoding/json"
"fmt"

"github.com/gojektech/weaver"
)

// ACL - Connects to an external endpoint
type ACL struct {
ID string `json:"id"`
Criterion string `json:"criterion"`
EndpointConfig *weaver.EndpointConfig `json:"endpoint"`
ID string `json:"id"`
Criterion string `json:"criterion"`
EndpointConfig *EndpointConfig `json:"endpoint"`

Endpoint *weaver.Endpoint
Endpoint *Endpoint
}

// GenACL - Generates an ACL from JSON
Expand Down
4 changes: 2 additions & 2 deletions etcd/aclkey.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ package etcd
import (
"fmt"

"github.com/gojektech/weaver/server"
"github.com/gojektech/weaver"
)

const (
Expand All @@ -19,6 +19,6 @@ func GenACLKey(key string) ACLKey {
return ACLKey(fmt.Sprintf("%s/acl", key))
}

func GenKey(acl *server.ACL, pfx string) ACLKey {
func GenKey(acl *weaver.ACL, pfx string) ACLKey {
return ACLKey(fmt.Sprintf(ACLKeyFormat, pfx, acl.ID))
}
8 changes: 4 additions & 4 deletions etcd/routeloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ type ETCDRouteLoader struct {
}

// PutACL - Upserts a given ACL
func (routeLoader *ETCDRouteLoader) PutACL(acl *server.ACL) (ACLKey, error) {
func (routeLoader *ETCDRouteLoader) PutACL(acl *weaver.ACL) (ACLKey, error) {
key := GenKey(acl, routeLoader.namespace)
val, err := json.Marshal(acl)
if err != nil {
Expand All @@ -47,12 +47,12 @@ func (routeLoader *ETCDRouteLoader) PutACL(acl *server.ACL) (ACLKey, error) {
}

// GetACL - Fetches an ACL given an ACLKey
func (routeLoader *ETCDRouteLoader) GetACL(key ACLKey) (*server.ACL, error) {
func (routeLoader *ETCDRouteLoader) GetACL(key ACLKey) (*weaver.ACL, error) {
res, err := etcd.NewKeysAPI(routeLoader.etcdClient).Get(context.Background(), string(key), nil)
if err != nil {
return nil, fmt.Errorf("fail to GET %s with %s", key, err.Error())
}
acl := &server.ACL{}
acl := &weaver.ACL{}
if err := json.Unmarshal([]byte(res.Node.Value), acl); err != nil {
return nil, err
}
Expand Down Expand Up @@ -110,7 +110,7 @@ func (routeLoader *ETCDRouteLoader) WatchRoutes(ctx context.Context, upsertRoute
continue
}
case "delete":
acl := &server.ACL{}
acl := &weaver.ACL{}
err := acl.GenACL(res.PrevNode.Value)
if err != nil {
logger.Errorf("error in unmarshalling %s: %v", res.PrevNode.Value, err)
Expand Down
29 changes: 14 additions & 15 deletions etcd/routeloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ import (
etcd "github.com/coreos/etcd/client"
"github.com/gojektech/weaver/config"
"github.com/gojektech/weaver/pkg/logger"
"github.com/gojektech/weaver/server"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
Expand Down Expand Up @@ -44,7 +43,7 @@ func TestETCDRouteLoaderSuite(tst *testing.T) {
}

func (es *ETCDRouteLoaderSuite) TestPutACL() {
aclPut := &server.ACL{
aclPut := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && Path(`/ping`)",
EndpointConfig: &weaver.EndpointConfig{
Expand Down Expand Up @@ -74,7 +73,7 @@ func (es *ETCDRouteLoaderSuite) TestPutACL() {
}

func (es *ETCDRouteLoaderSuite) TestBootstrapRoutes() {
aclPut := &server.ACL{
aclPut := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && Path(`/ping`)",
EndpointConfig: &weaver.EndpointConfig{
Expand All @@ -87,15 +86,15 @@ func (es *ETCDRouteLoaderSuite) TestBootstrapRoutes() {
key, err := es.ng.PutACL(aclPut)
assert.NoError(es.T(), err, "failed to PUT %s", aclPut)

aclsChan := make(chan *server.ACL, 1)
aclsChan := make(chan *weaver.ACL, 1)
es.ng.BootstrapRoutes(context.Background(), genRouteProcessorMock(aclsChan))

deepEqual(es.T(), aclPut, <-aclsChan)
assert.Nil(es.T(), es.ng.DelACL(key), "fail to DELETE %+v", aclPut)
}

func (es *ETCDRouteLoaderSuite) TestBootstrapRoutesSucceedWhenARouteUpsertFails() {
aclPut := &server.ACL{
aclPut := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && Path(`/ping`)",
EndpointConfig: &weaver.EndpointConfig{
Expand Down Expand Up @@ -144,7 +143,7 @@ func (es *ETCDRouteLoaderSuite) TestBootstrapRoutesSucceedWhenARouteHasInvalidDa
func (es *ETCDRouteLoaderSuite) TestWatchRoutesUpsertRoutesWhenRoutesSet() {
newACL := newTestACL("path")

aclsUpserted := make(chan *server.ACL, 1)
aclsUpserted := make(chan *weaver.ACL, 1)

watchCtx, cancelWatch := context.WithCancel(context.Background())
defer cancelWatch()
Expand All @@ -164,7 +163,7 @@ func (es *ETCDRouteLoaderSuite) TestWatchRoutesUpsertRoutesWhenRoutesUpdated() {
updatedACL := newTestACL("header")

_, err := es.ng.PutACL(newACL)
aclsUpserted := make(chan *server.ACL, 1)
aclsUpserted := make(chan *weaver.ACL, 1)
watchCtx, cancelWatch := context.WithCancel(context.Background())
defer cancelWatch()

Expand All @@ -184,7 +183,7 @@ func (es *ETCDRouteLoaderSuite) TestWatchRoutesDeleteRouteWhenARouteIsDeleted()
key, err := es.ng.PutACL(newACL)
require.NoError(es.T(), err, "fail to PUT ACL %+v", newACL)

aclsDeleted := make(chan *server.ACL, 1)
aclsDeleted := make(chan *weaver.ACL, 1)

watchCtx, cancelWatch := context.WithCancel(context.Background())
defer cancelWatch()
Expand All @@ -198,8 +197,8 @@ func (es *ETCDRouteLoaderSuite) TestWatchRoutesDeleteRouteWhenARouteIsDeleted()
deepEqual(es.T(), newACL, <-aclsDeleted)
}

func newTestACL(matcher string) *server.ACL {
return &server.ACL{
func newTestACL(matcher string) *weaver.ACL {
return &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && Path(`/ping`)",
EndpointConfig: &weaver.EndpointConfig{
Expand All @@ -221,14 +220,14 @@ func newTestACL(matcher string) *server.ACL {
}
}

func genRouteProcessorMock(c chan *server.ACL) func(*server.ACL) error {
return func(acl *server.ACL) error {
func genRouteProcessorMock(c chan *weaver.ACL) func(*weaver.ACL) error {
return func(acl *weaver.ACL) error {
c <- acl
return nil
}
}

func deepEqual(t *testing.T, expected *server.ACL, actual *server.ACL) {
func deepEqual(t *testing.T, expected *weaver.ACL, actual *weaver.ACL) {
assert.Equal(t, expected.ID, actual.ID)
assert.Equal(t, expected.Criterion, actual.Criterion)
assertEqualJSON(t, expected.EndpointConfig.ShardConfig, actual.EndpointConfig.ShardConfig)
Expand All @@ -249,10 +248,10 @@ func assertEqualJSON(t *testing.T, json1, json2 json.RawMessage) {
assert.True(t, reflect.DeepEqual(jsonVal1, jsonVal2))
}

func failingUpsertRouteFunc(acl *server.ACL) error {
func failingUpsertRouteFunc(acl *weaver.ACL) error {
return errors.New("error")
}

func successUpsertRouteFunc(acl *server.ACL) error {
func successUpsertRouteFunc(acl *weaver.ACL) error {
return nil
}
8 changes: 4 additions & 4 deletions server/handler_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ func (ps *ProxySuite) TestProxyHandlerOnSuccessfulRouting() {
_, _ = w.Write([]byte("foobar"))
}))

acl := &ACL{
acl := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && PathRegexp(`/(GF-|R-).*`)",
EndpointConfig: &weaver.EndpointConfig{
Expand Down Expand Up @@ -88,7 +88,7 @@ func (ps *ProxySuite) TestProxyHandlerOnBodyBasedMatcherWithModuloSharding() {
_, _ = w.Write([]byte("foobar"))
}))

acl := &ACL{
acl := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && PathRegexp(`/drivers`)",
EndpointConfig: &weaver.EndpointConfig{
Expand Down Expand Up @@ -135,7 +135,7 @@ func (ps *ProxySuite) TestProxyHandlerOnPathBasedMatcherWithModuloSharding() {
_, _ = w.Write([]byte("foobar"))
}))

acl := &ACL{
acl := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && PathRegexp(`/drivers`)",
EndpointConfig: &weaver.EndpointConfig{
Expand Down Expand Up @@ -187,7 +187,7 @@ func (ps *ProxySuite) TestProxyHandlerOnFailureRouting() {

func (ps *ProxySuite) TestProxyHandlerOnMissingBackend() {

acl := &ACL{
acl := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && PathRegexp(`/(GF-|R-).*`)",
EndpointConfig: &weaver.EndpointConfig{
Expand Down
10 changes: 7 additions & 3 deletions server/loader.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,13 @@
package server

import "context"
import (
"context"

type UpsertRouteFunc func(*ACL) error
type DeleteRouteFunc func(*ACL) error
"github.com/gojektech/weaver"
)

type UpsertRouteFunc func(*weaver.ACL) error
type DeleteRouteFunc func(*weaver.ACL) error

type RouteLoader interface {
BootstrapRoutes(context.Context, UpsertRouteFunc) error
Expand Down
9 changes: 5 additions & 4 deletions server/router.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ import (
"fmt"
"net/http"

"github.com/gojektech/weaver"
"github.com/pkg/errors"
"github.com/vulcand/route"
)
Expand All @@ -16,7 +17,7 @@ type Router struct {

type apiName string

func (router *Router) Route(req *http.Request) (*ACL, error) {
func (router *Router) Route(req *http.Request) (*weaver.ACL, error) {
rt, err := router.Router.Route(req)
if err != nil {
return nil, errors.Wrapf(err, "failed to find route with url: %s", req.URL)
Expand All @@ -26,7 +27,7 @@ func (router *Router) Route(req *http.Request) (*ACL, error) {
return nil, errors.WithStack(fmt.Errorf("route not found: %s", req.URL))
}

acl, ok := rt.(*ACL)
acl, ok := rt.(*weaver.ACL)
if !ok {
return nil, errors.WithStack(fmt.Errorf("error in casting %v to acl", rt))
}
Expand All @@ -49,10 +50,10 @@ func (router *Router) BootstrapRoutes(ctx context.Context) error {
return router.loader.BootstrapRoutes(ctx, router.upsertACL)
}

func (router *Router) upsertACL(acl *ACL) error {
func (router *Router) upsertACL(acl *weaver.ACL) error {
return router.UpsertRoute(acl.Criterion, acl)
}

func (router *Router) deleteACL(acl *ACL) error {
func (router *Router) deleteACL(acl *weaver.ACL) error {
return router.RemoveRoute(acl.Criterion)
}
3 changes: 1 addition & 2 deletions server/router_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func (rs *RouterSuite) TestRouteReturnsACL() {
nil)

// timeout is float64 because there are no integers in json
acl := &ACL{
acl := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && PathRegexp(`/(GF-|R-).*`)",
EndpointConfig: &weaver.EndpointConfig{
Expand Down Expand Up @@ -136,4 +136,3 @@ func (rs *RouterSuite) TestWatchRouteUpdatesCallsWatchRoutesOfLoader() {

routeLoader.AssertExpectations(rs.T())
}

0 comments on commit 389701c

Please sign in to comment.