Skip to content
This repository was archived by the owner on Jun 7, 2023. It is now read-only.

Adding namespace to SAN - #265

Merged
knative-prow[bot] merged 17 commits into
knative-extensions:mainfrom
davidhadas:namespaceSANs
Mar 23, 2023
Merged

knative-prow[bot] merged 17 commits into
knative-extensions:mainfrom
davidhadas:namespaceSANs

Conversation

@davidhadas

@davidhadas davidhadas commented Mar 3, 2023 •

Copy link
Copy Markdown
Contributor

Fixes #264

This PR aims at a minimalistic change

  1. Add namespace SANs to certificates + modify tests accordingly.
  2. Since it is meant to be used also for mTLS, the produced certificates need to serve dual use - both as a client certificate and as a server certificate. For example, the Activator will use this certificate as both client and server. This resulted in merging the creation of a client template with that of a server template.

Note:
While working on this PR many other opportunities to merge code poped up. The terminology used by the code (data-plane, control-plane) is somewhat unclear and it is hard to see the relationship between the naming and the certificates created and their use. These are left for a separate PR after a discussion of the correct terminology and the intent for the separation between control-plane and data-plane certificates.

Items for review:

  • Do we want the SAN to be <namespace> or <prefix>-<namespace>? For some reason, the author of this PR followed knative-<namespace> but cant justify why this prefix is helpful.

@knative-prow knative-prow Bot added do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 3, 2023
@codecov

codecov Bot commented Mar 3, 2023 •

Copy link
Copy Markdown

Codecov Report

Merging #265 (4ed838c) into main (e5eadf2) will decrease coverage by 0.44%.
The diff coverage is 66.66%.

@@            Coverage Diff             @@
##             main     #265      +/-   ##
==========================================
- Coverage   73.11%   72.68%   -0.44%     
==========================================
  Files          23       23              
  Lines        1380     1380              
==========================================
- Hits         1009     1003       -6     
- Misses        315      319       +4     
- Partials       56       58       +2     
Impacted Files Coverage Δ
pkg/network/tls.go 0.00% <0.00%> (ø)
pkg/certificates/reconciler/certificates.go 69.52% <100.00%> (ø)
pkg/reconciler/tls_dialer_factory.go 85.36% <100.00%> (ø)

... and 1 file with indirect coverage changes

Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here.

@davidhadas davidhadas changed the title WIP: Adding namespace to SAN - for CreateDataPlaneCert only WIP: Adding namespace to SAN Mar 3, 2023
Signed-off-by: David Hadas <[email protected]>
Signed-off-by: David Hadas <[email protected]>
@davidhadas

Copy link
Copy Markdown
Contributor Author

/retest

@knative-prow knative-prow Bot added size/L Denotes a PR that changes 100-499 lines, ignoring generated files. and removed size/M Denotes a PR that changes 30-99 lines, ignoring generated files. labels Mar 21, 2023
@davidhadas

Copy link
Copy Markdown
Contributor Author

Current code terminology is a bit confusing.
Is there a clear distinction between "control-plane" certificates "data-plane" certificates?
Why are they separated in code?
Does it really matter from TLS and certificate point of view if the entity (client or server) is classified as a control or data?

I would like to first have a fix for SANs in this PR, then consider if we need to open an issue and create a second PR to rearrange the code by removing the current separation of "control-plane" vs. "data-plane" certificates if this is not needed. Alternatively, we may need to add some remarks and make it more clear or improve naming in this project to make it clearer when we refer to clients or server etc..

@davidhadas

Copy link
Copy Markdown
Contributor Author

/retest

@davidhadas davidhadas changed the title WIP: Adding namespace to SAN Adding namespace to SAN Mar 21, 2023
@knative-prow knative-prow Bot removed the do-not-merge/work-in-progress Indicates that a PR should not merge because it is a work in progress. label Mar 21, 2023
@davidhadas

Copy link
Copy Markdown
Contributor Author

/cc @ReToCode @nak3 @evankanderson

@knative-prow
knative-prow Bot requested review from evankanderson and nak3 March 21, 2023 21:09
@knative-prow
knative-prow Bot requested a review from ReToCode March 21, 2023 21:09
Comment thread pkg/certificates/certs.go Outdated
// Create cert template suitable for CA and hence signing
func createCACertTemplate(expirationInterval time.Duration) (*x509.Certificate, error) {
rootCert, err := createCertTemplate(expirationInterval)
rootCert, err := createCertTemplate(expirationInterval, FakeDnsName)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Will it create duplicated FakeDnsName in DNSNames: []string{FakeDnsName, FakeDnsName} in createCertTemplate()?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Yes it will. This is at the CA Template where I am not sure why we have Subject Alternate Name values in the first place. I believe it can probably be removed in a later cleanup PR.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you take a list of SANs in createCertTemplate, you can leave the list empty here.

(In fact, you could remove the DNSNames from the CA cert if you require the callers of createCertTemplate to pass in fakeDnsName...)

Comment thread pkg/certificates/certs.go
clientCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
clientCert.Subject = pkix.Name{
cert.KeyUsage = x509.KeyUsageDigitalSignature
cert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Sorry I am also not sure why the original author separated the data-plane vs control-plane as you raised the question #265 (comment)
But if it is no problem for having both x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth as security practice, I think it is not necessary for the separation.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

... thinking about this, I can't see any special risk in a having a certificate with both usages.

My thinking:

  • Key Usages instruct the other side (client or server) about whether the cert is intended for that authorization purpose.
  • In order to confuse the client / server usage, you would need to do something like initiate a simultaneous TCP handshake (where the victim thinks they are the client, but the remote side thinks they are the server). This is likely to fall down pretty quickly after the TLS handshake, if not before, because none of our protocols are "palindromic" (you can speak them the same as client or server).
  • Additionally, we're only using this for network communication, not for storing records, so it would be hard to "trick" a server into signing something and then putting it in storage on the server's behalf.

@ReToCode ReToCode left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I agree with you wrt naming. Maybe we could also think about something better for the CA:
CommonName: "control-plane"
I think its not ideal, as this is also used to sign data-plane certs and potentially more in the future. Could it be something like knative-internal-ca?

Comment thread pkg/certificates/certs.go Outdated
return nil, err
}
serverCertTemplate, err := createServerCertTemplate(expirationInterval)
certTemplate, err := createClientServerCertTemplate(expirationInterval, san)

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Naming is really a bit off overall. Why is this ClientServer? The certs are just used as TLS server certs, right? If we at some point add a client cert for mTLS, we should start talking about client-certs.

@davidhadas davidhadas Mar 22, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Where does the requirement to have separate certificates for client and server come from?
Certificates identify an entity - is there any reason why that entity should use two separate certificates (and reconcile them, load them etc.) - one when it is a client and another when it is a server?

This PR should enable mTLS as well so the certificates need to be defined for use as client and server (or we need to create an additional set of certificates per namespace, which I am not sure is required).

This use to be Server and changed to ClientServer. I am planning a additional PR to consolidate the code and perform renaming as needed - in this PR I changed the name from Server to ClientServer simply since it is longer true to call it a server certificate when wen enable the certificate for use as both client and server.

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

I see, the current name just implies where it is used (for now just server side, future maybe also client side). Going with your explanation, maybe we could just name it createCertificateTemplate then?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

These were my thoughts also, until I realized we already have a function named createCertTemplate :) - so now, adding a function namedcreateCertificateTemplate is very confusing.

So let's fix that in the next PR when we will focus on renaming....

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 that a single cert for both client and server seems safe (see above, and usage elsewhere with, for example, service meshes).

I'd recommend fixing the public naming now along with the breaking API change, and leaving the internal function naming to sort out later.

Comment thread pkg/certificates/certs.go Outdated
Organization: []string{Organization},
// Do not use the same value with CA.
CommonName: "control-plane-client",
CommonName: "control-plane-client-server",

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

Same here, why is this client-server?

@davidhadas davidhadas Mar 22, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

See response to the other comment on the same subject

@davidhadas

Copy link
Copy Markdown
Contributor Author

CommonName: "control-plane"
I think its not ideal, as this is also used to sign data-plane certs and potentially more in the future. Could it be something like knative-internal-ca?

This requires changes in other packages as well.
If this is ok, I prefer to do that in a separate PR after we make sure that the new names will work system-wide. Or, that we can add the new names (e.g. knative-internal-ca) now and delete the old names later in a separate PR after making the system work with the new names.

@ReToCode

Copy link
Copy Markdown

This requires changes in other packages as well. If this is ok, I prefer to do that in a separate PR after we make sure that the new names will work system-wide

I'm fine with that. But I think we will, for a short period of time, break some stuff in other repos anyways. We should just make sure that the PRs to fix it are available in a timely manner.

@ReToCode ReToCode left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

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

/lgtm

@knative-prow knative-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Mar 22, 2023

@evankanderson evankanderson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Overall, I like this, but I think we might want to adjust things a little bit:

  • Bite the bullet and add a new CreateServerCert method which takes a list of SANs.
  • Expose fakeDnsName as LegacyFakeDnsName
  • Migrate callers to CreateServerCert, and add LegacyFakeDnsName to the list of SANs provided at the caller site.
  • (Add a flag at the caller site to be able to drop LegacyFakeDnsName in the future)

Comment thread .codecov.yaml

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Can this be a separate PR?

It seems useful, but unrelated to the rest of the change.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I added it since it was required to get this PR pass the tests...

Comment thread pkg/certificates/certs.go Outdated

// Copy-pasted from https://github.com/knative/pkg/blob/975a1cf9e4470b26ce54d9cc628dbd50716b6b95/webhook/certificates/resources/certs.go
func createCertTemplate(expirationInterval time.Duration) (*x509.Certificate, error) {
func createCertTemplate(expirationInterval time.Duration, san string) (*x509.Certificate, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should this take a list of SANs, given some of our migration plans?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

sure

Comment thread pkg/certificates/certs.go Outdated
// Create cert template suitable for CA and hence signing
func createCACertTemplate(expirationInterval time.Duration) (*x509.Certificate, error) {
rootCert, err := createCertTemplate(expirationInterval)
rootCert, err := createCertTemplate(expirationInterval, FakeDnsName)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

If you take a list of SANs in createCertTemplate, you can leave the list empty here.

(In fact, you could remove the DNSNames from the CA cert if you require the callers of createCertTemplate to pass in fakeDnsName...)

Comment thread pkg/certificates/certs.go
clientCert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth}
clientCert.Subject = pkix.Name{
cert.KeyUsage = x509.KeyUsageDigitalSignature
cert.ExtKeyUsage = []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth, x509.ExtKeyUsageClientAuth}

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

... thinking about this, I can't see any special risk in a having a certificate with both usages.

My thinking:

  • Key Usages instruct the other side (client or server) about whether the cert is intended for that authorization purpose.
  • In order to confuse the client / server usage, you would need to do something like initiate a simultaneous TCP handshake (where the victim thinks they are the client, but the remote side thinks they are the server). This is likely to fall down pretty quickly after the TLS handshake, if not before, because none of our protocols are "palindromic" (you can speak them the same as client or server).
  • Additionally, we're only using this for network communication, not for storing records, so it would be hard to "trick" a server into signing something and then putting it in storage on the server's behalf.

Comment thread pkg/certificates/certs.go Outdated
Organization: []string{Organization},
// Do not use the same value with CA.
CommonName: "control-plane-client",
CommonName: "control-plane-client-server",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we put in the SAN for the CommonName?

If not (and we're not worried about changing the CN), I might remove control-plane from the CN, but this is also fine.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Which SAN? we have a list of SANs. If I recall, the common name is ignored given SANs.
Still it is customary to put some meaningful common name in a certificate.

I would like to post here "control-protocol-certificate" just to move one step away from control-plan and data-plan issues that we will need to solve next anyhow.

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

This is really a nit, and isn't super-important either way.

Comment thread pkg/certificates/certs.go Outdated

// CreateControlPlaneCert generates the certificate for the client
func CreateControlPlaneCert(ctx context.Context, caKey *rsa.PrivateKey, caCertificate *x509.Certificate, expirationInterval time.Duration) (*KeyPair, error) {
func CreateControlPlaneCert(ctx context.Context, caKey *rsa.PrivateKey, caCertificate *x509.Certificate, expirationInterval time.Duration, san string) (*KeyPair, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

I'd suggest not making a breaking change in the library. Two options:

  1. Make this san ...string, and have empty string maintain the current behavior.
  2. Add a new function, e.g. CreateServerCert which takes the new arguments.

When we make breaking changes in these libraries, it gums up the dependency / buoy process for upgrading all the clients.

We could still remove this function (if we want) after we've migrated the callers, but I'd prefer not to get in the habit of making breaking library changes in broad dependencies (and, sadly, control-protocol has broad dependencies).

@davidhadas davidhadas Mar 23, 2023 •

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

nice.
Let's go with option 1

Comment thread pkg/certificates/certs.go Outdated

// CreateDataPlaneCert generates the certificate for the server
func CreateDataPlaneCert(ctx context.Context, caKey *rsa.PrivateKey, caCertificate *x509.Certificate, expirationInterval time.Duration) (*KeyPair, error) {
func CreateDataPlaneCert(ctx context.Context, caKey *rsa.PrivateKey, caCertificate *x509.Certificate, expirationInterval time.Duration, san string) (*KeyPair, error) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Ditto in terms of breaking signatures.

If CreateControlPlaneCert and CreateDataPlaneCert do the same thing now, it seems like a new function might be for the best long-run.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Let's leave this for a subsequent PR.
There are some opportunities to merge code and rethink what we name here as data-plane or control-plane and if it has anything to do with data or control.
We also need to think if we need separation of data and control certificate wise...
If so, we don't do that today (considering that everything in knative-serving now uses the same certificate ;) )

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Now realizing that this relates to the comment below asking to fix now Public Naming...

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

ok, consolidated code into CreateCet(), left the two existing public functions CreateControlPlaneCert and CreateDataPlaneCert as deprecated with the intention to clean them soon.

Comment thread pkg/certificates/certs.go Outdated
return nil, err
}
serverCertTemplate, err := createServerCertTemplate(expirationInterval)
certTemplate, err := createClientServerCertTemplate(expirationInterval, san)

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

👍 that a single cert for both client and server seems safe (see above, and usage elsewhere with, for example, service meshes).

I'd recommend fixing the public naming now along with the breaking API change, and leaving the internal function naming to sort out later.

Comment thread pkg/network/tls.go
ClientCAs: certPool,
ClientAuth: tls.RequireAndVerifyClientCert,
ServerName: certificates.FakeDnsName,
ServerName: "knative-myns",

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Should we check the old name as well as the new name?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

My assumption is that we are on the path to getting to namespace SANs, we keep the old names just to ensure we get there in gradual non disruptive steps...

@davidhadas

Copy link
Copy Markdown
Contributor Author
  • Bite the bullet and add a new CreateServerCert method which takes a list of SANs.
    Added CreateCert as it creates cert for both client and server
  • Expose fakeDnsName as LegacyFakeDnsName
    Already exposed as FakeDnsName, changed to LegacyFakeDnsName
  • Migrate callers to CreateServerCert, and add LegacyFakeDnsName to the list of SANs provided at the caller site.
    Migrated all internal callers, we will need to now migrate any other caller from other packages.
  • (Add a flag at the caller site to be able to drop LegacyFakeDnsName in the future)
    I think a PR will do it.

@davidhadas

davidhadas commented Mar 23, 2023 •

Copy link
Copy Markdown
Contributor Author

Do we want the namespace SAN to be <namespace> or <prefix>-<namespace>?
For some reason, the author of this PR followed knative-<namespace> but can't justify why this prefix is helpful...
Should we leave it? or change it?

@evankanderson

Copy link
Copy Markdown
Contributor

Do we want the SAN to be or -? For some reason, the author of this PR followed knative- but cant justify why this prefix is helpful.

I think the prefix is helpful for helping users connect the dots if the prefix is sufficiently specific.

@knative-prow knative-prow Bot removed the lgtm Indicates that a PR is ready to be merged. label Mar 23, 2023

@evankanderson evankanderson left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

/lgtm
/approve

Comment on lines 101 to 105
if secret.Labels[r.secretTypeLabelName] == dataPlaneSecretType {
keyPair, err = certificates.CreateDataPlaneCert(ctx, caPk, caCert, expirationInterval)
keyPair, err = certificates.CreateCert(ctx, caPk, caCert, expirationInterval, sans...)
} else if secret.Labels[r.secretTypeLabelName] == controlPlaneSecretType {
keyPair, err = certificates.CreateControlPlaneCert(ctx, caPk, caCert, expirationInterval)
keyPair, err = certificates.CreateCert(ctx, caPk, caCert, expirationInterval, sans...)
} else {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Do we need both if branches here still?

@evankanderson evankanderson Mar 23, 2023 •

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

(This can be a followup PR, or maybe we're tearing all this code out...)

@knative-prow knative-prow Bot added the lgtm Indicates that a PR is ready to be merged. label Mar 23, 2023
@knative-prow

knative-prow Bot commented Mar 23, 2023

Copy link
Copy Markdown

[APPROVALNOTIFIER] This PR is APPROVED

This pull-request has been approved by: davidhadas, evankanderson, ReToCode

The full list of commands accepted by this bot can be found here.

The pull request process is described here

Details Needs approval from an approver in each of these files:

Approvers can indicate their approval by writing /approve in a comment
Approvers can cancel approval by writing /approve cancel in a comment

@knative-prow knative-prow Bot added the approved Indicates a PR has been approved by an approver from all required OWNERS files. label Mar 23, 2023
@knative-prow
knative-prow Bot merged commit 33b1cf0 into knative-extensions:main Mar 23, 2023
Comment on lines +20 to +22
Organization = "knative.dev"
LegacyFakeDnsName = "data-plane." + Organization
DataPlaneNamePrefix = "knative-"

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

A lot of downstream repos seem to use the FakeDnsName constant - so this is breaking? Are you doing follow up PRs to the various net-* repos?

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

I suggest we fix this here - see #269
And later follow up with a more gradual change in dependencies.

@davidhadas

Copy link
Copy Markdown
Contributor Author

I suggest we fix this here - see #269
And later follow up with a more gradual change in dependencies.

Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Labels

approved Indicates a PR has been approved by an approver from all required OWNERS files. lgtm Indicates that a PR is ready to be merged. size/L Denotes a PR that changes 100-499 lines, ignoring generated files.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Change certificates to include namespace specific SANs

5 participants