git.iko.soy/typeable/oauth2-proxy
on master help sign in
Add tests for SecretSource validation
commit1dac1419b3269e20cef06b820ddf5d5d49e67f16 authorJoel Speed <joel.speed@hotmail.co.uk> dateSat 24 Oct 2020, 07:17 (6 years ago) parent8059a81
2 files changed+141−1
pkg/validation/common.go+3−1 pkg/validation/common_test.go+138
pkg/validation/common.go+3−1
@@ -8,6 +8,8 @@ import (
88 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
99)
1010
11+const multipleValuesForSecretSource = "multiple values specified for secret source: specify either value, fromEnv of fromFile"
12+
1113func validateSecretSource(source options.SecretSource) string {
1214 switch {
1315 case len(source.Value) > 0 && source.FromEnv == "" && source.FromFile == "":
@@ -17,7 +19,7 @@ func validateSecretSource(source options.SecretSource) string {
1719 case len(source.Value) == 0 && source.FromEnv == "" && source.FromFile != "":
1820 return validateSecretSourceFile(source.FromFile)
1921 default:
20− return "multiple values specified for secret source: specify either value, fromEnv of fromFile"
22+ return multipleValuesForSecretSource
2123 }
2224}
2325
pkg/validation/common_test.gonew file+138
@@ -0,0 +1,138 @@
1+package validation
2+
3+import (
4+ "encoding/base64"
5+ "io/ioutil"
6+ "os"
7+
8+ "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
9+ . "github.com/onsi/ginkgo"
10+ . "github.com/onsi/ginkgo/extensions/table"
11+ . "github.com/onsi/gomega"
12+)
13+
14+var _ = Describe("Common", func() {
15+ var validSecretSourceValue []byte
16+ const validSecretSourceEnv = "OAUTH2_PROXY_TEST_SECRET_SOURCE_ENV"
17+ var validSecretSourceFile string
18+
19+ BeforeEach(func() {
20+ validSecretSourceValue = []byte(base64.StdEncoding.EncodeToString([]byte("This is a secret source value")))
21+ Expect(os.Setenv(validSecretSourceEnv, "This is a secret source env")).To(Succeed())
22+ tmp, err := ioutil.TempFile("", "oauth2-proxy-secret-source-test")
23+ Expect(err).ToNot(HaveOccurred())
24+ defer tmp.Close()
25+
26+ _, err = tmp.Write([]byte("This is a secret source file"))
27+ Expect(err).ToNot(HaveOccurred())
28+
29+ validSecretSourceFile = tmp.Name()
30+ })
31+
32+ AfterEach(func() {
33+ Expect(os.Unsetenv(validSecretSourceEnv)).To(Succeed())
34+ Expect(os.Remove(validSecretSourceFile)).To(Succeed())
35+ })
36+
37+ type validateSecretSourceTableInput struct {
38+ source func() options.SecretSource
39+ expectedMsg string
40+ }
41+
42+ DescribeTable("validateSecretSource should",
43+ func(in validateSecretSourceTableInput) {
44+ Expect(validateSecretSource(in.source())).To(Equal(in.expectedMsg))
45+ },
46+ Entry("with no entries", validateSecretSourceTableInput{
47+ source: func() options.SecretSource {
48+ return options.SecretSource{}
49+ },
50+ expectedMsg: multipleValuesForSecretSource,
51+ }),
52+ Entry("with a Value and FromEnv", validateSecretSourceTableInput{
53+ source: func() options.SecretSource {
54+ return options.SecretSource{
55+ Value: validSecretSourceValue,
56+ FromEnv: validSecretSourceEnv,
57+ }
58+ },
59+ expectedMsg: multipleValuesForSecretSource,
60+ }),
61+ Entry("with a Value and FromFile", validateSecretSourceTableInput{
62+ source: func() options.SecretSource {
63+ return options.SecretSource{
64+ Value: validSecretSourceValue,
65+ FromFile: validSecretSourceFile,
66+ }
67+ },
68+ expectedMsg: multipleValuesForSecretSource,
69+ }),
70+ Entry("with FromEnv and FromFile", validateSecretSourceTableInput{
71+ source: func() options.SecretSource {
72+ return options.SecretSource{
73+ FromEnv: validSecretSourceEnv,
74+ FromFile: validSecretSourceFile,
75+ }
76+ },
77+ expectedMsg: multipleValuesForSecretSource,
78+ }),
79+ Entry("with a Value, FromEnv and FromFile", validateSecretSourceTableInput{
80+ source: func() options.SecretSource {
81+ return options.SecretSource{
82+ Value: validSecretSourceValue,
83+ FromEnv: validSecretSourceEnv,
84+ FromFile: validSecretSourceFile,
85+ }
86+ },
87+ expectedMsg: multipleValuesForSecretSource,
88+ }),
89+ Entry("with a valid Value", validateSecretSourceTableInput{
90+ source: func() options.SecretSource {
91+ return options.SecretSource{
92+ Value: validSecretSourceValue,
93+ }
94+ },
95+ expectedMsg: "",
96+ }),
97+ Entry("with a valid FromEnv", validateSecretSourceTableInput{
98+ source: func() options.SecretSource {
99+ return options.SecretSource{
100+ FromEnv: validSecretSourceEnv,
101+ }
102+ },
103+ expectedMsg: "",
104+ }),
105+ Entry("with a valid FromFile", validateSecretSourceTableInput{
106+ source: func() options.SecretSource {
107+ return options.SecretSource{
108+ FromFile: validSecretSourceFile,
109+ }
110+ },
111+ expectedMsg: "",
112+ }),
113+ Entry("with an invalid Value", validateSecretSourceTableInput{
114+ source: func() options.SecretSource {
115+ return options.SecretSource{
116+ Value: []byte("Invalid Base64 Value"),
117+ }
118+ },
119+ expectedMsg: "error decoding secret value: illegal base64 data at input byte 7",
120+ }),
121+ Entry("with an invalid FromEnv", validateSecretSourceTableInput{
122+ source: func() options.SecretSource {
123+ return options.SecretSource{
124+ FromEnv: "INVALID_ENV",
125+ }
126+ },
127+ expectedMsg: "error loading secret from environent: no value for for key \"INVALID_ENV\"",
128+ }),
129+ Entry("with an invalid FromFile", validateSecretSourceTableInput{
130+ source: func() options.SecretSource {
131+ return options.SecretSource{
132+ FromFile: "invalidFile",
133+ }
134+ },
135+ expectedMsg: "error loadig secret from file: stat invalidFile: no such file or directory",
136+ }),
137+ )
138+})