This repository has been archived by the owner on Jun 29, 2022. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 48
Implement automatic certificate rotation #1201
Closed
Closed
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3,3 +3,4 @@ clusterDNS: 10.0.0.10 | |
clusterDomain: cluster.local | ||
enableTLSBootstrap: true | ||
cloudProvider: | ||
kubernetesCACert: "" |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,158 @@ | ||
package cluster | ||
|
||
import ( | ||
"context" | ||
"fmt" | ||
"time" | ||
|
||
log "github.com/sirupsen/logrus" | ||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" | ||
"k8s.io/client-go/kubernetes" | ||
|
||
"github.com/kinvolk/lokomotive/pkg/k8sutil" | ||
"github.com/kinvolk/lokomotive/pkg/platform" | ||
) | ||
|
||
type certificateRotatorConfig struct { | ||
clientSet *kubernetes.Clientset | ||
newCACert string | ||
logger *log.Entry | ||
daemonSetsToRestart []platform.Workload | ||
deploymentsToRestart []platform.Workload | ||
} | ||
|
||
type certificateRotator struct { | ||
config certificateRotatorConfig | ||
} | ||
|
||
const ( | ||
// Time to wait between updating DaemonSet/Deployment and start looking if | ||
// workload has converged. This should account for kube-controller-manager election time, | ||
// reconciliation period and time spent in reconciliation loop (based on number of workloads | ||
// in the cluster). 10 seconds might not be enough. | ||
kubeControllerManagerReconciliationPeriod = 10 * time.Second | ||
) | ||
|
||
func newCertificateRotator(config certificateRotatorConfig) (*certificateRotator, error) { | ||
if config.clientSet == nil { | ||
return nil, fmt.Errorf("clientSet can't be nil") | ||
} | ||
|
||
if config.newCACert == "" { | ||
return nil, fmt.Errorf("new CA certificate can't be empty") | ||
} | ||
|
||
return &certificateRotator{ | ||
config: config, | ||
}, nil | ||
} | ||
|
||
func (cr *certificateRotator) restartDaemonSetAndWaitToConverge(ns, name string) error { | ||
dsc := cr.config.clientSet.AppsV1().DaemonSets(ns) | ||
|
||
generation, err := k8sutil.RolloutRestartDaemonSet(dsc, name) | ||
if err != nil { | ||
return fmt.Errorf("restarting: %w", err) | ||
} | ||
|
||
// TODO: make sure this is the right value. | ||
time.Sleep(kubeControllerManagerReconciliationPeriod) | ||
|
||
options := k8sutil.WaitOptions{ | ||
Generation: generation, | ||
} | ||
|
||
if err := k8sutil.WaitForDaemonSet(dsc, name, options); err != nil { | ||
return fmt.Errorf("waiting for DaemonSet to converge: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cr *certificateRotator) restartDeploymentAndWaitToConverge(ns, name string) error { | ||
deployClient := cr.config.clientSet.AppsV1().Deployments(ns) | ||
|
||
generation, err := k8sutil.RolloutRestartDeployment(deployClient, name) | ||
if err != nil { | ||
return fmt.Errorf("restarting: %w", err) | ||
} | ||
|
||
// TODO: make sure this is the right value. | ||
time.Sleep(kubeControllerManagerReconciliationPeriod) | ||
|
||
options := k8sutil.WaitOptions{ | ||
Generation: generation, | ||
} | ||
|
||
if err := k8sutil.WaitForDeployment(deployClient, name, options); err != nil { | ||
return fmt.Errorf("waiting for Deployment to converge: %w", err) | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cr *certificateRotator) rotate() error { | ||
cr.config.logger.Printf("Waiting for all service account tokens on the cluster to be updated...") | ||
|
||
if err := cr.waitForUpdatedServiceAccountTokens(); err != nil { | ||
return fmt.Errorf("waiting for all service account tokens to be updated: %w", err) | ||
} | ||
|
||
cr.config.logger.Printf("All service account tokens has been updated with new Kubernetes CA certificate") | ||
|
||
for _, daemonSet := range cr.config.daemonSetsToRestart { | ||
cr.config.logger.Printf("Restarting DaemonSet %s/%s to pick up new Kubernetes CA Certificate", | ||
daemonSet.Namespace, daemonSet.Name) | ||
|
||
if err := cr.restartDaemonSetAndWaitToConverge(daemonSet.Namespace, daemonSet.Name); err != nil { | ||
return fmt.Errorf("restarting DaemonSet %s/%s: %w", daemonSet.Namespace, daemonSet.Name, err) | ||
} | ||
} | ||
|
||
for _, deployment := range cr.config.deploymentsToRestart { | ||
cr.config.logger.Printf("Restarting Deployment %s/%s to pick up new Kubernetes CA Certificate", | ||
deployment.Namespace, deployment.Name) | ||
|
||
if err := cr.restartDeploymentAndWaitToConverge(deployment.Namespace, deployment.Name); err != nil { | ||
return fmt.Errorf("restarting Deployment %s/%s: %w", deployment.Namespace, deployment.Name, err) | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cr *certificateRotator) waitForUpdatedServiceAccountTokens() error { | ||
for { | ||
allUpToDate, err := cr.allServiceAccountTokensIncludeNewCA() | ||
if err != nil { | ||
return fmt.Errorf("checking if all service account tokens include new CA certificate: %w", err) | ||
} | ||
|
||
if allUpToDate { | ||
cr.config.logger.Printf("all service account tokens are up to date and have new CA certificate") | ||
|
||
break | ||
} | ||
} | ||
|
||
return nil | ||
} | ||
|
||
func (cr *certificateRotator) allServiceAccountTokensIncludeNewCA() (bool, error) { | ||
secrets, err := cr.config.clientSet.CoreV1().Secrets("").List(context.TODO(), metav1.ListOptions{ | ||
FieldSelector: "type=kubernetes.io/service-account-token", | ||
}) | ||
if err != nil { | ||
return false, fmt.Errorf("getting secrets: %v", err) | ||
} | ||
|
||
allUpToDate := true | ||
|
||
for _, v := range secrets.Items { | ||
if string(v.Data["ca.crt"]) != cr.config.newCACert { | ||
allUpToDate = false | ||
} | ||
} | ||
|
||
return allUpToDate, nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Might it be an idea to watch the leader election to ensure this instead of guessing a time