Skip to content

Commit da57fc2

Browse files
committed
Fix regression in writing authorized principals
Commit ac6d38e introduced a regression in writing of the authorized_principals file, resulting in an empty file. The function `regeneratePrincipalsKeys` in `services/asymkey/ssh_key_authorized_principals.go` calls the function `WriteAuthorizedStringForValidKey` for a PublicKey of type KeyTypePrincipal, and ssh.ParseAuthorizedKey would always fail. This commit adds additional logic to this function to restore the previous behaviour when writing the principals file. Fixes: 36212 Signed-off-by: Peter Verraedt <peter.verraedt@kuleuven.be>
1 parent 8f672ce commit da57fc2

File tree

1 file changed

+16
-7
lines changed

1 file changed

+16
-7
lines changed

models/asymkey/ssh_key_authorized_keys.go

Lines changed: 16 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -51,11 +51,22 @@ func WriteAuthorizedStringForValidKey(key *PublicKey, w io.Writer) error {
5151
}
5252

5353
func writeAuthorizedStringForKey(key *PublicKey, w io.Writer) (keyValid bool, err error) {
54-
const tpl = AuthorizedStringCommentPrefix + "\n" + `command=%s,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,no-user-rc,restrict %s %s` + "\n"
55-
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key.Content))
56-
if err != nil {
57-
return false, err
54+
const tpl = AuthorizedStringCommentPrefix + "\n" + `command=%s,no-port-forwarding,no-X11-forwarding,no-agent-forwarding,no-pty,no-user-rc,restrict %s` + "\n"
55+
56+
var sshKey string
57+
58+
if key.Type == KeyTypePrincipal {
59+
sshKey = fmt.Sprintf("%s # user-%d", key.Content, key.OwnerID)
60+
} else {
61+
pubKey, _, _, _, err := ssh.ParseAuthorizedKey([]byte(key.Content))
62+
if err != nil {
63+
return false, err
64+
}
65+
66+
sshKeyMarshalled := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(pubKey)))
67+
sshKey = fmt.Sprintf("%s user-%d", sshKeyMarshalled, key.OwnerID)
5868
}
69+
5970
// now the key is valid, the code below could only return template/IO related errors
6071
sbCmd := &strings.Builder{}
6172
err = setting.SSH.AuthorizedKeysCommandTemplateTemplate.Execute(sbCmd, map[string]any{
@@ -69,9 +80,7 @@ func writeAuthorizedStringForKey(key *PublicKey, w io.Writer) (keyValid bool, er
6980
return true, err
7081
}
7182
sshCommandEscaped := util.ShellEscape(sbCmd.String())
72-
sshKeyMarshalled := strings.TrimSpace(string(ssh.MarshalAuthorizedKey(pubKey)))
73-
sshKeyComment := fmt.Sprintf("user-%d", key.OwnerID)
74-
_, err = fmt.Fprintf(w, tpl, sshCommandEscaped, sshKeyMarshalled, sshKeyComment)
83+
_, err = fmt.Fprintf(w, tpl, sshCommandEscaped, sshKey)
7584
return true, err
7685
}
7786

0 commit comments

Comments
 (0)