-
Notifications
You must be signed in to change notification settings - Fork 4
fix: detect and warn on inconsistent SSL host/certificate configuration #437
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
shreemaan-abhishek
wants to merge
5
commits into
master
Choose a base branch
from
fix/ssl-conflict-detector-host-and-mtls
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b5a960e
fix: match overlapping hosts and mTLS config in SSL conflict detector
shreemaan-abhishek 338395a
fix: warn when ApisixTls certificate does not cover declared SNI hosts
shreemaan-abhishek 33cd852
fix: report overlapping SSL configuration as an admission warning
shreemaan-abhishek bd5fbbe
test(e2e): expect SSL overlap to warn, not reject
shreemaan-abhishek 832c1dc
fix: emit the SSL conflict warning on a single line
shreemaan-abhishek File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,94 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one | ||
| // or more contributor license agreements. See the NOTICE file | ||
| // distributed with this work for additional information | ||
| // regarding copyright ownership. The ASF licenses this file | ||
| // to you under the Apache License, Version 2.0 (the | ||
| // "License"); you may not use this file except in compliance | ||
| // with the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, | ||
| // software distributed under the License is distributed on an | ||
| // "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
| // KIND, either express or implied. See the License for the | ||
| // specific language governing permissions and limitations | ||
| // under the License. | ||
|
|
||
| package translator | ||
|
|
||
| import ( | ||
| "crypto/rand" | ||
| "crypto/rsa" | ||
| "crypto/x509" | ||
| "crypto/x509/pkix" | ||
| "encoding/pem" | ||
| "math/big" | ||
| "slices" | ||
| "testing" | ||
| "time" | ||
|
|
||
| apiv2 "github.com/apache/apisix-ingress-controller/api/v2" | ||
| ) | ||
|
|
||
| func genCertWithSANs(t *testing.T, sans []string) []byte { | ||
| t.Helper() | ||
| priv, err := rsa.GenerateKey(rand.Reader, 2048) | ||
| if err != nil { | ||
| t.Fatalf("failed to generate key: %v", err) | ||
| } | ||
| serial, err := rand.Int(rand.Reader, big.NewInt(1<<62)) | ||
| if err != nil { | ||
| t.Fatalf("failed to generate serial: %v", err) | ||
| } | ||
| cn := "test" | ||
| if len(sans) > 0 { | ||
| cn = sans[0] | ||
| } | ||
| template := &x509.Certificate{ | ||
| SerialNumber: serial, | ||
| Subject: pkix.Name{CommonName: cn}, | ||
| DNSNames: sans, | ||
| NotBefore: time.Now().Add(-time.Hour), | ||
| NotAfter: time.Now().Add(24 * time.Hour), | ||
| KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature, | ||
| ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth}, | ||
| BasicConstraintsValid: true, | ||
| } | ||
| der, err := x509.CreateCertificate(rand.Reader, template, template, &priv.PublicKey, priv) | ||
| if err != nil { | ||
| t.Fatalf("failed to create cert: %v", err) | ||
| } | ||
| return pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: der}) | ||
| } | ||
|
|
||
| func hostTypes(hosts ...string) []apiv2.HostType { | ||
| out := make([]apiv2.HostType, 0, len(hosts)) | ||
| for _, h := range hosts { | ||
| out = append(out, apiv2.HostType(h)) | ||
| } | ||
| return out | ||
| } | ||
|
|
||
| func TestUncoveredSNIHosts(t *testing.T) { | ||
| cases := []struct { | ||
| name string | ||
| sans []string | ||
| hosts []string | ||
| wantUncovered []string | ||
| }{ | ||
| {"exact match", []string{"shop.example.com"}, []string{"shop.example.com"}, nil}, | ||
| {"wildcard covers subdomain", []string{"*.example.com"}, []string{"shop.example.com"}, nil}, | ||
| {"declared host not in SANs", []string{"internal.corp.local"}, []string{"shop.example.com"}, []string{"shop.example.com"}}, | ||
| {"one of many uncovered", []string{"a.example.com"}, []string{"a.example.com", "b.example.com"}, []string{"b.example.com"}}, | ||
| {"no DNS SANs is lenient", nil, []string{"shop.example.com"}, nil}, | ||
| } | ||
| for _, c := range cases { | ||
| t.Run(c.name, func(t *testing.T) { | ||
| uncovered, _ := uncoveredSNIHosts(genCertWithSANs(t, c.sans), hostTypes(c.hosts...)) | ||
| if !slices.Equal(uncovered, c.wantUncovered) { | ||
| t.Fatalf("uncoveredSNIHosts() = %v, want %v", uncovered, c.wantUncovered) | ||
| } | ||
| }) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| // Licensed to the Apache Software Foundation (ASF) under one or more | ||
| // contributor license agreements. See the NOTICE file distributed with | ||
| // this work for additional information regarding copyright ownership. | ||
| // The ASF licenses this file to You under the Apache License, Version 2.0 | ||
| // (the "License"); you may not use this file except in compliance with | ||
| // the License. You may obtain a copy of the License at | ||
| // | ||
| // http://www.apache.org/licenses/LICENSE-2.0 | ||
| // | ||
| // Unless required by applicable law or agreed to in writing, software | ||
| // distributed under the License is distributed on an "AS IS" BASIS, | ||
| // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
| // See the License for the specific language governing permissions and | ||
| // limitations under the License. | ||
|
|
||
| package ssl | ||
|
|
||
| import "testing" | ||
|
|
||
| func TestHostsOverlap(t *testing.T) { | ||
| cases := []struct { | ||
| a, b string | ||
| want bool | ||
| }{ | ||
| {"example.com", "example.com", true}, // identical exact | ||
| {"*.example.com", "*.example.com", true}, // identical wildcard | ||
| {"app.example.com", "*.example.com", true}, // exact covered by wildcard | ||
| {"*.example.com", "app.example.com", true}, // wildcard covers exact (inverse) | ||
| {"App.Example.com", "*.EXAMPLE.com", true}, // case-insensitive | ||
| {"a.b.example.com", "*.example.com", false}, // multi-label not covered | ||
| {"example.com", "*.example.com", false}, // apex not covered by its wildcard | ||
| {"app.example.com", "app.other.com", false}, // different exacts | ||
| {"*.example.com", "*.other.com", false}, // distinct wildcards never overlap | ||
| {"*.a.example.com", "*.example.com", false}, // wildcards at different depths | ||
| {"", "example.com", false}, // empty | ||
| } | ||
| for _, c := range cases { | ||
| if got := HostsOverlap(c.a, c.b); got != c.want { | ||
| t.Errorf("HostsOverlap(%q, %q) = %v, want %v", c.a, c.b, got, c.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestHostCoveredBy(t *testing.T) { | ||
| cases := []struct { | ||
| host, san string | ||
| want bool | ||
| }{ | ||
| {"app.example.com", "app.example.com", true}, // exact SAN covers exact host | ||
| {"app.example.com", "*.example.com", true}, // wildcard SAN covers subdomain | ||
| {"App.Example.com", "*.EXAMPLE.com", true}, // case-insensitive | ||
| {"*.example.com", "*.example.com", true}, // wildcard host needs identical wildcard SAN | ||
| {"*.example.com", "app.example.com", false}, // exact SAN can't cover a wildcard host | ||
| {"a.b.example.com", "*.example.com", false}, // wildcard SAN is single-label only | ||
| {"shop.example.com", "internal.corp.local", false}, // unrelated SAN | ||
| {"example.com", "*.example.com", false}, // apex not covered by its wildcard | ||
| {"app.example.com", "", false}, // empty SAN | ||
| } | ||
| for _, c := range cases { | ||
| if got := HostCoveredBy(c.host, c.san); got != c.want { | ||
| t.Errorf("HostCoveredBy(%q, %q) = %v, want %v", c.host, c.san, got, c.want) | ||
| } | ||
| } | ||
| } | ||
|
|
||
| func TestParentWildcard(t *testing.T) { | ||
| cases := []struct { | ||
| host, want string | ||
| }{ | ||
| {"app.example.com", "*.example.com"}, | ||
| {"example.com", "*.com"}, | ||
| {"*.example.com", ""}, // already a wildcard | ||
| {"localhost", ""}, // no parent label | ||
| {"", ""}, | ||
| } | ||
| for _, c := range cases { | ||
| if got := ParentWildcard(c.host); got != c.want { | ||
| t.Errorf("ParentWildcard(%q) = %q, want %q", c.host, got, c.want) | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[P1] Apply the warning behavior to
ValidateUpdatetoo. This hunk changes only create; the update path still returns a hard error for the same conflict. An existing ApisixTls in an overlapping setup therefore still cannot be edited, which is one of the cases this PR says the warning conversion fixes. Please mirror this flow inValidateUpdateand add an update regression test.