{"record":{"id":"f784bdb3b912245c","repo":"kubernetes/kops","slug":"found-multiple-sshkeys-with-name-q","errorCode":null,"errorMessage":"Found multiple SSHKeys with Name %q","messagePattern":"Found multiple SSHKeys with Name %q","errorType":"exception","errorClass":null,"httpStatus":null,"severity":"error","filePath":"upup/pkg/fi/cloudup/awstasks/sshkey.go","lineNumber":80,"sourceCode":"func (e *SSHKey) find(ctx context.Context, cloud awsup.AWSCloud) (*SSHKey, error) {\n\trequest := &ec2.DescribeKeyPairsInput{\n\t\tKeyNames: []string{fi.ValueOf(e.Name)},\n\t}\n\n\tresponse, err := cloud.EC2().DescribeKeyPairs(ctx, request)\n\tif err != nil && awsup.AWSErrorCode(err) != \"InvalidKeyPair.NotFound\" {\n\t\treturn nil, fmt.Errorf(\"error listing SSHKeys: %v\", err)\n\t}\n\n\tif response == nil || len(response.KeyPairs) == 0 {\n\t\tif e.IsExistingKey() && *e.Name != \"\" {\n\t\t\treturn nil, fmt.Errorf(\"unable to find specified SSH key %q\", *e.Name)\n\t\t}\n\t\treturn nil, nil\n\t}\n\n\tif len(response.KeyPairs) != 1 {\n\t\treturn nil, fmt.Errorf(\"Found multiple SSHKeys with Name %q\", *e.Name)\n\t}\n\n\tk := response.KeyPairs[0]\n\tactual := &SSHKey{\n\t\tID:             k.KeyPairId,\n\t\tName:           k.KeyName,\n\t\tKeyFingerprint: k.KeyFingerprint,\n\t\tTags:           mapEC2TagsToMap(k.Tags),\n\t\tShared:         e.Shared,\n\t}\n\n\t// Avoid spurious changes\n\tif k.KeyType == ec2types.KeyTypeEd25519 {\n\t\t// Trim the trailing \"=\" and prefix with \"SHA256:\" to match the output of \"ssh-keygen -lf\"\n\t\tfingerprint := fi.ValueOf(k.KeyFingerprint)\n\t\tfingerprint = strings.TrimRight(fingerprint, \"=\")\n\t\tfingerprint = fmt.Sprintf(\"SHA256:%s\", fingerprint)\n\t\tactual.KeyFingerprint = new(fingerprint)","sourceCodeStart":62,"sourceCodeEnd":98,"githubUrl":"https://github.com/kubernetes/kops/blob/4c8573c808a73d578c5eadc86d410646ea0b0d73/upup/pkg/fi/cloudup/awstasks/sshkey.go#L62-L98","documentation":"If DescribeKeyPairs unexpectedly returns more than one key pair for the queried name, find aborts with \"Found multiple SSHKeys with Name\". EC2 normally enforces key-pair name uniqueness, so this indicates an API anomaly or a name-match behavior change.","triggerScenarios":"response.KeyPairs has length > 1 after a DescribeKeyPairs call filtered by KeyNames — only possible through EC2 API anomalies, eventual consistency during concurrent imports of the same name, or a mocked/test environment.","commonSituations":"Concurrent kOps runs importing the same key name simultaneously; stale/test double EC2 backends; cross-account aggregation bugs in tooling wrapping the API.","solutions":["List keys with aws ec2 describe-key-pairs and delete the duplicate.","Ensure only one kOps process applies at a time (avoid concurrent applies).","Re-run kops apply after cleanup; uniqueness should be restored.","If seen repeatedly in CI, serialize key import steps."],"exampleFix":"// before\naws ec2 import-key-pair --key-name same-key &  # concurrent runs\n// after\nrun kops apply sequentially / lock CI jobs with a mutex","handlingStrategy":"fallback","validationCode":"out, _ := exec.Command(\"aws\", \"ec2\", \"describe-key-pairs\", \"--query\", \"KeyPairs\").Output()\n// check for duplicate names across the account before applying","typeGuard":null,"tryCatchPattern":"if err != nil && strings.Contains(err.Error(), \"Found multiple SSHKeys\") {\n    // fallback: clean duplicates out-of-band, then retry once\n    cleanupDuplicateKeys(); return runKopsApply()\n}","preventionTips":["Never run concurrent applies that import the same key name.","Delete stale duplicate keys with aws ec2 delete-key-pair.","Serialize CI jobs that manage the same cluster."],"tags":["aws","ec2","ssh-key","duplicate"],"backgroundTag":"duplicate-resource-name","analyzedSha":"4c8573c808a73d578c5eadc86d410646ea0b0d73","analyzedAt":"2026-09-05T04:13:19.212Z","contentChangedAt":"2026-09-05T04:13:19.212Z","schemaVersion":2},"datasetVersion":"2026-09-12T12:17:11.808Z"}