|
| 1 | +package main |
| 2 | + |
| 3 | +import ( |
| 4 | + "context" |
| 5 | + "encoding/json" |
| 6 | + "errors" |
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "strings" |
| 10 | + |
| 11 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 12 | + kubernetes "k8s.io/client-go/kubernetes" |
| 13 | + clientauthenticationv1 "k8s.io/client-go/pkg/apis/clientauthentication/v1" |
| 14 | + "k8s.io/client-go/rest" |
| 15 | + "k8s.io/client-go/tools/clientcmd" |
| 16 | + "sigs.k8s.io/cluster-inventory-api/pkg/credentialplugin" |
| 17 | +) |
| 18 | + |
| 19 | +type Provider struct { |
| 20 | + // KubeClient is the typed client for core Kubernetes resources (e.g. Secret). |
| 21 | + KubeClient kubernetes.Interface |
| 22 | + // Namespace, if set, overrides namespace inference. |
| 23 | + Namespace string |
| 24 | +} |
| 25 | + |
| 26 | +// NewDefault constructs a Provider with pre-initialized typed clientsets and an inferred namespace. |
| 27 | +func NewDefault() (*Provider, error) { |
| 28 | + // Build Kubernetes rest.Config via in-cluster first, then fallback to kubeconfig |
| 29 | + cfg, err := rest.InClusterConfig() |
| 30 | + if err != nil { |
| 31 | + kubeconfig := os.Getenv("KUBECONFIG") |
| 32 | + cfg, err = clientcmd.BuildConfigFromFlags("", kubeconfig) |
| 33 | + if err != nil { |
| 34 | + return nil, fmt.Errorf("failed to build kube client config: %w", err) |
| 35 | + } |
| 36 | + } |
| 37 | + |
| 38 | + kubeClient, err := kubernetes.NewForConfig(cfg) |
| 39 | + if err != nil { |
| 40 | + return nil, fmt.Errorf("failed to create kubernetes clientset: %w", err) |
| 41 | + } |
| 42 | + |
| 43 | + return &Provider{KubeClient: kubeClient, Namespace: inferNamespace()}, nil |
| 44 | +} |
| 45 | + |
| 46 | +// ProviderName is the name of the credential provider. |
| 47 | +const ProviderName = "secretreader" |
| 48 | + |
| 49 | +// SecretTokenKey is the `Secret.data` key. |
| 50 | +const SecretTokenKey = "token" |
| 51 | + |
| 52 | +func (Provider) Name() string { return ProviderName } |
| 53 | + |
| 54 | +func (p Provider) GetToken(ctx context.Context, info clientauthenticationv1.ExecCredential) (clientauthenticationv1.ExecCredentialStatus, error) { |
| 55 | + // Require pre-initialized typed clients |
| 56 | + if p.KubeClient == nil { |
| 57 | + return clientauthenticationv1.ExecCredentialStatus{}, errors.New("provider clients are not initialized; construct with NewDefault or set clients") |
| 58 | + } |
| 59 | + |
| 60 | + // Require clusterName to be present in extensions config |
| 61 | + type execClusterConfig struct { |
| 62 | + ClusterName string `json:"clusterName"` |
| 63 | + } |
| 64 | + // Validate presence of cluster config |
| 65 | + if info.Spec.Cluster == nil || len(info.Spec.Cluster.Config.Raw) == 0 { |
| 66 | + return clientauthenticationv1.ExecCredentialStatus{}, fmt.Errorf("missing ExecCredential.Spec.Cluster.Config") |
| 67 | + } |
| 68 | + var cfg execClusterConfig |
| 69 | + if err := json.Unmarshal(info.Spec.Cluster.Config.Raw, &cfg); err != nil { |
| 70 | + return clientauthenticationv1.ExecCredentialStatus{}, fmt.Errorf("invalid ExecCredential.Spec.Cluster.Config: %w", err) |
| 71 | + } |
| 72 | + if cfg.ClusterName == "" { |
| 73 | + return clientauthenticationv1.ExecCredentialStatus{}, fmt.Errorf("missing clusterName in ExecCredential.Spec.Cluster.Config") |
| 74 | + } |
| 75 | + clusterName := cfg.ClusterName |
| 76 | + |
| 77 | + // Read Secret <namespace>/<clusterName> via typed client and return token |
| 78 | + sec, err := p.KubeClient.CoreV1().Secrets(p.Namespace).Get(ctx, clusterName, metav1.GetOptions{}) |
| 79 | + if err != nil { |
| 80 | + return clientauthenticationv1.ExecCredentialStatus{}, fmt.Errorf("failed to get secret %s/%s: %w", p.Namespace, clusterName, err) |
| 81 | + } |
| 82 | + data, ok := sec.Data[SecretTokenKey] |
| 83 | + if !ok || len(data) == 0 { |
| 84 | + return clientauthenticationv1.ExecCredentialStatus{}, fmt.Errorf("secret %s/%s missing %q key", p.Namespace, clusterName, SecretTokenKey) |
| 85 | + } |
| 86 | + |
| 87 | + return clientauthenticationv1.ExecCredentialStatus{Token: string(data)}, nil |
| 88 | +} |
| 89 | + |
| 90 | +// inferNamespace determines the namespace to read Secrets from, preferring kubeconfig current-context |
| 91 | +func inferNamespace() string { |
| 92 | + // kubeconfig current-context namespace |
| 93 | + rules := clientcmd.NewDefaultClientConfigLoadingRules() |
| 94 | + if path := os.Getenv("KUBECONFIG"); strings.TrimSpace(path) != "" { |
| 95 | + rules.ExplicitPath = path |
| 96 | + } |
| 97 | + cc := clientcmd.NewNonInteractiveDeferredLoadingClientConfig(rules, &clientcmd.ConfigOverrides{}) |
| 98 | + if n, _, err := cc.Namespace(); err == nil && strings.TrimSpace(n) != "" { |
| 99 | + return n |
| 100 | + } |
| 101 | + // in-cluster kubeconfig is unavailable; library returns default namespace |
| 102 | + return "default" |
| 103 | +} |
| 104 | + |
| 105 | +func main() { |
| 106 | + p, err := NewDefault() |
| 107 | + if err != nil { |
| 108 | + panic(err) |
| 109 | + } |
| 110 | + credentialplugin.Run(*p) |
| 111 | +} |
0 commit comments