/
create_edge_router.go
127 lines (107 loc) · 4.33 KB
/
create_edge_router.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
Copyright NetFoundry Inc.
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
https://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 edge
import (
"fmt"
"github.com/Jeffail/gabs"
"github.com/openziti/ziti/ziti/cmd/api"
cmdhelper "github.com/openziti/ziti/ziti/cmd/helpers"
"github.com/spf13/cobra"
"io"
"os"
)
type createEdgeRouterOptions struct {
api.EntityOptions
isTunnelerEnabled bool
roleAttributes []string
jwtOutputFile string
appData map[string]string
cost uint16
noTraversal bool
disabled bool
}
func NewCreateEdgeRouterCmd(out io.Writer, errOut io.Writer) *cobra.Command {
options := &createEdgeRouterOptions{
EntityOptions: api.NewEntityOptions(out, errOut),
}
cmd := &cobra.Command{
Use: "edge-router <name>",
Aliases: []string{"gateway", "er"},
Short: "creates an edge router managed by the Ziti Edge Controller",
Long: "creates an edge router managed by the Ziti Edge Controller",
Args: cobra.ExactArgs(1),
Run: func(cmd *cobra.Command, args []string) {
options.Cmd = cmd
options.Args = args
err := runCreateEdgeRouter(options)
cmdhelper.CheckErr(err)
},
SuggestFor: []string{},
}
// allow interspersing positional args and flags
cmd.Flags().SetInterspersed(true)
cmd.Flags().StringSliceVarP(&options.roleAttributes, "role-attributes", "a", nil, "comma-separated role attributes for the new router")
cmd.Flags().BoolVarP(&options.isTunnelerEnabled, "tunneler-enabled", "t", false, "Can this edge router be used as a tunneler")
cmd.Flags().StringVarP(&options.jwtOutputFile, "jwt-output-file", "o", "", "File to which to output the JWT used for enrolling the edge router")
cmd.Flags().StringToStringVar(&options.appData, "app-Data", nil, "Custom application data")
cmd.Flags().Uint16Var(&options.cost, "cost", 0, "Specifies the router cost. Default 0.")
cmd.Flags().BoolVar(&options.noTraversal, "no-traversal", false, "Disallow traversal for this edge router. Default to allowed(false).")
cmd.Flags().BoolVar(&options.disabled, "disabled", false, "Disabled routers can't connect to controllers")
options.AddCommonFlags(cmd)
return cmd
}
// runCreateEdgeRouter implements the command to create a gateway on the edge controller
func runCreateEdgeRouter(o *createEdgeRouterOptions) error {
entityData := gabs.New()
api.SetJSONValue(entityData, o.Args[0], "name")
api.SetJSONValue(entityData, o.isTunnelerEnabled, "isTunnelerEnabled")
api.SetJSONValue(entityData, o.roleAttributes, "roleAttributes")
api.SetJSONValue(entityData, o.appData, "appData")
api.SetJSONValue(entityData, o.cost, "cost")
api.SetJSONValue(entityData, o.noTraversal, "noTraversal")
api.SetJSONValue(entityData, o.disabled, "disabled")
o.SetTags(entityData)
result, err := CreateEntityOfType("edge-routers", entityData.String(), &o.Options)
if err := o.LogCreateResult("edge router", result, err); err != nil {
return err
}
if o.jwtOutputFile != "" {
id := result.S("data", "id").Data().(string)
if err := getEdgeRouterJwt(o.EntityOptions.Options, o.jwtOutputFile, id); err != nil {
return err
}
}
return nil
}
func getEdgeRouterJwt(o api.Options, jwtOutputFile string, id string) error {
newRouter, err := DetailEntityOfType("edge-routers", id, o.OutputJSONResponse, o.Out, o.Timeout, o.Verbose)
if err != nil {
return err
}
if newRouter == nil {
return fmt.Errorf("no error during edge router creation, but edge router with id %v not found... unable to extract JWT", id)
}
jwt := newRouter.Path("enrollmentJwt").Data().(string)
if jwt == "" {
return fmt.Errorf("enrollment JWT not present for new edge router")
}
if err := os.WriteFile(jwtOutputFile, []byte(jwt), 0600); err != nil {
fmt.Printf("Failed to write JWT to file(%v)\n", jwtOutputFile)
return err
}
jwtExpiration := newRouter.Path("enrollmentExpiresAt").Data().(string)
if jwtExpiration != "" {
fmt.Printf("Enrollment expires at %v\n", jwtExpiration)
}
return err
}