Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow to specify private key algorithm and size #168

Merged
merged 4 commits into from Mar 22, 2024

Conversation

MartinWeindel
Copy link
Member

What this PR does / why we need it:
The algorithm and size for the private key can now be specified in the certificate spec section to override the default algorithm RSA with key size 2048.
Supported algorithms are RSA and ECDSA. For RSA the allowed key sizes are 2048, 3072, and 4096 with 2048 as default is not specified explicitly. For ECDSA the allowed key sizes are 256 and 384 with 256 as default.
These algorithms and key sizes are supported by Let's encrypt. For other ACME servers please check their documentation for information about supported combinations.

Which issue(s) this PR fixes:
Fixes #

Special notes for your reviewer:

Release note:

The algorithm and size for the private key can now be specified in the certificate spec section to override the default algorithm `RSA` with key size 2048.
Supported algorithms are `RSA` and `ECDSA`. For `RSA` the allowed key sizes are `2048`, `3072`, and `4096` with `2048` as default is not specified explicitly. For `ECDSA` the allowed key sizes are `256` and `384` with `256` as default.
These algorithms and key sizes are supported by Let's Encrypt. For other ACME servers please check their documentation for information about supported combinations.

@MartinWeindel MartinWeindel requested a review from a team as a code owner March 19, 2024 16:22
@gardener-robot-ci-1 gardener-robot-ci-1 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Mar 19, 2024
@MartinWeindel
Copy link
Member Author

/invite @timuthy

@gardener-robot-ci-1 gardener-robot-ci-1 added needs/ok-to-test Needs approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Mar 19, 2024
@gardener-robot gardener-robot added kind/api-change API change with impact on API users needs/second-opinion Needs second review by someone else needs/review Needs review size/xl Size of pull request is huge (see gardener-robot robot/bots/size.py) labels Mar 19, 2024
@gardener-robot
Copy link

@MartinWeindel Command "/invite @timuthy " failed with "Reviews may only be requested from collaborators. One or more of the users or teams you specified is not a collaborator of the gardener/cert-management repository.".

Additional Information
Redacted in public. Check backend logs.

@timuthy
Copy link
Contributor

timuthy commented Mar 20, 2024

/assign

@gardener-robot-ci-3 gardener-robot-ci-3 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Mar 21, 2024
@gardener-robot-ci-1 gardener-robot-ci-1 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Mar 21, 2024
Copy link
Contributor

@timuthy timuthy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice feature 🙂

README.md Outdated
@@ -352,6 +354,33 @@ spec:

In this case the secret `my-secret` will contains the labels.

### Specifying private key algorithm and size

By default, the certificate uses `RSA` with a key size of 2048 bits for the private key
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
By default, the certificate uses `RSA` with a key size of 2048 bits for the private key
By default, the certificate uses `RSA` with a key size of 2048 bits for the private key.

// key size of 2048 will be used for `RSA` key algorithm and
// key size of 256 will be used for `ECDSA` key algorithm.
// +optional
Algorithm PrivateKeyAlgorithm `json:"algorithm,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Algorithm PrivateKeyAlgorithm `json:"algorithm,omitempty"`
Algorithm *PrivateKeyAlgorithm `json:"algorithm,omitempty"`

// and will default to `256` if not specified.
// No other values are allowed.
// +optional
Size int `json:"size,omitempty"`
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
Size int `json:"size,omitempty"`
Size *int32 `json:"size,omitempty"`

See https://github.com/kubernetes/community/blob/master/contributors/devel/sig-architecture/api-conventions.md#primitive-types.

return keyType, nil
}

// FromKeyType converts key type back to
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc string incomplete.

}
return client.Certificate.Obtain(request)
}

func obtainForCSR(client *lego.Client, csr []byte, deactivateAuthz bool, preferredChain string) (*certificate.Resource, error) {
// ToKeyType extracts the key type from the
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doc string incomplete.

Comment on lines +121 to +122
// key size of 256 will be used for `ECDSA` key algorithm.
// +optional
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we work with // +kubebuilder:validation:Enum=RSA;ECDSA here, maybe also for the size?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Interestingly, you can really define an enumeration for numbers.🤩

@gardener-robot gardener-robot added the needs/changes Needs (more) changes label Mar 21, 2024
@gardener-robot-ci-2 gardener-robot-ci-2 added reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) and removed reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) labels Mar 21, 2024
@gardener-robot-ci-3 gardener-robot-ci-3 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Mar 21, 2024
@gardener-robot-ci-2 gardener-robot-ci-2 removed the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Mar 21, 2024
Copy link
Contributor

@timuthy timuthy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

One doubt left, the rest looks good to me ✅

}
newPrivateKey := createPrivateKey(info.PrivateKeyAlgorithm, info.PrivateKeySize)
if !reflect.DeepEqual(spec.PrivateKey, newPrivateKey) {
spec.PrivateKey = newPrivateKey
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Not sure if I understand the code correctly: Does this mean that the controller writes back the defaulted values to the certificate.spec.privateKey if it's not set before? If yes, won't this cause all existing certificates to be re-generated when this feature is rolled out because the spec checksum changes?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If the source object has no private key related annotations, the function createPrivateKey returns nil. Even if you would add these annotations with the default values, the generated certificate resource will be updated, but no new certificate would be requested, as the hash code is unchanged.

Copy link
Contributor

@timuthy timuthy left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

/lgtm

@gardener-robot gardener-robot added reviewed/lgtm Has approval for merging and removed needs/changes Needs (more) changes needs/review Needs review labels Mar 22, 2024
@gardener-robot gardener-robot removed the needs/second-opinion Needs second review by someone else label Mar 22, 2024
@gardener-robot-ci-3 gardener-robot-ci-3 added the reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) label Mar 22, 2024
@MartinWeindel
Copy link
Member Author

@timuthy Thanks for taking the time for a review!

@MartinWeindel MartinWeindel merged commit 56e575a into master Mar 22, 2024
7 checks passed
@MartinWeindel MartinWeindel deleted the private-key-spec branch March 22, 2024 08:10
@gardener-robot gardener-robot added the status/closed Issue is closed (either delivered or triaged) label Mar 22, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
kind/api-change API change with impact on API users needs/ok-to-test Needs approval for testing (check PR in detail before setting this label because PR is run on CI/CD) reviewed/lgtm Has approval for merging reviewed/ok-to-test Has approval for testing (check PR in detail before setting this label because PR is run on CI/CD) size/xl Size of pull request is huge (see gardener-robot robot/bots/size.py) status/closed Issue is closed (either delivered or triaged)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

6 participants