From 201982f25f23643fc83ae36181c1207ab714ad5d Mon Sep 17 00:00:00 2001 From: Michael Quigley Date: Thu, 13 Oct 2022 15:15:44 -0400 Subject: [PATCH] better disable cleanup; keep frontend identity around after disable --- cmd/zrok/disable.go | 9 ++++++-- cmd/zrok/enable.go | 2 +- cmd/zrok/http_backend.go | 2 +- zrokdir/zrokdir.go | 44 +++++++++++++++++++++++++--------------- 4 files changed, 37 insertions(+), 20 deletions(-) diff --git a/cmd/zrok/disable.go b/cmd/zrok/disable.go index d13e86e9..d0d7ac3e 100644 --- a/cmd/zrok/disable.go +++ b/cmd/zrok/disable.go @@ -55,11 +55,16 @@ func (cmd *disableCommand) run(_ *cobra.Command, args []string) { } panic(err) } - if err := zrokdir.Delete(); err != nil { + if err := zrokdir.DeleteEnvironment(); err != nil { if !panicInstead { - showError("error removing local zrok directory", err) + showError("error removing zrok environment", err) } panic(err) } + if err := zrokdir.DeleteZitiIdentity("backend"); err != nil { + if !panicInstead { + showError("error removing zrok backend identity", err) + } + } fmt.Printf("zrok environment '%v' disabled for '%v'\n", env.ZitiIdentityId, env.ZrokToken) } diff --git a/cmd/zrok/enable.go b/cmd/zrok/enable.go index 2016b5ec..837e4cd0 100644 --- a/cmd/zrok/enable.go +++ b/cmd/zrok/enable.go @@ -79,7 +79,7 @@ func (cmd *enableCommand) run(_ *cobra.Command, args []string) { } panic(err) } - if err := zrokdir.WriteZitiIdentity("environment", resp.Payload.Cfg); err != nil { + if err := zrokdir.SaveZitiIdentity("backend", resp.Payload.Cfg); err != nil { if !panicInstead { showError("there was an error writing the environment file", err) } diff --git a/cmd/zrok/http_backend.go b/cmd/zrok/http_backend.go index da1e6705..d9c13f98 100644 --- a/cmd/zrok/http_backend.go +++ b/cmd/zrok/http_backend.go @@ -67,7 +67,7 @@ func (self *httpBackendCommand) run(_ *cobra.Command, args []string) { } panic(err) } - zif, err := zrokdir.ZitiIdentityFile("environment") + zif, err := zrokdir.ZitiIdentityFile("backend") if err != nil { ui.Close() if !panicInstead { diff --git a/zrokdir/zrokdir.go b/zrokdir/zrokdir.go index 1331a199..c445427f 100644 --- a/zrokdir/zrokdir.go +++ b/zrokdir/zrokdir.go @@ -14,17 +14,6 @@ type Environment struct { ApiEndpoint string `json:"api_endpoint"` } -func Delete() error { - path, err := zrokDir() - if err != nil { - return err - } - if err := os.RemoveAll(path); err != nil { - return err - } - return nil -} - func LoadEnvironment() (*Environment, error) { ef, err := environmentFile() if err != nil { @@ -59,7 +48,27 @@ func SaveEnvironment(env *Environment) error { return nil } -func WriteZitiIdentity(name, data string) error { +func DeleteEnvironment() error { + ef, err := environmentFile() + if err != nil { + return errors.Wrap(err, "error getting environment file") + } + if err := os.Remove(ef); err != nil { + return errors.Wrap(err, "error removing environment file") + } + + return nil +} + +func ZitiIdentityFile(name string) (string, error) { + zrd, err := zrokDir() + if err != nil { + return "", err + } + return filepath.Join(zrd, "identities", fmt.Sprintf("%v.json", name)), nil +} + +func SaveZitiIdentity(name, data string) error { zif, err := ZitiIdentityFile(name) if err != nil { return err @@ -73,12 +82,15 @@ func WriteZitiIdentity(name, data string) error { return nil } -func ZitiIdentityFile(name string) (string, error) { - zrd, err := zrokDir() +func DeleteZitiIdentity(name string) error { + zif, err := ZitiIdentityFile(name) if err != nil { - return "", err + return errors.Wrapf(err, "error getting ziti identity file path for '%v'", name) } - return filepath.Join(zrd, "identities", fmt.Sprintf("%v.json", name)), nil + if err := os.Remove(zif); err != nil { + return errors.Wrapf(err, "error removing ziti identity file '%v'", zif) + } + return nil } func environmentFile() (string, error) {