-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Signed-off-by: Xeckt <jordansavell1198@gmail.com> use parseauthorizedkey for ssh public keys Signed-off-by: Xeckt <jordansavell1198@gmail.com> update daemon for new changes for ssh public keys Signed-off-by: Xeckt <jordansavell1198@gmail.com> update tests for new changes Moved new dhcptestcontent from the createtempfile func to a constant on the daemon_tests.go so that createtempfile has a clearer usage with less code Signed-off-by: Xeckt <jordansavell1198@gmail.com> move createtempfile errors back to fatal logs (forgot to put back from debugging) Signed-off-by: Xeckt <jordansavell1198@gmail.com> change test_readsshhostkeypublicfiles "line" args struct field to "content" This aims to make the test more obvious that this is the content going into the temporary file Signed-off-by: Xeckt <jordansavell1198@gmail.com> add a function to comment field for publickey struct to detect comments, fix test for comments Signed-off-by: Xeckt <jordansavell1198@gmail.com> rename type on publickey struct back to algorithm Signed-off-by: Xeckt <jordansavell1198@gmail.com> move ssh functions from utils.go to ssh.go, add new function for ssh strings Signed-off-by: Xeckt <jordansavell1198@gmail.com> fix daemon.go keydata field to use new ssh string function Signed-off-by: Xeckt <jordansavell1198@gmail.com> update tests for new ssh implementation Signed-off-by: Xeckt <jordansavell1198@gmail.com> wrap file close in closure function for error handling in creattemptestfile Signed-off-by: Xeckt <jordansavell1198@gmail.com> move ssh tests from utils test into own file Signed-off-by: Xeckt <jordansavell1198@gmail.com> add copyright for ssh files Signed-off-by: Xeckt <jordansavell1198@gmail.com> fix linter issues Signed-off-by: Xeckt <jordansavell1198@gmail.com>
- Loading branch information
Showing
8 changed files
with
172 additions
and
151 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,53 @@ | ||
/* | ||
SPDX-License-Identifier: Apache-2.0 | ||
Copyright (C) 2022-2023 Intel Corporation | ||
Copyright (c) 2022 Dell Inc, or its subsidiaries. | ||
Copyright (C) 2022 Red Hat. | ||
*/ | ||
|
||
package secureagent | ||
|
||
import ( | ||
"encoding/base64" | ||
"log" | ||
"os" | ||
"path/filepath" | ||
"strings" | ||
|
||
"golang.org/x/crypto/ssh" | ||
) | ||
|
||
func readSSHHostKeyPublicFiles(pattern string) []ssh.PublicKey { | ||
results := []ssh.PublicKey{} | ||
|
||
files, err := filepath.Glob(pattern) | ||
if err != nil { | ||
log.Printf("[ERROR] Error getting ssh host public keys file list: %v", err) | ||
return results | ||
} | ||
|
||
for _, f := range files { | ||
// nolint:gosec | ||
data, err := os.ReadFile(f) | ||
if err != nil { | ||
log.Printf("[ERROR] Error reading public key file %s: %v", f, err) | ||
continue | ||
} | ||
|
||
key, _, _, _, err := ssh.ParseAuthorizedKey(data) | ||
if err != nil { | ||
log.Printf("[ERROR] Problem parsing public key file %s: %v\n"+ | ||
"Check the key file has the correct format", f, err.Error()) | ||
continue | ||
} | ||
results = append(results, key) | ||
} | ||
return results | ||
} | ||
|
||
func getSSHHostKeyString(key ssh.PublicKey, fullString bool) string { | ||
if fullString { | ||
return strings.TrimSuffix(string(ssh.MarshalAuthorizedKey(key)), "\n") // returns algorithm + key | ||
} | ||
return base64.StdEncoding.EncodeToString(key.Marshal()) // returns just the key | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// SPDX-License-Identifier: Apache-2.0 | ||
// Copyright (C) 2022-2023 Red Hat. | ||
package secureagent | ||
|
||
import ( | ||
"reflect" | ||
"testing" | ||
) | ||
|
||
func Test_readSSHHostKeyPublicFiles(t *testing.T) { | ||
type args struct { | ||
file string | ||
content string | ||
Algorithm string | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want string | ||
}{ | ||
{ | ||
name: "Test OK line in files no comment", | ||
args: args{ | ||
file: "/tmp/test.pub", | ||
content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", | ||
Algorithm: "ssh-ed25519", | ||
}, | ||
want: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", | ||
}, | ||
{ | ||
name: "Test OK line in files with comment", | ||
args: args{ | ||
file: "/tmp/test.pub", | ||
content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment", | ||
Algorithm: "ssh-ed25519", | ||
}, | ||
want: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", | ||
}, | ||
{ | ||
name: "Test too many parts in file", | ||
args: args{ | ||
file: "/tmp/test.pub", | ||
content: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR comment error", | ||
Algorithm: "ssh-ed25519", | ||
}, | ||
want: "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAID0mjQXlOvkM2HO5vTrSOdHOl3BGOqDiHrx8yYdbP8xR", | ||
}, | ||
{ | ||
name: "Test not enough parts in file", | ||
args: args{ | ||
file: "/tmp/test.pub", | ||
content: "ssh-ed25519", | ||
}, | ||
want: "ssh-ed25519", | ||
}, | ||
{ | ||
name: "Test file doesn't exist", | ||
args: args{ | ||
file: "/tmp/test.pub", | ||
content: "", | ||
}, | ||
want: "", | ||
}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if tt.args.content != "" { | ||
createTempTestFile(tt.args.file, tt.args.content, true) | ||
} | ||
for _, key := range readSSHHostKeyPublicFiles(tt.args.file) { | ||
if got := getSSHHostKeyString(key, true); !reflect.DeepEqual(got, tt.want) { | ||
t.Errorf("readSSHHostKeyPublicFiles() - got: %v, want %v", got, tt.want) | ||
} | ||
} | ||
deleteTempTestFile(tt.args.file) | ||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.