Skip to content

Commit

Permalink
Implement re-reading the teleport identity
Browse files Browse the repository at this point in the history
  • Loading branch information
tuladhar committed Nov 9, 2023
1 parent 9c91def commit aec7a0e
Show file tree
Hide file tree
Showing 5 changed files with 49 additions and 12 deletions.
37 changes: 33 additions & 4 deletions internal/controller/cluster_controller.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@ package controller

import (
"context"
"crypto/sha512"
"encoding/hex"
"time"

"github.com/giantswarm/microerror"
Expand All @@ -36,10 +38,11 @@ import (

// ClusterReconciler reconciles a Cluster object
type ClusterReconciler struct {
Client client.Client
Log logr.Logger
Scheme *runtime.Scheme
Teleport *teleport.Teleport
Client client.Client
Log logr.Logger
Scheme *runtime.Scheme
Teleport *teleport.Teleport
Namespace string
}

//+kubebuilder:rbac:groups=cluster.x-k8s.io.giantswarm.io,resources=clusters,verbs=get;list;watch;create;update;patch;delete
Expand Down Expand Up @@ -68,6 +71,32 @@ func (r *ClusterReconciler) Reconcile(ctx context.Context, req ctrl.Request) (ct
}
log.Info("Reconciling cluster", "cluster", cluster)

now := time.Now()
diff := now.Sub(r.Teleport.SecretConfig.LastRead)
seconds := diff.Seconds()
minutes := seconds / 60
hasher := sha512.New()
hasher.Write([]byte(r.Teleport.SecretConfig.IdentityFile))
sum := hasher.Sum(nil)
hashString := hex.EncodeToString(sum)

log.Info("Teleport identity", "last-read-minutes-ago", minutes, "hash", hashString)

if time.Since(r.Teleport.SecretConfig.LastRead) > 1*time.Minute {
log.Info("Retrieving new identity", "secretName", key.TeleportBotSecretName)

newSecretConfig, err := teleport.GetConfigFromSecret(ctx, r.Client, r.Namespace)
if err != nil {
return ctrl.Result{}, microerror.Mask(err)
}
r.Teleport.SecretConfig = newSecretConfig

if r.Teleport.TeleportClient, err = teleport.NewClient(ctx, newSecretConfig.ProxyAddr, newSecretConfig.IdentityFile); err != nil {
return ctrl.Result{}, microerror.Mask(err)
}
log.Info("Re-connected to teleport cluster with new identity", "proxyAddr", newSecretConfig.ProxyAddr)
}

registerName := cluster.Name
if cluster.Name != r.Teleport.SecretConfig.ManagementClusterName {
registerName = key.GetRegisterName(r.Teleport.SecretConfig.ManagementClusterName, cluster.Name)
Expand Down
10 changes: 6 additions & 4 deletions internal/controller/cluster_controller_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,10 +101,11 @@ func Test_ClusterController(t *testing.T) {
log := ctrl.Log.WithName("test")

controller := &ClusterReconciler{
Client: ctrlClient,
Log: log,
Scheme: scheme.Scheme,
Teleport: teleport.New(tc.namespace, tc.secretConfig, test.NewMockTokenGenerator(tc.token)),
Client: ctrlClient,
Log: log,
Scheme: scheme.Scheme,
Namespace: tc.namespace,
Teleport: teleport.New(tc.namespace, tc.secretConfig, test.NewMockTokenGenerator(tc.token)),
}
controller.Teleport.TeleportClient = test.NewTeleportClient(test.FakeTeleportClientConfig{
Tokens: tc.tokens,
Expand Down Expand Up @@ -179,6 +180,7 @@ func newSecretConfig() *teleport.SecretConfig {
AppName: test.AppName,
AppVersion: test.AppVersion,
IdentityFile: test.IdentityFileValue,
LastRead: test.LastReadValue,
ManagementClusterName: test.ManagementClusterName,
ProxyAddr: test.ProxyAddr,
TeleportVersion: test.TeleportVersion,
Expand Down
3 changes: 3 additions & 0 deletions internal/pkg/teleport/secret.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package teleport
import (
"context"
"fmt"
"time"

"github.com/giantswarm/microerror"
"github.com/go-logr/logr"
Expand All @@ -17,6 +18,7 @@ import (

type SecretConfig struct {
ProxyAddr string
LastRead time.Time
IdentityFile string
TeleportVersion string
ManagementClusterName string
Expand Down Expand Up @@ -80,6 +82,7 @@ func GetConfigFromSecret(ctx context.Context, ctrlClient client.Client, namespac

return &SecretConfig{
IdentityFile: identityFile,
LastRead: time.Now(),
ProxyAddr: proxyAddr,
ManagementClusterName: managementClusterName,
TeleportVersion: teleportVersion,
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/test/resources.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,8 @@ const (
ConfigMapValuesFormat = "authToken: %s\nproxyAddr: %s\nroles: kube\nkubeClusterName: %s\nteleportVersionOverride: %s"
)

var LastReadValue = time.Now()

type MockTokenGenerator struct {
token string
}
Expand Down
9 changes: 5 additions & 4 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,10 +124,11 @@ func main() {
setupLog.Info("Connected to teleport cluster", "proxyAddr", tele.SecretConfig.ProxyAddr)

if err = (&controller.ClusterReconciler{
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Cluster"),
Scheme: mgr.GetScheme(),
Teleport: tele,
Client: mgr.GetClient(),
Log: ctrl.Log.WithName("controllers").WithName("Cluster"),
Scheme: mgr.GetScheme(),
Teleport: tele,
Namespace: namespace,
}).SetupWithManager(mgr); err != nil {
setupLog.Error(err, "unable to create controller", "controller", "Cluster")
os.Exit(1)
Expand Down

0 comments on commit aec7a0e

Please sign in to comment.