Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature: Use env vars to set list of Opaque secret keys #2

Merged
merged 1 commit into from
Apr 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
File renamed without changes.
1 change: 1 addition & 0 deletions cmd/kubectl-view-cert/model.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
type Certificate struct {
SecretName string
Namespace string
SecretKey string
Type string
Version int
SerialNumber string
Expand Down
4 changes: 3 additions & 1 deletion cmd/kubectl-view-cert/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -353,7 +353,7 @@ func getResourceInterface(allNs bool, secretName string) (string, dynamic.Resour
}

func parseData(ns, secretName string, data map[string]interface{}, secretKey string, listKeys, showCA bool) (certData, caCertData *Certificate, secretKeys *[]string) {
secretCertData, err := parse.NewCertificateData(ns, secretName, data, secretKey, listKeys, showCA)
secretCertData, secretKeysList, err := parse.NewCertificateData(ns, secretName, data, secretKey, listKeys, showCA)
if err != nil {
klog.V(1).Infoln("msg", "failed to parse secret '"+ns+"/"+secretName+"'", "err", err)
return nil, nil, nil
Expand All @@ -380,6 +380,7 @@ func parseData(ns, secretName string, data map[string]interface{}, secretKey str
SecretName: parsedCerts.SecretName,
Namespace: parsedCerts.Namespace,
Type: secretCertData.Type,
SecretKey: secretKeysList[0],
IsCA: parsedCerts.Certificate.IsCA,
Issuer: parsedCerts.Certificate.Issuer.String(),
SerialNumber: fmt.Sprintf("%x", parsedCerts.Certificate.SerialNumber),
Expand All @@ -396,6 +397,7 @@ func parseData(ns, secretName string, data map[string]interface{}, secretKey str
caCertData = &Certificate{
SecretName: parsedCerts.SecretName,
Namespace: parsedCerts.Namespace,
SecretKey: secretKeysList[1],
Type: secretCertData.Type,
IsCA: parsedCerts.CaCertificate.IsCA,
Issuer: parsedCerts.CaCertificate.Issuer.String(),
Expand Down
37 changes: 31 additions & 6 deletions internal/parse/parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,8 @@ import (
"encoding/base64"
"encoding/pem"
"fmt"
"os"
"strings"
)

// CertificateData struct contains base64 pem data
Expand All @@ -26,10 +28,14 @@ type ParsedCertificateData struct {
}

// NewCertificateData takes secret data and extracts base64 pem strings
func NewCertificateData(ns, secretName string, data map[string]interface{}, secretKey string, listKeys, showCa bool) (*CertificateData, error) {
// nolint gocognit
func NewCertificateData(ns, secretName string, data map[string]interface{}, secretKey string, listKeys, showCa bool) (*CertificateData, []string, error) {
_, ok := data["data"]
var keysList []string
returnCertPemKey := "tls.crt"
returnCaPemKey := "ca.crt"
if !ok {
return nil, nil
return nil, nil, nil
}
certsMap := data["data"].(map[string]interface{})

Expand All @@ -43,24 +49,43 @@ func NewCertificateData(ns, secretName string, data map[string]interface{}, secr
certData.Certificate = fmt.Sprintf("%v", val)
}

return &certData, nil
return &certData, nil, nil
}

secretType := fmt.Sprintf("%v", data["type"])

secretCrtPemKeyList := strings.Split(os.Getenv("CRT_PEM_KEY_LIST"), ",")
secretCaPemKeyList := strings.Split(os.Getenv("CA_PEM_KEY_LIST"), ",")
// nolint gosec
if secretType == "kubernetes.io/tls" ||
secretType == "Opaque" {
if val, ok := certsMap["tls.crt"]; ok {
certData.Certificate = fmt.Sprintf("%v", val)
} else {
for _, crtPemKey := range secretCrtPemKeyList {
if val, ok := certsMap[crtPemKey]; ok {
certData.Certificate = fmt.Sprintf("%v", val)
returnCertPemKey = crtPemKey
break
}
}
}
if showCa {
if val, ok := certsMap["ca.crt"]; ok {
certData.CaCertificate = fmt.Sprintf("%v", val)
} else {
for _, caPemKey := range secretCaPemKeyList {
if val, ok := certsMap[caPemKey]; ok {
certData.CaCertificate = fmt.Sprintf("%v", val)
returnCaPemKey = caPemKey
break
}
}
}
}
keysList = append(keysList, returnCertPemKey, returnCaPemKey)
certData.Type = secretType
return &certData, nil
return &certData, keysList, nil
}

if listKeys && certsMap != nil && len(certsMap) > 0 {
Expand All @@ -72,10 +97,10 @@ func NewCertificateData(ns, secretName string, data map[string]interface{}, secr
i++
}

return &certData, nil
return &certData, nil, nil
}

return nil, fmt.Errorf("unsupported secret type %s", secretType)
return nil, nil, fmt.Errorf("unsupported secret type %s", secretType)
}

// ParseCertificates method parses each base64 pem strings and creates x509 certificates
Expand Down