-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Adding read and parse functionalities for ed25519 keys
- Loading branch information
1 parent
9cea7b5
commit 314811f
Showing
9 changed files
with
195 additions
and
9 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,18 @@ | ||
package ssh | ||
|
||
func (a *access) checkIfFileContainsAPrivateEd25519Key(fileName string) (bool, error) { | ||
return fileContentMatches(fileName, a.isEd25519PrivateKey) | ||
} | ||
|
||
func (a *access) filesContainingEd25519PrivateKeys(fileNameList []string) []string { | ||
result := filter(fileNameList, ignoringErrors(a.checkIfFileContainsAPrivateEd25519Key)) | ||
return result | ||
} | ||
|
||
func checkIfFileContainsAPublicEd25519Key(fileName string) (bool, error) { | ||
return fileContentMatches(fileName, isEd25519PublicKey) | ||
} | ||
|
||
func filesContainingEd25519PublicKeys(fileNameList []string) []string { | ||
return filter(fileNameList, ignoringErrors(checkIfFileContainsAPublicEd25519Key)) | ||
} |
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,91 @@ | ||
package ssh | ||
|
||
import "path/filepath" | ||
|
||
func (s *sshSuite) Test_filesContainingEd25519PrivateKeys_ReturnsAnEmptyListIfAnEmptyListIsProvided() { | ||
fileNameList := []string{} | ||
|
||
a, _ := accessWithTestLogging() | ||
selected := a.filesContainingEd25519PrivateKeys(fileNameList) | ||
|
||
s.Empty(selected) | ||
} | ||
|
||
func (s *sshSuite) Test_filesContainingEd25519PrivateKeys_ReturnsAnEmptyListIfAListWithANonExistingFileIsProvided() { | ||
fileNameList := []string{"File that doesn't exist"} | ||
|
||
a, _ := accessWithTestLogging() | ||
selected := a.filesContainingEd25519PrivateKeys(fileNameList) | ||
|
||
s.Empty(selected) | ||
} | ||
|
||
func (s *sshSuite) Test_filesContainingEd25519PrivateKeys_ReturnsAnEmptyListIfAListWithAnEmptyFileIsProvided() { | ||
// Given | ||
fileName := "Empty file" | ||
s.createEmptyFile(s.tdir, fileName) | ||
fileNameList := []string{filepath.Join(s.tdir, fileName)} | ||
|
||
// When | ||
a, _ := accessWithTestLogging() | ||
selected := a.filesContainingEd25519PrivateKeys(fileNameList) | ||
|
||
// Then | ||
s.Empty(selected) | ||
} | ||
|
||
func (s *sshSuite) Test_filesContainingEd25519PrivateKeys_ReturnsAnEmptyListIfAListWithAFileThatDoesntContainAnEd25519PrivateKeyIsProvided() { | ||
// Given | ||
fileName := "Empty file" | ||
s.createFileWithContent(s.tdir, fileName, "not a Ed25519 private key") | ||
fileNameList := []string{filepath.Join(s.tdir, fileName)} | ||
|
||
// When | ||
a, _ := accessWithTestLogging() | ||
selected := a.filesContainingEd25519PrivateKeys(fileNameList) | ||
|
||
// Then | ||
s.Empty(selected) | ||
} | ||
|
||
func (s *sshSuite) Test_filesContainingEd25519PrivateKeys_ReturnsAListWithOneFileNameIfAListWithAFileThatContainsAnEd25519PrivateKeyIsProvided() { | ||
// Given | ||
fileName := "File-with-content" | ||
s.createFileWithContent(s.tdir, fileName, correctEd25519PrivateKey) | ||
fileNameList := []string{filepath.Join(s.tdir, fileName)} | ||
|
||
// When | ||
a, _ := accessWithTestLogging() | ||
selected := a.filesContainingEd25519PrivateKeys(fileNameList) | ||
|
||
// Then | ||
s.Equal(selected, []string{filepath.Join(s.tdir, fileName)}) | ||
} | ||
|
||
const correctEd25519PrivateKeyOther = ` | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW | ||
QyNTUxOQAAACCRcAFuKgCAnlEuMGswxk18tn2JVXH+7OkVSDbBC2WQ2gAAAJCs0I2+rNCN | ||
vgAAAAtzc2gtZWQyNTUxOQAAACCRcAFuKgCAnlEuMGswxk18tn2JVXH+7OkVSDbBC2WQ2g | ||
AAAED9p1K4JP1ykaLj705pfax2AVvTXryKkJxEXkp3eIuLm5FwAW4qAICeUS4wazDGTXy2 | ||
fYlVcf7s6RVINsELZZDaAAAACmZhdXN0b0BDQUQBAgM= | ||
-----END OPENSSH PRIVATE KEY----- | ||
` | ||
|
||
func (s *sshSuite) Test_filesContainingEd25519PrivateKeys_ReturnsAListWithSeveralFileNamesThatContainsEd25519Key() { | ||
// Given | ||
s.createFileWithContent(s.tdir, "key_file1", correctECDSASSHPrivateKey) | ||
s.createFileWithContent(s.tdir, "key_file2", correctEd25519PrivateKey) | ||
s.createEmptyFile(s.tdir, "key_file3") | ||
s.createFileWithContent(s.tdir, "key_file4", correctEd25519PrivateKeyOther) | ||
|
||
fileList := s.withDirectory("key_file1", "key_file2", "key_file3", "key_file4", "key_file5") | ||
|
||
// When | ||
a, _ := accessWithTestLogging() | ||
selected := a.filesContainingEd25519PrivateKeys(fileList) | ||
|
||
// Then | ||
expected := s.withDirectory("key_file2", "key_file4") | ||
s.Equal(expected, selected) | ||
} |
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,23 @@ | ||
package ssh | ||
|
||
const ed25519Algorithm = "ssh-ed25519" | ||
|
||
func (a *access) isEd25519PrivateKey(pk string) bool { | ||
priv, ok := a.parsePrivateKey(pk) | ||
if !ok { | ||
return false | ||
} | ||
return priv.isEd25519() | ||
} | ||
|
||
func (k *publicKey) isEd25519() bool { | ||
return k.isAlgorithm(ed25519Algorithm) | ||
} | ||
|
||
func isEd25519PublicKey(k string) bool { | ||
pub, ok := parsePublicKey(k) | ||
if !ok { | ||
return false | ||
} | ||
return pub.isEd25519() | ||
} |
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,5 @@ | ||
package ssh | ||
|
||
func (k *privateKey) isEd25519() bool { | ||
return k.isAlgorithm(ed25519Algorithm) | ||
} |
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,21 @@ | ||
package ssh | ||
|
||
const correctEd25519PrivateKey = ` | ||
-----BEGIN OPENSSH PRIVATE KEY----- | ||
b3BlbnNzaC1rZXktdjEAAAAABG5vbmUAAAAEbm9uZQAAAAAAAAABAAAAMwAAAAtzc2gtZW | ||
QyNTUxOQAAACC7eNn1eQ/DPPtfZUAie2p9I1TAuj91YucOlbHyxV7hygAAAJC+YoKmvmKC | ||
pgAAAAtzc2gtZWQyNTUxOQAAACC7eNn1eQ/DPPtfZUAie2p9I1TAuj91YucOlbHyxV7hyg | ||
AAAEBVI12MKVSate/Pvx/nqIe2B4/J3Y8qURPhFGcUZyEtgbt42fV5D8M8+19lQCJ7an0j | ||
VMC6P3Vi5w6VsfLFXuHKAAAACmZhdXN0b0BDQUQBAgM= | ||
-----END OPENSSH PRIVATE KEY----- | ||
` | ||
|
||
func (s *sshSuite) Test_parsePrivateKey_AStringContainingACorrectEd25519KeyShouldBeConsideredAPrivateKey() { | ||
pk := correctEd25519PrivateKey | ||
|
||
a, _ := accessWithTestLogging() | ||
priv, ok := a.parsePrivateKey(pk) | ||
|
||
s.True(ok) | ||
s.Equal("ssh-ed25519", priv.algorithm) | ||
} |
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