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

cmd/contour: hot-reload certificates and key #2198

Merged
merged 1 commit into from
Feb 14, 2020

Conversation

tsaarni
Copy link
Member

@tsaarni tsaarni commented Feb 5, 2020

This change adds support for certificate rotation for XDS gRPC interface
between Contour and Envoy. It is achieved by lazily loading certificates
and key every time new TLS connection is established by Envoy.

This change addresses only the certificate rotation in Contour (server)
and similar support is needed for Envoy (client) to cover the whole use
case.

Signed-off-by: Tero Saarni [email protected]

@tsaarni
Copy link
Member Author

tsaarni commented Feb 5, 2020

This is just initial stab at #2143, all comments are more than welcome!

At this point I generated two sets of certificates for tests, but I saw #1994 and I wonder would you like to move to certificates that are generated on-the-fly by code?

@codecov
Copy link

codecov bot commented Feb 5, 2020

Codecov Report

Merging #2198 into master will increase coverage by 0.55%.
The diff coverage is n/a.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #2198      +/-   ##
==========================================
+ Coverage   78.16%   78.72%   +0.55%     
==========================================
  Files          57       57              
  Lines        5034     5048      +14     
==========================================
+ Hits         3935     3974      +39     
+ Misses       1013      984      -29     
- Partials       86       90       +4     
Impacted Files Coverage Δ
cmd/contour/contour.go 4.16% <0.00%> (+1.38%) ⬆️
cmd/contour/servecontext.go 94.49% <0.00%> (+26.07%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 7e31f54...9c78362. Read the comment docs.

@stevesloka stevesloka added this to the 1.2.0 milestone Feb 6, 2020
@stevesloka stevesloka added the release-note Denotes a PR that will be considered when it comes time to generate release notes. label Feb 6, 2020
@stevesloka
Copy link
Member

Thanks for this @tsaarni! I want to take a look at this soon. =)

@jpeach
Copy link
Contributor

jpeach commented Feb 7, 2020

I'd also like to review before merging.

@tsaarni
Copy link
Member Author

tsaarni commented Feb 7, 2020

Here is hands-on instruction how I tested this on a deployed system:

  1. Use openssl s_client to establish TLS connection and to observe the "Not Before" and "Not After" dates in the printed server certificate
kubectl -n projectcontour exec \
  $(kubectl -n projectcontour get pod -l app=envoy -o jsonpath='{.items[0].metadata.name}') -- \
  openssl s_client -connect contour:8001 -CAfile /ca/cacert.pem -cert /certs/tls.crt -key /certs/tls.key \
  | openssl x509 -text -noout
  1. Re-create the server certificate and key for Contour and overwrite the old one with it:
kubectl -n projectcontour create secret tls contourcert \
  --cert=certs/contour.pem --key=certs/contour-key.pem \
  --dry-run -o yaml \
  | kubectl apply -f -
  1. Wait for about 2 minutes for kubelet to update the secrets to the filesystem
  2. Run openssl s_client again to observe that the "Not Before" and "Not After" validity times were updated
  3. Do steps 1-4 again to ensure that the process can be repeated multiple times
  4. Observe that Envoy is still up and in good state by checking pod state and by running traffic
  5. Delete Envoy pod to trigger new TLS connection from client
  6. Observe that the connection was established successfully by checking Envoy pod readiness state, logs and by running traffic

In this sequence I assume that in step 2. the server certificate was re-created with the same old CA i.e. CA was not re-created.

Copy link
Member

@youngnick youngnick left a comment

Choose a reason for hiding this comment

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

Thanks for this. This LGTM, I like the use of testdata for the testing certs.

Also, thanks for the detailed testing-this-PR steps, that saved a lot of time in review.

Copy link
Contributor

@jpeach jpeach left a comment

Choose a reason for hiding this comment

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

I agree that this looks pretty good. I had a few small nits, but very happy with the tests here.

We should be sure that we are OK with reloading the certificates on every TLS connection, and file a separate issue to make sure that we document this as part of the certificate rotation guide.

@@ -221,6 +221,7 @@ func (ctx *serveContext) tlsconfig() *tls.Config {
err := ctx.verifyTLSFlags()
check(err)

// load certificates and key to catch configuration errors early
Copy link
Contributor

Choose a reason for hiding this comment

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

Nit: Comments should be full sentences with punctuation (here and in other files):

// Load certificates and key to catch configuration errors early.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, I have now rewritten all my comments as full sentences.

cmd/contour/servecontext.go Show resolved Hide resolved
cmd/contour/servecontext.go Show resolved Hide resolved

// create temporary directory to store certificates and key for the server
configDir, err := ioutil.TempDir("", "contour-testdata-")
checkErr(t, err)
Copy link
Contributor

Choose a reason for hiding this comment

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

In this case, checkErr should call t.Fatalf. It looks like all the other uses in this file should be fatal too, so we can rename it to checkFatalErr or something, and update it.

Copy link
Member Author

Choose a reason for hiding this comment

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

I renamed checkErr() to checkFatalErr() and changed it to call t.Fatal.

expectedServerCert: "testdata/2/contourcert.pem",
expectError: false,
},
"fail to connect with bad client certificate": {
Copy link
Contributor

Choose a reason for hiding this comment

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

IIUC, this fails because the client and server don't agree on the CA root certificate to expect? Let's update the description.

Copy link
Member Author

Choose a reason for hiding this comment

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

Yes exactly, I updated the description.

This change adds support for certificate rotation for XDS gRPC interface
between Contour and Envoy. It is achieved by lazily loading certificates
and key every time new TLS connection is established by Envoy.

This change addresses only the certificate rotation in Contour (server)
and similar support is needed for Envoy (client) to cover the whole use
case.

Signed-off-by: Tero Saarni <[email protected]>
Copy link
Contributor

@jpeach jpeach left a comment

Choose a reason for hiding this comment

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

LGTM

@jpeach jpeach merged commit 639889f into projectcontour:master Feb 14, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
release-note Denotes a PR that will be considered when it comes time to generate release notes.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants