Skip to content

Commit

Permalink
Merge pull request #1111 from flanksource/refactor-auth-structs
Browse files Browse the repository at this point in the history
chore: inline auth structs
  • Loading branch information
moshloop authored Jul 6, 2023
2 parents c84e752 + 2dbc3a8 commit a882550
Show file tree
Hide file tree
Showing 69 changed files with 3,041 additions and 3,186 deletions.
83 changes: 81 additions & 2 deletions api/context/context.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

v1 "github.com/flanksource/canary-checker/api/v1"
"github.com/flanksource/commons/logger"
ctemplate "github.com/flanksource/commons/template"
"github.com/flanksource/duty"
"github.com/flanksource/duty/models"
"github.com/flanksource/duty/types"
Expand Down Expand Up @@ -55,12 +56,90 @@ func (ctx *Context) GetEnvValueFromCache(env types.EnvVar) (string, error) {
return duty.GetEnvValueFromCache(ctx.Kubernetes, env, ctx.Namespace)
}

func getDomain(username string) string {
parts := strings.Split(username, "@")
if len(parts) == 2 {
return parts[1]
}
return ""
}

func (ctx *Context) GetConnection(conn v1.Connection) (*models.Connection, error) {
var _conn *models.Connection
var err error

if _conn, err = ctx.HydrateConnectionByURL(conn.Connection); err != nil {
return nil, err
}

if _conn == nil {
_conn = &models.Connection{
URL: conn.URL,
}
}

if conn.URL != "" {
// override the url specified at the connection level
_conn.URL = conn.URL
}

if _conn.Username == "" || _conn.Password == "" {
// no username specified at connection level, use the one from inline connection
auth, err := ctx.GetAuthValues(conn.Authentication)
if err != nil {
return nil, err
}
_conn.Username = auth.Username.ValueStatic
_conn.Password = auth.Password.ValueStatic
}

data := map[string]interface{}{
"name": ctx.Canary.Name,
"namespace": ctx.Namespace,
"username": _conn.Username,
"password": _conn.Password,
"domain": getDomain(_conn.Username),
}
templater := ctemplate.StructTemplater{
Values: data,
// access go values in template requires prefix everything with .
// to support $(username) instead of $(.username) we add a function for each var
ValueFunctions: true,
DelimSets: []ctemplate.Delims{
{Left: "{{", Right: "}}"},
{Left: "$(", Right: ")"},
},
RequiredTag: "template",
}
if err := templater.Walk(_conn); err != nil {
return nil, err
}

return _conn, nil
}

func (ctx Context) GetAuthValues(auth v1.Authentication) (v1.Authentication, error) {
// in case nil we are sending empty string values for username and password
if auth.IsEmpty() {
return auth, nil
}
var err error

if auth.Username.ValueStatic, err = ctx.GetEnvValueFromCache(auth.Username); err != nil {
return auth, err
}
if auth.Password.ValueStatic, err = ctx.GetEnvValueFromCache(auth.Password); err != nil {
return auth, err
}
return auth, nil
}

func (ctx *Context) HydrateConnectionByURL(connectionName string) (*models.Connection, error) {
if !strings.HasPrefix(connectionName, "connection://") {
if connectionName == "" {
return nil, nil
}

if connectionName == "" {
if !strings.HasPrefix(connectionName, "connection://") {
return nil, nil
}

Expand Down
Loading

0 comments on commit a882550

Please sign in to comment.