Handle comment in local ssh key
This commit is contained in:
parent
cb914c1873
commit
d410830f7f
2 changed files with 30 additions and 20 deletions
|
|
@ -360,7 +360,7 @@ func hasUploadedPublicKeyForConfig(
|
|||
return false, fmt.Errorf("getting local ssh keys: %w", err)
|
||||
}
|
||||
|
||||
var configuredPublicKeys []string
|
||||
configuredPublicKeys := make(map[string]bool)
|
||||
for _, privateKeyPath := range configuredPrivateKeyPaths {
|
||||
publicKeyPath := privateKeyPath + ".pub"
|
||||
|
||||
|
|
@ -371,7 +371,15 @@ func hasUploadedPublicKeyForConfig(
|
|||
continue
|
||||
}
|
||||
|
||||
configuredPublicKeys = append(configuredPublicKeys, string(publicKeyContent))
|
||||
parts := strings.SplitN(string(publicKeyContent), " ", 3)
|
||||
if len(parts) < 2 {
|
||||
// Unexpected format, skip it
|
||||
continue
|
||||
}
|
||||
|
||||
publicKeyWithoutComment := strings.Join(parts[:2], " ")
|
||||
|
||||
configuredPublicKeys[publicKeyWithoutComment] = true
|
||||
}
|
||||
|
||||
if len(configuredPublicKeys) == 0 {
|
||||
|
|
@ -379,11 +387,6 @@ func hasUploadedPublicKeyForConfig(
|
|||
return false, nil
|
||||
}
|
||||
|
||||
publicKeyMap := make(map[string]bool)
|
||||
for _, publicKey := range configuredPublicKeys {
|
||||
publicKeyMap[publicKey] = true
|
||||
}
|
||||
|
||||
user, err := apiClient.GetUser(ctx)
|
||||
if err != nil {
|
||||
return false, fmt.Errorf("fetching user account: %w", err)
|
||||
|
|
@ -399,7 +402,7 @@ func hasUploadedPublicKeyForConfig(
|
|||
}
|
||||
|
||||
for _, uploadedPublicKey := range uploadedPublicKeys {
|
||||
if publicKeyMap[uploadedPublicKey] {
|
||||
if configuredPublicKeys[uploadedPublicKey] {
|
||||
return true, nil
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -185,40 +185,47 @@ func TestHasUploadedPublicKeyForConfig(t *testing.T) {
|
|||
},
|
||||
{
|
||||
// Has API keys, but no local keys
|
||||
apiAuthorizedPublicKeys: []string{"test-key"},
|
||||
apiAuthorizedPublicKeys: []string{"ssh-rsa test-key"},
|
||||
wantResult: false,
|
||||
},
|
||||
{
|
||||
// No API keys, but has local keys
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile", "test-key"}},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile", "ssh-rsa test-key"}},
|
||||
wantResult: false,
|
||||
},
|
||||
{
|
||||
// API keys and local keys, but not matching
|
||||
apiAuthorizedPublicKeys: []string{"test-api-key"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile", "test-local-key"}},
|
||||
apiAuthorizedPublicKeys: []string{"ssh-rsa test-api-key"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile", "ssh-rsa test-local-key"}},
|
||||
wantResult: false,
|
||||
},
|
||||
|
||||
// Successful tests
|
||||
{
|
||||
apiAuthorizedPublicKeys: []string{"test-key"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile", "test-key"}},
|
||||
apiAuthorizedPublicKeys: []string{"ssh-rsa test-key"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile", "ssh-rsa test-key"}},
|
||||
wantResult: true,
|
||||
},
|
||||
{
|
||||
apiAuthorizedPublicKeys: []string{"test-key-1", "test-key-2"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile1", "test-key-1"}},
|
||||
apiAuthorizedPublicKeys: []string{"ssh-rsa test-key-1", "ssh-rsa test-key-2"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile1", "ssh-rsa test-key-1"}},
|
||||
wantResult: true,
|
||||
},
|
||||
{
|
||||
apiAuthorizedPublicKeys: []string{"test-key-1"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile1", "test-key-1"}, {"keyfile2", "test-key-2"}},
|
||||
apiAuthorizedPublicKeys: []string{"ssh-rsa test-key-1"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile1", "ssh-rsa test-key-1"}, {"keyfile2", "ssh-rsa test-key-2"}},
|
||||
wantResult: true,
|
||||
},
|
||||
{
|
||||
apiAuthorizedPublicKeys: []string{"test-key-1", "test-key-2"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile3", "test-key-3"}, {"keyfile2", "test-key-2"}},
|
||||
apiAuthorizedPublicKeys: []string{"ssh-rsa test-key-1", "ssh-rsa test-key-2"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile3", "ssh-rsa test-key-3"}, {"keyfile2", "ssh-rsa test-key-2"}},
|
||||
wantResult: true,
|
||||
},
|
||||
|
||||
// Extra case - local key contain comments
|
||||
{
|
||||
apiAuthorizedPublicKeys: []string{"ssh-rsa test-key-1", "ssh-rsa test-key"},
|
||||
localKeyPairs: []testLocalKeyPair{{"keyfile3", "ssh-rsa test-key a comment on the key"}},
|
||||
wantResult: true,
|
||||
},
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue