mirror of
https://github.com/openziti/zrok.git
synced 2025-03-30 17:16:05 +02:00
rough 'create account' api skeleton
This commit is contained in:
parent
53b7c3c7b7
commit
64e2864fab
@ -1,10 +1,6 @@
|
|||||||
package main
|
package main
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/go-openapi/runtime"
|
|
||||||
httptransport "github.com/go-openapi/runtime/client"
|
|
||||||
"github.com/go-openapi/strfmt"
|
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_zrok_client"
|
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_zrok_client/metadata"
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_client/metadata"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
"github.com/spf13/cobra"
|
"github.com/spf13/cobra"
|
||||||
@ -21,13 +17,10 @@ var apiCmd = &cobra.Command{
|
|||||||
}
|
}
|
||||||
|
|
||||||
var apiVersionCmd = &cobra.Command{
|
var apiVersionCmd = &cobra.Command{
|
||||||
Use: "version <endpoint>",
|
Use: "version",
|
||||||
Short: "Get API version",
|
Short: "Get API version",
|
||||||
Run: func(_ *cobra.Command, args []string) {
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
transport := httptransport.New(args[0], "", nil)
|
zrok := newZrokClient()
|
||||||
transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer()
|
|
||||||
transport.Consumers["application/zrok.v1+json"] = runtime.JSONConsumer()
|
|
||||||
zrok := rest_zrok_client.New(transport, strfmt.Default)
|
|
||||||
resp, err := zrok.Metadata.Version(metadata.NewVersionParams())
|
resp, err := zrok.Metadata.Version(metadata.NewVersionParams())
|
||||||
if err != nil {
|
if err != nil {
|
||||||
panic(err)
|
panic(err)
|
||||||
|
15
cmd/zrok/client.go
Normal file
15
cmd/zrok/client.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/go-openapi/runtime"
|
||||||
|
httptransport "github.com/go-openapi/runtime/client"
|
||||||
|
"github.com/go-openapi/strfmt"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_client"
|
||||||
|
)
|
||||||
|
|
||||||
|
func newZrokClient() *rest_zrok_client.Zrok {
|
||||||
|
transport := httptransport.New(endpoint, "", nil)
|
||||||
|
transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer()
|
||||||
|
transport.Consumers["application/zrok.v1+json"] = runtime.JSONConsumer()
|
||||||
|
return rest_zrok_client.New(transport, strfmt.Default)
|
||||||
|
}
|
40
cmd/zrok/controller.go
Normal file
40
cmd/zrok/controller.go
Normal file
@ -0,0 +1,40 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/openziti-test-kitchen/zrok/controller"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/controller/store"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
rootCmd.AddCommand(controllerCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var controllerCmd = &cobra.Command{
|
||||||
|
Use: "controller <configPath>",
|
||||||
|
Short: "Start a zrok controller",
|
||||||
|
Aliases: []string{"ctrl"},
|
||||||
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
|
tokens := strings.Split(endpoint, ":")
|
||||||
|
if len(tokens) != 2 {
|
||||||
|
panic(errors.Errorf("malformed endpoint '%v'", endpoint))
|
||||||
|
}
|
||||||
|
host := tokens[0]
|
||||||
|
port, err := strconv.Atoi(tokens[1])
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
if err := controller.Run(&controller.Config{
|
||||||
|
Host: host,
|
||||||
|
Port: port,
|
||||||
|
Store: &store.Config{
|
||||||
|
Path: "zrok.db",
|
||||||
|
},
|
||||||
|
}); err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
},
|
||||||
|
}
|
35
cmd/zrok/create.go
Normal file
35
cmd/zrok/create.go
Normal file
@ -0,0 +1,35 @@
|
|||||||
|
package main
|
||||||
|
|
||||||
|
import (
|
||||||
|
"github.com/openziti-test-kitchen/zrok/rest_model"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_client/identity"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
|
"github.com/spf13/cobra"
|
||||||
|
)
|
||||||
|
|
||||||
|
func init() {
|
||||||
|
createCmd.AddCommand(createAccountCmd)
|
||||||
|
rootCmd.AddCommand(createCmd)
|
||||||
|
}
|
||||||
|
|
||||||
|
var createCmd = &cobra.Command{
|
||||||
|
Use: "create",
|
||||||
|
Short: "create objects",
|
||||||
|
}
|
||||||
|
|
||||||
|
var createAccountCmd = &cobra.Command{
|
||||||
|
Use: "account <username>",
|
||||||
|
Short: "create new zrok account",
|
||||||
|
Run: func(_ *cobra.Command, args []string) {
|
||||||
|
zrok := newZrokClient()
|
||||||
|
req := identity.NewCreateAccountParams()
|
||||||
|
req.Body = &rest_model.AccountRequest{
|
||||||
|
Username: args[0],
|
||||||
|
}
|
||||||
|
resp, err := zrok.Identity.CreateAccount(req)
|
||||||
|
if err != nil {
|
||||||
|
panic(err)
|
||||||
|
}
|
||||||
|
logrus.Infof("api token = '%v'", resp.Payload.APIToken)
|
||||||
|
},
|
||||||
|
}
|
@ -2,8 +2,6 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"github.com/michaelquigley/pfxlog"
|
"github.com/michaelquigley/pfxlog"
|
||||||
"github.com/openziti-test-kitchen/zrok/controller"
|
|
||||||
"github.com/openziti-test-kitchen/zrok/controller/store"
|
|
||||||
"github.com/openziti-test-kitchen/zrok/http"
|
"github.com/openziti-test-kitchen/zrok/http"
|
||||||
"github.com/openziti-test-kitchen/zrok/proxy"
|
"github.com/openziti-test-kitchen/zrok/proxy"
|
||||||
"github.com/sirupsen/logrus"
|
"github.com/sirupsen/logrus"
|
||||||
@ -16,7 +14,7 @@ import (
|
|||||||
func init() {
|
func init() {
|
||||||
pfxlog.GlobalInit(logrus.InfoLevel, pfxlog.DefaultOptions().SetTrimPrefix("github.com/openziti-test-kitchen/"))
|
pfxlog.GlobalInit(logrus.InfoLevel, pfxlog.DefaultOptions().SetTrimPrefix("github.com/openziti-test-kitchen/"))
|
||||||
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
|
rootCmd.PersistentFlags().BoolVarP(&verbose, "verbose", "v", false, "enable verbose logging")
|
||||||
rootCmd.AddCommand(controllerCmd)
|
rootCmd.PersistentFlags().StringVarP(&endpoint, "endpoint", "e", "localhost:10888", "zrok endpoint address")
|
||||||
rootCmd.AddCommand(httpCmd)
|
rootCmd.AddCommand(httpCmd)
|
||||||
rootCmd.AddCommand(proxyCmd)
|
rootCmd.AddCommand(proxyCmd)
|
||||||
}
|
}
|
||||||
@ -31,23 +29,7 @@ var rootCmd = &cobra.Command{
|
|||||||
},
|
},
|
||||||
}
|
}
|
||||||
var verbose bool
|
var verbose bool
|
||||||
|
var endpoint string
|
||||||
var controllerCmd = &cobra.Command{
|
|
||||||
Use: "controller <configPath>",
|
|
||||||
Short: "Start a zrok controller",
|
|
||||||
Aliases: []string{"ctrl"},
|
|
||||||
Run: func(_ *cobra.Command, args []string) {
|
|
||||||
if err := controller.Run(&controller.Config{
|
|
||||||
Host: "0.0.0.0",
|
|
||||||
Port: 10888,
|
|
||||||
Store: &store.Config{
|
|
||||||
Path: "zrok.db",
|
|
||||||
},
|
|
||||||
}); err != nil {
|
|
||||||
panic(err)
|
|
||||||
}
|
|
||||||
},
|
|
||||||
}
|
|
||||||
|
|
||||||
var httpCmd = &cobra.Command{
|
var httpCmd = &cobra.Command{
|
||||||
Use: "http <identity>",
|
Use: "http <identity>",
|
||||||
|
@ -7,8 +7,10 @@ import (
|
|||||||
"github.com/openziti-test-kitchen/zrok/rest_model"
|
"github.com/openziti-test-kitchen/zrok/rest_model"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_zrok_server"
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_server"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations"
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations"
|
||||||
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations/identity"
|
||||||
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations/metadata"
|
"github.com/openziti-test-kitchen/zrok/rest_zrok_server/operations/metadata"
|
||||||
"github.com/pkg/errors"
|
"github.com/pkg/errors"
|
||||||
|
"github.com/sirupsen/logrus"
|
||||||
)
|
)
|
||||||
|
|
||||||
var str *store.Store
|
var str *store.Store
|
||||||
@ -26,9 +28,8 @@ func Run(cfg *Config) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
api := operations.NewZrokAPI(swaggerSpec)
|
api := operations.NewZrokAPI(swaggerSpec)
|
||||||
api.MetadataVersionHandler = metadata.VersionHandlerFunc(func(_ metadata.VersionParams) middleware.Responder {
|
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)
|
||||||
return metadata.NewGetOK().WithPayload(&rest_model.Version{Version: "v0.0.0; sk3tch"})
|
api.IdentityCreateAccountHandler = identity.CreateAccountHandlerFunc(createAccountHandler)
|
||||||
})
|
|
||||||
|
|
||||||
server := rest_zrok_server.NewServer(api)
|
server := rest_zrok_server.NewServer(api)
|
||||||
defer func() { _ = server.Shutdown() }()
|
defer func() { _ = server.Shutdown() }()
|
||||||
@ -40,3 +41,18 @@ func Run(cfg *Config) error {
|
|||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func versionHandler(_ metadata.VersionParams) middleware.Responder {
|
||||||
|
return metadata.NewGetOK().WithPayload(&rest_model.Version{Version: "v0.0.0; sk3tch"})
|
||||||
|
}
|
||||||
|
|
||||||
|
func createAccountHandler(params identity.CreateAccountParams) middleware.Responder {
|
||||||
|
logrus.Infof("received account request for username '%v'", params.Body.Username)
|
||||||
|
apiToken, err := generateApiToken()
|
||||||
|
if err != nil {
|
||||||
|
return middleware.Error(500, err.Error())
|
||||||
|
}
|
||||||
|
return identity.NewCreateAccountCreated().WithPayload(&rest_model.AccountResponse{
|
||||||
|
APIToken: apiToken,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
15
controller/identity.go
Normal file
15
controller/identity.go
Normal file
@ -0,0 +1,15 @@
|
|||||||
|
package controller
|
||||||
|
|
||||||
|
import (
|
||||||
|
"crypto/rand"
|
||||||
|
"encoding/hex"
|
||||||
|
"github.com/pkg/errors"
|
||||||
|
)
|
||||||
|
|
||||||
|
func generateApiToken() (string, error) {
|
||||||
|
bytes := make([]byte, 64)
|
||||||
|
if _, err := rand.Read(bytes); err != nil {
|
||||||
|
return "", errors.Wrap(err, "error generating random api token")
|
||||||
|
}
|
||||||
|
return hex.EncodeToString(bytes), nil
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user