rough 'create account' api skeleton

This commit is contained in:
Michael Quigley 2022-07-25 13:17:52 -04:00
parent 53b7c3c7b7
commit 64e2864fab
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
7 changed files with 128 additions and 32 deletions

View File

@ -1,10 +1,6 @@
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"
"github.com/openziti-test-kitchen/zrok/rest_zrok_client/metadata"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
@ -21,13 +17,10 @@ var apiCmd = &cobra.Command{
}
var apiVersionCmd = &cobra.Command{
Use: "version <endpoint>",
Use: "version",
Short: "Get API version",
Run: func(_ *cobra.Command, args []string) {
transport := httptransport.New(args[0], "", nil)
transport.Producers["application/zrok.v1+json"] = runtime.JSONProducer()
transport.Consumers["application/zrok.v1+json"] = runtime.JSONConsumer()
zrok := rest_zrok_client.New(transport, strfmt.Default)
zrok := newZrokClient()
resp, err := zrok.Metadata.Version(metadata.NewVersionParams())
if err != nil {
panic(err)

15
cmd/zrok/client.go Normal file
View 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
View 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
View 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)
},
}

View File

@ -2,8 +2,6 @@ package main
import (
"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/proxy"
"github.com/sirupsen/logrus"
@ -16,7 +14,7 @@ import (
func init() {
pfxlog.GlobalInit(logrus.InfoLevel, pfxlog.DefaultOptions().SetTrimPrefix("github.com/openziti-test-kitchen/"))
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(proxyCmd)
}
@ -31,23 +29,7 @@ var rootCmd = &cobra.Command{
},
}
var verbose bool
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 endpoint string
var httpCmd = &cobra.Command{
Use: "http <identity>",

View File

@ -7,8 +7,10 @@ import (
"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/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/pkg/errors"
"github.com/sirupsen/logrus"
)
var str *store.Store
@ -26,9 +28,8 @@ func Run(cfg *Config) error {
}
api := operations.NewZrokAPI(swaggerSpec)
api.MetadataVersionHandler = metadata.VersionHandlerFunc(func(_ metadata.VersionParams) middleware.Responder {
return metadata.NewGetOK().WithPayload(&rest_model.Version{Version: "v0.0.0; sk3tch"})
})
api.MetadataVersionHandler = metadata.VersionHandlerFunc(versionHandler)
api.IdentityCreateAccountHandler = identity.CreateAccountHandlerFunc(createAccountHandler)
server := rest_zrok_server.NewServer(api)
defer func() { _ = server.Shutdown() }()
@ -40,3 +41,18 @@ func Run(cfg *Config) error {
}
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
View 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
}