Răsfoiți Sursa

Close idle connections on client certificate renew

This commit changes the ca client that returns a "tls.Config" to close
connections sitting idle in a keep-alive state. This forces new client
connections to use the new certificate.
Mariano Cano 11 luni în urmă
părinte
comite
5edadf7e4a
2 a modificat fișierele cu 14 adăugiri și 0 ștergeri
  1. 11 0
      ca/client.go
  2. 3 0
      ca/tls.go

+ 11 - 0
ca/client.go

@@ -80,6 +80,10 @@ func (c *uaClient) SetTransport(tr http.RoundTripper) {
 	c.Client.Transport = tr
 	c.Client.Transport = tr
 }
 }
 
 
+func (c *uaClient) CloseIdleConnections() {
+	c.Client.CloseIdleConnections()
+}
+
 func (c *uaClient) Get(u string) (*http.Response, error) {
 func (c *uaClient) Get(u string) (*http.Response, error) {
 	return c.GetWithContext(context.Background(), u)
 	return c.GetWithContext(context.Background(), u)
 }
 }
@@ -639,6 +643,13 @@ func (c *Client) SetTransport(tr http.RoundTripper) {
 	c.client.SetTransport(tr)
 	c.client.SetTransport(tr)
 }
 }
 
 
+// CloseIdleConnections closes any connections on its Transport which were
+// previously connected from previous requests but are now sitting idle in a
+// "keep-alive" state. It does not interrupt any connections currently in use.
+func (c *Client) CloseIdleConnections() {
+	c.client.CloseIdleConnections()
+}
+
 // Version performs the version request to the CA with an empty context and returns the
 // Version performs the version request to the CA with an empty context and returns the
 // api.VersionResponse struct.
 // api.VersionResponse struct.
 func (c *Client) Version() (*api.VersionResponse, error) {
 func (c *Client) Version() (*api.VersionResponse, error) {

+ 3 - 0
ca/tls.go

@@ -367,6 +367,9 @@ func getPEM(i interface{}) ([]byte, error) {
 
 
 func getRenewFunc(ctx *TLSOptionCtx, client *Client, tr *http.Transport, pk crypto.PrivateKey) RenewFunc {
 func getRenewFunc(ctx *TLSOptionCtx, client *Client, tr *http.Transport, pk crypto.PrivateKey) RenewFunc {
 	return func() (*tls.Certificate, error) {
 	return func() (*tls.Certificate, error) {
+		// Close connections in keep-alive state
+		defer client.CloseIdleConnections()
+
 		// Get updated list of roots
 		// Get updated list of roots
 		if err := ctx.applyRenew(); err != nil {
 		if err := ctx.applyRenew(); err != nil {
 			return nil, err
 			return nil, err