forked from argoproj/argo-workflows
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: tls config validation, follow up on argoproj#1070 (argoproj#1076)
* fix: tls config validation, follow up on argoproj#1070 Signed-off-by: Derek Wang <whynowy@gmail.com> * test cases Signed-off-by: Derek Wang <whynowy@gmail.com>
- Loading branch information
Showing
3 changed files
with
104 additions
and
16 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package common | ||
|
||
import "errors" | ||
|
||
// ValidateTLSConfig validates a TLS configuration. | ||
func ValidateTLSConfig(tlsConfig *TLSConfig) error { | ||
if tlsConfig == nil { | ||
return nil | ||
} | ||
var caCertSet, clientCertSet, clientKeySet bool | ||
|
||
if tlsConfig.CACertSecret != nil || tlsConfig.DeprecatedCACertPath != "" { | ||
caCertSet = true | ||
} | ||
|
||
if tlsConfig.ClientCertSecret != nil || tlsConfig.DeprecatedClientCertPath != "" { | ||
clientCertSet = true | ||
} | ||
|
||
if tlsConfig.ClientKeySecret != nil || tlsConfig.DeprecatedClientKeyPath != "" { | ||
clientKeySet = true | ||
} | ||
|
||
if !caCertSet && !clientCertSet && !clientKeySet { | ||
return errors.New("invalid tls config, please configure either caCertSecret, or clientCertSecret and clientKeySecret, or both") | ||
} | ||
|
||
if (clientCertSet || clientKeySet) && (!clientCertSet || !clientKeySet) { | ||
return errors.New("invalid tls config, both clientCertSecret and clientKeySecret need to be configured") | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,72 @@ | ||
package common | ||
|
||
import ( | ||
strings "strings" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
corev1 "k8s.io/api/core/v1" | ||
) | ||
|
||
func fakeTLSConfig(t *testing.T) *TLSConfig { | ||
t.Helper() | ||
return &TLSConfig{ | ||
CACertSecret: &corev1.SecretKeySelector{ | ||
Key: "fake-key1", | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "fake-name1", | ||
}, | ||
}, | ||
ClientCertSecret: &corev1.SecretKeySelector{ | ||
Key: "fake-key2", | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "fake-name2", | ||
}, | ||
}, | ||
ClientKeySecret: &corev1.SecretKeySelector{ | ||
Key: "fake-key3", | ||
LocalObjectReference: corev1.LocalObjectReference{ | ||
Name: "fake-name3", | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func TestValidateTLSConfig(t *testing.T) { | ||
t.Run("test empty", func(t *testing.T) { | ||
c := &TLSConfig{} | ||
err := ValidateTLSConfig(c) | ||
assert.NotNil(t, err) | ||
assert.True(t, strings.Contains(err.Error(), "please configure either caCertSecret, or clientCertSecret and clientKeySecret, or both")) | ||
}) | ||
|
||
t.Run("test clientKeySecret is set, clientCertSecret is empty", func(t *testing.T) { | ||
c := fakeTLSConfig(t) | ||
c.CACertSecret = nil | ||
c.ClientCertSecret = nil | ||
err := ValidateTLSConfig(c) | ||
assert.NotNil(t, err) | ||
assert.True(t, strings.Contains(err.Error(), "both clientCertSecret and clientKeySecret need to be configured")) | ||
}) | ||
|
||
t.Run("test only caCertSecret is set", func(t *testing.T) { | ||
c := fakeTLSConfig(t) | ||
c.ClientCertSecret = nil | ||
c.ClientKeySecret = nil | ||
err := ValidateTLSConfig(c) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("test clientCertSecret and clientKeySecret are set", func(t *testing.T) { | ||
c := fakeTLSConfig(t) | ||
c.CACertSecret = nil | ||
err := ValidateTLSConfig(c) | ||
assert.Nil(t, err) | ||
}) | ||
|
||
t.Run("test all of 3 are set", func(t *testing.T) { | ||
c := fakeTLSConfig(t) | ||
err := ValidateTLSConfig(c) | ||
assert.Nil(t, err) | ||
}) | ||
} |