From cd1316f54ff6f6022bc8d936ae69af8418c8b9db Mon Sep 17 00:00:00 2001 From: Luca Errani Date: Mon, 15 Nov 2021 10:34:16 +0100 Subject: [PATCH] SSH bastion: support spaces in ssh keys' comments --- operators/pkg/bastion-controller/bastion_controller_test.go | 3 ++- operators/pkg/bastion-controller/helpers.go | 6 ++++-- 2 files changed, 6 insertions(+), 3 deletions(-) diff --git a/operators/pkg/bastion-controller/bastion_controller_test.go b/operators/pkg/bastion-controller/bastion_controller_test.go index ac52d8196..4eaf26339 100644 --- a/operators/pkg/bastion-controller/bastion_controller_test.go +++ b/operators/pkg/bastion-controller/bastion_controller_test.go @@ -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{ diff --git a/operators/pkg/bastion-controller/helpers.go b/operators/pkg/bastion-controller/helpers.go index 9a0fae629..c019acbdb 100644 --- a/operators/pkg/bastion-controller/helpers.go +++ b/operators/pkg/bastion-controller/helpers.go @@ -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], @@ -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], @@ -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 { @@ -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())