Skip to content

Commit

Permalink
SSH bastion: support spaces in ssh keys' comments
Browse files Browse the repository at this point in the history
  • Loading branch information
luco5826 committed Nov 15, 2021
1 parent 77d579b commit cd1316f
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 3 deletions.
3 changes: 2 additions & 1 deletion operators/pkg/bastion-controller/bastion_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ var _ = Describe("Bastion controller - creating two tenants", func() {

PublicKeysTenant1 = []string{
"ssh-ed25519 publicKeyString_1 comment_1",
"ssh-rsa publicKeyString_2",
"ssh-ed25519 publicKeyString_2 comment_2 with spaces",
"ssh-rsa publicKeyString_3",
"invalid_entry",
}
PublicKeysTenant2 = []string{
Expand Down
6 changes: 4 additions & 2 deletions operators/pkg/bastion-controller/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ type AuthorizedKeysEntry struct {

// Decompose converts a string into an AuthorizedKeysEntry object.
func Decompose(entry string) (AuthorizedKeysEntry, error) {
entryComponents := strings.Split(entry, string(" "))
entryComponents := strings.SplitN(entry, string(" "), 3)
if len(entryComponents) == 3 {
return AuthorizedKeysEntry{
Algo: entryComponents[0],
Expand All @@ -59,7 +59,7 @@ func Decompose(entry string) (AuthorizedKeysEntry, error) {

// Create converts a string and an id into an AuthorizedKeysEntry object.
func Create(entry, id string) (AuthorizedKeysEntry, error) {
entryComponents := strings.Split(entry, string(" "))
entryComponents := strings.SplitN(entry, string(" "), 3)
if len(entryComponents) == 3 || len(entryComponents) == 2 {
return AuthorizedKeysEntry{
Algo: entryComponents[0],
Expand All @@ -81,6 +81,7 @@ func decomposeAndPurgeEntries(keys []string, tenantID string) []string {
for i, key := range keys {
entry, err := Decompose(key)
if err != nil {
klog.Warningf("Skipping key %s: %s", key, err.Error())
continue
}
if entry.ID == tenantID {
Expand All @@ -99,6 +100,7 @@ func composeAndMarkEntries(keys, tenantKeys []string, tenantID string) []string
for i := range tenantKeys {
entry, err := Create(tenantKeys[i], tenantID)
if err != nil {
klog.Warningf("Skipping key %s: %s", tenantKeys[i], err.Error())
continue
}
keys = append(keys, entry.Compose())
Expand Down

0 comments on commit cd1316f

Please sign in to comment.