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

refactor: certificates generation package and secrets #777

Merged
merged 7 commits into from
Jun 26, 2024

Conversation

fabriziosestito
Copy link
Contributor

@fabriziosestito fabriziosestito commented Jun 20, 2024

Description

This PR refactors the generation utilities in the admissionregistration package,
removing duplication and simplifying the current implementation.
It renames admissionregistration in certs and reactors the existing reconciler unit test in integration tests clauses.

Additional info

  • This PR also changes the golangci configuration to add an exception for the ok map index in the varnamelen linter. However we should uniform and clean up the golangci configurations across all the KW repos, this is tracked by: Uniform golangci configuration across all repositories #778

  • There are a few incongruences with the naming, for instance, this status

    PolicyServerCASecretReconciled PolicyServerConditionType = "CASecretReconciled"
    should be probably named CertSecretReconciled instead. Fixing this is out of scope for this PR and I will create an issue about it.

Fixes: #775, #664

Copy link

codecov bot commented Jun 20, 2024

Codecov Report

Attention: Patch coverage is 81.57895% with 14 lines in your changes missing coverage. Please review.

Project coverage is 72.97%. Comparing base (e3750b2) to head (c5a217e).

Files Patch % Lines
internal/pkg/admission/policy-server-ca-secret.go 73.33% 6 Missing and 2 partials ⚠️
internal/pkg/certs/certs.go 86.36% 3 Missing and 3 partials ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #777      +/-   ##
==========================================
- Coverage   74.05%   72.97%   -1.09%     
==========================================
  Files          28       26       -2     
  Lines        1808     1780      -28     
==========================================
- Hits         1339     1299      -40     
- Misses        353      366      +13     
+ Partials      116      115       -1     
Flag Coverage Δ
integration-tests 60.61% <81.57%> (-1.28%) ⬇️
unit-tests 37.87% <0.00%> (-12.33%) ⬇️

Flags with carried forward coverage won't be shown. Click here to find out more.

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@fabriziosestito fabriziosestito force-pushed the refactor/cert branch 4 times, most recently from fef8105 to bcb9c32 Compare June 21, 2024 12:03
@fabriziosestito fabriziosestito changed the title wip refactor cert refactor: certificates generation package and secrets Jun 21, 2024
@fabriziosestito fabriziosestito marked this pull request as ready for review June 21, 2024 12:04
@fabriziosestito fabriziosestito requested a review from a team as a code owner June 21, 2024 12:04
@fabriziosestito fabriziosestito self-assigned this Jun 21, 2024
@fabriziosestito fabriziosestito added this to the 1.15 milestone Jun 21, 2024
Copy link
Member

@flavio flavio left a comment

Choose a reason for hiding this comment

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

I think we should address some naming confusion about Policy Server CA, see my notes.

Overall LGTM

@@ -164,6 +164,14 @@ func getTestPolicyServerService(policyServerName string) (*corev1.Service, error
return &service, nil
}

func getTestPolicyServerCASecret() (*corev1.Secret, error) {
Copy link
Member

Choose a reason for hiding this comment

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

This is retrieving the Root CA created by our controller. I suggest to rename this helper function to reflect that.

Maybe something like getInternalRootCASecret ?

type generateCertFunc = func(ca []byte, commonName string, extraSANs []string, CAPrivateKey *rsa.PrivateKey) ([]byte, []byte, error)

func (r *Reconciler) fetchOrInitializePolicyServerCASecret(ctx context.Context, policyServer *policiesv1.PolicyServer, caSecret *corev1.Secret, generateCert generateCertFunc) error {
func (r *Reconciler) fetchOrInitializePolicyServerCASecret(ctx context.Context, policyServer *policiesv1.PolicyServer, caSecret *corev1.Secret) error {
Copy link
Member

Choose a reason for hiding this comment

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

Again, there's a bit of naming confusion here we inherited from the past.

This method creates the certificate used by a specific Policy Server instance, there's no CA associated with a Policy Server

}

func (r *Reconciler) fetchOrInitializePolicyServerCARootSecret(ctx context.Context, policyServer *policiesv1.PolicyServer, generateCA generateCAFunc, pemEncodeCertificate pemEncodeCertificateFunc) (*corev1.Secret, error) {
func (r *Reconciler) fetchOrInitializePolicyServerCARootSecret(ctx context.Context, policyServer *policiesv1.PolicyServer) (*corev1.Secret, error) {
Copy link
Member

Choose a reason for hiding this comment

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

Same here, the naming is confusing since no CA is stored inside of the Secret that is returned

return policyServerSecret, nil
}

func updateSecretCA(policyServerSecret *corev1.Secret, generateCA generateCAFunc, pemEncodeCertificate pemEncodeCertificateFunc) error {
caRoot, err := generateCA()
func updateCASecret(policyServerSecret *corev1.Secret) error {
Copy link
Member

Choose a reason for hiding this comment

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

This function is always generating the CA, it doesn't look into the contents of the secret that is passed as parameter.

Why don't we change the signature of the function to be something like that?

func createInternalCASecret() (policyServerSecret *corev1.Secret, error)

Copy link
Member

Choose a reason for hiding this comment

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

Maybe something for later, but I would also rename this file since it can mislead into thinking each Policy Server has a dedicated CA

internal/pkg/certs/ca.go Outdated Show resolved Hide resolved
internal/pkg/certs/cert.go Outdated Show resolved Hide resolved
internal/pkg/certs/cert.go Outdated Show resolved Hide resolved
@fabriziosestito
Copy link
Contributor Author

@flavio I've addressed the comments in c3cbdf4.

I tracked all the naming-related comments in: #782

Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
Signed-off-by: Fabrizio Sestito <fabrizio.sestito@suse.com>
@fabriziosestito
Copy link
Contributor Author

Blocked: waiting for 1.14 release

Copy link
Member

@flavio flavio left a comment

Choose a reason for hiding this comment

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

Approving, the naming changes I've suggested are going to be done inside of future PRs

@fabriziosestito fabriziosestito merged commit b79efed into kubewarden:main Jun 26, 2024
8 of 9 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Refactor admissionregistration package in cert
3 participants