-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #5 from Shackelford-Arden/auth-caching
Adding support for caching Nomad and Consul tokens
- Loading branch information
Showing
22 changed files
with
475 additions
and
98 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
kind: Added | ||
body: caching of Nomad and Consul tokens | ||
time: 2024-04-26T16:51:01.905576593-05:00 | ||
custom: | ||
Author: Shackelford-Arden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
kind: Changed | ||
body: Moved config file into dedicated hctx directory | ||
time: 2024-04-26T16:52:38.373349377-05:00 | ||
custom: | ||
Author: Shackelford-Arden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
kind: Fixed | ||
body: Ensuring use and unset aliases are included in the activate script | ||
time: 2024-04-26T16:50:36.886496648-05:00 | ||
custom: | ||
Author: Shackelford-Arden |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,120 @@ | ||
package cache | ||
|
||
import ( | ||
"encoding/json" | ||
"fmt" | ||
"github.com/Shackelford-Arden/hctx/config" | ||
"github.com/Shackelford-Arden/hctx/models" | ||
"os" | ||
) | ||
|
||
const FilePerms = os.FileMode(0600) | ||
const FileName = "cache.json" | ||
|
||
func CachePath() (string, error) { | ||
|
||
// Get user homedir | ||
userHome, homeErr := os.UserHomeDir() | ||
if homeErr != nil { | ||
return "", fmt.Errorf("failed to get user homedir: %s", homeErr) | ||
} | ||
|
||
return fmt.Sprintf("%s/%s/%s/%s", userHome, config.ConfigParentDir, config.ConfigDir, FileName), nil | ||
} | ||
|
||
type Cache map[string]models.StackCache | ||
|
||
func NewCache(cachePath string) (*Cache, error) { | ||
|
||
if cachePath == "" { | ||
cp, err := CachePath() | ||
if err != nil { | ||
return nil, fmt.Errorf("failed getting cache path: %s", err.Error()) | ||
} | ||
cachePath = cp | ||
} | ||
|
||
cachePathStat, err := os.Stat(cachePath) | ||
if os.IsNotExist(err) { | ||
cacheCreated, createErr := os.Create(cachePath) | ||
if createErr != nil { | ||
return nil, fmt.Errorf("failed to create %s: %s", cachePath, createErr) | ||
} | ||
|
||
emptyCache := []byte("{}") | ||
if _, err := cacheCreated.Write(emptyCache); err != nil { | ||
return nil, fmt.Errorf("failed to write empty cache to %s: %s", cachePath, err) | ||
} | ||
} | ||
|
||
// Set appropriate permissions | ||
if cachePathStat == nil { | ||
cachePathStat, _ = os.Stat(cachePath) | ||
} | ||
|
||
currentPerm := cachePathStat.Mode().Perm() | ||
if currentPerm != FilePerms { | ||
setPermErr := os.Chmod(cachePath, FilePerms) | ||
if setPermErr != nil { | ||
return nil, fmt.Errorf("failed to set permissions on %s: %s", cachePath, setPermErr) | ||
} | ||
} | ||
|
||
cacheFile, _ := os.ReadFile(cachePath) | ||
var cache *Cache | ||
|
||
cacheParseErr := json.Unmarshal(cacheFile, &cache) | ||
if cacheParseErr != nil { | ||
return nil, fmt.Errorf("failed to unmarshal %s: %s", cachePath, cacheParseErr) | ||
} | ||
|
||
return cache, nil | ||
} | ||
|
||
func (c *Cache) Update(stackName string, data models.StackCache) error { | ||
(*c)[stackName] = data | ||
saveErr := c.Save("") | ||
if saveErr != nil { | ||
return fmt.Errorf("failed to update cache: %s", saveErr.Error()) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (c *Cache) Get(stackName string) *models.StackCache { | ||
var cacheStack *models.StackCache | ||
|
||
for name, cache := range *c { | ||
if name == stackName { | ||
cacheStack = &cache | ||
break | ||
} | ||
} | ||
|
||
return cacheStack | ||
} | ||
|
||
func (c *Cache) Save(path string) error { | ||
|
||
cp := path | ||
|
||
if path == "" { | ||
cachePath, err := CachePath() | ||
if err != nil { | ||
return fmt.Errorf("failed getting cache path: %s", err.Error()) | ||
} | ||
cp = cachePath | ||
} | ||
|
||
cacheData, err := json.MarshalIndent(*c, "", " ") | ||
if err != nil { | ||
return fmt.Errorf("failed marshalling cache data: %s", err.Error()) | ||
} | ||
|
||
writeErr := os.WriteFile(cp, cacheData, FilePerms) | ||
if writeErr != nil { | ||
return fmt.Errorf("failed writing cache data to %s: %s", cp, writeErr) | ||
} | ||
|
||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
package cache | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
) | ||
|
||
func TestNewEmptyCache(t *testing.T) { | ||
|
||
cachePath := "testdata/empty-cache.json" | ||
|
||
cache, err := NewCache(cachePath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cache == nil { | ||
t.Fatal("cache is nil when it shouldn't be") | ||
} | ||
} | ||
|
||
func TestCreateNonExistentCacheFile(t *testing.T) { | ||
|
||
tmpCachePath := "/tmp/missing-cache.json" | ||
defer os.Remove(tmpCachePath) | ||
|
||
cache, err := NewCache(tmpCachePath) | ||
if err != nil { | ||
t.Fatal(err) | ||
|
||
} | ||
|
||
// Validate permissions are set correctly | ||
cacheStat, err := os.Stat(tmpCachePath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
if cacheStat.Mode().Perm() != FilePerms { | ||
t.Fatalf("cache file %s has an invalid permissions %d", tmpCachePath, cacheStat.Mode()) | ||
} | ||
|
||
if cache == nil { | ||
t.Fatal("cache should not be nil") | ||
} | ||
} | ||
|
||
func TestValidCache(t *testing.T) { | ||
cachePath := "testdata/valid-cache.json" | ||
|
||
cache, err := NewCache(cachePath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cacheItem := cache.Get("test") | ||
|
||
if cacheItem == nil { | ||
t.Fatal("cache item is nil when it shouldn't be") | ||
} | ||
|
||
if cacheItem.NomadToken != "test-token" && cacheItem.ConsulToken != "" { | ||
t.Fatal("cache item is not valid") | ||
} | ||
} | ||
|
||
func TestMissingCacheItem(t *testing.T) { | ||
cachePath := "testdata/valid-cache.json" | ||
|
||
cache, err := NewCache(cachePath) | ||
if err != nil { | ||
t.Fatal(err) | ||
} | ||
|
||
cacheItem := cache.Get("fake-test") | ||
|
||
if cacheItem != nil { | ||
t.Fatal("cached item should be nil, as fake-test should be missing.") | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1 @@ | ||
{} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,5 @@ | ||
{ | ||
"test": { | ||
"nomad-token": "test-token" | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,22 @@ | ||
package cache | ||
|
||
import ( | ||
"github.com/Shackelford-Arden/hctx/models" | ||
"os" | ||
) | ||
|
||
func GetCacheableValues() models.StackCache { | ||
var currentValues models.StackCache | ||
|
||
nt := os.Getenv(models.NomadToken) | ||
if nt != "" { | ||
currentValues.NomadToken = nt | ||
} | ||
|
||
ct := os.Getenv(models.ConsulToken) | ||
if nt != "" { | ||
currentValues.ConsulToken = ct | ||
} | ||
|
||
return currentValues | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.