Skip to content

Commit 4d19f5d

Browse files
Annotations/AuthTLS: Allow named redirects. (#13819)
Signed-off-by: Dean Coakley <dean.s.coakley@gmail.com> Co-authored-by: Dean Coakley <dean.s.coakley@gmail.com>
1 parent c3f453e commit 4d19f5d

File tree

2 files changed

+31
-15
lines changed

2 files changed

+31
-15
lines changed

internal/ingress/annotations/authtls/main.go

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,7 +42,7 @@ const (
4242

4343
var (
4444
authVerifyClientRegex = regexp.MustCompile(`^(on|off|optional|optional_no_ca)$`)
45-
redirectRegex = regexp.MustCompile(`^((https?://)?[A-Za-z0-9\-.]+(:\d+)?)?(/[A-Za-z0-9\-_.]+)*/?$`)
45+
redirectRegex = regexp.MustCompile(`^(@[A-Za-z0-9_-]+|((https?://)?[A-Za-z0-9\-.]+(:\d+)?)?(/[A-Za-z0-9\-_.]+)*/?)$`)
4646
)
4747

4848
var authTLSAnnotations = parser.Annotation{
@@ -148,12 +148,12 @@ func (a authTLS) Parse(ing *networking.Ingress) (interface{}, error) {
148148
var err error
149149
config := &Config{}
150150

151-
tlsauthsecret, err := parser.GetStringAnnotation(annotationAuthTLSSecret, ing, a.annotationConfig.Annotations)
151+
authTLSSecret, err := parser.GetStringAnnotation(annotationAuthTLSSecret, ing, a.annotationConfig.Annotations)
152152
if err != nil {
153153
return &Config{}, err
154154
}
155155

156-
ns, _, err := k8s.ParseNameNS(tlsauthsecret)
156+
ns, _, err := k8s.ParseNameNS(authTLSSecret)
157157
if err != nil {
158158
return &Config{}, ing_errors.NewLocationDenied(err.Error())
159159
}
@@ -166,7 +166,7 @@ func (a authTLS) Parse(ing *networking.Ingress) (interface{}, error) {
166166
return &Config{}, ing_errors.NewLocationDenied("cross namespace secrets are not supported")
167167
}
168168

169-
authCert, err := a.r.GetAuthCertificate(tlsauthsecret)
169+
authCert, err := a.r.GetAuthCertificate(authTLSSecret)
170170
if err != nil {
171171
e := fmt.Errorf("error obtaining certificate: %w", err)
172172
return &Config{}, ing_errors.LocationDeniedError{Reason: e}

internal/ingress/annotations/authtls/main_test.go

Lines changed: 27 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -48,14 +48,7 @@ func buildIngress() *networking.Ingress {
4848
Namespace: api.NamespaceDefault,
4949
},
5050
Spec: networking.IngressSpec{
51-
DefaultBackend: &networking.IngressBackend{
52-
Service: &networking.IngressServiceBackend{
53-
Name: "default-backend",
54-
Port: networking.ServiceBackendPort{
55-
Number: 80,
56-
},
57-
},
58-
},
51+
DefaultBackend: &defaultBackend,
5952
Rules: []networking.IngressRule{
6053
{
6154
Host: "foo.bar.com",
@@ -163,15 +156,38 @@ func TestAnnotations(t *testing.T) {
163156
if u.ValidationDepth != 2 {
164157
t.Errorf("expected %v but got %v", 2, u.ValidationDepth)
165158
}
166-
if u.ErrorPage != "ok.com/error" {
167-
t.Errorf("expected %v but got %v", "ok.com/error", u.ErrorPage)
168-
}
169159
if u.PassCertToUpstream != true {
170160
t.Errorf("expected %v but got %v", true, u.PassCertToUpstream)
171161
}
172162
if u.MatchCN != "CN=(hello-app|ok|goodbye)" {
173163
t.Errorf("expected %v but got %v", "CN=(hello-app|ok|goodbye)", u.MatchCN)
174164
}
165+
166+
for _, tc := range []struct {
167+
name string
168+
errorPage string
169+
want string
170+
}{
171+
{"url redirect", "ok.com/error", "ok.com/error"},
172+
{"named redirect numeric", "@401", "@401"},
173+
{"named redirect alphanumeric with underscores", "@four_oh_one", "@four_oh_one"},
174+
} {
175+
t.Run(tc.name, func(t *testing.T) {
176+
data[parser.GetAnnotationWithPrefix(annotationAuthTLSErrorPage)] = tc.errorPage
177+
ing.SetAnnotations(data)
178+
i, err := NewParser(fakeSecret).Parse(ing)
179+
if err != nil {
180+
t.Errorf("Unexpected error with ingress: %v", err)
181+
}
182+
u, ok := i.(*Config)
183+
if !ok {
184+
t.Errorf("expected *Config but got %v", u)
185+
}
186+
if u.ErrorPage != tc.want {
187+
t.Errorf("expected %v but got %v", tc.want, u.ErrorPage)
188+
}
189+
})
190+
}
175191
}
176192

177193
func TestInvalidAnnotations(t *testing.T) {

0 commit comments

Comments
 (0)