git.iko.soy/typeable/oauth2-proxy
on master help sign in
163 lines 4.2 kB history raw
1package validation
3import (
4 "encoding/base64"
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("Headers", func() {
12 type validateHeaderTableInput struct {
13 headers []options.Header
14 expectedMsgs []string
15 }
17 validHeader1 := options.Header{
18 Name: "X-Email",
19 Values: []options.HeaderValue{
20 {
21 ClaimSource: &options.ClaimSource{
22 Claim: "email",
23 },
24 },
25 },
26 }
28 validHeader2 := options.Header{
29 Name: "X-Forwarded-Auth",
30 Values: []options.HeaderValue{
31 {
32 SecretSource: &options.SecretSource{
33 Value: []byte(base64.StdEncoding.EncodeToString([]byte("secret"))),
34 },
35 },
36 },
37 }
39 validHeader3 := options.Header{
40 Name: "Authorization",
41 Values: []options.HeaderValue{
42 {
43 ClaimSource: &options.ClaimSource{
44 Claim: "email",
45 BasicAuthPassword: &options.SecretSource{
46 Value: []byte(base64.StdEncoding.EncodeToString([]byte("secret"))),
47 },
48 },
49 },
50 },
51 }
53 DescribeTable("validateHeaders",
54 func(in validateHeaderTableInput) {
55 Expect(validateHeaders(in.headers)).To(ConsistOf(in.expectedMsgs))
56 },
57 Entry("with no headers", validateHeaderTableInput{
58 headers: []options.Header{},
59 expectedMsgs: []string{},
60 }),
61 Entry("with valid headers", validateHeaderTableInput{
62 headers: []options.Header{
63 validHeader1,
64 validHeader2,
65 validHeader3,
66 },
67 expectedMsgs: []string{},
68 }),
69 Entry("with multiple headers with the same name", validateHeaderTableInput{
70 headers: []options.Header{
71 validHeader1,
72 validHeader1,
73 validHeader2,
74 validHeader2,
75 },
76 expectedMsgs: []string{
77 "multiple headers found with name \"X-Email\": header names must be unique",
78 "multiple headers found with name \"X-Forwarded-Auth\": header names must be unique",
79 },
80 }),
81 Entry("with an unamed header", validateHeaderTableInput{
82 headers: []options.Header{
83 {},
84 validHeader2,
85 },
86 expectedMsgs: []string{
87 "header has empty name: names are required for all headers",
88 },
89 }),
90 Entry("with a header which has a claim and secret source", validateHeaderTableInput{
91 headers: []options.Header{
92 {
93 Name: "With-Claim-And-Secret",
94 Values: []options.HeaderValue{
95 {
96 ClaimSource: &options.ClaimSource{},
97 SecretSource: &options.SecretSource{},
98 },
99 },
100 },
101 validHeader1,
102 },
103 expectedMsgs: []string{
104 "invalid header \"With-Claim-And-Secret\": invalid values: header value has multiple entries: only one entry per value is allowed",
105 },
106 }),
107 Entry("with a header which has a claim without a claim", validateHeaderTableInput{
108 headers: []options.Header{
109 {
110 Name: "Without-Claim",
111 Values: []options.HeaderValue{
112 {
113 ClaimSource: &options.ClaimSource{
114 Prefix: "prefix",
115 },
116 },
117 },
118 },
119 validHeader3,
120 },
121 expectedMsgs: []string{
122 "invalid header \"Without-Claim\": invalid values: claim should not be empty",
123 },
124 }),
125 Entry("with a header with invalid secret source", validateHeaderTableInput{
126 headers: []options.Header{
127 {
128 Name: "With-Invalid-Secret",
129 Values: []options.HeaderValue{
130 {
131 SecretSource: &options.SecretSource{},
132 },
133 },
134 },
135 validHeader1,
136 },
137 expectedMsgs: []string{
138 "invalid header \"With-Invalid-Secret\": invalid values: multiple values specified for secret source: specify either value, fromEnv of fromFile",
139 },
140 }),
141 Entry("with a header with invalid basicAuthPassword source", validateHeaderTableInput{
142 headers: []options.Header{
143 {
144 Name: "With-Invalid-Basic-Auth",
145 Values: []options.HeaderValue{
146 {
147 ClaimSource: &options.ClaimSource{
148 Claim: "user",
149 BasicAuthPassword: &options.SecretSource{
150 FromEnv: "UNKNOWN_ENV",
151 },
152 },
153 },
154 },
155 },
156 validHeader1,
157 },
158 expectedMsgs: []string{
159 "invalid header \"With-Invalid-Basic-Auth\": invalid values: invalid basicAuthPassword: error loading secret from environent: no value for for key \"UNKNOWN_ENV\"",
160 },
161 }),
162 )
163})