@@ -3,104 +3,110 @@ package kubernetes
33import (
44 "encoding/base64"
55 "fmt"
6+ "strings"
67)
78
89// AuthType represents the type of Kubernetes authentication.
910type AuthType string
11+ type ContextKey string
1012
1113const (
1214 // AuthTypeToken represents token-based authentication.
1315 AuthTypeToken AuthType = "token"
1416 // AuthTypeClientCertificate represents client certificate authentication.
1517 AuthTypeClientCertificate AuthType = "client_certificate"
16- // AuthTypeUnknown represents unknown or unsupported authentication type.
17- AuthTypeUnknown AuthType = "unknown"
18- AuthHeadersContextKey string = "k8s_auth_headers"
18+ // AuthHeadersContextKey is the context key for the Kubernetes authentication headers.
19+ AuthHeadersContextKey ContextKey = "k8s_auth_headers"
1920)
2021
2122// K8sAuthHeaders represents Kubernetes API authentication headers.
2223type K8sAuthHeaders struct {
23- // ClusterURL is the Kubernetes cluster URL.
24- ClusterURL string
25- // ClusterCertificateAuthorityData is the base64-encoded CA certificate .
26- ClusterCertificateAuthorityData string
24+ // Server is the Kubernetes cluster URL.
25+ Server string
26+ // ClusterCertificateAuthorityData is the Certificate Authority data .
27+ CertificateAuthorityData [] byte
2728 // AuthorizationToken is the optional bearer token for authentication.
2829 AuthorizationToken string
29- // ClientCertificateData is the optional base64-encoded client certificate.
30- ClientCertificateData string
31- // ClientKeyData is the optional base64-encoded client key.
32- ClientKeyData string
30+ // ClientCertificateData is the optional client certificate data.
31+ ClientCertificateData []byte
32+ // ClientKeyData is the optional client key data.
33+ ClientKeyData []byte
34+ // InsecureSkipTLSVerify is the optional flag to skip TLS verification.
35+ InsecureSkipTLSVerify bool
36+ }
37+
38+ // GetDecodedData decodes and returns the data.
39+ func GetDecodedData (data string ) ([]byte , error ) {
40+ return base64 .StdEncoding .DecodeString (data )
3341}
3442
3543func NewK8sAuthHeadersFromHeaders (data map [string ]any ) (* K8sAuthHeaders , error ) {
36- authHeaders := & K8sAuthHeaders {}
3744 var ok bool
38- authHeaders .ClusterURL , ok = data [string (CustomClusterURLHeader )].(string )
39- if ! ok || authHeaders .ClusterURL == "" {
40- return nil , fmt .Errorf ("%s header is required" , CustomClusterURLHeader )
45+ var err error
46+
47+ // Initialize auth headers.
48+ authHeaders := & K8sAuthHeaders {
49+ InsecureSkipTLSVerify : false ,
50+ }
51+
52+ // Get cluster URL from headers.
53+ authHeaders .Server , ok = data [string (CustomServerHeader )].(string )
54+ if ! ok || authHeaders .Server == "" {
55+ return nil , fmt .Errorf ("%s header is required" , CustomServerHeader )
4156 }
4257
43- authHeaders .ClusterCertificateAuthorityData , ok = data [string (CustomCertificateAuthorityDataHeader )].(string )
44- if ! ok || authHeaders .ClusterCertificateAuthorityData == "" {
58+ // Get certificate authority data from headers.
59+ certificateAuthorityDataBase64 , ok := data [string (CustomCertificateAuthorityDataHeader )].(string )
60+ if ! ok || certificateAuthorityDataBase64 == "" {
4561 return nil , fmt .Errorf ("%s header is required" , CustomCertificateAuthorityDataHeader )
4662 }
63+ // Decode certificate authority data.
64+ authHeaders .CertificateAuthorityData , err = GetDecodedData (certificateAuthorityDataBase64 )
65+ if err != nil {
66+ return nil , fmt .Errorf ("invalid certificate authority data: %w" , err )
67+ }
68+
69+ // Get insecure skip TLS verify flag from headers.
70+ if data [string (CustomInsecureSkipTLSVerifyHeader )] != nil && strings .ToLower (data [string (CustomInsecureSkipTLSVerifyHeader )].(string )) == "true" {
71+ authHeaders .InsecureSkipTLSVerify = true
72+ }
4773
48- // Token or client certificate and key data (optional) .
74+ // Get authorization token from headers .
4975 authHeaders .AuthorizationToken , _ = data [string (CustomAuthorizationHeader )].(string )
50- authHeaders .ClientCertificateData , _ = data [string (CustomClientCertificateDataHeader )].(string )
51- authHeaders .ClientKeyData , _ = data [string (CustomClientKeyDataHeader )].(string )
5276
53- // Check if either token auth or client certificate auth is provided
54- hasTokenAuth := authHeaders .AuthorizationToken != ""
55- hasClientCertAuth := authHeaders .ClientCertificateData != "" && authHeaders .ClientKeyData != ""
77+ // Get client certificate data from headers.
78+ clientCertificateDataBase64 , _ := data [string (CustomClientCertificateDataHeader )].(string )
79+ if clientCertificateDataBase64 != "" {
80+ authHeaders .ClientCertificateData , err = GetDecodedData (clientCertificateDataBase64 )
81+ if err != nil {
82+ return nil , fmt .Errorf ("invalid client certificate data: %w" , err )
83+ }
84+ }
85+ // Get client key data from headers.
86+ clientKeyDataBase64 , _ := data [string (CustomClientKeyDataHeader )].(string )
87+ if clientKeyDataBase64 != "" {
88+ authHeaders .ClientKeyData , err = GetDecodedData (clientKeyDataBase64 )
89+ if err != nil {
90+ return nil , fmt .Errorf ("invalid client key data: %w" , err )
91+ }
92+ }
5693
57- if ! hasTokenAuth && ! hasClientCertAuth {
58- return nil , fmt .Errorf ("either %s header or (%s and %s) headers are required" , CustomAuthorizationHeader , CustomClientCertificateDataHeader , CustomClientKeyDataHeader )
94+ // Check if a valid authentication type is provided.
95+ _ , err = authHeaders .GetAuthType ()
96+ if err != nil {
97+ return nil , fmt .Errorf ("either %s header for token authentication or (%s and %s) headers for client certificate authentication required" , CustomAuthorizationHeader , CustomClientCertificateDataHeader , CustomClientKeyDataHeader )
5998 }
6099
61100 return authHeaders , nil
62101}
63102
64103// GetAuthType returns the authentication type based on the provided headers.
65- func (h * K8sAuthHeaders ) GetAuthType () AuthType {
104+ func (h * K8sAuthHeaders ) GetAuthType () ( AuthType , error ) {
66105 if h .AuthorizationToken != "" {
67- return AuthTypeToken
106+ return AuthTypeToken , nil
68107 }
69- if h .ClientCertificateData != "" && h .ClientKeyData != "" {
70- return AuthTypeClientCertificate
108+ if h .ClientCertificateData != nil && h .ClientKeyData != nil {
109+ return AuthTypeClientCertificate , nil
71110 }
72- return AuthTypeUnknown
111+ return "" , fmt . Errorf ( "invalid authentication type" )
73112}
74-
75- // GetDecodedCertificateAuthorityData decodes and returns the CA certificate data.
76- func (h * K8sAuthHeaders ) GetDecodedCertificateAuthorityData () ([]byte , error ) {
77- data , err := base64 .StdEncoding .DecodeString (h .ClusterCertificateAuthorityData )
78- if err != nil {
79- return nil , fmt .Errorf ("failed to decode certificate authority data: %w" , err )
80- }
81- return data , nil
82- }
83-
84- // // GetDecodedClientCertificateData decodes and returns the client certificate data.
85- // func (h *K8sAuthHeaders) GetDecodedClientCertificateData() ([]byte, error) {
86- // if h.ClientCertificateData == nil || *h.ClientCertificateData == "" {
87- // return nil, errors.New("client certificate data is not available")
88- // }
89- // data, err := base64.StdEncoding.DecodeString(*h.ClientCertificateData)
90- // if err != nil {
91- // return nil, fmt.Errorf("failed to decode client certificate data: %w", err)
92- // }
93- // return data, nil
94- // }
95-
96- // // GetDecodedClientKeyData decodes and returns the client key data.
97- // func (h *K8sAuthHeaders) GetDecodedClientKeyData() ([]byte, error) {
98- // if h.ClientKeyData == nil || *h.ClientKeyData == "" {
99- // return nil, errors.New("client key data is not available")
100- // }
101- // data, err := base64.StdEncoding.DecodeString(*h.ClientKeyData)
102- // if err != nil {
103- // return nil, fmt.Errorf("failed to decode client key data: %w", err)
104- // }
105- // return data, nil
106- // }
0 commit comments