Skip to content

Commit

Permalink
Setting Use to clear out ACL tokens when switching stacks (#10)
Browse files Browse the repository at this point in the history
  • Loading branch information
Shackelford-Arden authored Jul 12, 2024
1 parent c805513 commit b4dbbbf
Show file tree
Hide file tree
Showing 3 changed files with 40 additions and 4 deletions.
5 changes: 5 additions & 0 deletions .changes/unreleased/Added-20240711-233913.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
kind: Added
body: Set Use to clear previous ACL tokens when called to avoid conflicts
time: 2024-07-11T23:39:13.520023-05:00
custom:
Author: Shackelford-Arden
18 changes: 14 additions & 4 deletions cmd/use.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,14 @@ import (
"fmt"

"github.com/Shackelford-Arden/hctx/cache"
"github.com/Shackelford-Arden/hctx/models"
"github.com/urfave/cli/v2"
)

// Use sets the appropriate environment variables to use
// the selected Stack.
// If caching is disabled, this will also unset any
// existing env vars for tokens (ie NOMAD_TOKEN).
func Use(ctx *cli.Context) error {

stackName := ctx.Args().First()
Expand All @@ -18,7 +23,7 @@ func Use(ctx *cli.Context) error {
}

currentStack := AppConfig.GetCurrentStack()
// Get current stacks tokens, if any and cache them
// If caching is enabled, set to cache.
if currentStack != nil && AppConfig.CacheAuth {
toCache := cache.GetCacheableValues()
updateErr := AppCache.Update(currentStack.Name, toCache)
Expand All @@ -27,10 +32,15 @@ func Use(ctx *cli.Context) error {
}
}

// rehydrate env w/ new stack cache, if present
newStackCache := AppCache.Get(selectedStack.Name)
useOut := unsetTokens(AppConfig.Shell)
var stackCache *models.StackCache
if AppConfig.CacheAuth {
stackCache = AppCache.Get(selectedStack.Name)
}

useOut += selectedStack.Use(AppConfig.Shell, stackCache, AppConfig.CacheAuth)

fmt.Print(selectedStack.Use(AppConfig.Shell, newStackCache, AppConfig.CacheAuth))
fmt.Println(useOut)

return nil
}
21 changes: 21 additions & 0 deletions cmd/utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package cmd

func unsetTokens(shell string) string {

output := ""

switch shell {
case "bash":
fallthrough
case "zsh":
fallthrough
default:
output = `
unset NOMAD_TOKEN
unset CONSUL_TOKEN
unset VAULT_TOKEN
`
}

return output
}

0 comments on commit b4dbbbf

Please sign in to comment.