mirror of
https://github.com/pomerium/pomerium.git
synced 2025-05-03 20:36:03 +02:00
* refactor backend, implement encrypted store * refactor in-memory store * wip * wip * wip * add syncer test * fix redis expiry * fix linting issues * fix test by skipping non-config records * fix backoff import * fix init issues * fix query * wait for initial sync before starting directory sync * add type to SyncLatest * add more log messages, fix deadlock in in-memory store, always return server version from SyncLatest * update sync types and tests * add redis tests * skip macos in github actions * add comments to proto * split getBackend into separate methods * handle errors in initVersion * return different error for not found vs other errors in get * use exponential backoff for redis transaction retry * rename raw to result * use context instead of close channel * store type urls as constants in databroker * use timestampb instead of ptypes * fix group merging not waiting * change locked names * update GetAll to return latest record version * add method to grpcutil to get the type url for a protobuf type
96 lines
1.8 KiB
Go
96 lines
1.8 KiB
Go
package main
|
|
|
|
import (
|
|
"crypto/sha256"
|
|
"encoding/hex"
|
|
"encoding/json"
|
|
"fmt"
|
|
"os"
|
|
"path/filepath"
|
|
"time"
|
|
)
|
|
|
|
func configHome() string {
|
|
cfgDir, err := os.UserConfigDir()
|
|
if err != nil {
|
|
fatalf("error getting user config dir: %v", err)
|
|
}
|
|
|
|
ch := filepath.Join(cfgDir, "pomerium-cli")
|
|
err = os.MkdirAll(ch, 0o755)
|
|
if err != nil {
|
|
fatalf("error creating user config dir: %v", err)
|
|
}
|
|
|
|
return ch
|
|
}
|
|
|
|
func cachePath() string {
|
|
return filepath.Join(configHome(), "cache", "exec-credential")
|
|
}
|
|
|
|
func cachedCredentialPath(serverURL string) string {
|
|
h := sha256.New()
|
|
_, _ = h.Write([]byte(serverURL))
|
|
id := hex.EncodeToString(h.Sum(nil))
|
|
return filepath.Join(cachePath(), id+".json")
|
|
}
|
|
|
|
func loadCachedCredential(serverURL string) *ExecCredential {
|
|
fn := cachedCredentialPath(serverURL)
|
|
|
|
f, err := os.Open(fn)
|
|
if err != nil {
|
|
return nil
|
|
}
|
|
defer func() { _ = f.Close() }()
|
|
|
|
var creds ExecCredential
|
|
err = json.NewDecoder(f).Decode(&creds)
|
|
if err != nil {
|
|
_ = os.Remove(fn)
|
|
return nil
|
|
}
|
|
|
|
if creds.Status == nil {
|
|
_ = os.Remove(fn)
|
|
return nil
|
|
}
|
|
|
|
ts := creds.Status.ExpirationTimestamp
|
|
if ts.IsZero() || ts.Before(time.Now()) {
|
|
_ = os.Remove(fn)
|
|
return nil
|
|
}
|
|
|
|
return &creds
|
|
}
|
|
|
|
func saveCachedCredential(serverURL string, creds *ExecCredential) {
|
|
fn := cachedCredentialPath(serverURL)
|
|
|
|
err := os.MkdirAll(filepath.Dir(fn), 0o755)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to create cache directory: %v", err)
|
|
return
|
|
}
|
|
|
|
f, err := os.Create(fn)
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to create cache file: %v", err)
|
|
return
|
|
}
|
|
|
|
err = json.NewEncoder(f).Encode(creds)
|
|
if err != nil {
|
|
_ = f.Close()
|
|
fmt.Fprintf(os.Stderr, "failed to encode credentials to cache file: %v", err)
|
|
return
|
|
}
|
|
|
|
err = f.Close()
|
|
if err != nil {
|
|
fmt.Fprintf(os.Stderr, "failed to close cache file: %v", err)
|
|
return
|
|
}
|
|
}
|