Skip to content

Commit

Permalink
fix: scope the env cache to the rest config
Browse files Browse the repository at this point in the history
  • Loading branch information
adityathebe authored and moshloop committed Jan 14, 2025
1 parent ffab61a commit ec13cc7
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 4 deletions.
18 changes: 18 additions & 0 deletions context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"time"

commons "github.com/flanksource/commons/context"
"github.com/flanksource/commons/hash"
"github.com/flanksource/commons/logger"
dutyGorm "github.com/flanksource/duty/gorm"
dutyKubernetes "github.com/flanksource/duty/kubernetes"
Expand Down Expand Up @@ -341,6 +342,23 @@ func (k Context) Pool() *pgxpool.Pool {

}

// KubeAuthFingerprint generates a unique SHA-256 hash to identify the Kubernetes API server
// and client authentication details from the REST configuration.
func (k *Context) KubeAuthFingerprint() string {
rs := k.KubernetesRestConfig()
if rs == nil {
return ""
}

return hash.Sha256Hex(fmt.Sprintf("%s/%s/%s/%s/%s/%s",
rs.Host,
rs.Username,
rs.Password,
rs.BearerToken,
rs.BearerTokenFile,
rs.TLSClientConfig.CertData))
}

func (k *Context) Kubernetes() kubernetes.Interface {
v, ok := k.Value("kubernetes").(kubernetes.Interface)
if !ok || v == nil {
Expand Down
8 changes: 4 additions & 4 deletions context/envvar.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func GetEnvStringFromCache(ctx Context, env string, namespace string) (string, e
}

func GetHelmValueFromCache(ctx Context, namespace, releaseName, key string) (string, error) {
id := fmt.Sprintf("helm/%s/%s/%s", namespace, releaseName, key)
id := fmt.Sprintf("helm/%s/%s/%s/%s", ctx.KubeAuthFingerprint(), namespace, releaseName, key)
if value, found := envCache.Get(id); found {
return value.(string), nil
}
Expand Down Expand Up @@ -156,7 +156,7 @@ func GetHelmValueFromCache(ctx Context, namespace, releaseName, key string) (str
}

func GetSecretFromCache(ctx Context, namespace, name, key string) (string, error) {
id := fmt.Sprintf("secret/%s/%s/%s", namespace, name, key)
id := fmt.Sprintf("secret/%s/%s/%s/%s", ctx.KubeAuthFingerprint(), namespace, name, key)
if value, found := envCache.Get(id); found {
return value.(string), nil
}
Expand All @@ -180,7 +180,7 @@ func GetSecretFromCache(ctx Context, namespace, name, key string) (string, error
}

func GetConfigMapFromCache(ctx Context, namespace, name, key string) (string, error) {
id := fmt.Sprintf("cm/%s/%s/%s", namespace, name, key)
id := fmt.Sprintf("cm/%s/%s/%s/%s", ctx.KubeAuthFingerprint(), namespace, name, key)
if value, found := envCache.Get(id); found {
return value.(string), nil
}
Expand All @@ -202,7 +202,7 @@ func GetConfigMapFromCache(ctx Context, namespace, name, key string) (string, er
}

func GetServiceAccountTokenFromCache(ctx Context, namespace, serviceAccount string) (string, error) {
id := fmt.Sprintf("sa-token/%s/%s", namespace, serviceAccount)
id := fmt.Sprintf("sa-token/%s/%s/%s", ctx.KubeAuthFingerprint(), namespace, serviceAccount)
if value, found := envCache.Get(id); found {
return value.(string), nil
}
Expand Down

0 comments on commit ec13cc7

Please sign in to comment.