@@ -1,149 +1,124 @@
1package validation
1package validation
6 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
4 "github.com/oauth2-proxy/oauth2-proxy/pkg/apis/options"
7− "github.com/stretchr/testify/assert"
5+ . "github.com/onsi/ginkgo"
6+ . "github.com/onsi/ginkgo/extensions/table"
7+ . "github.com/onsi/gomega"
10−func Test_validateAllowlists(t *testing.T) {
10+var _ = Describe("Allowlist", func() {
11− opts := &options.Options{
11+ type validateRoutesTableInput struct {
12− SkipAuthRoutes: []string{
12+ routes []string
13− "POST=/foo/bar",
13+ errStrings []string
14− "PUT=^/foo/bar$",
14+ }
16− SkipAuthRegex: []string{"/foo/baz"},
16+ type validateRegexesTableInput struct {
17− TrustedIPs: []string{
17+ regexes []string
18− "10.32.0.1/32",
18+ errStrings []string
19− "43.36.201.0/24",
19+ }
21+ type validateTrustedIPsTableInput struct {
22− assert.Equal(t, []string{}, validateAllowlists(opts))
25−func Test_validateRoutes(t *testing.T) {
26+ DescribeTable("validateRoutes",
26− testCases := map[string]struct {
27+ func(r *validateRoutesTableInput) {
27− Regexes []string
28+ opts := &options.Options{
28− Expected []string
29+ SkipAuthRoutes: r.routes,
31+ Expect(validateRoutes(opts)).To(ConsistOf(r.errStrings))
30− "Valid regex routes": {
33+ Entry("Valid regex routes", &validateRoutesTableInput{
31− Regexes: []string{
34+ routes: []string{
33 "POST=/foo/bar",
36 "POST=/foo/bar",
34 "PUT=^/foo/bar$",
37 "PUT=^/foo/bar$",
35 "DELETE=/crazy/(?:regex)?/[^/]+/stuff$",
38 "DELETE=/crazy/(?:regex)?/[^/]+/stuff$",
37− Expected: []string{},
40+ errStrings: []string{},
39− "Bad regexes do not compile": {
42+ Entry("Bad regexes do not compile", &validateRoutesTableInput{
40− Regexes: []string{
43+ routes: []string{
41 "POST=/(foo",
44 "POST=/(foo",
42 "OPTIONS=/foo/bar)",
45 "OPTIONS=/foo/bar)",
43 "GET=^]/foo/bar[$",
46 "GET=^]/foo/bar[$",
44 "GET=^]/foo/bar[$",
47 "GET=^]/foo/bar[$",
46− Expected: []string{
49+ errStrings: []string{
47 "error compiling regex //(foo/: error parsing regexp: missing closing ): `/(foo`",
50 "error compiling regex //(foo/: error parsing regexp: missing closing ): `/(foo`",
48 "error compiling regex //foo/bar)/: error parsing regexp: unexpected ): `/foo/bar)`",
51 "error compiling regex //foo/bar)/: error parsing regexp: unexpected ): `/foo/bar)`",
49 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
52 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
50 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
53 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
55− for testName, tc := range testCases {
58+ DescribeTable("validateRegexes",
56− t.Run(testName, func(t *testing.T) {
59+ func(r *validateRegexesTableInput) {
57 opts := &options.Options{
60 opts := &options.Options{
58− SkipAuthRoutes: tc.Regexes,
61+ SkipAuthRegex: r.regexes,
60− msgs := validateRoutes(opts)
63+ Expect(validateRegexes(opts)).To(ConsistOf(r.errStrings))
61− assert.Equal(t, tc.Expected, msgs)
66−func Test_validateRegexes(t *testing.T) {
67− testCases := map[string]struct {
71− "Valid regex routes": {
65+ Entry("Valid regex routes", &validateRegexesTableInput{
72− Regexes: []string{
66+ regexes: []string{
74 "/foo/bar",
68 "/foo/bar",
75 "^/foo/bar$",
69 "^/foo/bar$",
76 "/crazy/(?:regex)?/[^/]+/stuff$",
70 "/crazy/(?:regex)?/[^/]+/stuff$",
78− Expected: []string{},
72+ errStrings: []string{},
80− "Bad regexes do not compile": {
74+ Entry("Bad regexes do not compile", &validateRegexesTableInput{
81− Regexes: []string{
75+ regexes: []string{
83 "/foo/bar)",
77 "/foo/bar)",
84 "^]/foo/bar[$",
78 "^]/foo/bar[$",
85 "^]/foo/bar[$",
79 "^]/foo/bar[$",
87− Expected: []string{
81+ errStrings: []string{
88 "error compiling regex //(foo/: error parsing regexp: missing closing ): `/(foo`",
82 "error compiling regex //(foo/: error parsing regexp: missing closing ): `/(foo`",
89 "error compiling regex //foo/bar)/: error parsing regexp: unexpected ): `/foo/bar)`",
83 "error compiling regex //foo/bar)/: error parsing regexp: unexpected ): `/foo/bar)`",
90 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
84 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
91 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
85 "error compiling regex /^]/foo/bar[$/: error parsing regexp: missing closing ]: `[$`",
96− for testName, tc := range testCases {
90+ DescribeTable("validateTrustedIPs",
97− t.Run(testName, func(t *testing.T) {
91+ func(t *validateTrustedIPsTableInput) {
98 opts := &options.Options{
92 opts := &options.Options{
99− SkipAuthRegex: tc.Regexes,
93+ TrustedIPs: t.trustedIPs,
101− msgs := validateRegexes(opts)
95+ Expect(validateTrustedIPs(opts)).To(ConsistOf(t.errStrings))
102− assert.Equal(t, tc.Expected, msgs)
107−func Test_validateTrustedIPs(t *testing.T) {
108− testCases := map[string]struct {
112− "Non-overlapping valid IPs": {
97+ Entry("Non-overlapping valid IPs", &validateTrustedIPsTableInput{
113− TrustedIPs: []string{
98+ trustedIPs: []string{
114 "127.0.0.1",
99 "127.0.0.1",
115 "10.32.0.1/32",
100 "10.32.0.1/32",
116 "43.36.201.0/24",
101 "43.36.201.0/24",
118 "2a12:105:ee7:9234:0:0:0:0/64",
103 "2a12:105:ee7:9234:0:0:0:0/64",
120− Expected: []string{},
105+ errStrings: []string{},
122− "Overlapping valid IPs": {
107+ Entry("Overlapping valid IPs", &validateTrustedIPsTableInput{
123− TrustedIPs: []string{
108+ trustedIPs: []string{
124 "135.180.78.199",
109 "135.180.78.199",
125 "135.180.78.199/32",
110 "135.180.78.199/32",
126 "d910:a5a1:16f8:ddf5:e5b9:5cef:a65e:41f4",
111 "d910:a5a1:16f8:ddf5:e5b9:5cef:a65e:41f4",
127 "d910:a5a1:16f8:ddf5:e5b9:5cef:a65e:41f4/128",
112 "d910:a5a1:16f8:ddf5:e5b9:5cef:a65e:41f4/128",
129− Expected: []string{},
114+ errStrings: []string{},
131− "Invalid IPs": {
116+ Entry("Invalid IPs", &validateTrustedIPsTableInput{
132− TrustedIPs: []string{"[::1]", "alkwlkbn/32"},
117+ trustedIPs: []string{"[::1]", "alkwlkbn/32"},
133− Expected: []string{
118+ errStrings: []string{
134 "trusted_ips[0] ([::1]) could not be recognized",
119 "trusted_ips[0] ([::1]) could not be recognized",
135 "trusted_ips[1] (alkwlkbn/32) could not be recognized",
120 "trusted_ips[1] (alkwlkbn/32) could not be recognized",
140− for testName, tc := range testCases {
141− t.Run(testName, func(t *testing.T) {
142− opts := &options.Options{
143− TrustedIPs: tc.TrustedIPs,
145− msgs := validateTrustedIPs(opts)
146− assert.Equal(t, tc.Expected, msgs)