Skip to content

Commit

Permalink
#38 Add API access to public key locations
Browse files Browse the repository at this point in the history
  • Loading branch information
piratax007 committed Jun 28, 2022
1 parent 0b9329a commit 44d80b2
Show file tree
Hide file tree
Showing 4 changed files with 93 additions and 9 deletions.
1 change: 1 addition & 0 deletions api/key_entry.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,4 +2,5 @@ package api

type KeyEntry interface {
Locations() []string
PublicKeyLocations() []string
}
5 changes: 5 additions & 0 deletions gui/key_list_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,11 @@ func (ke *keyEntryMock) Locations() []string {
return returns.Get(0).([]string)
}

func (ke *keyEntryMock) PublicKeyLocations() []string {
returns := ke.Called()
return returns.Get(0).([]string)
}

func (s *guiSuite) Test_createKeyEntryBoxFrom_CreatesAGTKIBoxWithTheGivenASSHKeyEntry() {
box := s.setupBuildingOfKeyEntry("/home/amnesia/id_rsa.pub")

Expand Down
32 changes: 24 additions & 8 deletions ssh/key_representation.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,23 +41,39 @@ func createKeypairRepresentation(private *privateKeyRepresentation, public *publ
}
}

// Locations implement the KeyEntry interface
func (k *privateKeyRepresentation) Locations() []string {
if k.path == "" {
func nilOrStringSlice(s string) []string {
if s == "" {
return nil
}
return []string{k.path}
return []string{s}
}

// Locations implement the KeyEntry interface
func (k *privateKeyRepresentation) Locations() []string {
return nilOrStringSlice(k.path)
}

// PublicKeyLocations implement the KeyEntry interface
func (k *privateKeyRepresentation) PublicKeyLocations() []string {
return nil
}

// Locations implement the KeyEntry interface
func (k *publicKeyRepresentation) Locations() []string {
if k.path == "" {
return nil
}
return []string{k.path}
return nilOrStringSlice(k.path)
}

// PublicKeyLocations implement the KeyEntry interface
func (k *publicKeyRepresentation) PublicKeyLocations() []string {
return k.Locations()
}

// Locations implement the KeyEntry interface
func (k *keypairRepresentation) Locations() []string {
return append(k.private.Locations(), k.public.Locations()...)
}

// PublicKeyLocations implement the KeyEntry interface
func (k *keypairRepresentation) PublicKeyLocations() []string {
return k.public.PublicKeyLocations()
}
64 changes: 63 additions & 1 deletion ssh/key_representation_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,20 @@ func (s *sshSuite) Test_privateKeyRepresentation_Locations_returnsASliceWithTheP
s.Equal([]string{"another private key"}, pk.Locations())
}

func (s *sshSuite) Test_privateKeyRepresentation_PublicKeyLocations_returnsAnEmptySliceIfNoFilenameIsGiven() {
pk := createPrivateKeyRepresentation("")

s.Empty(pk.PublicKeyLocations())
}

func (s *sshSuite) Test_privateKeyRepresentation_PublicKeyLocations_returnsAnEmptySliceEvenIfAFilenameIsGiven() {
pk := createPrivateKeyRepresentation("/foo/bar/hello.priv")
s.Empty(pk.PublicKeyLocations())

pk = createPrivateKeyRepresentation("another private key")
s.Empty(pk.PublicKeyLocations())
}

func (s *sshSuite) Test_publicKeyRepresentation_Locations_returnsAnEmptySliceIfNoFilenameIsGiven() {
pk := createPublicKeyRepresentation("")

Expand All @@ -80,7 +94,21 @@ func (s *sshSuite) Test_publicKeyRepresentation_Locations_returnsASliceWithThePa
s.Equal([]string{"a public RSA key file"}, pk.Locations())
}

func (s *sshSuite) Test_keypairRepresentation_Locations_returnsAnEmptyList_ifNeitherPrivateOrPublicKeyHasEmptyPath() {
func (s *sshSuite) Test_publicKeyRepresentation_PublicKeyLocations_returnsAnEmptySliceIfNoFilenameIsGiven() {
pk := createPublicKeyRepresentation("")

s.Empty(pk.PublicKeyLocations())
}

func (s *sshSuite) Test_publicKeyRepresentation_PublicKeyLocations_returnsASliceWithThePathToThePublicKey() {
pk := createPublicKeyRepresentation("/foo/bar/hello.pub")
s.Equal([]string{"/foo/bar/hello.pub"}, pk.PublicKeyLocations())

pk = createPublicKeyRepresentation("a public RSA key file")
s.Equal([]string{"a public RSA key file"}, pk.PublicKeyLocations())
}

func (s *sshSuite) Test_keypairRepresentation_Locations_returnsAnEmptyList_ifBothPrivateOrPublicKeyHaveEmptyPaths() {
priv := createPrivateKeyRepresentation("")
pub := createPublicKeyRepresentation("")

Expand Down Expand Up @@ -122,3 +150,37 @@ func (s *sshSuite) Test_keypairRepresentation_Locations_returnsThePrivateAndPubl

s.Equal([]string{"/home/another private key", "pub.rsa.4096.{{{"}, kp2.Locations())
}

func (s *sshSuite) Test_keypairRepresentation_PublicKeyLocations_returnsAnEmptyList_ifBothKeysHaveEmptyPaths() {
priv := createPrivateKeyRepresentation("")
pub := createPublicKeyRepresentation("")

kp := createKeypairRepresentation(priv, pub)

s.Empty(kp.PublicKeyLocations())
}

func (s *sshSuite) Test_keypairRepresentation_PublicKeyLocations_returnsAnEmptyList_ifOnlyThePrivateKeyHasAPath() {
priv := createPrivateKeyRepresentation("/home/foo/.ssh/privatekey.ed25519")
pub := createPublicKeyRepresentation("")

kp := createKeypairRepresentation(priv, pub)

s.Empty(kp.PublicKeyLocations())
}

func (s *sshSuite) Test_keypairRepresentation_PublicKeyLocations_returnsOnlyThePublicPaths() {
priv1 := createPrivateKeyRepresentation("/home/amnesia/.ssh/foo_rsa")
pub1 := createPublicKeyRepresentation("/home/amnesia/.ssh/foo_rsa.pub")

kp1 := createKeypairRepresentation(priv1, pub1)

s.Equal([]string{"/home/amnesia/.ssh/foo_rsa.pub"}, kp1.PublicKeyLocations())

priv2 := createPrivateKeyRepresentation("/home/another private key")
pub2 := createPublicKeyRepresentation("pub.rsa.4096.{{{")

kp2 := createKeypairRepresentation(priv2, pub2)

s.Equal([]string{"pub.rsa.4096.{{{"}, kp2.PublicKeyLocations())
}

0 comments on commit 44d80b2

Please sign in to comment.