more zrokdir refinement; identities (#22)

This commit is contained in:
Michael Quigley 2022-08-23 10:43:24 -04:00
parent 0ace01cb9a
commit 3bde4594db
No known key found for this signature in database
GPG Key ID: 9B60314A9DD20A62
3 changed files with 21 additions and 16 deletions

View File

@ -63,7 +63,7 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) {
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 {
if err := zrokdir.WriteZitiIdentity("environment", resp.Payload.Cfg); err != nil {
panic(err)
}
logrus.Infof("enabled, identity = '%v'", resp.Payload.Identity)

View File

@ -55,18 +55,18 @@ func (self *httpBindCommand) run(_ *cobra.Command, args []string) {
tb.SetInputMode(tb.InputEsc)
}
idCfg, err := zrokdir.IdentityConfigFile()
env, err := zrokdir.LoadEnvironment()
if err != nil {
panic(err)
}
zif, err := zrokdir.ZitiIdentityFile("environment")
if err != nil {
panic(err)
}
cfg := &bind.Config{
IdentityPath: idCfg,
IdentityPath: zif,
EndpointAddress: args[0],
}
env, err := zrokdir.LoadEnvironment()
if err != nil {
panic(err)
}
zrok := newZrokClient()
auth := httptransport.APIKeyAuth("X-TOKEN", "header", env.ZrokToken)

View File

@ -2,8 +2,8 @@ package zrokdir
import (
"encoding/json"
"fmt"
"github.com/pkg/errors"
"github.com/sirupsen/logrus"
"os"
"path/filepath"
)
@ -30,7 +30,6 @@ func LoadEnvironment() (*Environment, error) {
}
func SaveEnvironment(env *Environment) error {
logrus.Infof("saving environment")
data, err := json.MarshalIndent(env, "", " ")
if err != nil {
return errors.Wrap(err, "error marshaling environment")
@ -39,29 +38,35 @@ func SaveEnvironment(env *Environment) error {
if err != nil {
return errors.Wrap(err, "error getting environment file")
}
if err := os.MkdirAll(filepath.Dir(ef), os.FileMode(0700)); err != nil {
return errors.Wrapf(err, "error creating zrokdir path '%v'", filepath.Dir(ef))
}
if err := os.WriteFile(ef, data, os.FileMode(0600)); err != nil {
return errors.Wrap(err, "error saving environment file")
}
return nil
}
func WriteIdentityConfig(data string) error {
path, err := IdentityConfigFile()
func WriteZitiIdentity(name, data string) error {
zif, err := ZitiIdentityFile(name)
if err != nil {
return err
}
if err := os.WriteFile(path, []byte(data), os.FileMode(400)); err != nil {
return err
if err := os.MkdirAll(filepath.Dir(zif), os.FileMode(0700)); err != nil {
return errors.Wrapf(err, "error creating zrokdir path '%v'", filepath.Dir(zif))
}
if err := os.WriteFile(zif, []byte(data), os.FileMode(0600)); err != nil {
return errors.Wrapf(err, "error writing ziti identity file '%v'", zif)
}
return nil
}
func IdentityConfigFile() (string, error) {
zrok, err := zrokDir()
func ZitiIdentityFile(name string) (string, error) {
zrd, err := zrokDir()
if err != nil {
return "", err
}
return filepath.Join(zrok, "identity.json"), nil
return filepath.Join(zrd, "identities", fmt.Sprintf("%v.json", name)), nil
}
func environmentFile() (string, error) {