-
Notifications
You must be signed in to change notification settings - Fork 6
verify an identity block for a particular addresss #60
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
Merged
Merged
Changes from 7 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
9990ebf
add a function to verify a particular address would be valid for a gi…
dovholuknf 5265871
use require instead
dovholuknf 1d20881
add typed errors and rework assertions to better use require
dovholuknf 7bbfd29
golanglint-ci change
dovholuknf 7cf89b4
undo unexpected change. must have been a find/replace mistake
dovholuknf e0ca5d1
forgot copyright
dovholuknf c234f76
add tests for wildcard too
dovholuknf 1fe3eee
rename address to hostnameOrIp
dovholuknf 476a301
refactor to hostnameOrIp
dovholuknf 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
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,242 @@ | ||
| /* | ||
| Copyright 2019 NetFoundry Inc. | ||
|
|
||
| Licensed 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 | ||
|
|
||
| https://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 identity | ||
|
|
||
| import ( | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "errors" | ||
| "github.com/stretchr/testify/require" | ||
| "net" | ||
| "testing" | ||
| ) | ||
|
|
||
| // mockIdentity implements the Identity interface for testing | ||
| type mockIdentity struct { | ||
| serverCerts []*tls.Certificate | ||
| clientCert *tls.Certificate | ||
| } | ||
|
|
||
| func (m *mockIdentity) Cert() *tls.Certificate { return m.clientCert } | ||
| func (m *mockIdentity) ServerCert() []*tls.Certificate { return m.serverCerts } | ||
| func (m *mockIdentity) CA() *x509.CertPool { return nil } | ||
| func (m *mockIdentity) CaPool() *CaPool { return nil } | ||
| func (m *mockIdentity) ServerTLSConfig() *tls.Config { return nil } | ||
| func (m *mockIdentity) ClientTLSConfig() *tls.Config { return nil } | ||
| func (m *mockIdentity) Reload() error { return nil } | ||
| func (m *mockIdentity) WatchFiles() error { return nil } | ||
| func (m *mockIdentity) StopWatchingFiles() {} | ||
| func (m *mockIdentity) SetCert(_ string) error { return nil } | ||
| func (m *mockIdentity) SetServerCert(_ string) error { return nil } | ||
| func (m *mockIdentity) GetConfig() *Config { return nil } | ||
| func (m *mockIdentity) ValidFor(_ string) error { return nil } | ||
|
|
||
| const ( | ||
| validDNS = "example.com" | ||
| invalidDNS = "invalid.com" | ||
| validIP4 = "192.168.1.1" | ||
| invalidIP4 = "10.0.0.1" | ||
| validIP6 = "::1" | ||
| invalidIP6 = "fe80::1" | ||
| validPort = "443" | ||
| expandedIPv6 = "2001:0db8:0000:0000:0000:ff00:0042:8329" | ||
| compressedIPv6 = "2001:0db8::ff00:0042:8329" | ||
| ) | ||
|
|
||
| // Helper to create a mock identity with certs | ||
| func createMockIdentity(dnsNames []string, ipAddresses []string) *TokenId { | ||
| leaf := &x509.Certificate{} | ||
| leaf.DNSNames = append(leaf.DNSNames, dnsNames...) | ||
| for _, ip := range ipAddresses { | ||
| leaf.IPAddresses = append(leaf.IPAddresses, net.ParseIP(ip)) | ||
| } | ||
|
|
||
| tlsCert := &tls.Certificate{Leaf: leaf} | ||
| mi := &mockIdentity{ | ||
| serverCerts: []*tls.Certificate{tlsCert}, | ||
| clientCert: tlsCert, | ||
| } | ||
| id := &TokenId{ | ||
| Identity: mi, | ||
| Token: "", | ||
| Data: nil, | ||
| } | ||
| return id | ||
| } | ||
|
|
||
| func TestValidFor_ValidHostname(t *testing.T) { | ||
| id := createMockIdentity([]string{validDNS}, []string{}) | ||
|
|
||
| err := id.ValidFor(validDNS + ":" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_InvalidHostname(t *testing.T) { | ||
| id := createMockIdentity([]string{validDNS}, []string{}) | ||
|
|
||
| err := id.ValidFor(invalidDNS + ":" + validPort) | ||
| require.True(t, errors.Is(err, ErrInvalidAddressForIdentity)) | ||
| var addrErr *AddressError | ||
| if errors.As(err, &addrErr) { | ||
| require.Equal(t, addrErr.Host, invalidDNS) | ||
| require.Contains(t, addrErr.ValidFor, validDNS) | ||
| } | ||
| } | ||
|
|
||
| func TestValidFor_ValidIPv4(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{validIP4}) | ||
|
|
||
| err := id.ValidFor(validIP4 + ":" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_InvalidIPv4(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{validIP4}) | ||
|
|
||
| err := id.ValidFor(invalidIP4 + ":" + validPort) | ||
| require.True(t, errors.Is(err, ErrInvalidAddressForIdentity)) | ||
| var addrErr *AddressError | ||
| if errors.As(err, &addrErr) { | ||
| require.Equal(t, addrErr.Host, invalidIP4) | ||
| require.Contains(t, addrErr.ValidFor, validIP4) | ||
| } | ||
| } | ||
|
|
||
| func TestValidFor_ValidIPv6(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{validIP6}) | ||
|
|
||
| err := id.ValidFor("[" + validIP6 + "]:" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_InvalidIPv6(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{validIP6}) | ||
|
|
||
| err := id.ValidFor("[" + invalidIP6 + "]:" + validPort) | ||
| require.True(t, errors.Is(err, ErrInvalidAddressForIdentity)) | ||
| var addrErr *AddressError | ||
| if errors.As(err, &addrErr) { | ||
| require.Equal(t, addrErr.Host, invalidIP6) | ||
| require.Contains(t, addrErr.ValidFor, validIP6) | ||
| } | ||
| } | ||
|
|
||
| func TestValidFor_ValidMixed(t *testing.T) { | ||
| id := createMockIdentity([]string{validDNS}, []string{validIP4}) | ||
|
|
||
| err1 := id.ValidFor(validDNS + ":" + validPort) | ||
| require.NoError(t, err1) | ||
| err2 := id.ValidFor(validIP4 + ":" + validPort) | ||
| require.NoError(t, err2) | ||
| } | ||
|
|
||
| func TestValidFor_InvalidMixed(t *testing.T) { | ||
| id := createMockIdentity([]string{validDNS}, []string{validIP4}) | ||
|
|
||
| err1 := id.ValidFor(invalidDNS + ":" + validPort) | ||
| require.True(t, errors.Is(err1, ErrInvalidAddressForIdentity)) | ||
| var addrErr1 *AddressError | ||
| if errors.As(err1, &addrErr1) { | ||
| require.Equal(t, addrErr1.Host, invalidDNS) | ||
| require.Contains(t, addrErr1.ValidFor, validDNS) | ||
| require.Contains(t, addrErr1.ValidFor, validIP4) | ||
| } | ||
|
|
||
| err2 := id.ValidFor(invalidIP4 + ":" + validPort) | ||
| require.True(t, errors.Is(err2, ErrInvalidAddressForIdentity)) | ||
| var addrErr2 *AddressError | ||
| if errors.As(err2, &addrErr2) { | ||
| require.Equal(t, addrErr2.Host, invalidIP4) | ||
| require.Contains(t, addrErr2.ValidFor, validDNS) | ||
| require.Contains(t, addrErr2.ValidFor, validIP4) | ||
| } | ||
| } | ||
|
|
||
| func TestValidFor_NoCerts(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{}) | ||
|
|
||
| err := id.ValidFor(validDNS + ":" + validPort) | ||
| require.True(t, errors.Is(err, ErrInvalidAddressForIdentity)) | ||
| var addrErr *AddressError | ||
| if errors.As(err, &addrErr) { | ||
| require.Equal(t, addrErr.Host, validDNS) | ||
| require.Empty(t, addrErr.ValidFor) | ||
| } | ||
| } | ||
|
|
||
| func TestValidFor_ExpandedIPv6(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{expandedIPv6}) | ||
|
|
||
| err := id.ValidFor("[" + expandedIPv6 + "]:" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_CompressedIPv6(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{compressedIPv6}) | ||
|
|
||
| err := id.ValidFor("[" + compressedIPv6 + "]:" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_ExpandedIPv6_MatchesCompressed(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{expandedIPv6}) | ||
|
|
||
| err := id.ValidFor("[" + compressedIPv6 + "]:" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_CompressedIPv6_MatchesExpanded(t *testing.T) { | ||
| id := createMockIdentity([]string{}, []string{compressedIPv6}) | ||
|
|
||
| err := id.ValidFor("[" + expandedIPv6 + "]:" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_InvalidAddress(t *testing.T) { | ||
| id := createMockIdentity([]string{""}, []string{}) | ||
|
|
||
| err := id.ValidFor("tls:" + validPort) | ||
| require.ErrorIs(t, err, ErrInvalidAddress) | ||
| } | ||
|
|
||
| func TestValidFor_Wildcard(t *testing.T) { | ||
| id := createMockIdentity([]string{"*." + validDNS}, []string{}) | ||
|
|
||
| err := id.ValidFor("ctrl.example.com:" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_Wildcard2(t *testing.T) { | ||
| id := createMockIdentity([]string{"*." + validDNS}, []string{}) | ||
|
|
||
| err := id.ValidFor("other.example.com:" + validPort) | ||
| require.NoError(t, err) | ||
| } | ||
|
|
||
| func TestValidFor_NonWildcardAddlSubdomains(t *testing.T) { | ||
| id := createMockIdentity([]string{"*.another.domain." + validDNS}, []string{}) | ||
|
|
||
| err := id.ValidFor("other.example.com:" + validPort) | ||
| require.ErrorIs(t, err, ErrInvalidAddressForIdentity) | ||
| } | ||
|
|
||
| func TestValidFor_NonWildcardCert(t *testing.T) { | ||
| id := createMockIdentity([]string{validDNS}, []string{}) | ||
|
|
||
| err := id.ValidFor("ctrl.example.com:" + validPort) | ||
| require.ErrorIs(t, err, ErrInvalidAddressForIdentity) | ||
| } |
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.
Uh oh!
There was an error while loading. Please reload this page.
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.
This should probably use url.Parse() so it will handle
http://andtls:and any other<proto>:[//]<hostname>:<port>[/<path>][?<querystring>]format. Still can use the.Hoston it to then split hostname port. The URL parsing won't do that for you.