-
Notifications
You must be signed in to change notification settings - Fork 14
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 <[email protected]>
- 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.