Add enroll to ziti edge CLI. Fixes GH-182

This commit is contained in:
Paul Lorenz
2020-08-21 17:31:28 -04:00
parent 5595885ac5
commit e3ead934fc
5 changed files with 77 additions and 43 deletions
+1
View File
@@ -7,6 +7,7 @@
* Session Performance Fixes
* [Supported unindexed FK constraints in bbolt](https://github.com/openziti/foundation/issues/119)
* [Improve API Session and Session creation performance](https://github.com/openziti/edge/issues/281)
* [Make enrollment available from the ziti CLI](https://github.com/openziti/ziti/issues/182)
# Release 0.16.0
@@ -14,7 +14,7 @@
limitations under the License.
*/
package subcmd
package enrollment
import (
"encoding/json"
@@ -42,7 +42,30 @@ const idnameDesc = "Names the identity. Ignored if not 3rd party auto enrollment
const outFlag = "out"
func init() {
func NewEnrollCommand() *cobra.Command {
var enrollSubCmd = &cobra.Command{
SilenceErrors: true,
SilenceUsage: true,
Use: "enroll",
Short: "enroll an identity",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if verbose {
logrus.SetLevel(logrus.DebugLevel)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
//set the formatter for enrolling via ziti-tunnel
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
DisableTimestamp: true,
TimestampFormat: "",
PadLevelText: true,
})
logrus.SetReportCaller(false) // for enrolling don't bother with this
return processEnrollment()
},
}
enrollSubCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, verboseDesc)
enrollSubCmd.Flags().StringVarP(&jwtpath, "jwt", "j", "", jwtpathDesc)
enrollSubCmd.Flags().StringVarP(&outpath, outFlag, "o", "", outpathDesc)
@@ -62,30 +85,7 @@ func init() {
_ = enrollSubCmd.MarkFlagRequired("jwt")
root.AddCommand(enrollSubCmd)
}
var enrollSubCmd = &cobra.Command{
SilenceErrors: true,
SilenceUsage: true,
Use: "enroll",
Short: "enroll an identity",
PersistentPreRun: func(cmd *cobra.Command, args []string) {
if verbose {
logrus.SetLevel(logrus.DebugLevel)
}
},
RunE: func(cmd *cobra.Command, args []string) error {
//set the formatter for enrolling via ziti-tunnel
logrus.SetFormatter(&logrus.TextFormatter{
ForceColors: true,
DisableTimestamp: true,
TimestampFormat: "",
PadLevelText: true,
})
logrus.SetReportCaller(false) // for enrolling don't bother with this
return processEnrollment()
},
return enrollSubCmd
}
func processEnrollment() error {
@@ -135,10 +135,10 @@ func processEnrollment() error {
}
output, err := os.Create(outpath)
defer output.Close()
if err != nil {
return fmt.Errorf("failed to open file '%s': %s", outpath, err.Error())
}
defer func() { _ = output.Close() }()
enc := json.NewEncoder(output)
enc.SetEscapeHTML(false)
+44 -16
View File
@@ -17,12 +17,15 @@
package loop2
import (
"github.com/openziti/ziti/ziti-fabric-test/subcmd/loop2/pb"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/foundation/identity/dotziti"
"github.com/openziti/foundation/identity/identity"
"github.com/openziti/foundation/transport"
"github.com/michaelquigley/pfxlog"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/ziti/ziti-fabric-test/subcmd/loop2/pb"
"github.com/spf13/cobra"
"net"
"strings"
)
func init() {
@@ -41,17 +44,42 @@ var listenerCmdIdentity string
var listenerCmdBindAddress string
func listener(_ *cobra.Command, _ []string) {
_, id, err := dotziti.LoadIdentity(listenerCmdIdentity)
if strings.HasPrefix(listenerCmdBindAddress, "edge") {
listenEdge()
} else {
_, id, err := dotziti.LoadIdentity(listenerCmdIdentity)
if err != nil {
panic(err)
}
bindAddress, err := transport.ParseAddress(listenerCmdBindAddress)
if err != nil {
panic(err)
}
listen(bindAddress, id)
}
}
func listenEdge() {
log := pfxlog.ContextLogger(dialerCmdEndpoint)
defer log.Error("exited")
log.Info("started")
context := ziti.NewContext()
service := strings.TrimPrefix(dialerCmdEndpoint, "edge:")
listener, err := context.Listen(service)
if err != nil {
panic(err)
}
bindAddress, err := transport.ParseAddress(listenerCmdBindAddress)
if err != nil {
panic(err)
for {
if conn, err := listener.Accept(); err != nil {
panic(err)
} else {
go handle(conn, dialerCmdEndpoint)
}
}
listen(bindAddress, id)
}
func listen(bind transport.Address, i *identity.TokenId) {
@@ -68,18 +96,18 @@ func listen(bind transport.Address, i *identity.TokenId) {
for {
select {
case peer := <-incoming:
if peer != nil {
go handle(peer)
} else {
return
if peer != nil {
go handle(peer.Conn(), peer.Detail().String())
} else {
return
}
}
}
}
}
func handle(peer transport.Connection) {
log := pfxlog.ContextLogger(peer.Detail().String())
if proto, err := newProtocol(peer.Conn()); err == nil {
func handle(conn net.Conn, context string) {
log := pfxlog.ContextLogger(context)
if proto, err := newProtocol(conn); err == nil {
if test, err := proto.rxTest(); err == nil {
var result *loop2_pb.Result
if err := proto.run(test); err == nil {
@@ -23,6 +23,7 @@ import (
"github.com/openziti/edge/tunnel/intercept"
"github.com/openziti/sdk-golang/ziti"
"github.com/openziti/sdk-golang/ziti/config"
"github.com/openziti/ziti/common/enrollment"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
@@ -41,6 +42,8 @@ func init() {
root.PersistentFlags().Uint(svcPollRateFlag, 15, "Set poll rate for service updates (seconds)")
root.PersistentFlags().StringP(resolverCfgFlag, "r", "udp://127.0.0.1:53", "Resolver configuration")
root.PersistentFlags().StringVar(&logFormatter, "log-formatter", "", "Specify log formatter [json|pfxlog|text]")
root.AddCommand(enrollment.NewEnrollCommand())
}
var root = &cobra.Command{
@@ -17,6 +17,7 @@
package edge_controller
import (
"github.com/openziti/ziti/common/enrollment"
"io"
"github.com/Jeffail/gabs"
@@ -64,6 +65,7 @@ func populateEdgeCommands(f cmdutil.Factory, out io.Writer, errOut io.Writer, cm
cmd.AddCommand(newPolicyAdivsorCmd(f, out, errOut))
cmd.AddCommand(newVerifyCmd(f, out, errOut))
cmd.AddCommand(newDbCmd(f, out, errOut))
cmd.AddCommand(enrollment.NewEnrollCommand())
return cmd
}