Skip to content

Commit

Permalink
add smart routing generator
Browse files Browse the repository at this point in the history
  • Loading branch information
kubemq committed Jan 28, 2021
1 parent 145bf90 commit 98ec6a5
Show file tree
Hide file tree
Showing 5 changed files with 170 additions and 45 deletions.
32 changes: 15 additions & 17 deletions .idea/workspace.xml

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

71 changes: 66 additions & 5 deletions cmd/generate/authorization/authorization.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,90 @@ package authorization

import (
"context"
"encoding/json"
"fmt"
"github.com/AlecAivazis/survey/v2"
"github.com/kubemq-io/kubemqctl/pkg/config"
"github.com/kubemq-io/kubemqctl/pkg/utils"
"github.com/spf13/cobra"
"io/ioutil"
)

type AuthorizationOptions struct {
cfg *config.Config
}

var authorizationExamples = `
# Execute generate authorization policy file
kubemqctl generate az policy
kubemqctl generate az
`
var authorizationLong = `Generate and verify Authentication certificates and tokens`
var authorizationShort = `Generate and verify Authentication certificates and tokens`
var authorizationLong = `Generate authorization policy file`
var authorizationShort = `Generate authorization policy file`

func NewCmdAuthorization(ctx context.Context, cfg *config.Config) *cobra.Command {
o := &AuthorizationOptions{
cfg: cfg,
}
cmd := &cobra.Command{
Use: "authorization",
Aliases: []string{"access", "acc", "az"},
Short: authorizationShort,
Long: authorizationLong,
Example: authorizationExamples,
Run: func(cmd *cobra.Command, args []string) {
utils.CheckErr(cmd.Help())
ctx, cancel := context.WithCancel(ctx)
defer cancel()
utils.CheckErr(o.Complete(args, cfg.ConnectionType), cmd)
utils.CheckErr(o.Validate())
utils.CheckErr(o.Run(ctx))
},
}
cmd.AddCommand(NewCmdPolicy(ctx, cfg))

return cmd
}

func (o *AuthorizationOptions) Complete(args []string, transport string) error {
return nil
}

func (o *AuthorizationOptions) Validate() error {
return nil
}
func (o *AuthorizationOptions) Run(ctx context.Context) error {
var rules []*Rule
utils.Println("Create first rule:")
for {
r, err := getRule()
if err != nil {
return err
}
rules = append(rules, r)
addMoreRule := true
addMorePrompt := &survey.Confirm{
Message: "Add more rules to policy?",
Default: true,
Help: "",
}
err = survey.AskOne(addMorePrompt, &addMoreRule)
if err != nil {
return err
}
if !addMoreRule {
goto save
}
utils.Println("Create next rule:")
}
save:
data, err := json.MarshalIndent(rules, "", " ")
if err != nil {
return err
}
utils.Println("Authorization Rules:")
fmt.Println(string(data))
err = ioutil.WriteFile("policy.json", data, 0600)
if err != nil {
return err
}
utils.Println("Authorization data save to policy.json file")
return nil
}
4 changes: 4 additions & 0 deletions cmd/generate/generate.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"github.com/kubemq-io/kubemqctl/cmd/generate/authentication"
"github.com/kubemq-io/kubemqctl/cmd/generate/authorization"
"github.com/kubemq-io/kubemqctl/cmd/generate/routing"
"github.com/kubemq-io/kubemqctl/pkg/config"
"github.com/kubemq-io/kubemqctl/pkg/utils"
"github.com/spf13/cobra"
Expand All @@ -16,6 +17,8 @@ var generateExamples = `
# Execute generate authorization commands
kubemqctl generate az
# Execute generate smart routing file
kubemqctl generate routes
`
var generateLong = `Generate various kubemq related artifacts`
var generateShort = `Generate various kubemq related artifacts`
Expand All @@ -33,5 +36,6 @@ func NewCmdGenerate(ctx context.Context, cfg *config.Config) *cobra.Command {
}
cmd.AddCommand(authentication.NewCmdAuthentication(ctx, cfg))
cmd.AddCommand(authorization.NewCmdAuthorization(ctx, cfg))
cmd.AddCommand(routing.NewCmdRouting(ctx, cfg))
return cmd
}
62 changes: 62 additions & 0 deletions cmd/generate/routing/route.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,62 @@
package routing

import (
"fmt"
"github.com/AlecAivazis/survey/v2"
"strings"
)

type Route struct {
Key string
Routes string
}

func getRoute() (*Route, error) {
r := &Route{}
qs := []*survey.Question{
{
Name: "key",
Prompt: &survey.Input{
Message: "Set Route Key: (key1 ...)",
Default: "",
Help: "",
Suggest: nil,
},
Validate: survey.Required,
},
{
Name: "routes",
Prompt: &survey.Input{
Message: "Set Routes: (queues:foo.bar;events:baz.foo...)",
Default: "",
Help: "",
Suggest: nil,
},
Validate: func(ans interface{}) error {
val := ans.(string)
routes := strings.Split(val, ";")
if len(routes) == 0 {
return fmt.Errorf("routes must have at least one route")
}
for _, route := range routes {
kv := strings.Split(route, ":")
if len(kv) != 2 {
return fmt.Errorf("single route must have a channel value")
}
switch kv[0] {
case "queues", "events", "events_store", "routes":
default:
return fmt.Errorf("route must start with 'queues' or 'events' or 'events_store")
}
}
return nil
},
},
}
err := survey.Ask(qs, r)
if err != nil {
return nil, err
}

return r, nil
}
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package authorization
package routing

import (
"context"
Expand All @@ -11,25 +11,25 @@ import (
"io/ioutil"
)

type PolicyOptions struct {
type RoutingOptions struct {
cfg *config.Config
verify bool
}

var policyExamples = `
# Execute generate authorization policy file
kubemqctl generate az policy
# Execute generate smart routing file
kubemqctl generate routes
`
var policyLong = `Generate KubeMQ Authorization access control file`
var policyShort = `Generate KubeMQ Authorization access control file`
var policyLong = `Generate KubeMQ Smart Routing file`
var policyShort = `Generate KubeMQ Smart Routing file`

func NewCmdPolicy(ctx context.Context, cfg *config.Config) *cobra.Command {
o := &PolicyOptions{
func NewCmdRouting(ctx context.Context, cfg *config.Config) *cobra.Command {
o := &RoutingOptions{
cfg: cfg,
}
cmd := &cobra.Command{
Use: "policy",
Aliases: []string{"p"},
Use: "routes",
Aliases: []string{"r", "route"},
Short: policyShort,
Long: policyLong,
Example: policyExamples,
Expand All @@ -45,25 +45,25 @@ func NewCmdPolicy(ctx context.Context, cfg *config.Config) *cobra.Command {
return cmd
}

func (o *PolicyOptions) Complete(args []string, transport string) error {
func (o *RoutingOptions) Complete(args []string, transport string) error {
return nil
}

func (o *PolicyOptions) Validate() error {
func (o *RoutingOptions) Validate() error {
return nil
}
func (o *PolicyOptions) Run(ctx context.Context) error {
var rules []*Rule
utils.Println("Create first rule:")
func (o *RoutingOptions) Run(ctx context.Context) error {
var routes []*Route
utils.Println("Create first route:")
for {
r, err := getRule()
r, err := getRoute()
if err != nil {
return err
}
rules = append(rules, r)
routes = append(routes, r)
addMoreRule := true
addMorePrompt := &survey.Confirm{
Message: "Add more rules to policy?",
Message: "Add more routes ?",
Default: true,
Help: "",
}
Expand All @@ -74,19 +74,19 @@ func (o *PolicyOptions) Run(ctx context.Context) error {
if !addMoreRule {
goto save
}
utils.Println("Create next rule:")
utils.Println("Create next route:")
}
save:
data, err := json.MarshalIndent(rules, "", " ")
data, err := json.MarshalIndent(routes, "", " ")
if err != nil {
return err
}
utils.Println("Policy Rules:")
utils.Println("Routing Rules:")
fmt.Println(string(data))
err = ioutil.WriteFile("policy.json", data, 0600)
err = ioutil.WriteFile("routes.json", data, 0600)
if err != nil {
return err
}
utils.Println("Policy data save to policy.json file")
utils.Println("Routing data save to routes.json file")
return nil
}

0 comments on commit 98ec6a5

Please sign in to comment.