|
9 | 9 |
|
10 | 10 | v1 "github.com/flanksource/canary-checker/api/v1"
|
11 | 11 | "github.com/flanksource/commons/logger"
|
| 12 | + ctemplate "github.com/flanksource/commons/template" |
12 | 13 | "github.com/flanksource/duty"
|
13 | 14 | "github.com/flanksource/duty/models"
|
14 | 15 | "github.com/flanksource/duty/types"
|
@@ -55,12 +56,90 @@ func (ctx *Context) GetEnvValueFromCache(env types.EnvVar) (string, error) {
|
55 | 56 | return duty.GetEnvValueFromCache(ctx.Kubernetes, env, ctx.Namespace)
|
56 | 57 | }
|
57 | 58 |
|
| 59 | +func getDomain(username string) string { |
| 60 | + parts := strings.Split(username, "@") |
| 61 | + if len(parts) == 2 { |
| 62 | + return parts[1] |
| 63 | + } |
| 64 | + return "" |
| 65 | +} |
| 66 | + |
| 67 | +func (ctx *Context) GetConnection(conn v1.Connection) (*models.Connection, error) { |
| 68 | + var _conn *models.Connection |
| 69 | + var err error |
| 70 | + |
| 71 | + if _conn, err = ctx.HydrateConnectionByURL(conn.Connection); err != nil { |
| 72 | + return nil, err |
| 73 | + } |
| 74 | + |
| 75 | + if _conn == nil { |
| 76 | + _conn = &models.Connection{ |
| 77 | + URL: conn.URL, |
| 78 | + } |
| 79 | + } |
| 80 | + |
| 81 | + if conn.URL != "" { |
| 82 | + // override the url specified at the connection level |
| 83 | + _conn.URL = conn.URL |
| 84 | + } |
| 85 | + |
| 86 | + if _conn.Username == "" || _conn.Password == "" { |
| 87 | + // no username specified at connection level, use the one from inline connection |
| 88 | + auth, err := ctx.GetAuthValues(conn.Authentication) |
| 89 | + if err != nil { |
| 90 | + return nil, err |
| 91 | + } |
| 92 | + _conn.Username = auth.Username.ValueStatic |
| 93 | + _conn.Password = auth.Password.ValueStatic |
| 94 | + } |
| 95 | + |
| 96 | + data := map[string]interface{}{ |
| 97 | + "name": ctx.Canary.Name, |
| 98 | + "namespace": ctx.Namespace, |
| 99 | + "username": _conn.Username, |
| 100 | + "password": _conn.Password, |
| 101 | + "domain": getDomain(_conn.Username), |
| 102 | + } |
| 103 | + templater := ctemplate.StructTemplater{ |
| 104 | + Values: data, |
| 105 | + // access go values in template requires prefix everything with . |
| 106 | + // to support $(username) instead of $(.username) we add a function for each var |
| 107 | + ValueFunctions: true, |
| 108 | + DelimSets: []ctemplate.Delims{ |
| 109 | + {Left: "{{", Right: "}}"}, |
| 110 | + {Left: "$(", Right: ")"}, |
| 111 | + }, |
| 112 | + RequiredTag: "template", |
| 113 | + } |
| 114 | + if err := templater.Walk(_conn); err != nil { |
| 115 | + return nil, err |
| 116 | + } |
| 117 | + |
| 118 | + return _conn, nil |
| 119 | +} |
| 120 | + |
| 121 | +func (ctx Context) GetAuthValues(auth v1.Authentication) (v1.Authentication, error) { |
| 122 | + // in case nil we are sending empty string values for username and password |
| 123 | + if auth.IsEmpty() { |
| 124 | + return auth, nil |
| 125 | + } |
| 126 | + var err error |
| 127 | + |
| 128 | + if auth.Username.ValueStatic, err = ctx.GetEnvValueFromCache(auth.Username); err != nil { |
| 129 | + return auth, err |
| 130 | + } |
| 131 | + if auth.Password.ValueStatic, err = ctx.GetEnvValueFromCache(auth.Password); err != nil { |
| 132 | + return auth, err |
| 133 | + } |
| 134 | + return auth, nil |
| 135 | +} |
| 136 | + |
58 | 137 | func (ctx *Context) HydrateConnectionByURL(connectionName string) (*models.Connection, error) {
|
59 |
| - if !strings.HasPrefix(connectionName, "connection://") { |
| 138 | + if connectionName == "" { |
60 | 139 | return nil, nil
|
61 | 140 | }
|
62 | 141 |
|
63 |
| - if connectionName == "" { |
| 142 | + if !strings.HasPrefix(connectionName, "connection://") { |
64 | 143 | return nil, nil
|
65 | 144 | }
|
66 | 145 |
|
|
0 commit comments