Add templating coalesce function (#4492)
This commit is contained in:
1 parent
7105270cd5
commit
4542eef22b
8 files changed
+52
-8
No files matched your search
@@ -1345,6 +1345,17 @@ func TestRepository_sshCertIssuingCredentialLibrary_retrieveCredential(t *testin
|
||||
opts: []Option{WithKeyType(KeyTypeEcdsa), WithKeyBits(256)},
|
||||
retOpts: []credential.Option{credential.WithTemplateData(template.Data{Account: template.Account{Email: util.Pointer("rise-of-the-template@foobar.com")}})},
|
||||
},
|
||||
{
|
||||
name: "vault issue ec(256) cert with coalesce username",
|
||||
username: `{{coalesce .Account.LoginName .Account.Name .Account.Email}}`,
|
||||
expected: map[string]any{
|
||||
"username": "name-that-name",
|
||||
"valid_principals": []string{"name-that-name"},
|
||||
},
|
||||
vaulthPath: "ssh/issue/boundary",
|
||||
opts: []Option{WithKeyType(KeyTypeEcdsa), WithKeyBits(256)},
|
||||
retOpts: []credential.Option{credential.WithTemplateData(template.Data{Account: template.Account{Name: util.Pointer("name-that-name"), LoginName: util.Pointer(""), Email: util.Pointer("rise-of-the-template@foobar.com")}})},
|
||||
},
|
||||
{
|
||||
name: "vault issue ec(384) cert with disallowed extension",
|
||||
username: "username-10-because-789",
|
||||
|
||||
@@ -10,6 +10,7 @@ import (
|
||||
"fmt"
|
||||
"hash"
|
||||
"hash/fnv"
|
||||
"log"
|
||||
"net/http"
|
||||
"slices"
|
||||
"strings"
|
||||
@@ -599,8 +600,10 @@ func (v verifier) performAuthCheck(ctx context.Context) (
|
||||
return
|
||||
}
|
||||
userData.Account.Name = util.Pointer(acct.GetName())
|
||||
log.Println("account name", *userData.Account.Name)
|
||||
userData.Account.Email = util.Pointer(acct.GetEmail())
|
||||
userData.Account.LoginName = util.Pointer(acct.GetLoginName())
|
||||
log.Println("account login name", *userData.Account.LoginName)
|
||||
userData.Account.Subject = util.Pointer(acct.GetSubject())
|
||||
}
|
||||
|
||||
|
||||
@@ -12,3 +12,14 @@ func truncateFrom(str, sep string) string {
|
||||
before, _, _ := strings.Cut(str, sep)
|
||||
return before
|
||||
}
|
||||
|
||||
// coalesce will return the first non-empty string in the list of strings, and
|
||||
// an empty string if all parameters are empty.
|
||||
func coalesce(vals ...string) string {
|
||||
for _, val := range vals {
|
||||
if val != "" {
|
||||
return val
|
||||
}
|
||||
}
|
||||
return ""
|
||||
}
|
||||
@@ -33,6 +33,7 @@ func New(ctx context.Context, raw string) (*Parsed, error) {
|
||||
raw: raw,
|
||||
funcMap: map[string]any{
|
||||
"truncateFrom": truncateFrom,
|
||||
"coalesce": coalesce,
|
||||
},
|
||||
}
|
||||
|
||||
|
||||
@@ -39,7 +39,7 @@ func TestErrors(t *testing.T) {
|
||||
require.NotNil(parsed)
|
||||
assert.Equal(ts, parsed.raw)
|
||||
assert.NotNil(parsed.tmpl)
|
||||
assert.Len(parsed.funcMap, 1)
|
||||
assert.Len(parsed.funcMap, 2)
|
||||
|
||||
// Test out errors on the parsed value
|
||||
|
||||
@@ -102,6 +102,8 @@ func TestGenerate(t *testing.T) {
|
||||
{{ .Account.Subject }}
|
||||
{{ .Account.Email }}
|
||||
{{ truncateFrom .Account.Email "@" }}
|
||||
{{ coalesce "" .Account.LoginName .Account.Name }}
|
||||
{{ coalesce "" "" .Account.Name .Account.LoginName }}
|
||||
`)
|
||||
|
||||
parsed, err := New(ctx, raw)
|
||||
@@ -128,6 +130,8 @@ accountLoginName
|
||||
accountSubject
|
||||
account@email.com
|
||||
account
|
||||
accountLoginName
|
||||
accountName
|
||||
`)
|
||||
|
||||
assert.Equal(exp, out)
|
||||
|
||||
@@ -19,10 +19,11 @@ type Data struct {
|
||||
// request to be from a different auth method, in which case it may not match
|
||||
// what's in the Account struct.
|
||||
type User struct {
|
||||
Id *string
|
||||
Name *string
|
||||
FullName *string
|
||||
Email *string
|
||||
Id *string
|
||||
Name *string
|
||||
LoginName *string
|
||||
FullName *string
|
||||
Email *string
|
||||
}
|
||||
|
||||
// Account contains account information. Not all fields will always be
|
||||
|
||||
Reference in new issue
Block a user