|
| 1 | +package service |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + |
| 6 | + "github.com/stretchr/testify/assert" |
| 7 | + "github.com/tinyauthapp/tinyauth/internal/model" |
| 8 | + networking "k8s.io/api/networking/v1" |
| 9 | +) |
| 10 | + |
| 11 | +func TestKubernetesIngressExtractorExtract(t *testing.T) { |
| 12 | + meta := &ResourceMeta{Typ: ResourceTypeIngress, Name: "route", Namespace: "default"} |
| 13 | + tests := []struct { |
| 14 | + name string |
| 15 | + ingress *networking.Ingress |
| 16 | + want ExtractionResult |
| 17 | + }{ |
| 18 | + { |
| 19 | + name: "domain, name and wildcard match across hosts", |
| 20 | + ingress: testIngress("route", map[string]string{ |
| 21 | + "tinyauth.apps.dashboard.config.domain": "app.example.com", |
| 22 | + "tinyauth.apps.dashboard.users.allow": "alice", |
| 23 | + "tinyauth.apps.portal.users.allow": "bob", |
| 24 | + "tinyauth.apps.other.users.allow": "carol", |
| 25 | + }, "app.example.com", "Portal.example.com"), |
| 26 | + want: ExtractionResult{Meta: meta, Apps: map[string]model.App{ |
| 27 | + "dashboard": {Config: model.AppConfig{Domain: "app.example.com"}, Users: model.AppUsers{Allow: "alice"}}, |
| 28 | + "portal": {Users: model.AppUsers{Allow: "bob"}}, |
| 29 | + }}, |
| 30 | + }, |
| 31 | + { |
| 32 | + name: "wildcard matches configured domain", |
| 33 | + ingress: testIngress("route", map[string]string{ |
| 34 | + "tinyauth.apps.dashboard.config.domain": "dashboard.example.com", |
| 35 | + }, "*.example.com"), |
| 36 | + want: ExtractionResult{Meta: meta, Apps: map[string]model.App{ |
| 37 | + "dashboard": {Config: model.AppConfig{Domain: "dashboard.example.com"}}, |
| 38 | + }}, |
| 39 | + }, |
| 40 | + { |
| 41 | + name: "hostless rule matches name", |
| 42 | + ingress: testIngress("route", map[string]string{"tinyauth.apps.dashboard.users.allow": "alice"}, ""), |
| 43 | + want: ExtractionResult{Meta: meta, Apps: map[string]model.App{ |
| 44 | + "dashboard": {Users: model.AppUsers{Allow: "alice"}}, |
| 45 | + }}, |
| 46 | + }, |
| 47 | + { |
| 48 | + name: "invalid domain falls back to name", |
| 49 | + ingress: testIngress("route", map[string]string{"tinyauth.apps.dashboard.config.domain": "dömain.example.com"}, "dashboard.example.com"), |
| 50 | + want: ExtractionResult{Meta: meta, Apps: map[string]model.App{ |
| 51 | + "dashboard": {Config: model.AppConfig{Domain: "dömain.example.com"}}, |
| 52 | + }}, |
| 53 | + }, |
| 54 | + { |
| 55 | + name: "unmatched annotations yield empty apps", |
| 56 | + ingress: testIngress("route", map[string]string{"tinyauth.apps.other.users.allow": "alice"}, "dashboard.example.com"), |
| 57 | + want: ExtractionResult{Meta: meta, Apps: map[string]model.App{}}, |
| 58 | + }, |
| 59 | + { |
| 60 | + name: "no annotations yield empty apps", |
| 61 | + ingress: testIngress("route", nil, "dashboard.example.com"), |
| 62 | + want: ExtractionResult{Meta: meta, Apps: map[string]model.App{}}, |
| 63 | + }, |
| 64 | + { |
| 65 | + name: "invalid annotations", |
| 66 | + ingress: testIngress("route", map[string]string{"tinyauth.apps.dashboard.users.invalid": "alice"}, "dashboard.example.com"), |
| 67 | + want: ExtractionResult{Meta: meta}, |
| 68 | + }, |
| 69 | + { |
| 70 | + name: "no rules", |
| 71 | + ingress: testIngress("route", nil), |
| 72 | + want: ExtractionResult{Meta: meta}, |
| 73 | + }, |
| 74 | + { |
| 75 | + name: "missing name", |
| 76 | + ingress: testIngress("", nil, "app.example.com"), |
| 77 | + want: ExtractionResult{}, |
| 78 | + }, |
| 79 | + { |
| 80 | + name: "missing namespace", |
| 81 | + ingress: &networking.Ingress{}, |
| 82 | + want: ExtractionResult{}, |
| 83 | + }, |
| 84 | + } |
| 85 | + extractor := NewKubernetesIngressExtractor(KubernetesIngressExtractorInput{Log: kubernetesTestLogger()}) |
| 86 | + for _, tt := range tests { |
| 87 | + t.Run(tt.name, func(t *testing.T) { |
| 88 | + assert.Equal(t, tt.want, extractor.Extract(tt.ingress)) |
| 89 | + }) |
| 90 | + } |
| 91 | +} |
| 92 | + |
| 93 | +func TestKubernetesIngressExtractorHostsAndPaths(t *testing.T) { |
| 94 | + extractor := NewKubernetesIngressExtractor(KubernetesIngressExtractorInput{Log: kubernetesTestLogger()}) |
| 95 | + for _, tt := range []struct { |
| 96 | + name string |
| 97 | + rules []networking.IngressRule |
| 98 | + hosts []string |
| 99 | + paths []string |
| 100 | + }{ |
| 101 | + {"no rules", nil, nil, nil}, |
| 102 | + {"no HTTP paths", []networking.IngressRule{{Host: "app.example.com"}}, []string{"app.example.com"}, nil}, |
| 103 | + {"catch-all path", []networking.IngressRule{{Host: "app.example.com", IngressRuleValue: networking.IngressRuleValue{HTTP: &networking.HTTPIngressRuleValue{Paths: []networking.HTTPIngressPath{{Path: "/"}}}}}}, []string{"app.example.com"}, []string{"/"}}, |
| 104 | + {"specific paths", []networking.IngressRule{{Host: "app.example.com", IngressRuleValue: networking.IngressRuleValue{HTTP: &networking.HTTPIngressRuleValue{Paths: []networking.HTTPIngressPath{{Path: "/login"}, {Path: "/admin"}}}}}}, []string{"app.example.com"}, []string{"/login", "/admin"}}, |
| 105 | + } { |
| 106 | + t.Run(tt.name, func(t *testing.T) { |
| 107 | + assert.Equal(t, tt.hosts, extractor.getHosts(tt.rules)) |
| 108 | + if len(tt.rules) > 0 { |
| 109 | + assert.Equal(t, tt.paths, extractor.getPaths(tt.rules[0])) |
| 110 | + } |
| 111 | + }) |
| 112 | + } |
| 113 | +} |
| 114 | + |
| 115 | +func TestKubernetesHostMatching(t *testing.T) { |
| 116 | + for _, tt := range []struct { |
| 117 | + name, host, hostname string |
| 118 | + want bool |
| 119 | + }{ |
| 120 | + {"exact", "app.example.com", "app.example.com", true}, |
| 121 | + {"case insensitive and trailing dot", "App.Example.com.", "app.example.com", true}, |
| 122 | + {"wildcard", "*.example.com", "app.example.com", true}, |
| 123 | + {"wildcard case insensitive", "*.Example.com", "App.example.com", true}, |
| 124 | + {"wildcard excludes apex", "*.example.com", "example.com", false}, |
| 125 | + {"wildcard excludes empty label", "*.example.com", ".example.com", false}, |
| 126 | + {"wildcard excludes nested labels", "*.example.com", "deep.app.example.com", false}, |
| 127 | + {"wildcard excludes other suffix", "*.example.com", "app.other.com", false}, |
| 128 | + {"different host", "app.example.com", "other.example.com", false}, |
| 129 | + {"empty host", "", "other.example.com", true}, |
| 130 | + } { |
| 131 | + t.Run(tt.name, func(t *testing.T) { |
| 132 | + assert.Equal(t, tt.want, hostMatchesHostname(tt.host, tt.hostname)) |
| 133 | + }) |
| 134 | + } |
| 135 | +} |
| 136 | + |
| 137 | +func TestKubernetesHostCoversName(t *testing.T) { |
| 138 | + for _, tt := range []struct { |
| 139 | + host, name string |
| 140 | + want bool |
| 141 | + }{ |
| 142 | + {"", "dashboard", true}, |
| 143 | + {"dashboard.example.com", "dashboard", true}, |
| 144 | + {"Dashboard.example.com", "dashboard", true}, |
| 145 | + {"*.example.com", "dashboard", true}, |
| 146 | + {"other.example.com", "dashboard", false}, |
| 147 | + } { |
| 148 | + t.Run(tt.host+"/"+tt.name, func(t *testing.T) { |
| 149 | + assert.Equal(t, tt.want, hostCoversName(tt.host, tt.name)) |
| 150 | + }) |
| 151 | + } |
| 152 | +} |
0 commit comments