-
Notifications
You must be signed in to change notification settings - Fork 66
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Environment variables with 'https_address' in them should have 'https…
…://' scheme
- Loading branch information
1 parent
7d83e20
commit 18e2024
Showing
6 changed files
with
192 additions
and
86 deletions.
There are no files selected for viewing
This file contains 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 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 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 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 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,91 @@ | ||
// Copyright 2024 the Pinniped contributors. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package testlib | ||
|
||
import ( | ||
"net" | ||
"net/url" | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
|
||
"go.pinniped.dev/internal/certauthority" | ||
) | ||
|
||
type SupervisorIssuer struct { | ||
issuerURL *url.URL | ||
ip net.IP | ||
} | ||
|
||
func NewSupervisorIssuer(t *testing.T, issuer string) SupervisorIssuer { | ||
t.Helper() | ||
|
||
t.Logf("NewSupervisorIssuer: %s", issuer) | ||
|
||
issuerURL, err := url.Parse(issuer) | ||
require.NoError(t, err) | ||
require.NotEmpty(t, issuerURL.Hostname(), "hostname cannot be empty, usually this happens when the scheme is empty. issuer=%q", issuer) | ||
|
||
ip := net.ParseIP(issuerURL.Hostname()) | ||
|
||
return SupervisorIssuer{ | ||
issuerURL: issuerURL, | ||
ip: ip, | ||
} | ||
} | ||
|
||
func (s SupervisorIssuer) Issuer() string { | ||
return s.issuerURL.String() | ||
} | ||
|
||
func (s SupervisorIssuer) Address() string { | ||
return s.issuerURL.Host | ||
} | ||
|
||
func (s SupervisorIssuer) Hostname() string { | ||
return s.issuerURL.Hostname() | ||
} | ||
|
||
func (s SupervisorIssuer) Port(defaultPort string) string { | ||
port := s.issuerURL.Port() | ||
if port == "" { | ||
return defaultPort | ||
} | ||
return s.issuerURL.Port() | ||
} | ||
|
||
func (s SupervisorIssuer) Hostnames() []string { | ||
if s.IsIPAddress() { | ||
return nil // don't want DNS records in the cert when using IP address | ||
} | ||
return []string{s.issuerURL.Hostname()} | ||
} | ||
|
||
func (s SupervisorIssuer) IPs() []net.IP { | ||
if !s.IsIPAddress() { | ||
return nil | ||
} | ||
return []net.IP{s.ip} | ||
} | ||
|
||
func (s SupervisorIssuer) IssuerServerCert( | ||
t *testing.T, | ||
ca *certauthority.CA, | ||
) ([]byte, []byte) { | ||
t.Helper() | ||
|
||
t.Logf("issuing server cert for Supervisor: hostname=%+v, ips=%+v", | ||
s.Hostnames(), s.IPs()) | ||
|
||
cert, err := ca.IssueServerCert(s.Hostnames(), s.IPs(), 24*time.Hour) | ||
require.NoError(t, err) | ||
certPEM, keyPEM, err := certauthority.ToPEM(cert) | ||
require.NoError(t, err) | ||
return certPEM, keyPEM | ||
} | ||
|
||
func (s SupervisorIssuer) IsIPAddress() bool { | ||
return s.ip != nil | ||
} |
This file contains 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,82 @@ | ||
// Copyright 2024 the Pinniped contributors. All Rights Reserved. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package testlib | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
) | ||
|
||
func TestSupervisorIssuer(t *testing.T) { | ||
tests := []struct { | ||
name string | ||
issuer string | ||
wantHostname string | ||
wantAddress string | ||
wantIP net.IP | ||
wantIsIPAddress bool | ||
}{ | ||
{ | ||
name: "works for localhost", | ||
issuer: "https://localhost:443", | ||
wantHostname: "localhost", | ||
wantAddress: "localhost:443", | ||
}, | ||
{ | ||
name: "works for localhost with path", | ||
issuer: "https://localhost:443/some/path", | ||
wantHostname: "localhost", | ||
wantAddress: "localhost:443", | ||
}, | ||
{ | ||
name: "works for domain", | ||
issuer: "https://example.com:443", | ||
wantHostname: "example.com", | ||
wantAddress: "example.com:443", | ||
}, | ||
{ | ||
name: "works for domain with path", | ||
issuer: "https://example.com:443/some/path", | ||
wantHostname: "example.com", | ||
wantAddress: "example.com:443", | ||
}, | ||
{ | ||
name: "works for IPv4", | ||
issuer: "https://1.2.3.4:443", | ||
wantHostname: "", // don't want DNS records in the cert when using IP address | ||
wantAddress: "1.2.3.4:443", | ||
wantIP: net.ParseIP("1.2.3.4"), | ||
wantIsIPAddress: true, | ||
}, | ||
{ | ||
name: "works for IPv4 with path", | ||
issuer: "https://1.2.3.4:443/some/path", | ||
wantHostname: "", // don't want DNS records in the cert when using IP address | ||
wantAddress: "1.2.3.4:443", | ||
wantIP: net.ParseIP("1.2.3.4"), | ||
wantIsIPAddress: true, | ||
}, | ||
} | ||
|
||
for _, test := range tests { | ||
t.Run(test.name, func(t *testing.T) { | ||
supervisorIssuer := NewSupervisorIssuer(t, test.issuer) | ||
require.Equal(t, test.issuer, supervisorIssuer.Issuer()) | ||
require.Equal(t, test.wantAddress, supervisorIssuer.Address()) | ||
if test.wantHostname != "" { | ||
require.Equal(t, []string{test.wantHostname}, supervisorIssuer.Hostnames()) | ||
} else { | ||
require.Nil(t, supervisorIssuer.Hostnames()) | ||
} | ||
if test.wantIP != nil { | ||
require.Equal(t, []net.IP{test.wantIP}, supervisorIssuer.IPs()) | ||
} else { | ||
require.Nil(t, supervisorIssuer.IPs()) | ||
} | ||
require.Equal(t, test.wantIsIPAddress, supervisorIssuer.IsIPAddress()) | ||
}) | ||
} | ||
} |