From 4e7a4f065d8efcd6a49afb25b493fbc2a550a792 Mon Sep 17 00:00:00 2001 From: Xeckt Date: Fri, 21 Jun 2024 15:52:59 +0100 Subject: [PATCH] add a function to comment field for publickey struct to detect comments, fix test for comments Signed-off-by: Xeckt --- sztp-agent/pkg/secureagent/utils.go | 14 +++++++++++--- sztp-agent/pkg/secureagent/utils_test.go | 12 ++++++++---- 2 files changed, 19 insertions(+), 7 deletions(-) diff --git a/sztp-agent/pkg/secureagent/utils.go b/sztp-agent/pkg/secureagent/utils.go index bc2396cc..291d52f5 100644 --- a/sztp-agent/pkg/secureagent/utils.go +++ b/sztp-agent/pkg/secureagent/utils.go @@ -185,10 +185,18 @@ func readSSHHostKeyPublicFiles(pattern string) []publicKey { "Check the key file has the correct format", f, err.Error()) continue } + + keyParts := strings.Fields(string(data)) + results = append(results, publicKey{ - Type: key.Type(), - Data: strings.Fields(string(data))[1], - Comment: "", + Type: key.Type(), + Data: keyParts[1], + Comment: func() string { + if len(keyParts) == 3 { + return keyParts[2] + } + return "" + }(), }) } return results diff --git a/sztp-agent/pkg/secureagent/utils_test.go b/sztp-agent/pkg/secureagent/utils_test.go index 0f37193c..aef0b2f8 100644 --- a/sztp-agent/pkg/secureagent/utils_test.go +++ b/sztp-agent/pkg/secureagent/utils_test.go @@ -141,7 +141,8 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) { content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", keyType: "ssh-ed25519", }, - want: []publicKey{{Type: "ssh-ed25519", Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, + want: []publicKey{{Type: "ssh-ed25519", + Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, }, { name: "Test OK line in files with comment", @@ -150,7 +151,9 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) { content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment", keyType: "ssh-ed25519", }, - want: []publicKey{{Type: "ssh-ed25519", Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, + want: []publicKey{{Type: "ssh-ed25519", + Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", + Comment: "comment"}}, }, { name: "Test too many parts in file", @@ -159,7 +162,8 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) { content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment error", keyType: "ssh-ed25519", }, - want: []publicKey{{Type: "ssh-ed25519", Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, + want: []publicKey{{Type: "ssh-ed25519", + Data: "AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR"}}, }, { name: "Test not enough parts in file", @@ -184,7 +188,7 @@ func Test_readSSHHostKeyPublicFiles(t *testing.T) { createTempTestFile(tt.args.file, tt.args.content, true) } if got := readSSHHostKeyPublicFiles(tt.args.file); !reflect.DeepEqual(got, tt.want) { - t.Errorf("readSSHHostKeyPublicFiles() = %v, want %v", got, tt.want) + t.Errorf("readSSHHostKeyPublicFiles() - got: %v, want %v", got, tt.want) } deleteTempTestFile(tt.args.file) })