k8s: add flush-credentials command (#2379)

* k8s: add flush-credentials command

* Update cmd/pomerium-cli/kubernetes.go

Co-authored-by: Travis Groth <travisgroth@users.noreply.github.com>

Co-authored-by: Travis Groth <travisgroth@users.noreply.github.com>
This commit is contained in:
Caleb Doxsey 2021-07-20 15:51:55 -06:00 committed by GitHub
parent 8a74fae2e7
commit 8be71800c4
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
2 changed files with 33 additions and 0 deletions

View file

@ -5,6 +5,7 @@ import (
"encoding/hex" "encoding/hex"
"encoding/json" "encoding/json"
"fmt" "fmt"
"io/fs"
"os" "os"
"path/filepath" "path/filepath"
"time" "time"
@ -36,6 +37,25 @@ func cachedCredentialPath(serverURL string) string {
return filepath.Join(cachePath(), id+".json") return filepath.Join(cachePath(), id+".json")
} }
func clearAllCachedCredentials() {
_ = filepath.Walk(cachePath(), func(p string, fi fs.FileInfo, err error) error {
if err != nil {
return err
}
if fi.IsDir() {
return nil
}
return os.Remove(p)
})
}
func clearCachedCredential(serverURL string) {
fn := cachedCredentialPath(serverURL)
_ = os.Remove(fn)
}
func loadCachedCredential(serverURL string) *ExecCredential { func loadCachedCredential(serverURL string) *ExecCredential {
fn := cachedCredentialPath(serverURL) fn := cachedCredentialPath(serverURL)

View file

@ -18,6 +18,7 @@ import (
func init() { func init() {
addTLSFlags(kubernetesExecCredentialCmd) addTLSFlags(kubernetesExecCredentialCmd)
kubernetesCmd.AddCommand(kubernetesExecCredentialCmd) kubernetesCmd.AddCommand(kubernetesExecCredentialCmd)
kubernetesCmd.AddCommand(kubernetesFlushCredentialsCmd)
rootCmd.AddCommand(kubernetesCmd) rootCmd.AddCommand(kubernetesCmd)
} }
@ -25,6 +26,18 @@ var kubernetesCmd = &cobra.Command{
Use: "k8s", Use: "k8s",
} }
var kubernetesFlushCredentialsCmd = &cobra.Command{
Use: "flush-credentials [API Server URL]",
RunE: func(cmd *cobra.Command, args []string) error {
if len(args) == 0 {
clearAllCachedCredentials()
} else {
clearCachedCredential(args[0])
}
return nil
},
}
var kubernetesExecCredentialCmd = &cobra.Command{ var kubernetesExecCredentialCmd = &cobra.Command{
Use: "exec-credential", Use: "exec-credential",
RunE: func(cmd *cobra.Command, args []string) error { RunE: func(cmd *cobra.Command, args []string) error {