Skip to content

Commit

Permalink
refactor: remove type annotation in place of label
Browse files Browse the repository at this point in the history
  • Loading branch information
ssttehrani committed Oct 24, 2023
1 parent 23a2b98 commit 37e4734
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 36 deletions.
7 changes: 0 additions & 7 deletions pkg/auth/htpasswd.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,6 @@ import (
)

const (
// AnnotationAuthType marks Secrets that can be used for basic Auth.
AnnotationAuthType = "auth.contour.snappcloud.io/type"
// AnnotationAuthRealm marks Secrets that match our authentication realm.
AnnotationAuthRealm = "auth.contour.snappcloud.io/realm"
secretRefKey = "secretRef"
Expand Down Expand Up @@ -128,11 +126,6 @@ func (h *Htpasswd) Check(ctx context.Context, request *Request) (*Response, erro
}

func (h *Htpasswd) verifyFetchSecretData(secret *v1.Secret) (bool, []byte) {
// Only look at basic auth Secrets.
if secret.Annotations[AnnotationAuthType] != "basic" {
return false, nil
}

// Accept the secret if it is for our realm or for any realm.
if realm := secret.Annotations[AnnotationAuthRealm]; realm != "" {
if realm != h.Realm && realm != "*" {
Expand Down
35 changes: 6 additions & 29 deletions pkg/auth/htpasswd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,6 @@ func TestHtpasswdAuth(t *testing.T) {
Name: "notmatched-label",
Namespace: "notmatched",
Annotations: map[string]string{
AnnotationAuthType: "basic",
AnnotationAuthRealm: "*",
},
},
Expand All @@ -54,11 +53,10 @@ func TestHtpasswdAuth(t *testing.T) {
// filtered by wrong annotation
&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "notmatched-annotation-auth-realm",
Name: "notmatched-annotation",
Namespace: "notmatched",
Labels: map[string]string{"app": "authserver"},
Labels: map[string]string{"auth.contour.snappcloud.io/type": "basic"},
Annotations: map[string]string{
AnnotationAuthType: "basic",
AnnotationAuthRealm: "wrong",
},
},
Expand All @@ -68,30 +66,12 @@ func TestHtpasswdAuth(t *testing.T) {
"auth": []byte("notmatched:$apr1$4W6cRE66$iANZepJfRTrpk3OxlzxAC0"),
},
},
// filtered by wrong annotation
&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "notmatched-annotation-auth-type",
Namespace: "notmatched",
Labels: map[string]string{"app": "authserver"},
Annotations: map[string]string{
AnnotationAuthType: "wrong",
AnnotationAuthRealm: "*",
},
},
Type: v1.SecretTypeOpaque,
Data: map[string][]byte{
// user=notmatched, pass=notmatched
"auth": []byte("notmatched:$apr1$4W6cRE66$iANZepJfRTrpk3OxlzxAC0"),
},
},
&v1.Secret{
ObjectMeta: metav1.ObjectMeta{
Name: "example1",
Namespace: "ns1",
Labels: map[string]string{"app": "authserver"},
Labels: map[string]string{"auth.contour.snappcloud.io/type": "basic"},
Annotations: map[string]string{
AnnotationAuthType: "basic",
AnnotationAuthRealm: "*",
},
},
Expand All @@ -105,9 +85,8 @@ func TestHtpasswdAuth(t *testing.T) {
ObjectMeta: metav1.ObjectMeta{
Name: "example2",
Namespace: "ns1",
Labels: map[string]string{"app": "authserver"},
Labels: map[string]string{"auth.contour.snappcloud.io/type": "basic"},
Annotations: map[string]string{
AnnotationAuthType: "basic",
AnnotationAuthRealm: "*",
},
},
Expand All @@ -119,7 +98,7 @@ func TestHtpasswdAuth(t *testing.T) {
},
)

selector, err := labels.Parse("app=authserver")
selector, err := labels.Parse("auth.contour.snappcloud.io/type=basic")
if err != nil {
t.Fatalf("failed to parse selector: %s", err)
}
Expand Down Expand Up @@ -158,9 +137,7 @@ func TestHtpasswdAuth(t *testing.T) {
//nolint:lll
assert.False(t, auth.Match("notmatched", "notmatched", "notmatched/notmatched-label"), "auth for notmatched:notmatched should fail (filtered by label selector)")
//nolint:lll
assert.False(t, auth.Match("notmatched", "notmatched", "notmatched/notmatched-annotation-auth-realm"), "auth for notmatched:notmatched should fail (filtered by wrong annotation)")
//nolint:lll
assert.False(t, auth.Match("notmatched", "notmatched", "notmatched/notmatched-annotation-auth-type"), "auth for notmatched:notmatched should fail (filtered by wrong annotation)")
assert.False(t, auth.Match("notmatched", "notmatched", "notmatched/notmatched-annotation"), "auth for notmatched:notmatched should fail (filtered by wrong annotation)")

// Check an unauthorized response.
response, err := auth.Check(context.TODO(), &Request{
Expand Down

0 comments on commit 37e4734

Please sign in to comment.