diff --git a/context/envvar.go b/context/envvar.go index 1150a146..7286cb83 100644 --- a/context/envvar.go +++ b/context/envvar.go @@ -16,6 +16,9 @@ import ( var envCache = cache.New(5*time.Minute, 10*time.Minute) func GetEnvValueFromCache(ctx Context, input types.EnvVar, namespace string) (string, error) { + if namespace == "" { + namespace = ctx.GetNamespace() + } if input.ValueFrom == nil { return input.ValueStatic, nil } diff --git a/models/connections.go b/models/connections.go index b2265e09..acd65731 100644 --- a/models/connections.go +++ b/models/connections.go @@ -105,6 +105,62 @@ func (c Connection) AsMap(removeFields ...string) map[string]any { return asMap(c, removeFields...) } +func (c Connection) Merge(ctx types.GetEnvVarFromCache, from any) (*Connection, error) { + if v, ok := from.(types.WithUsernamePassword); ok { + username := v.GetUsername() + if !username.IsEmpty() { + val, err := ctx.GetEnvValueFromCache(username, "") + if err != nil { + return nil, err + } + c.Username = val + } + password := v.GetPassword() + if !password.IsEmpty() { + val, err := ctx.GetEnvValueFromCache(password, "") + if err != nil { + return nil, err + } + c.Password = val + } + } + + if v, ok := from.(types.WithCertificate); ok { + cert := v.GetCertificate() + if !cert.IsEmpty() { + val, err := ctx.GetEnvValueFromCache(cert, "") + if err != nil { + return nil, err + } + c.Certificate = val + } + } + + if v, ok := from.(types.WithURL); ok { + url := v.GetURL() + if !url.IsEmpty() { + val, err := ctx.GetEnvValueFromCache(url, "") + if err != nil { + return nil, err + } + c.URL = val + } + } + + if v, ok := from.(types.WithProperties); ok { + + if c.Properties == nil { + c.Properties = make(types.JSONStringMap) + } + for k, v := range v.GetProperties() { + c.Properties[k] = v + } + } + + return &c, nil + +} + // AsGoGetterURL returns the connection as a url that's supported by https://github.com/hashicorp/go-getter // Connection details are added to the url as query params func (c Connection) AsGoGetterURL() (string, error) { @@ -125,6 +181,10 @@ func (c Connection) AsGoGetterURL() (string, error) { case ConnectionTypeGit: q := parsedURL.Query() + if c.Username != "" || c.Password != "" { + parsedURL.User = url.UserPassword(c.Username, c.Password) + } + if c.Certificate != "" { q.Set("sshkey", base64.URLEncoding.EncodeToString([]byte(c.Certificate))) } diff --git a/types/envvar.go b/types/envvar.go index f3fbb8f5..6f3e9014 100644 --- a/types/envvar.go +++ b/types/envvar.go @@ -18,6 +18,30 @@ type EnvVar struct { ValueFrom *EnvVarSource `json:"valueFrom,omitempty" yaml:"valueFrom,omitempty" protobuf:"bytes,3,opt,name=valueFrom"` } +// With* interfaces provide a mechanism for connections to have their values overwritten by values specified lower in the +// heirachy + +type WithUsernamePassword interface { + GetUsername() EnvVar + GetPassword() EnvVar +} + +type WithCertificate interface { + GetCertificate() EnvVar +} + +type WithURL interface { + GetURL() EnvVar +} + +type WithProperties interface { + GetProperties() map[string]string +} + +type GetEnvVarFromCache interface { + GetEnvValueFromCache(e EnvVar, namespace string) (string, error) +} + func (e EnvVar) String() string { if e.ValueFrom == nil { return e.ValueStatic