Skip to content

Commit

Permalink
add secret type for getAndValidateSecret() (#6877)
Browse files Browse the repository at this point in the history
  • Loading branch information
pdabelf5 committed Nov 28, 2024
1 parent e18dd3f commit 1e670f7
Showing 1 changed file with 12 additions and 8 deletions.
20 changes: 12 additions & 8 deletions cmd/nginx-ingress/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -579,7 +579,7 @@ func processDefaultServerSecret(kubeClient *kubernetes.Clientset, nginxManager n
var sslRejectHandshake bool

if *defaultServerSecret != "" {
secret, err := getAndValidateSecret(kubeClient, *defaultServerSecret)
secret, err := getAndValidateSecret(kubeClient, *defaultServerSecret, api_v1.SecretTypeTLS)
if err != nil {
return sslRejectHandshake, fmt.Errorf("error trying to get the default server TLS secret %v: %w", *defaultServerSecret, err)
}
Expand All @@ -603,7 +603,7 @@ func processDefaultServerSecret(kubeClient *kubernetes.Clientset, nginxManager n
func processWildcardSecret(kubeClient *kubernetes.Clientset, nginxManager nginx.Manager) (bool, error) {
isWildcardEnabled := *wildcardTLSSecret != ""
if isWildcardEnabled {
secret, err := getAndValidateSecret(kubeClient, *wildcardTLSSecret)
secret, err := getAndValidateSecret(kubeClient, *wildcardTLSSecret, api_v1.SecretTypeTLS)
if err != nil {
return false, fmt.Errorf("error trying to get the wildcard TLS secret %v: %w", *wildcardTLSSecret, err)
}
Expand Down Expand Up @@ -673,7 +673,8 @@ func getSocketClient(sockPath string) *http.Client {
}

// getAndValidateSecret gets and validates a secret.
func getAndValidateSecret(kubeClient *kubernetes.Clientset, secretNsName string) (secret *api_v1.Secret, err error) {
// nolint:unparam
func getAndValidateSecret(kubeClient *kubernetes.Clientset, secretNsName string, secretType api_v1.SecretType) (secret *api_v1.Secret, err error) {
ns, name, err := k8s.ParseNamespaceName(secretNsName)
if err != nil {
return nil, fmt.Errorf("could not parse the %v argument: %w", secretNsName, err)
Expand All @@ -682,9 +683,12 @@ func getAndValidateSecret(kubeClient *kubernetes.Clientset, secretNsName string)
if err != nil {
return nil, fmt.Errorf("could not get %v: %w", secretNsName, err)
}
err = secrets.ValidateTLSSecret(secret)
if err != nil {
return nil, fmt.Errorf("%v is invalid: %w", secretNsName, err)
switch secretType {
case api_v1.SecretTypeTLS:
err = secrets.ValidateTLSSecret(secret)
if err != nil {
return nil, fmt.Errorf("%v is invalid: %w", secretNsName, err)
}
}
return secret, nil
}
Expand Down Expand Up @@ -791,7 +795,7 @@ func createPlusAndLatencyCollectors(
syslogListener = metrics.NewSyslogFakeServer()

if *prometheusTLSSecretName != "" {
prometheusSecret, err = getAndValidateSecret(kubeClient, *prometheusTLSSecretName)
prometheusSecret, err = getAndValidateSecret(kubeClient, *prometheusTLSSecretName, api_v1.SecretTypeTLS)
if err != nil {
nl.Fatalf(l, "Error trying to get the prometheus TLS secret %v: %v", *prometheusTLSSecretName, err)
}
Expand Down Expand Up @@ -843,7 +847,7 @@ func createHealthProbeEndpoint(kubeClient *kubernetes.Clientset, plusClient *cli
var err error

if *serviceInsightTLSSecretName != "" {
serviceInsightSecret, err = getAndValidateSecret(kubeClient, *serviceInsightTLSSecretName)
serviceInsightSecret, err = getAndValidateSecret(kubeClient, *serviceInsightTLSSecretName, api_v1.SecretTypeTLS)
if err != nil {
nl.Fatalf(l, "Error trying to get the service insight TLS secret %v: %v", *serviceInsightTLSSecretName, err)
}
Expand Down

0 comments on commit 1e670f7

Please sign in to comment.