Skip to content

Commit a7a891f

Browse files
committed
ssh: Fix template regex test for defaultExtensions to allow additional text (#16018)
* ssh: Fix template regex test for defaultExtensions - The regex to identify if our defaultExtensions contains a template was a little too greedy, requiring the entire field to be just the regex. Allow additional text within the value field to be added * Add cl
1 parent e61c51f commit a7a891f

File tree

3 files changed

+11
-3
lines changed

3 files changed

+11
-3
lines changed

builtin/logical/ssh/backend_test.go

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1480,6 +1480,8 @@ func TestBackend_DefExtTemplatingEnabled(t *testing.T) {
14801480
"default_extensions_template": true,
14811481
"default_extensions": map[string]interface{}{
14821482
"login@foobar.com": "{{identity.entity.aliases." + userpassAccessor + ".name}}",
1483+
"login@foobar2.com": "{{identity.entity.aliases." + userpassAccessor + ".name}}, " +
1484+
"{{identity.entity.aliases." + userpassAccessor + ".name}}_foobar",
14831485
},
14841486
})
14851487
if err != nil {
@@ -1505,7 +1507,8 @@ func TestBackend_DefExtTemplatingEnabled(t *testing.T) {
15051507
}
15061508

15071509
defaultExtensionPermissions := map[string]string{
1508-
"login@foobar.com": testUserName,
1510+
"login@foobar.com": testUserName,
1511+
"login@foobar2.com": fmt.Sprintf("%s, %s_foobar", testUserName, testUserName),
15091512
}
15101513

15111514
err = validateSSHCertificate(parsedKey.(*ssh.Certificate), sshKeyID, ssh.UserCert, []string{"tuber"}, map[string]string{}, defaultExtensionPermissions, 16*time.Hour)

builtin/logical/ssh/path_sign.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,8 @@ type creationBundle struct {
3636
Extensions map[string]string
3737
}
3838

39+
var containsTemplateRegex = regexp.MustCompile(`{{.+?}}`)
40+
3941
func pathSign(b *backend) *framework.Path {
4042
return &framework.Path{
4143
Pattern: "sign/" + framework.GenericNameWithAtRegex("role"),
@@ -220,7 +222,7 @@ func (b *backend) calculateValidPrincipals(data *framework.FieldData, req *logic
220222
for _, principal := range strutil.RemoveDuplicates(strutil.ParseStringSlice(principalsAllowedByRole, ","), false) {
221223
if role.AllowedUsersTemplate {
222224
// Look for templating markers {{ .* }}
223-
matched, _ := regexp.MatchString(`{{.+?}}`, principal)
225+
matched := containsTemplateRegex.MatchString(principal)
224226
if matched {
225227
if req.EntityID != "" {
226228
// Retrieve principal based on template + entityID from request.
@@ -384,7 +386,7 @@ func (b *backend) calculateExtensions(data *framework.FieldData, req *logical.Re
384386
if role.DefaultExtensionsTemplate {
385387
for extensionKey, extensionValue := range role.DefaultExtensions {
386388
// Look for templating markers {{ .* }}
387-
matched, _ := regexp.MatchString(`^{{.+?}}$`, extensionValue)
389+
matched := containsTemplateRegex.MatchString(extensionValue)
388390
if matched {
389391
if req.EntityID != "" {
390392
// Retrieve extension value based on template + entityID from request.

changelog/16018.txt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
```release-note:improvement
2+
secrets/ssh: Allow additional text along with a template definition in defaultExtension value fields.
3+
```

0 commit comments

Comments
 (0)