Skip to content

Commit

Permalink
Merge pull request #382 from openziti/v0.4_zrok_sdk
Browse files Browse the repository at this point in the history
zrok SDK (#34, #379)
  • Loading branch information
michaelquigley authored Jul 31, 2023
2 parents 992549f + 6893a53 commit 2cc6e95
Show file tree
Hide file tree
Showing 66 changed files with 2,031 additions and 814 deletions.
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

FEATURE: New `zrok overview` command, which returns all of the account details as a single JSON structure. See the OpenAPI spec at `specs/zrok.yml` for more details of the `/api/v1/overview` endpoint (https://github.com/openziti/zrok/issues/374)

FEATURE: New `zrok` SDK (https://github.com/openziti/zrok/issues/34). `pastebin` example illustrates basic SDK usage (see `sdk/examples/pastebin/README.md` for details) ((https://github.com/openziti/zrok/issues/379)

# v0.4.2

Some days are just like this. `v0.4.2` is a re-do of `v0.4.1`. Trying to get Homebrew working and had a bad release. Hopefully this is the one.
Expand Down
28 changes: 14 additions & 14 deletions cmd/zrok/accessPrivate.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,11 +8,11 @@ import (
"github.com/openziti/zrok/endpoints/proxy"
"github.com/openziti/zrok/endpoints/tcpTunnel"
"github.com/openziti/zrok/endpoints/udpTunnel"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok"
"github.com/openziti/zrok/rest_client_zrok/share"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/tui"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"net/url"
Expand Down Expand Up @@ -48,28 +48,28 @@ func newAccessPrivateCommand() *accessPrivateCommand {
func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
shrToken := args[0]

zrd, err := zrokdir.Load()
env, err := environment.LoadRoot()
if err != nil {
tui.Error("unable to load zrokdir", err)
tui.Error("error loading environment", err)
}

if zrd.Env == nil {
if !env.IsEnabled() {
tui.Error("unable to load environment; did you 'zrok enable'?", nil)
}

zrok, err := zrd.Client()
zrok, err := env.Client()
if err != nil {
if !panicInstead {
tui.Error("unable to create zrok client", err)
}
panic(err)
}

auth := httptransport.APIKeyAuth("X-TOKEN", "header", zrd.Env.Token)
auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.Environment().Token)
req := share.NewAccessParams()
req.Body = &rest_model_zrok.AccessRequest{
ShrToken: shrToken,
EnvZID: zrd.Env.ZId,
EnvZID: env.Environment().ZitiIdentity,
}
accessResp, err := zrok.Share.Access(req, auth)
if err != nil {
Expand Down Expand Up @@ -101,20 +101,20 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
case "tcpTunnel":
fe, err := tcpTunnel.NewFrontend(&tcpTunnel.FrontendConfig{
BindAddress: cmd.bindAddress,
IdentityName: "backend",
IdentityName: env.EnvironmentIdentityName(),
ShrToken: args[0],
RequestsChan: requests,
})
if err != nil {
if !panicInstead {
tui.Error("unable to create private frontend", err)
tui.Error("unable to create private access", err)
}
panic(err)
}
go func() {
if err := fe.Run(); err != nil {
if !panicInstead {
tui.Error("error starting frontend", err)
tui.Error("error starting access", err)
}
panic(err)
}
Expand All @@ -123,7 +123,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
case "udpTunnel":
fe, err := udpTunnel.NewFrontend(&udpTunnel.FrontendConfig{
BindAddress: cmd.bindAddress,
IdentityName: "backend",
IdentityName: env.EnvironmentIdentityName(),
ShrToken: args[0],
RequestsChan: requests,
IdleTime: time.Minute,
Expand All @@ -144,7 +144,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
}()

default:
cfg := proxy.DefaultFrontendConfig("backend")
cfg := proxy.DefaultFrontendConfig(env.EnvironmentIdentityName())
cfg.ShrToken = shrToken
cfg.Address = cmd.bindAddress
cfg.RequestsChan = requests
Expand All @@ -168,7 +168,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() {
<-c
cmd.destroy(accessResp.Payload.FrontendToken, zrd.Env.ZId, shrToken, zrok, auth)
cmd.destroy(accessResp.Payload.FrontendToken, env.Environment().ZitiIdentity, shrToken, zrok, auth)
os.Exit(0)
}()

Expand Down Expand Up @@ -203,7 +203,7 @@ func (cmd *accessPrivateCommand) run(_ *cobra.Command, args []string) {
}

close(requests)
cmd.destroy(accessResp.Payload.FrontendToken, zrd.Env.ZId, shrToken, zrok, auth)
cmd.destroy(accessResp.Payload.FrontendToken, env.Environment().ZitiIdentity, shrToken, zrok, auth)
}
}

Expand Down
4 changes: 1 addition & 3 deletions cmd/zrok/adminBootstrap.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ func init() {

type adminBootstrap struct {
cmd *cobra.Command
skipCtrl bool
skipFrontend bool
}

Expand All @@ -26,7 +25,6 @@ func newAdminBootstrap() *adminBootstrap {
}
command := &adminBootstrap{cmd: cmd}
cmd.Run = command.run
cmd.Flags().BoolVar(&command.skipCtrl, "skip-ctrl", false, "Skip controller (ctrl) identity bootstrapping")
cmd.Flags().BoolVar(&command.skipFrontend, "skip-frontend", false, "Skip frontend identity bootstrapping")
return command
}
Expand All @@ -38,7 +36,7 @@ func (cmd *adminBootstrap) run(_ *cobra.Command, args []string) {
panic(err)
}
logrus.Infof(cf.Dump(inCfg, cf.DefaultOptions()))
if err := controller.Bootstrap(cmd.skipCtrl, cmd.skipFrontend, inCfg); err != nil {
if err := controller.Bootstrap(cmd.skipFrontend, inCfg); err != nil {
panic(err)
}
logrus.Info("bootstrap complete!")
Expand Down
9 changes: 4 additions & 5 deletions cmd/zrok/adminCreateFrontend.go
Original file line number Diff line number Diff line change
@@ -1,14 +1,13 @@
package main

import (
"os"

"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/tui"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
)

func init() {
Expand All @@ -35,12 +34,12 @@ func (cmd *adminCreateFrontendCommand) run(_ *cobra.Command, args []string) {
publicName := args[1]
urlTemplate := args[2]

zrd, err := zrokdir.Load()
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}

zrok, err := zrd.Client()
zrok, err := env.Client()
if err != nil {
panic(err)
}
Expand Down
17 changes: 8 additions & 9 deletions cmd/zrok/adminCreateIdentity.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@ package main

import (
"fmt"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
"os"
Expand Down Expand Up @@ -32,7 +32,11 @@ func newAdminCreateIdentity() *adminCreateIdentity {
func (cmd *adminCreateIdentity) run(_ *cobra.Command, args []string) {
name := args[0]

zif, err := zrokdir.ZitiIdentityFile(name)
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}
zif, err := env.ZitiIdentityNamed(name)
if err != nil {
panic(err)
}
Expand All @@ -41,12 +45,7 @@ func (cmd *adminCreateIdentity) run(_ *cobra.Command, args []string) {
os.Exit(1)
}

zrd, err := zrokdir.Load()
if err != nil {
panic(err)
}

zrok, err := zrd.Client()
zrok, err := env.Client()
if err != nil {
panic(err)
}
Expand All @@ -59,7 +58,7 @@ func (cmd *adminCreateIdentity) run(_ *cobra.Command, args []string) {
panic(err)
}

if err := zrokdir.SaveZitiIdentity(name, resp.Payload.Cfg); err != nil {
if err := env.SaveZitiIdentityNamed(name, resp.Payload.Cfg); err != nil {
panic(err)
}

Expand Down
6 changes: 3 additions & 3 deletions cmd/zrok/adminDeleteFrontend.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand All @@ -30,12 +30,12 @@ func newAdminDeleteFrontendCommand() *adminDeleteFrontendCommand {
func (cmd *adminDeleteFrontendCommand) run(_ *cobra.Command, args []string) {
feToken := args[0]

zrd, err := zrokdir.Load()
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}

zrok, err := zrd.Client()
zrok, err := env.Client()
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/zrok/adminGenerate.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@ package main
import (
"fmt"
"github.com/jaevor/go-nanoid"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -43,12 +43,12 @@ func (cmd *adminGenerateCommand) run(_ *cobra.Command, args []string) {
}
}

zrd, err := zrokdir.Load()
env, err := environment.LoadRoot()
if err != nil {
logrus.Error("error loading zrokdir", err)
logrus.Error("error loading environment", err)
}

zrok, err := zrd.Client()
zrok, err := env.Client()
if err != nil {
if !panicInstead {
logrus.Error("error creating zrok api client", err)
Expand Down
6 changes: 3 additions & 3 deletions cmd/zrok/adminListFrontends.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,8 @@ package main
import (
"fmt"
"github.com/jedib0t/go-pretty/v6/table"
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/openziti/zrok/zrokdir"
"github.com/spf13/cobra"
"os"
"time"
Expand All @@ -31,12 +31,12 @@ func newAdminListFrontendsCommand() *adminListFrontendsCommand {
}

func (cmd *adminListFrontendsCommand) run(_ *cobra.Command, _ []string) {
zrd, err := zrokdir.Load()
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}

zrok, err := zrd.Client()
zrok, err := env.Client()
if err != nil {
panic(err)
}
Expand Down
6 changes: 3 additions & 3 deletions cmd/zrok/adminUpdateFrontend.go
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
package main

import (
"github.com/openziti/zrok/environment"
"github.com/openziti/zrok/rest_client_zrok/admin"
"github.com/openziti/zrok/rest_model_zrok"
"github.com/openziti/zrok/zrokdir"
"github.com/sirupsen/logrus"
"github.com/spf13/cobra"
)
Expand Down Expand Up @@ -38,12 +38,12 @@ func (cmd *adminUpdateFrontendCommand) run(_ *cobra.Command, args []string) {
panic("must specify at least one of public name or url template")
}

zrd, err := zrokdir.Load()
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}

zrok, err := zrd.Client()
zrok, err := env.Client()
if err != nil {
panic(err)
}
Expand Down
8 changes: 4 additions & 4 deletions cmd/zrok/configGet.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package main

import (
"fmt"
"github.com/openziti/zrok/zrokdir"
"github.com/openziti/zrok/environment"
"github.com/spf13/cobra"
)

Expand All @@ -28,15 +28,15 @@ func newConfigGetCommand() *configGetCommand {
func (cmd *configGetCommand) run(_ *cobra.Command, args []string) {
configName := args[0]

zrd, err := zrokdir.Load()
env, err := environment.LoadRoot()
if err != nil {
panic(err)
}

switch configName {
case "apiEndpoint":
if zrd.Cfg != nil && zrd.Cfg.ApiEndpoint != "" {
fmt.Printf("apiEndpoint = %v\n", zrd.Cfg.ApiEndpoint)
if env.Config() != nil && env.Config().ApiEndpoint != "" {
fmt.Printf("apiEndpoint = %v\n", env.Config().ApiEndpoint)
} else {
fmt.Println("apiEndpoint = <unset>")
}
Expand Down
Loading

1 comment on commit 2cc6e95

@vercel
Copy link

@vercel vercel bot commented on 2cc6e95 Jul 31, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Successfully deployed to the following URLs:

zrok – ./

zrok.vercel.app
zrok-openziti.vercel.app
zrok-git-main-openziti.vercel.app

Please sign in to comment.