zrokdir iteration (#22)

This commit is contained in:
Michael Quigley 2022-08-22 16:32:50 -04:00
parent 4d85dd26c3
commit 0ace01cb9a
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 34 additions and 55 deletions

View File

@ -60,10 +60,7 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
if err != nil { if err != nil {
panic(err) panic(err)
} }
if err := zrokdir.WriteToken(token); err != nil { if err := zrokdir.SaveEnvironment(&zrokdir.Environment{ZrokToken: token, ZitiIdentityId: resp.Payload.Identity}); err != nil {
panic(err)
}
if err := zrokdir.WriteIdentityId(resp.Payload.Identity); err != nil {
panic(err) panic(err)
} }
if err := zrokdir.WriteIdentityConfig(resp.Payload.Cfg); err != nil { if err := zrokdir.WriteIdentityConfig(resp.Payload.Cfg); err != nil {

View File

@ -63,20 +63,16 @@ func (self *httpBindCommand) run(_ *cobra.Command, args []string) {
IdentityPath: idCfg, IdentityPath: idCfg,
EndpointAddress: args[0], EndpointAddress: args[0],
} }
id, err := zrokdir.ReadIdentityId() env, err := zrokdir.LoadEnvironment()
if err != nil {
panic(err)
}
token, err := zrokdir.ReadToken()
if err != nil { if err != nil {
panic(err) panic(err)
} }
zrok := newZrokClient() zrok := newZrokClient()
auth := httptransport.APIKeyAuth("X-TOKEN", "header", token) auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.ZrokToken)
req := tunnel.NewTunnelParams() req := tunnel.NewTunnelParams()
req.Body = &rest_model_zrok.TunnelRequest{ req.Body = &rest_model_zrok.TunnelRequest{
ZitiIdentityID: id, ZitiIdentityID: env.ZitiIdentityId,
Endpoint: cfg.EndpointAddress, Endpoint: cfg.EndpointAddress,
AuthScheme: string(model.None), AuthScheme: string(model.None),
} }
@ -102,7 +98,7 @@ func (self *httpBindCommand) run(_ *cobra.Command, args []string) {
signal.Notify(c, os.Interrupt, syscall.SIGTERM) signal.Notify(c, os.Interrupt, syscall.SIGTERM)
go func() { go func() {
<-c <-c
self.destroy(id, cfg, zrok, auth) self.destroy(env.ZitiIdentityId, cfg, zrok, auth)
os.Exit(0) os.Exit(0)
}() }()
@ -158,7 +154,7 @@ func (self *httpBindCommand) run(_ *cobra.Command, args []string) {
switch e.ID { switch e.ID {
case "q", "<C-c>": case "q", "<C-c>":
ui.Close() ui.Close()
self.destroy(id, cfg, zrok, auth) self.destroy(env.ZitiIdentityId, cfg, zrok, auth)
os.Exit(0) os.Exit(0)
} }
} }

View File

@ -1,52 +1,46 @@
package zrokdir package zrokdir
import ( import (
"encoding/json"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os" "os"
"path/filepath" "path/filepath"
) )
func ReadToken() (string, error) { type Environment struct {
path, err := tokenFile() ZrokToken string `json:"zrok_token"`
if err != nil { ZitiIdentityId string `json:"ziti_identity"`
return "", err
}
token, err := os.ReadFile(path)
if err != nil {
return "", err
}
return string(token), nil
} }
func WriteToken(token string) error { func LoadEnvironment() (*Environment, error) {
path, err := tokenFile() ef, err := environmentFile()
if err != nil { 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 { data, err := os.ReadFile(ef)
return err 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) { func SaveEnvironment(env *Environment) error {
path, err := IdentityIdFile() logrus.Infof("saving environment")
data, err := json.MarshalIndent(env, "", " ")
if err != nil { if err != nil {
return "", err return errors.Wrap(err, "error marshaling environment")
} }
id, err := os.ReadFile(path) ef, err := environmentFile()
if err != nil { if err != nil {
return "", err return errors.Wrap(err, "error getting environment file")
} }
return string(id), nil if err := os.WriteFile(ef, data, os.FileMode(0600)); err != nil {
} return errors.Wrap(err, "error saving environment file")
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
} }
return nil return nil
} }
@ -62,14 +56,6 @@ func WriteIdentityConfig(data string) error {
return nil 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) { func IdentityConfigFile() (string, error) {
zrok, err := zrokDir() zrok, err := zrokDir()
if err != nil { if err != nil {
@ -78,12 +64,12 @@ func IdentityConfigFile() (string, error) {
return filepath.Join(zrok, "identity.json"), nil return filepath.Join(zrok, "identity.json"), nil
} }
func tokenFile() (string, error) { func environmentFile() (string, error) {
zrok, err := zrokDir() zrd, err := zrokDir()
if err != nil { if err != nil {
return "", err return "", err
} }
return filepath.Join(zrok, "token"), nil return filepath.Join(zrd, "environment.json"), nil
} }
func zrokDir() (string, error) { func zrokDir() (string, error) {