Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: api-sidecar-handler returns newlines in ssh key data #3844

Merged
merged 1 commit into from
Dec 1, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions services/api-sidecar-handler/internal/server/validate.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,7 @@ func validatePrivateKey(w http.ResponseWriter, r *http.Request) {
return
}
sshPubKey := ssh.MarshalAuthorizedKey(signer.PublicKey())
resp.PublicKey = string(sshPubKey)
resp.PublicKey = strings.TrimSpace(string(sshPubKey))
pub, _, _, _, err := ssh.ParseAuthorizedKey(sshPubKey)
if err != nil {
resp.Error = err.Error()
Expand All @@ -96,7 +96,7 @@ func validatePrivateKey(w http.ResponseWriter, r *http.Request) {
resp.SHA256Fingerprint = ssh.FingerprintSHA256(pub)
resp.MD5Fingerprint = ssh.FingerprintLegacyMD5(pub)
resp.Type = pub.Type()
resp.Value = strings.Split(string(sshPubKey), " ")[1]
resp.Value = strings.TrimSpace(strings.Split(string(sshPubKey), " ")[1])
log.Printf("validated private key with public fingerprint %s", resp.SHA256Fingerprint)
w.WriteHeader(http.StatusOK)
fmt.Fprint(w, resp.String())
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ func Test_validatePrivateKey(t *testing.T) {
name: "with private ed25519",
method: http.MethodPost,
input: fmt.Sprintf("key=%s", ed25519Key),
want: `{"publickey":"ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAD8E5wfvLg8vvfO9mmHVsZQK8dNgdKM5FrTxL4ORDq66Z50O8zUzBwF1VTO5Zx+qwB7najMdWsnW00BC6PMysSNJQD5HI4CokyKqmGdeSXcROYwvYOjlDQ+jD5qOSmkllRZZnkEYXE5FVBXaZWToyfGUGIoECvKGUQZxkBDHsbK13JdfA==\n","sha256fingerprint":"SHA256:RBRWA2mJFPK/8DtsxVoVzoSShFiuRAzlUBws7cXkwG0","md5fingerprint":"72:86:48:50:59:1b:97:81:21:27:e7:55:98:fa:35:95","type":"ecdsa-sha2-nistp521","value":"AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAD8E5wfvLg8vvfO9mmHVsZQK8dNgdKM5FrTxL4ORDq66Z50O8zUzBwF1VTO5Zx+qwB7najMdWsnW00BC6PMysSNJQD5HI4CokyKqmGdeSXcROYwvYOjlDQ+jD5qOSmkllRZZnkEYXE5FVBXaZWToyfGUGIoECvKGUQZxkBDHsbK13JdfA==\n"}`,
want: `{"publickey":"ecdsa-sha2-nistp521 AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAD8E5wfvLg8vvfO9mmHVsZQK8dNgdKM5FrTxL4ORDq66Z50O8zUzBwF1VTO5Zx+qwB7najMdWsnW00BC6PMysSNJQD5HI4CokyKqmGdeSXcROYwvYOjlDQ+jD5qOSmkllRZZnkEYXE5FVBXaZWToyfGUGIoECvKGUQZxkBDHsbK13JdfA==","sha256fingerprint":"SHA256:RBRWA2mJFPK/8DtsxVoVzoSShFiuRAzlUBws7cXkwG0","md5fingerprint":"72:86:48:50:59:1b:97:81:21:27:e7:55:98:fa:35:95","type":"ecdsa-sha2-nistp521","value":"AAAAE2VjZHNhLXNoYTItbmlzdHA1MjEAAAAIbmlzdHA1MjEAAACFBAD8E5wfvLg8vvfO9mmHVsZQK8dNgdKM5FrTxL4ORDq66Z50O8zUzBwF1VTO5Zx+qwB7najMdWsnW00BC6PMysSNJQD5HI4CokyKqmGdeSXcROYwvYOjlDQ+jD5qOSmkllRZZnkEYXE5FVBXaZWToyfGUGIoECvKGUQZxkBDHsbK13JdfA=="}`,
statusCode: http.StatusOK,
},
{
Expand Down
18 changes: 18 additions & 0 deletions services/api/database/migrations/20241119031013_trim_sshkeys.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.up = function(knex) {
return knex('ssh_key').update({
key_value: knex.raw("REPLACE(key_value, '\n', '')")
})
};

/**
* @param { import("knex").Knex } knex
* @returns { Promise<void> }
*/
exports.down = function(knex) {
// Nothing to do
return knex.schema
};