git.iko.soy/typeable/oauth2-proxy
on master help sign in
127 lines 3.8 kB history raw
1package validation
3import (
4 "os"
6 "github.com/oauth2-proxy/oauth2-proxy/v7/pkg/apis/options"
7 . "github.com/onsi/ginkgo/v2"
8 . "github.com/onsi/gomega"
9)
11var _ = Describe("Common", func() {
12 var validSecretSourceValue []byte
13 const validSecretSourceEnv = "OAUTH2_PROXY_TEST_SECRET_SOURCE_ENV"
14 var validSecretSourceFile string
16 BeforeEach(func() {
17 validSecretSourceValue = []byte("This is a secret source value")
18 Expect(os.Setenv(validSecretSourceEnv, "This is a secret source env")).To(Succeed())
19 tmp, err := os.CreateTemp("", "oauth2-proxy-secret-source-test")
20 Expect(err).ToNot(HaveOccurred())
21 defer tmp.Close()
23 _, err = tmp.Write([]byte("This is a secret source file"))
24 Expect(err).ToNot(HaveOccurred())
26 validSecretSourceFile = tmp.Name()
27 })
29 AfterEach(func() {
30 Expect(os.Unsetenv(validSecretSourceEnv)).To(Succeed())
31 Expect(os.Remove(validSecretSourceFile)).To(Succeed())
32 })
34 type validateSecretSourceTableInput struct {
35 source func() options.SecretSource
36 expectedMsg string
37 }
39 DescribeTable("validateSecretSource should",
40 func(in validateSecretSourceTableInput) {
41 Expect(validateSecretSource(in.source())).To(Equal(in.expectedMsg))
42 },
43 Entry("with no entries", validateSecretSourceTableInput{
44 source: func() options.SecretSource {
45 return options.SecretSource{}
46 },
47 expectedMsg: multipleValuesForSecretSource,
48 }),
49 Entry("with a Value and FromEnv", validateSecretSourceTableInput{
50 source: func() options.SecretSource {
51 return options.SecretSource{
52 Value: validSecretSourceValue,
53 FromEnv: validSecretSourceEnv,
54 }
55 },
56 expectedMsg: multipleValuesForSecretSource,
57 }),
58 Entry("with a Value and FromFile", validateSecretSourceTableInput{
59 source: func() options.SecretSource {
60 return options.SecretSource{
61 Value: validSecretSourceValue,
62 FromFile: validSecretSourceFile,
63 }
64 },
65 expectedMsg: multipleValuesForSecretSource,
66 }),
67 Entry("with FromEnv and FromFile", validateSecretSourceTableInput{
68 source: func() options.SecretSource {
69 return options.SecretSource{
70 FromEnv: validSecretSourceEnv,
71 FromFile: validSecretSourceFile,
72 }
73 },
74 expectedMsg: multipleValuesForSecretSource,
75 }),
76 Entry("with a Value, FromEnv and FromFile", validateSecretSourceTableInput{
77 source: func() options.SecretSource {
78 return options.SecretSource{
79 Value: validSecretSourceValue,
80 FromEnv: validSecretSourceEnv,
81 FromFile: validSecretSourceFile,
82 }
83 },
84 expectedMsg: multipleValuesForSecretSource,
85 }),
86 Entry("with a valid Value", validateSecretSourceTableInput{
87 source: func() options.SecretSource {
88 return options.SecretSource{
89 Value: validSecretSourceValue,
90 }
91 },
92 expectedMsg: "",
93 }),
94 Entry("with a valid FromEnv", validateSecretSourceTableInput{
95 source: func() options.SecretSource {
96 return options.SecretSource{
97 FromEnv: validSecretSourceEnv,
98 }
99 },
100 expectedMsg: "",
101 }),
102 Entry("with a valid FromFile", validateSecretSourceTableInput{
103 source: func() options.SecretSource {
104 return options.SecretSource{
105 FromFile: validSecretSourceFile,
106 }
107 },
108 expectedMsg: "",
109 }),
110 Entry("with an invalid FromEnv", validateSecretSourceTableInput{
111 source: func() options.SecretSource {
112 return options.SecretSource{
113 FromEnv: "INVALID_ENV",
114 }
115 },
116 expectedMsg: "error loading secret from environent: no value for for key \"INVALID_ENV\"",
117 }),
118 Entry("with an invalid FromFile", validateSecretSourceTableInput{
119 source: func() options.SecretSource {
120 return options.SecretSource{
121 FromFile: "invalidFile",
122 }
123 },
124 expectedMsg: "error loadig secret from file: stat invalidFile: no such file or directory",
125 }),
126 )
127})