@@ -76,6 +76,8 @@ func generateDaemonset(name, enforcer, runtime, socket, btfPresent, apparmorfs,
76
76
vols = append (vols , common .KubeArmorCaVolume ... )
77
77
volMnts = append (volMnts , common .KubeArmorCaVolumeMount ... )
78
78
common .AddOrReplaceArg ("-tlsEnabled=true" , "-tlsEnabled=false" , & daemonset .Spec .Template .Spec .Containers [0 ].Args )
79
+ } else {
80
+ common .AddOrReplaceArg ("-tlsEnabled=false" , "-tlsEnabled=true" , & daemonset .Spec .Template .Spec .Containers [0 ].Args )
79
81
}
80
82
daemonset .Spec .Template .Spec .Volumes = vols
81
83
daemonset .Spec .Template .Spec .InitContainers [0 ].VolumeMounts = commonVolMnts
@@ -426,7 +428,6 @@ func (clusterWatcher *ClusterWatcher) WatchRequiredResources() {
426
428
var caCert , tlsCrt , tlsKey * bytes.Buffer
427
429
var kGenErr , err , installErr error
428
430
RotateTls := false
429
- FirstRun := true
430
431
srvAccs := []* corev1.ServiceAccount {
431
432
addOwnership (deployments .GetServiceAccount (common .Namespace )).(* corev1.ServiceAccount ),
432
433
addOwnership (deployments .GetRelayServiceAccount (common .Namespace )).(* corev1.ServiceAccount ),
@@ -486,6 +487,8 @@ func (clusterWatcher *ClusterWatcher) WatchRequiredResources() {
486
487
relayServer .Spec .Template .Spec .Volumes =
487
488
append (relayServer .Spec .Template .Spec .Volumes , common .KubeArmorRelayTlsVolume ... )
488
489
common .AddOrReplaceArg ("-tlsEnabled=true" , "-tlsEnabled=false" , & relayServer .Spec .Template .Spec .Containers [0 ].Args )
490
+ } else {
491
+ common .AddOrReplaceArg ("-tlsEnabled=false" , "-tlsEnabled=true" , & relayServer .Spec .Template .Spec .Containers [0 ].Args )
489
492
}
490
493
491
494
// update images
@@ -631,10 +634,7 @@ func (clusterWatcher *ClusterWatcher) WatchRequiredResources() {
631
634
installErr = err
632
635
clusterWatcher .Log .Warnf ("Cannot create secret %s, error=%s" , secret .Name , err .Error ())
633
636
} else {
634
- RotateTls = true && ! FirstRun
635
- if ! FirstRun {
636
- clusterWatcher .Log .Warnf ("Secret was created, rotating TLS secrets" )
637
- }
637
+ caInK8sSecret = secret .Data ["ca.crt" ]
638
638
}
639
639
} else if err == nil {
640
640
caInK8sSecret = s .Data ["ca.crt" ]
@@ -672,10 +672,7 @@ func (clusterWatcher *ClusterWatcher) WatchRequiredResources() {
672
672
} else if err == nil {
673
673
if ! bytes .Equal (hook .Webhooks [0 ].ClientConfig .CABundle , caInK8sSecret ) {
674
674
// rotate
675
- RotateTls = true && ! FirstRun
676
- if ! FirstRun {
677
- clusterWatcher .Log .Warnf ("mutation CA cert does not match secret CA cert, rotating tls secrets" )
678
- }
675
+ RotateTls = true
679
676
}
680
677
} else {
681
678
installErr = err
@@ -698,107 +695,55 @@ func (clusterWatcher *ClusterWatcher) WatchRequiredResources() {
698
695
clusterWatcher .RotateTlsCerts ()
699
696
RotateTls = false
700
697
}
701
- if FirstRun {
702
- FirstRun = false
703
- }
704
698
time .Sleep (10 * time .Second )
705
699
}
706
700
}
707
701
708
702
func (clusterWatcher * ClusterWatcher ) RotateTlsCerts () {
709
- var suffix string
710
703
var caCert , tlsCrt , tlsKey * bytes.Buffer
711
704
var err error
712
- retries := 0
713
- for {
714
- if retries == 3 {
715
- return
716
- }
717
- retries ++
718
- suffix , err = common .GetFreeRandSuffix (clusterWatcher .Client , common .Namespace )
719
- if err == nil {
720
- clusterWatcher .Log .Infof ("Using suffix %s for all new temorary resources" , suffix )
721
- break
722
- }
723
- clusterWatcher .Log .Infof ("Cannot find a suffix, err=%s, retrying in 3 seconds..." , err .Error ())
724
- time .Sleep (3 * time .Second )
725
- }
726
- serviceName := deployments .KubeArmorControllerWebhookServiceName + "-" + suffix
727
- for {
728
- caCert , tlsCrt , tlsKey , err = common .GeneratePki (common .Namespace , serviceName )
729
- if err == nil {
730
- break
731
- }
732
- clusterWatcher .Log .Infof ("Couldn't generate TLS secret, retrying in 3 seconds" )
733
- time .Sleep (3 * time .Second )
734
- }
735
- tmpsecret := deployments .GetKubeArmorControllerTLSSecret (common .Namespace , caCert .String (), tlsCrt .String (), tlsKey .String ())
736
- tmpsecret = addOwnership (tmpsecret ).(* corev1.Secret )
737
- tmpsecret .Name = tmpsecret .GetName () + "-" + suffix
738
- _ , err = clusterWatcher .Client .CoreV1 ().Secrets (common .Namespace ).Create (context .Background (), tmpsecret , metav1.CreateOptions {})
705
+
706
+ origdeploy , err := clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Get (context .Background (), deployments .KubeArmorControllerDeploymentName , metav1.GetOptions {})
739
707
if err != nil {
740
- clusterWatcher .Log .Warnf ("Cannot create secret %s, error=%s" , tmpsecret .Name , err .Error ())
741
- }
742
- tmpdeploy := deployments .GetKubeArmorControllerDeployment (common .Namespace )
743
- tmpdeploy = addOwnership (tmpdeploy ).(* appsv1.Deployment )
744
- tmpdeploy .Name = tmpdeploy .GetName () + "-" + suffix
745
- for i , s := range tmpdeploy .Spec .Template .Spec .Volumes {
746
- if s .Name == "cert" {
747
- s .Secret .SecretName = tmpsecret .GetName ()
748
- tmpdeploy .Spec .Template .Spec .Volumes [i ] = s
749
- break
750
- }
708
+ clusterWatcher .Log .Warnf ("cannot get controller deployment, error=%s" , err .Error ())
751
709
}
752
- selectLabels := tmpdeploy .Spec .Selector .MatchLabels
753
- selectLabels ["kubearmor-app" ] = suffix
754
- tmpdeploy .Spec .Selector .MatchLabels = selectLabels
755
- origdeploy , _ := clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Get (context .Background (), deployments .KubeArmorControllerDeploymentName , metav1.GetOptions {})
756
- origdeploy = addOwnership (origdeploy ).(* appsv1.Deployment )
757
- tmpdeploy .Spec .Replicas = origdeploy .Spec .Replicas
758
- if _ , err := clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Create (context .Background (), tmpdeploy , metav1.CreateOptions {}); err != nil {
759
- clusterWatcher .Log .Warnf ("Cannot create deployment %s, error=%s" , tmpdeploy .Name , err .Error ())
760
- }
761
-
762
- time .Sleep (10 * time .Second )
763
-
764
- tmpservice := deployments .GetKubeArmorControllerWebhookService (common .Namespace )
765
- tmpservice = addOwnership (tmpservice ).(* corev1.Service )
766
- tmpservice .Name = serviceName
767
- tmpservice .Spec .Selector = selectLabels
768
- if _ , err := clusterWatcher .Client .CoreV1 ().Services (common .Namespace ).Create (context .Background (), tmpservice , metav1.CreateOptions {}); err != nil {
769
- clusterWatcher .Log .Warnf ("Cannot create deployment %s, error=%s" , tmpservice .Name , err .Error ())
770
- }
771
- tmpmutation := deployments .GetKubeArmorControllerMutationAdmissionConfiguration (common .Namespace , caCert .Bytes ())
772
- mutationName := tmpmutation .Name
773
- tmpmutation = addOwnership (tmpmutation ).(* v1.MutatingWebhookConfiguration )
774
- tmpmutation .Name = tmpmutation .Name + "-" + suffix
775
- tmpmutation .Webhooks [0 ].ClientConfig .Service .Name = tmpservice .GetName ()
776
- if _ , err := clusterWatcher .Client .AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Create (context .Background (), tmpmutation , metav1.CreateOptions {}); err != nil {
777
- clusterWatcher .Log .Warnf ("Cannot create mutation webhook %s, error=%s" , tmpmutation .Name , err .Error ())
778
- }
779
- clusterWatcher .Client .AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Delete (context .Background (), mutationName , metav1.DeleteOptions {})
710
+
780
711
caCert , tlsCrt , tlsKey , _ = common .GeneratePki (common .Namespace , deployments .KubeArmorControllerWebhookServiceName )
781
- secret := deployments .GetKubeArmorControllerTLSSecret (common .Namespace , caCert .String (), tlsCrt .String (), tlsKey .String ())
782
- secret = addOwnership (secret ).(* corev1.Secret )
783
- clusterWatcher .Client .CoreV1 ().Secrets (common .Namespace ).Update (context .Background (), secret , metav1.UpdateOptions {})
784
-
785
- replicas := int32 (0 )
786
- origdeploy .Spec .Replicas = & replicas
787
- clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Update (context .Background (), origdeploy , metav1.UpdateOptions {})
788
- time .Sleep (10 * time .Second )
789
- origdeploy , _ = clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Get (context .Background (), deployments .KubeArmorControllerDeploymentName , metav1.GetOptions {})
790
- origdeploy = addOwnership (origdeploy ).(* appsv1.Deployment )
791
- origdeploy .Spec .Replicas = tmpdeploy .Spec .Replicas
792
-
793
- clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Update (context .Background (), origdeploy , metav1.UpdateOptions {})
794
- mutation := deployments .GetKubeArmorControllerMutationAdmissionConfiguration (common .Namespace , caCert .Bytes ())
795
- mutation = addOwnership (mutation ).(* v1.MutatingWebhookConfiguration )
796
-
797
- clusterWatcher .Client .AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Create (context .Background (), mutation , metav1.CreateOptions {})
798
-
799
- clusterWatcher .Client .AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Delete (context .Background (), tmpmutation .Name , metav1.DeleteOptions {})
800
- clusterWatcher .Client .CoreV1 ().Services (common .Namespace ).Delete (context .Background (), tmpservice .Name , metav1.DeleteOptions {})
801
- clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Delete (context .Background (), tmpdeploy .Name , metav1.DeleteOptions {})
802
- clusterWatcher .Client .CoreV1 ().Secrets (common .Namespace ).Delete (context .Background (), tmpsecret .Name , metav1.DeleteOptions {})
712
+ replicas := origdeploy .Spec .Replicas
713
+
714
+ // TODO: Keep CA certificate in k8s secret
715
+
716
+ // == CLEANUP ==
717
+ // scale down controller deployment to 0
718
+ controllerDeployment := deployments .GetKubeArmorControllerDeployment (common .Namespace )
719
+ controllerDeployment = addOwnership (controllerDeployment ).(* appsv1.Deployment )
720
+ zeroReplicas := int32 (0 )
721
+ controllerDeployment .Spec .Replicas = & zeroReplicas
722
+ if _ , err := clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Update (context .Background (), controllerDeployment , metav1.UpdateOptions {}); err != nil {
723
+ clusterWatcher .Log .Warnf ("cannot scale down controller %s, error=%s" , controllerDeployment .Name , err .Error ())
724
+ }
725
+ // delete mutation webhook configuration
726
+ mutationWebhook := deployments .GetKubeArmorControllerMutationAdmissionConfiguration (common .Namespace , caCert .Bytes ())
727
+ mutationWebhook = addOwnership (mutationWebhook ).(* v1.MutatingWebhookConfiguration )
728
+ if err := clusterWatcher .Client .AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Delete (context .Background (), mutationWebhook .Name , metav1.DeleteOptions {}); err != nil {
729
+ clusterWatcher .Log .Warnf ("cannot delete mutation webhook %s, error=%s" , mutationWebhook .Name , err .Error ())
730
+ }
731
+ // == ROTATE ==
732
+ // update controller tls secret
733
+ controllerSecret := deployments .GetKubeArmorControllerTLSSecret (common .Namespace , caCert .String (), tlsCrt .String (), tlsKey .String ())
734
+ controllerSecret = addOwnership (controllerSecret ).(* corev1.Secret )
735
+ if _ , err := clusterWatcher .Client .CoreV1 ().Secrets (common .Namespace ).Update (context .Background (), controllerSecret , metav1.UpdateOptions {}); err != nil {
736
+ clusterWatcher .Log .Warnf ("cannot update controller tls secret %s, error=%s" , controllerSecret .Name , err .Error ())
737
+ }
738
+ // == ROLLOUT ==
739
+ // create mutation webhook configuration
740
+ if _ , err := clusterWatcher .Client .AdmissionregistrationV1 ().MutatingWebhookConfigurations ().Create (context .Background (), mutationWebhook , metav1.CreateOptions {}); err != nil {
741
+ clusterWatcher .Log .Warnf ("Cannot create mutation webhook %s, error=%s" , mutationWebhook .Name , err .Error ())
742
+ }
743
+ // scale up controller deployment to previous settings
744
+ controllerDeployment .Spec .Replicas = replicas
745
+ if _ , err := clusterWatcher .Client .AppsV1 ().Deployments (common .Namespace ).Update (context .Background (), controllerDeployment , metav1.UpdateOptions {}); err != nil {
746
+ clusterWatcher .Log .Warnf ("cannot scale down controller %s, error=%s" , controllerDeployment .Name , err .Error ())
747
+ }
803
748
clusterWatcher .Log .Info ("Tls rotation completed" )
804
749
}
0 commit comments