mirror of
https://github.com/openziti/zrok.git
synced 2025-02-22 05:01:01 +01:00
zrokdir iteration (#22)
This commit is contained in:
parent
4d85dd26c3
commit
0ace01cb9a
@ -60,10 +60,7 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := zrokdir.WriteToken(token); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := zrokdir.WriteIdentityId(resp.Payload.Identity); err != nil {
|
||||
if err := zrokdir.SaveEnvironment(&zrokdir.Environment{ZrokToken: token, ZitiIdentityId: resp.Payload.Identity}); err != nil {
|
||||
panic(err)
|
||||
}
|
||||
if err := zrokdir.WriteIdentityConfig(resp.Payload.Cfg); err != nil {
|
||||
|
@ -63,20 +63,16 @@ func (self *httpBindCommand) run(_ *cobra.Command, args []string) {
|
||||
IdentityPath: idCfg,
|
||||
EndpointAddress: args[0],
|
||||
}
|
||||
id, err := zrokdir.ReadIdentityId()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
token, err := zrokdir.ReadToken()
|
||||
env, err := zrokdir.LoadEnvironment()
|
||||
if err != nil {
|
||||
panic(err)
|
||||
}
|
||||
|
||||
zrok := newZrokClient()
|
||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", token)
|
||||
auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.ZrokToken)
|
||||
req := tunnel.NewTunnelParams()
|
||||
req.Body = &rest_model_zrok.TunnelRequest{
|
||||
ZitiIdentityID: id,
|
||||
ZitiIdentityID: env.ZitiIdentityId,
|
||||
Endpoint: cfg.EndpointAddress,
|
||||
AuthScheme: string(model.None),
|
||||
}
|
||||
@ -102,7 +98,7 @@ func (self *httpBindCommand) run(_ *cobra.Command, args []string) {
|
||||
signal.Notify(c, os.Interrupt, syscall.SIGTERM)
|
||||
go func() {
|
||||
<-c
|
||||
self.destroy(id, cfg, zrok, auth)
|
||||
self.destroy(env.ZitiIdentityId, cfg, zrok, auth)
|
||||
os.Exit(0)
|
||||
}()
|
||||
|
||||
@ -158,7 +154,7 @@ func (self *httpBindCommand) run(_ *cobra.Command, args []string) {
|
||||
switch e.ID {
|
||||
case "q", "<C-c>":
|
||||
ui.Close()
|
||||
self.destroy(id, cfg, zrok, auth)
|
||||
self.destroy(env.ZitiIdentityId, cfg, zrok, auth)
|
||||
os.Exit(0)
|
||||
}
|
||||
}
|
||||
|
@ -1,52 +1,46 @@
|
||||
package zrokdir
|
||||
|
||||
import (
|
||||
"encoding/json"
|
||||
"github.com/pkg/errors"
|
||||
"github.com/sirupsen/logrus"
|
||||
"os"
|
||||
"path/filepath"
|
||||
)
|
||||
|
||||
func ReadToken() (string, error) {
|
||||
path, err := tokenFile()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
token, err := os.ReadFile(path)
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return string(token), nil
|
||||
type Environment struct {
|
||||
ZrokToken string `json:"zrok_token"`
|
||||
ZitiIdentityId string `json:"ziti_identity"`
|
||||
}
|
||||
|
||||
func WriteToken(token string) error {
|
||||
path, err := tokenFile()
|
||||
func LoadEnvironment() (*Environment, error) {
|
||||
ef, err := environmentFile()
|
||||
if err != nil {
|
||||
return err
|
||||
return nil, errors.Wrap(err, "error getting environment file")
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(token), os.FileMode(400)); err != nil {
|
||||
return err
|
||||
data, err := os.ReadFile(ef)
|
||||
if err != nil {
|
||||
return nil, errors.Wrapf(err, "error reading environment file '%v'", ef)
|
||||
}
|
||||
return nil
|
||||
env := &Environment{}
|
||||
if err := json.Unmarshal(data, env); err != nil {
|
||||
return nil, errors.Wrapf(err, "error unmarshaling environment file '%v'", ef)
|
||||
}
|
||||
return env, nil
|
||||
}
|
||||
|
||||
func ReadIdentityId() (string, error) {
|
||||
path, err := IdentityIdFile()
|
||||
func SaveEnvironment(env *Environment) error {
|
||||
logrus.Infof("saving environment")
|
||||
data, err := json.MarshalIndent(env, "", " ")
|
||||
if err != nil {
|
||||
return "", err
|
||||
return errors.Wrap(err, "error marshaling environment")
|
||||
}
|
||||
id, err := os.ReadFile(path)
|
||||
ef, err := environmentFile()
|
||||
if err != nil {
|
||||
return "", err
|
||||
return errors.Wrap(err, "error getting environment file")
|
||||
}
|
||||
return string(id), nil
|
||||
}
|
||||
|
||||
func WriteIdentityId(id string) error {
|
||||
path, err := IdentityIdFile()
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
if err := os.WriteFile(path, []byte(id), os.FileMode(400)); err != nil {
|
||||
return err
|
||||
if err := os.WriteFile(ef, data, os.FileMode(0600)); err != nil {
|
||||
return errors.Wrap(err, "error saving environment file")
|
||||
}
|
||||
return nil
|
||||
}
|
||||
@ -62,14 +56,6 @@ func WriteIdentityConfig(data string) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
func IdentityIdFile() (string, error) {
|
||||
zrok, err := zrokDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(zrok, "identity.id"), nil
|
||||
}
|
||||
|
||||
func IdentityConfigFile() (string, error) {
|
||||
zrok, err := zrokDir()
|
||||
if err != nil {
|
||||
@ -78,12 +64,12 @@ func IdentityConfigFile() (string, error) {
|
||||
return filepath.Join(zrok, "identity.json"), nil
|
||||
}
|
||||
|
||||
func tokenFile() (string, error) {
|
||||
zrok, err := zrokDir()
|
||||
func environmentFile() (string, error) {
|
||||
zrd, err := zrokDir()
|
||||
if err != nil {
|
||||
return "", err
|
||||
}
|
||||
return filepath.Join(zrok, "token"), nil
|
||||
return filepath.Join(zrd, "environment.json"), nil
|
||||
}
|
||||
|
||||
func zrokDir() (string, error) {
|
||||
|
Loading…
Reference in New Issue
Block a user