Merge pull request #65063 from awly/fix-csr-private-key-reuse

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>.

Re-use private key after failed CSR

**What this PR does / why we need it**:
This fixes a regression introduced in 1.11.

If we create a new key on each CSR, if CSR fails the next attempt will
create a new one instead of reusing previous CSR.

If approver/signer don't handle CSRs as quickly as new nodes come up,
they can pile up and approver would keep handling old abandoned CSRs and
Nodes would keep timing out on startup.

**Release note**:
```release-note
NONE
```

Kubernetes-commit: c855accaecb5775b515dad54799c37db6df51694
This commit is contained in:
Kubernetes Publisher 2018-06-13 18:33:03 -07:00
commit bb0a4fa2f7

View File

@ -88,7 +88,8 @@ func WriteKey(keyPath string, data []byte) error {
// can't find one, it will generate a new key and store it there. // can't find one, it will generate a new key and store it there.
func LoadOrGenerateKeyFile(keyPath string) (data []byte, wasGenerated bool, err error) { func LoadOrGenerateKeyFile(keyPath string) (data []byte, wasGenerated bool, err error) {
loadedData, err := ioutil.ReadFile(keyPath) loadedData, err := ioutil.ReadFile(keyPath)
if err == nil { // Call verifyKeyData to ensure the file wasn't empty/corrupt.
if err == nil && verifyKeyData(loadedData) {
return loadedData, false, err return loadedData, false, err
} }
if !os.IsNotExist(err) { if !os.IsNotExist(err) {
@ -181,3 +182,12 @@ func PublicKeysFromFile(file string) ([]interface{}, error) {
} }
return keys, nil return keys, nil
} }
// verifyKeyData returns true if the provided data appears to be a valid private key.
func verifyKeyData(data []byte) bool {
if len(data) == 0 {
return false
}
_, err := ParsePrivateKeyPEM(data)
return err == nil
}