diff --git a/builtin/logical/ssh/backend_test.go b/builtin/logical/ssh/backend_test.go index 27934d42a..e6dc9aee1 100644 --- a/builtin/logical/ssh/backend_test.go +++ b/builtin/logical/ssh/backend_test.go @@ -28,6 +28,7 @@ import ( const ( testIP = "127.0.0.1" testUserName = "vaultssh" + testMultiUserName = "vaultssh,otherssh" testAdminUser = "vaultssh" testCaKeyType = "ca" testOTPKeyType = "otp" @@ -356,6 +357,15 @@ func TestBackend_AllowedUsersTemplate(t *testing.T) { ) } +func TestBackend_MultipleAllowedUsersTemplate(t *testing.T) { + testAllowedUsersTemplate(t, + "{{ identity.entity.metadata.ssh_username }}", + testUserName, map[string]string{ + "ssh_username": testMultiUserName, + }, + ) +} + func TestBackend_AllowedUsersTemplate_WithStaticPrefix(t *testing.T) { testAllowedUsersTemplate(t, "ssh-{{ identity.entity.metadata.ssh_username }}", diff --git a/builtin/logical/ssh/path_issue_sign.go b/builtin/logical/ssh/path_issue_sign.go index 8e6056c46..0ce45d518 100644 --- a/builtin/logical/ssh/path_issue_sign.go +++ b/builtin/logical/ssh/path_issue_sign.go @@ -176,18 +176,14 @@ func (b *backend) calculateValidPrincipals(data *framework.FieldData, req *logic parsedPrincipals := strutil.RemoveDuplicates(strutil.ParseStringSlice(validPrincipals, ","), false) // Build list of allowed Principals from template and static principalsAllowedByRole var allowedPrincipals []string - for _, principal := range strutil.RemoveDuplicates(strutil.ParseStringSlice(principalsAllowedByRole, ","), false) { - if enableTemplating { - rendered, err := b.renderPrincipal(principal, req) - if err != nil { - return nil, err - } - // Template returned a principal - allowedPrincipals = append(allowedPrincipals, rendered) - } else { - // Static principal - allowedPrincipals = append(allowedPrincipals, principal) + if enableTemplating { + rendered, err := b.renderPrincipal(principalsAllowedByRole, req) + if err != nil { + return nil, err } + allowedPrincipals = strutil.RemoveDuplicates(strutil.ParseStringSlice(rendered, ","), false) + } else { + allowedPrincipals = strutil.RemoveDuplicates(strutil.ParseStringSlice(principalsAllowedByRole, ","), false) } switch { diff --git a/changelog/16622.txt b/changelog/16622.txt new file mode 100644 index 000000000..37ae5abb5 --- /dev/null +++ b/changelog/16622.txt @@ -0,0 +1,3 @@ +```release-note:improvement +secrets/ssh: Evaluate ssh validprincipals user template before splitting +```