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

Adding ACL List, Show Cli #25

Open
wants to merge 26 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
26 commits
Select commit Hold shift + click to select a range
37721e6
WIP: list acls cli option
Mar 4, 2019
785ea71
Refactor to move cli packages to internal
Mar 13, 2019
4c8c7ff
Initial command setup
Mar 17, 2019
864bce5
Introduce basic parent command
Mar 17, 2019
839e2fd
Parent Command can register sub commands
Mar 18, 2019
9821600
Parent command registers only one cmd for cli handelr
Mar 18, 2019
9600236
Parent CMD executs responsible subcommand
Mar 19, 2019
89f7035
Refactor by merging parent command to command
Mar 23, 2019
065b9e8
Parent command can execute before action
Mar 24, 2019
3c34640
Cli can register base commands
Mar 24, 2019
3adef86
Cli converts commands to base commands
Mar 24, 2019
6bc5329
Cli setup logger and config before passing control to commands
Mar 25, 2019
02dfcfb
Introduce flag entity
Mar 25, 2019
278dcd8
Command can set flag for a command
Mar 25, 2019
b5e0c1b
cli considers command flags while constructing app
Mar 25, 2019
a64e952
App run with strings args gurantees execution of base command
Mar 26, 2019
a788ab8
Cli respects Parent Command and Registers all SubCommands
Mar 26, 2019
8532d4e
Basic server start command implementation
Mar 26, 2019
c14194c
Remove custom view layer
Mar 27, 2019
ed5fcd2
Etcd router ListAll - to list all valid acls
Mar 27, 2019
6d780eb
Views renders anything with json tags to stdout
Mar 28, 2019
b8a6870
Correct weaver usage and acl command name
Mar 28, 2019
5d79aed
Refactor to idiomatic go way
Mar 28, 2019
33e0be9
Not escaping HTML characters as we are printing to stdout
Mar 28, 2019
def2549
Implement acl show command
Mar 31, 2019
f35a5e8
Display help when acl id is not passed for show sub command
Sep 18, 2019
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
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ all: build fmt vet lint test coverage
default: build fmt vet lint test

ALL_PACKAGES=$(shell go list ./... | grep -v "vendor")
APP_EXECUTABLE="out/weaver-server"
APP_EXECUTABLE="out/weaver"
COMMIT_HASH=$(shell git rev-parse --verify head | cut -c-1-8)
BUILD_DATE=$(shell date +%Y-%m-%dT%H:%M:%S%z)

Expand All @@ -14,7 +14,7 @@ setup:

compile:
mkdir -p out/
GO111MODULE=on go build -o $(APP_EXECUTABLE) -ldflags "-X main.BuildDate=$(BUILD_DATE) -X main.Commit=$(COMMIT_HASH) -s -w" ./cmd/weaver-server
GO111MODULE=on go build -o $(APP_EXECUTABLE) -ldflags "-X main.BuildDate=$(BUILD_DATE) -X main.Commit=$(COMMIT_HASH) -s -w" ./cmd/weaver

build: deps compile fmt vet lint

Expand Down
77 changes: 0 additions & 77 deletions cmd/weaver-server/main.go

This file was deleted.

27 changes: 27 additions & 0 deletions cmd/weaver/main.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
package main

import (
"fmt"
"github.com/gojektech/weaver/internal/cli"
_ "github.com/gojektech/weaver/internal/commands"
_ "github.com/gojektech/weaver/internal/commands/acls"
_ "github.com/gojektech/weaver/internal/commands/server"
"os"
)

func main() {
app := cli.NewApp()
app.Name = "Weaver"
app.Version = fmt.Sprintf("%s built on %s (commit: %s)", Version, BuildDate, Commit)
app.Usage = "Start Server, Perform CRUD on ACLs"
app.Description = "An Advanced HTTP Reverse Proxy with Dynamic Sharding Strategies"
app.Commands = cli.GetBaseCommands()
app.Run(os.Args)
}

// Build information (will be injected during build)
var (
Version = "1.0.0"
Commit = "n/a"
BuildDate = "n/a"
)
37 changes: 30 additions & 7 deletions etcd/routeloader.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,29 @@ type RouteLoader struct {
namespace string
}

// ListAll - List all valid weaver acls
func (routeLoader *RouteLoader) ListAll() ([]*weaver.ACL, error) {
keysAPI, key := initEtcd(routeLoader)
res, err := keysAPI.Get(context.Background(), key, nil)
if err != nil {
return nil, fmt.Errorf("fail to LIST %s with %s", key, err)
}
acls := []*weaver.ACL{}
sort.Sort(res.Node.Nodes)
for _, nd := range res.Node.Nodes {
logger.Debugf("fetching node key %s", nd.Key)
aclKey := GenACLKey(nd.Key)
acl, err := routeLoader.GetACL(aclKey)
if err != nil {
logger.Errorf("error in fetching %s: %v", nd.Key, err)
} else {
acls = append(acls, acl)

}
}
return acls, nil
}

// PutACL - Upserts a given ACL
func (routeLoader *RouteLoader) PutACL(acl *weaver.ACL) (ACLKey, error) {
key := GenKey(acl, routeLoader.namespace)
Expand Down Expand Up @@ -81,8 +104,8 @@ func (routeLoader *RouteLoader) DelACL(key ACLKey) error {
}

func (routeLoader *RouteLoader) WatchRoutes(ctx context.Context, upsertRouteFunc server.UpsertRouteFunc, deleteRouteFunc server.DeleteRouteFunc) {
etc, key := initEtcd(routeLoader)
watcher := etc.Watcher(key, &etcd.WatcherOptions{Recursive: true})
keysAPI, key := initEtcd(routeLoader)
watcher := keysAPI.Watcher(key, &etcd.WatcherOptions{Recursive: true})

logger.Infof("starting etcd watcher on %s", key)
for {
Expand Down Expand Up @@ -130,12 +153,12 @@ func (routeLoader *RouteLoader) WatchRoutes(ctx context.Context, upsertRouteFunc

func (routeLoader *RouteLoader) BootstrapRoutes(ctx context.Context, upsertRouteFunc server.UpsertRouteFunc) error {
// TODO: Consider error scenarios and return an error when it makes sense
etc, key := initEtcd(routeLoader)
keysAPI, key := initEtcd(routeLoader)
logger.Infof("bootstrapping router using etcd on %s", key)
res, err := etc.Get(ctx, key, nil)
res, err := keysAPI.Get(ctx, key, nil)
if err != nil {
logger.Infof("creating router namespace on etcd using %s", key)
_, _ = etc.Set(ctx, key, "", &etcd.SetOptions{
_, _ = keysAPI.Set(ctx, key, "", &etcd.SetOptions{
Dir: true,
})
}
Expand Down Expand Up @@ -164,7 +187,7 @@ func (routeLoader *RouteLoader) BootstrapRoutes(ctx context.Context, upsertRoute

func initEtcd(routeLoader *RouteLoader) (etcd.KeysAPI, string) {
key := fmt.Sprintf("/%s/acls/", routeLoader.namespace)
etc := etcd.NewKeysAPI(routeLoader.etcdClient)
keysAPI := etcd.NewKeysAPI(routeLoader.etcdClient)

return etc, key
return keysAPI, key
}
22 changes: 22 additions & 0 deletions etcd/routeloader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,28 @@ func TestRouteLoaderSuite(tst *testing.T) {
suite.Run(tst, new(RouteLoaderSuite))
}

func (es *RouteLoaderSuite) TestListAll() {
aclPut := &weaver.ACL{
ID: "svc-01",
Criterion: "Method(`GET`) && Path(`/ping`)",
EndpointConfig: &weaver.EndpointConfig{
ShardFunc: "lookup",
Matcher: "path",
ShardExpr: "*",
ShardConfig: json.RawMessage(`{}`),
},
}

key, err := es.ng.PutACL(aclPut)
assert.NoError(es.T(), err, "failed to PUT %s", aclPut)

aclList, err := es.ng.ListAll()
assert.Nil(es.T(), err, "fail to ListAll ACLs")

deepEqual(es.T(), aclPut, aclList[0])
assert.Nil(es.T(), es.ng.DelACL(key), "fail to DELETE %+v", aclPut)
}

func (es *RouteLoaderSuite) TestPutACL() {
aclPut := &weaver.ACL{
ID: "svc-01",
Expand Down
4 changes: 3 additions & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,9 @@ require (
github.com/vulcand/route v0.0.0-20160805191529-61904570391b
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 // indirect
go.etcd.io/bbolt v1.3.2 // indirect
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 // indirect
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 // indirect
golang.org/x/net v0.0.0-20190322120337-addf6b3196f6 // indirect
golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc // indirect
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c // indirect
google.golang.org/grpc v1.18.0 // indirect
gopkg.in/airbrake/gobrake.v2 v2.0.9 // indirect
Expand Down
10 changes: 8 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -131,20 +131,26 @@ github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2 h1:eY9dn8+vbi4tKz5
github.com/xiang90/probing v0.0.0-20190116061207-43a291ad63a2/go.mod h1:UETIi67q53MR2AWcXfiuqkDkRtnGDLqkBTpCHuJHxtU=
go.etcd.io/bbolt v1.3.2 h1:Z/90sZLPOeCy2PwprqkFa25PdkusRzaj9P8zm/KNyvk=
go.etcd.io/bbolt v1.3.2/go.mod h1:IbVyRI1SCnLcuJnV2u8VeU0CEYM7e686BmAb1XKL+uU=
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613 h1:MQ/ZZiDsUapFFiMS+vzwXkCTeEKaum+Do5rINYJDmxc=
golang.org/x/crypto v0.0.0-20190131182504-b8fe1690c613/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576 h1:aUX/1G2gFSs4AsJJg2cL3HuoRhCSCz733FE5GUSuaT4=
golang.org/x/crypto v0.0.0-20190320223903-b7391e95e576/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc h1:a3CU5tJYVj92DY2LaA1kUkrsqD5/3mLDhx2NcNqyW+0=
golang.org/x/net v0.0.0-20181201002055-351d144fa1fc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
golang.org/x/net v0.0.0-20190322120337-addf6b3196f6 h1:78jEq2G3J16aXneH23HSnTQQTCwMHoyO8VEiUH+bpPM=
golang.org/x/net v0.0.0-20190322120337-addf6b3196f6/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg=
golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U=
golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f h1:Bl/8QSvNqXvPGPGXa2z5xUTmV7VDcZyvRZ+QQXkXTZQ=
golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM=
golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e h1:o3PsSEY8E4eXWkXrIP9YJALUkVZqzHJT5DOasTyn8Vs=
golang.org/x/sys v0.0.0-20180909124046-d0be0721c37e/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY=
golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc h1:4gbWbmmPFp4ySWICouJl6emP0MyS31yy9SrTlAGFT+g=
golang.org/x/sys v0.0.0-20190322080309-f49334f85ddc/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg=
golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ=
golang.org/x/time v0.0.0-20181108054448-85acf8d2951c h1:fqgJT0MGcGpPgpWU7VRdRjuArfcOvC4AoJmILihzhDg=
Expand Down
80 changes: 80 additions & 0 deletions internal/cli/cli.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
package cli

import (
"fmt"
"github.com/gojektech/weaver/config"
"github.com/gojektech/weaver/etcd"
"github.com/gojektech/weaver/pkg/logger"
baseCli "gopkg.in/urfave/cli.v1"
"os"
)

var registeredCommands = Commands{}

type Context struct {
RouteLoader *etcd.RouteLoader
*baseCli.Context
}

func RegisterAsBaseCommand(cmd *Command) error {
cliHandler := cmd.CliHandler()
for _, eachCmd := range registeredCommands {
if eachCmd.CliHandler() == cliHandler {
return fmt.Errorf("Another Command Regsitered for Cli Handler: %s", cliHandler)
}
}
registeredCommands = append(registeredCommands, cmd)
return nil
}

func GetBaseCommands() []baseCli.Command {
return getBaseCommandWithActionWrapper(registeredCommands, setup)
}

func getBaseCommandWithActionWrapper(cmds Commands, fn cmdAction) []baseCli.Command {
if fn == nil {
fn = func(c *Context) error { return nil }
}

baseCliCommands := []baseCli.Command{}
for idx, _ := range cmds {
eachCmd := cmds[idx]
baseCmd := baseCli.Command{
Name: eachCmd.name,
Usage: eachCmd.usage,
Description: eachCmd.description,
Flags: eachCmd.flags,
}
if eachCmd.isParentCommand {
baseCmd.Subcommands = getBaseCommandWithActionWrapper(
eachCmd.subCommands,
func(c *Context) error {
fn(c)
eachCmd.Exec(c)
return nil
},
)
} else {
baseCmd.Action = func(ctx *baseCli.Context) error {
c := &Context{Context: ctx}
fn(c)
return eachCmd.Exec(c)
}
}
baseCliCommands = append(baseCliCommands, baseCmd)
}
return baseCliCommands
}

func setup(c *Context) error {
os.Setenv("LOGGER_LEVEL", c.GlobalString("verbose"))
config.Load()
logger.SetupLogger()
return nil
}

func NewApp() *baseCli.App {
app := baseCli.NewApp()
app.Flags = []baseCli.Flag{NewStringFlag("verbose", "Error", "Verbosity of log level, ex: debug, info, warn, error, fatal, panic", "LOGGER_LEVEL")}
return app
}
Loading