Skip to content

Commit

Permalink
Merge pull request #332 from tlm/ssh-key-concat
Browse files Browse the repository at this point in the history
Adds a new concat func for keys.
  • Loading branch information
tlm authored Dec 12, 2023
2 parents 770ab8d + 1e7f164 commit 4b17505
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 0 deletions.
16 changes: 16 additions & 0 deletions ssh/authorisedkeys.go
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,22 @@ func ParseAuthorisedKey(line string) (*AuthorisedKey, error) {
}, nil
}

// ConcatAuthorisedKeys will joing two or more authorised keys together to form
// a string based list of authorised keys that can be read by ssh programs. Keys
// joined with a newline as the separator.
func ConcatAuthorisedKeys(a, b string) string {
if a == "" {
return b
}
if b == "" {
return a
}
if a[len(a)-1] != '\n' {
return a + "\n" + b
}
return a + b
}

// SplitAuthorisedKeys extracts a key slice from the specified key data,
// by splitting the key data into lines and ignoring comments and blank lines.
func SplitAuthorisedKeys(keyData string) []string {
Expand Down
11 changes: 11 additions & 0 deletions ssh/authorisedkeys_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -273,3 +273,14 @@ func (s *AuthorisedKeysKeysSuite) TestParseAuthorisedKey(c *gc.C) {
}
}
}

func (s *AuthorisedKeysKeysSuite) TestConcatAuthorisedKeys(c *gc.C) {
for _, test := range []struct{ a, b, result string }{
{"a", "", "a"},
{"", "b", "b"},
{"a", "b", "a\nb"},
{"a\n", "b", "a\nb"},
} {
c.Check(ssh.ConcatAuthorisedKeys(test.a, test.b), gc.Equals, test.result)
}
}

0 comments on commit 4b17505

Please sign in to comment.