|
|
@@ -9,8 +9,8 @@ import (
|
|
|
"crypto/rand"
|
|
|
"crypto/x509"
|
|
|
"net"
|
|
|
+ "net/netip"
|
|
|
"net/url"
|
|
|
- "reflect"
|
|
|
"strings"
|
|
|
"testing"
|
|
|
"time"
|
|
|
@@ -26,179 +26,144 @@ import (
|
|
|
"go.step.sm/crypto/x509util"
|
|
|
)
|
|
|
|
|
|
-func mustNebulaIPNet(t *testing.T, s string) *net.IPNet {
|
|
|
+func mustNebulaPrefix(t *testing.T, s string) netip.Prefix {
|
|
|
t.Helper()
|
|
|
- ip, ipNet, err := net.ParseCIDR(s)
|
|
|
+ p, err := netip.ParsePrefix(s)
|
|
|
require.NoError(t, err)
|
|
|
- if ip = ip.To4(); ip == nil {
|
|
|
- t.Fatalf("nebula only supports ipv4, have %s", s)
|
|
|
- }
|
|
|
- ipNet.IP = ip
|
|
|
- return ipNet
|
|
|
+ return p
|
|
|
}
|
|
|
|
|
|
-func mustNebulaCA(t *testing.T) (*cert.NebulaCertificate, ed25519.PrivateKey) {
|
|
|
+func mustNebulaCA(t *testing.T) (cert.Certificate, ed25519.PrivateKey) {
|
|
|
t.Helper()
|
|
|
pub, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
|
require.NoError(t, err)
|
|
|
- nc := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "TestCA",
|
|
|
- Groups: []string{"test"},
|
|
|
- Ips: []*net.IPNet{
|
|
|
- mustNebulaIPNet(t, "10.1.0.0/16"),
|
|
|
- },
|
|
|
- Subnets: []*net.IPNet{},
|
|
|
- NotBefore: time.Now(),
|
|
|
- NotAfter: time.Now().Add(10 * time.Minute),
|
|
|
- PublicKey: pub,
|
|
|
- IsCA: true,
|
|
|
- Curve: cert.Curve_CURVE25519,
|
|
|
- },
|
|
|
- }
|
|
|
- err = nc.Sign(cert.Curve_CURVE25519, priv)
|
|
|
+ now := time.Now()
|
|
|
+ tbs := &cert.TBSCertificate{
|
|
|
+ Version: cert.Version1,
|
|
|
+ Curve: cert.Curve_CURVE25519,
|
|
|
+ Name: "TestCA",
|
|
|
+ Groups: []string{"test"},
|
|
|
+ Networks: []netip.Prefix{netip.MustParsePrefix("10.1.0.0/16")},
|
|
|
+ NotBefore: time.Unix(now.Unix(), 0),
|
|
|
+ NotAfter: time.Unix(now.Add(10*time.Minute).Unix(), 0),
|
|
|
+ PublicKey: pub,
|
|
|
+ IsCA: true,
|
|
|
+ }
|
|
|
+ nc, err := tbs.Sign(nil, cert.Curve_CURVE25519, priv)
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
return nc, priv
|
|
|
}
|
|
|
|
|
|
-func mustExpiredNebulaCA(t *testing.T) (*cert.NebulaCertificate, ed25519.PrivateKey) {
|
|
|
+func mustExpiredNebulaCA(t *testing.T) (cert.Certificate, ed25519.PrivateKey) {
|
|
|
t.Helper()
|
|
|
pub, priv, err := ed25519.GenerateKey(rand.Reader)
|
|
|
require.NoError(t, err)
|
|
|
- nc := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "ExpiredTestCA",
|
|
|
- Groups: []string{"expired"},
|
|
|
- Ips: []*net.IPNet{
|
|
|
- mustNebulaIPNet(t, "10.2.0.0/16"),
|
|
|
- },
|
|
|
- Subnets: []*net.IPNet{},
|
|
|
- NotBefore: time.Now().Add(-2 * time.Hour),
|
|
|
- NotAfter: time.Now().Add(-1 * time.Hour),
|
|
|
- PublicKey: pub,
|
|
|
- IsCA: true,
|
|
|
- Curve: cert.Curve_CURVE25519,
|
|
|
- },
|
|
|
- }
|
|
|
- err = nc.Sign(cert.Curve_CURVE25519, priv)
|
|
|
+ now := time.Now()
|
|
|
+ tbs := &cert.TBSCertificate{
|
|
|
+ Version: cert.Version1,
|
|
|
+ Curve: cert.Curve_CURVE25519,
|
|
|
+ Name: "ExpiredTestCA",
|
|
|
+ Groups: []string{"expired"},
|
|
|
+ Networks: []netip.Prefix{netip.MustParsePrefix("10.2.0.0/16")},
|
|
|
+ NotBefore: time.Unix(now.Add(-2*time.Hour).Unix(), 0),
|
|
|
+ NotAfter: time.Unix(now.Add(-1*time.Hour).Unix(), 0),
|
|
|
+ PublicKey: pub,
|
|
|
+ IsCA: true,
|
|
|
+ }
|
|
|
+ nc, err := tbs.Sign(nil, cert.Curve_CURVE25519, priv)
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
return nc, priv
|
|
|
}
|
|
|
|
|
|
-func mustNebulaP256CA(t *testing.T) (*cert.NebulaCertificate, *ecdsa.PrivateKey) {
|
|
|
+func mustNebulaP256CA(t *testing.T) (cert.Certificate, *ecdsa.PrivateKey) {
|
|
|
t.Helper()
|
|
|
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
require.NoError(t, err)
|
|
|
- priv, err := key.ECDH()
|
|
|
+ ecdhPriv, err := key.ECDH()
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
- nc := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: "TestCA",
|
|
|
- Groups: []string{"test"},
|
|
|
- Ips: []*net.IPNet{
|
|
|
- mustNebulaIPNet(t, "10.1.0.0/16"),
|
|
|
- },
|
|
|
- Subnets: []*net.IPNet{},
|
|
|
- NotBefore: time.Now(),
|
|
|
- NotAfter: time.Now().Add(10 * time.Minute),
|
|
|
- PublicKey: priv.PublicKey().Bytes(),
|
|
|
- IsCA: true,
|
|
|
- Curve: cert.Curve_P256,
|
|
|
- },
|
|
|
- }
|
|
|
- err = nc.Sign(cert.Curve_P256, priv.Bytes())
|
|
|
+ now := time.Now()
|
|
|
+ tbs := &cert.TBSCertificate{
|
|
|
+ Version: cert.Version1,
|
|
|
+ Curve: cert.Curve_P256,
|
|
|
+ Name: "TestCA",
|
|
|
+ Groups: []string{"test"},
|
|
|
+ Networks: []netip.Prefix{netip.MustParsePrefix("10.1.0.0/16")},
|
|
|
+ NotBefore: time.Unix(now.Unix(), 0),
|
|
|
+ NotAfter: time.Unix(now.Add(10*time.Minute).Unix(), 0),
|
|
|
+ PublicKey: ecdhPriv.PublicKey().Bytes(),
|
|
|
+ IsCA: true,
|
|
|
+ }
|
|
|
+
|
|
|
+ // For P256 CAs, Sign expects the raw 32-byte scalar as the key.
|
|
|
+ nc, err := tbs.Sign(nil, cert.Curve_P256, key.D.FillBytes(make([]byte, 32)))
|
|
|
require.NoError(t, err)
|
|
|
return nc, key
|
|
|
}
|
|
|
|
|
|
-func mustNebulaCert(t *testing.T, name string, ipNet *net.IPNet, groups []string, ca *cert.NebulaCertificate, signer ed25519.PrivateKey) (*cert.NebulaCertificate, crypto.Signer) {
|
|
|
+func mustNebulaCert(t *testing.T, name string, network netip.Prefix, groups []string, ca cert.Certificate, signer ed25519.PrivateKey) (cert.Certificate, crypto.Signer) {
|
|
|
t.Helper()
|
|
|
|
|
|
pub, priv, err := x25519.GenerateKey(rand.Reader)
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
- issuer, err := ca.Sha256Sum()
|
|
|
- require.NoError(t, err)
|
|
|
-
|
|
|
- invertedGroups := make(map[string]struct{}, len(groups))
|
|
|
- for _, name := range groups {
|
|
|
- invertedGroups[name] = struct{}{}
|
|
|
- }
|
|
|
-
|
|
|
t1 := time.Now().Truncate(time.Second)
|
|
|
- nc := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: name,
|
|
|
- Ips: []*net.IPNet{ipNet},
|
|
|
- Subnets: []*net.IPNet{},
|
|
|
- Groups: groups,
|
|
|
- NotBefore: t1,
|
|
|
- NotAfter: t1.Add(5 * time.Minute),
|
|
|
- PublicKey: pub,
|
|
|
- IsCA: false,
|
|
|
- Issuer: issuer,
|
|
|
- InvertedGroups: invertedGroups,
|
|
|
- Curve: cert.Curve_CURVE25519,
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- err = nc.Sign(cert.Curve_CURVE25519, signer)
|
|
|
+ tbs := &cert.TBSCertificate{
|
|
|
+ Version: cert.Version1,
|
|
|
+ Curve: cert.Curve_CURVE25519,
|
|
|
+ Name: name,
|
|
|
+ Networks: []netip.Prefix{network},
|
|
|
+ Groups: groups,
|
|
|
+ NotBefore: t1,
|
|
|
+ NotAfter: t1.Add(5 * time.Minute),
|
|
|
+ PublicKey: pub,
|
|
|
+ IsCA: false,
|
|
|
+ }
|
|
|
+
|
|
|
+ nc, err := tbs.Sign(ca, cert.Curve_CURVE25519, signer)
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
return nc, priv
|
|
|
}
|
|
|
|
|
|
-func mustNebulaP256Cert(t *testing.T, name string, ipNet *net.IPNet, groups []string, ca *cert.NebulaCertificate, signer *ecdsa.PrivateKey) (*cert.NebulaCertificate, crypto.Signer) {
|
|
|
+func mustNebulaP256Cert(t *testing.T, name string, network netip.Prefix, groups []string, ca cert.Certificate, signer *ecdsa.PrivateKey) (cert.Certificate, crypto.Signer) {
|
|
|
t.Helper()
|
|
|
|
|
|
key, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
|
|
|
require.NoError(t, err)
|
|
|
- priv, err := key.ECDH()
|
|
|
+ ecdhPriv, err := key.ECDH()
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
- issuer, err := ca.Sha256Sum()
|
|
|
- require.NoError(t, err)
|
|
|
-
|
|
|
- invertedGroups := make(map[string]struct{}, len(groups))
|
|
|
- for _, name := range groups {
|
|
|
- invertedGroups[name] = struct{}{}
|
|
|
- }
|
|
|
-
|
|
|
t1 := time.Now().Truncate(time.Second)
|
|
|
- nc := &cert.NebulaCertificate{
|
|
|
- Details: cert.NebulaCertificateDetails{
|
|
|
- Name: name,
|
|
|
- Ips: []*net.IPNet{ipNet},
|
|
|
- Subnets: []*net.IPNet{},
|
|
|
- Groups: groups,
|
|
|
- NotBefore: t1,
|
|
|
- NotAfter: t1.Add(5 * time.Minute),
|
|
|
- PublicKey: priv.PublicKey().Bytes(),
|
|
|
- IsCA: false,
|
|
|
- Issuer: issuer,
|
|
|
- InvertedGroups: invertedGroups,
|
|
|
- Curve: cert.Curve_P256,
|
|
|
- },
|
|
|
- }
|
|
|
-
|
|
|
- ecdhKey, err := signer.ECDH()
|
|
|
+ tbs := &cert.TBSCertificate{
|
|
|
+ Version: cert.Version1,
|
|
|
+ Curve: cert.Curve_P256,
|
|
|
+ Name: name,
|
|
|
+ Networks: []netip.Prefix{network},
|
|
|
+ Groups: groups,
|
|
|
+ NotBefore: t1,
|
|
|
+ NotAfter: t1.Add(5 * time.Minute),
|
|
|
+ PublicKey: ecdhPriv.PublicKey().Bytes(),
|
|
|
+ IsCA: false,
|
|
|
+ }
|
|
|
+
|
|
|
+ ecdhSigner, err := signer.ECDH()
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
- err = nc.Sign(cert.Curve_P256, ecdhKey.Bytes())
|
|
|
+ nc, err := tbs.Sign(ca, cert.Curve_P256, ecdhSigner.Bytes())
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
return nc, key
|
|
|
}
|
|
|
|
|
|
-func mustNebulaProvisioner(t *testing.T) (*Nebula, *cert.NebulaCertificate, ed25519.PrivateKey) {
|
|
|
+func mustNebulaProvisioner(t *testing.T) (*Nebula, cert.Certificate, ed25519.PrivateKey) {
|
|
|
t.Helper()
|
|
|
|
|
|
nc, signer := mustNebulaCA(t)
|
|
|
- ncPem, err := nc.MarshalToPEM()
|
|
|
+ ncPem, err := nc.MarshalPEM()
|
|
|
require.NoError(t, err)
|
|
|
bTrue := true
|
|
|
p := &Nebula{
|
|
|
@@ -218,11 +183,11 @@ func mustNebulaProvisioner(t *testing.T) (*Nebula, *cert.NebulaCertificate, ed25
|
|
|
return p, nc, signer
|
|
|
}
|
|
|
|
|
|
-func mustNebulaP256Provisioner(t *testing.T) (*Nebula, *cert.NebulaCertificate, *ecdsa.PrivateKey) {
|
|
|
+func mustNebulaP256Provisioner(t *testing.T) (*Nebula, cert.Certificate, *ecdsa.PrivateKey) {
|
|
|
t.Helper()
|
|
|
|
|
|
nc, signer := mustNebulaP256CA(t)
|
|
|
- ncPem, err := nc.MarshalToPEM()
|
|
|
+ ncPem, err := nc.MarshalPEM()
|
|
|
require.NoError(t, err)
|
|
|
bTrue := true
|
|
|
p := &Nebula{
|
|
|
@@ -242,7 +207,7 @@ func mustNebulaP256Provisioner(t *testing.T) (*Nebula, *cert.NebulaCertificate,
|
|
|
return p, nc, signer
|
|
|
}
|
|
|
|
|
|
-func mustNebulaToken(t *testing.T, sub, iss, aud string, iat time.Time, sans []string, nc *cert.NebulaCertificate, key crypto.Signer, algorithm jose.SignatureAlgorithm) string {
|
|
|
+func mustNebulaToken(t *testing.T, sub, iss, aud string, iat time.Time, sans []string, nc cert.Certificate, key crypto.Signer, algorithm jose.SignatureAlgorithm) string {
|
|
|
t.Helper()
|
|
|
ncDer, err := nc.Marshal()
|
|
|
require.NoError(t, err)
|
|
|
@@ -278,7 +243,7 @@ func mustNebulaToken(t *testing.T, sub, iss, aud string, iat time.Time, sans []s
|
|
|
return tok
|
|
|
}
|
|
|
|
|
|
-func mustNebulaSSHToken(t *testing.T, sub, iss, aud string, iat time.Time, opts *SignSSHOptions, nc *cert.NebulaCertificate, key crypto.Signer, algorithm jose.SignatureAlgorithm) string {
|
|
|
+func mustNebulaSSHToken(t *testing.T, sub, iss, aud string, iat time.Time, opts *SignSSHOptions, nc cert.Certificate, key crypto.Signer, algorithm jose.SignatureAlgorithm) string {
|
|
|
t.Helper()
|
|
|
ncDer, err := nc.Marshal()
|
|
|
require.NoError(t, err)
|
|
|
@@ -321,10 +286,10 @@ func mustNebulaSSHToken(t *testing.T, sub, iss, aud string, iat time.Time, opts
|
|
|
|
|
|
func TestNebula_Init(t *testing.T) {
|
|
|
nc, _ := mustNebulaCA(t)
|
|
|
- ncPem, err := nc.MarshalToPEM()
|
|
|
+ ncPem, err := nc.MarshalPEM()
|
|
|
require.NoError(t, err)
|
|
|
expiredNC, _ := mustExpiredNebulaCA(t)
|
|
|
- expiredPEM, err := expiredNC.MarshalToPEM()
|
|
|
+ expiredPEM, err := expiredNC.MarshalPEM()
|
|
|
require.NoError(t, err)
|
|
|
expiredPEM = append(expiredPEM, ncPem...) // needed so that regular error isn't triggered
|
|
|
|
|
|
@@ -428,7 +393,7 @@ func TestNebula_GetIDForToken(t *testing.T) {
|
|
|
|
|
|
func TestNebula_GetTokenID(t *testing.T) {
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- c1, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"group"}, ca, signer)
|
|
|
+ c1, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
t1 := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], now(), []string{"test.lan", "10.1.0.1"}, c1, priv, jose.XEdDSA)
|
|
|
_, claims, err := parseToken(t1)
|
|
|
require.NoError(t, err)
|
|
|
@@ -540,7 +505,7 @@ func TestNebula_GetEncryptedKey(t *testing.T) {
|
|
|
func TestNebula_AuthorizeSign(t *testing.T) {
|
|
|
ctx := context.TODO()
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
ok := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], now(), []string{"test.lan", "10.1.0.1"}, crt, priv, jose.XEdDSA)
|
|
|
okNoSANs := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], now(), nil, crt, priv, jose.XEdDSA)
|
|
|
|
|
|
@@ -582,7 +547,7 @@ func TestNebula_AuthorizeSSHSign(t *testing.T) {
|
|
|
ctx := context.TODO()
|
|
|
// Ok provisioner
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
ok := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], now(), &SignSSHOptions{
|
|
|
CertType: "host",
|
|
|
KeyID: "test.lan",
|
|
|
@@ -691,12 +656,12 @@ func TestNebula_AuthorizeRevoke(t *testing.T) {
|
|
|
ctx := context.TODO()
|
|
|
// Ok provisioner
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
ok := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Revoke[0], now(), nil, crt, priv, jose.XEdDSA)
|
|
|
|
|
|
// Fail different CA
|
|
|
nc, signer := mustNebulaCA(t)
|
|
|
- crt, priv = mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, nc, signer)
|
|
|
+ crt, priv = mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, nc, signer)
|
|
|
failToken := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Revoke[0], now(), nil, crt, priv, jose.XEdDSA)
|
|
|
|
|
|
type args struct {
|
|
|
@@ -725,12 +690,12 @@ func TestNebula_AuthorizeSSHRevoke(t *testing.T) {
|
|
|
ctx := context.TODO()
|
|
|
// Ok provisioner
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
ok := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRevoke[0], now(), nil, crt, priv, jose.XEdDSA)
|
|
|
|
|
|
// Fail different CA
|
|
|
nc, signer := mustNebulaCA(t)
|
|
|
- crt, priv = mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, nc, signer)
|
|
|
+ crt, priv = mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, nc, signer)
|
|
|
failToken := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRevoke[0], now(), nil, crt, priv, jose.XEdDSA)
|
|
|
|
|
|
// Provisioner with SSH disabled
|
|
|
@@ -764,7 +729,7 @@ func TestNebula_AuthorizeSSHRevoke(t *testing.T) {
|
|
|
|
|
|
func TestNebula_AuthorizeSSHRenew(t *testing.T) {
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
t1 := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRenew[0], now(), nil, crt, priv, jose.XEdDSA)
|
|
|
|
|
|
type args struct {
|
|
|
@@ -787,16 +752,14 @@ func TestNebula_AuthorizeSSHRenew(t *testing.T) {
|
|
|
t.Errorf("Nebula.AuthorizeSSHRenew() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
return
|
|
|
}
|
|
|
- if !reflect.DeepEqual(got, tt.want) {
|
|
|
- t.Errorf("Nebula.AuthorizeSSHRenew() = %v, want %v", got, tt.want)
|
|
|
- }
|
|
|
+ assert.Equal(t, tt.want, got)
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func TestNebula_AuthorizeSSHRekey(t *testing.T) {
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
t1 := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHRekey[0], now(), nil, crt, priv, jose.XEdDSA)
|
|
|
|
|
|
type args struct {
|
|
|
@@ -820,12 +783,8 @@ func TestNebula_AuthorizeSSHRekey(t *testing.T) {
|
|
|
t.Errorf("Nebula.AuthorizeSSHRekey() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
return
|
|
|
}
|
|
|
- if !reflect.DeepEqual(got, tt.want) {
|
|
|
- t.Errorf("Nebula.AuthorizeSSHRekey() got = %v, want %v", got, tt.want)
|
|
|
- }
|
|
|
- if !reflect.DeepEqual(got1, tt.want1) {
|
|
|
- t.Errorf("Nebula.AuthorizeSSHRekey() got1 = %v, want %v", got1, tt.want1)
|
|
|
- }
|
|
|
+ assert.Equal(t, tt.want, got)
|
|
|
+ assert.Equal(t, tt.want1, got1)
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -833,7 +792,7 @@ func TestNebula_AuthorizeSSHRekey(t *testing.T) {
|
|
|
func TestNebula_authorizeToken(t *testing.T) {
|
|
|
t1 := now()
|
|
|
p, ca, signer := mustNebulaProvisioner(t)
|
|
|
- crt, priv := mustNebulaCert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaCert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
ok := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], t1, []string{"10.1.0.1"}, crt, priv, jose.XEdDSA)
|
|
|
okNoSANs := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], t1, nil, crt, priv, jose.XEdDSA)
|
|
|
okSSH := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], t1, &SignSSHOptions{
|
|
|
@@ -882,21 +841,20 @@ func TestNebula_authorizeToken(t *testing.T) {
|
|
|
audiences []string
|
|
|
}
|
|
|
tests := []struct {
|
|
|
- name string
|
|
|
- p *Nebula
|
|
|
- args args
|
|
|
- want *cert.NebulaCertificate
|
|
|
- want1 *jwtPayload
|
|
|
- wantErr bool
|
|
|
+ name string
|
|
|
+ p *Nebula
|
|
|
+ args args
|
|
|
+ wantClaims *jwtPayload
|
|
|
+ wantErr bool
|
|
|
}{
|
|
|
- {"ok x509", p, args{ok, p.ctl.Audiences.Sign}, crt, &jwtPayload{
|
|
|
+ {"ok x509", p, args{ok, p.ctl.Audiences.Sign}, &jwtPayload{
|
|
|
Claims: x509Claims,
|
|
|
SANs: []string{"10.1.0.1"},
|
|
|
}, false},
|
|
|
- {"ok x509 no sans", p, args{okNoSANs, p.ctl.Audiences.Sign}, crt, &jwtPayload{
|
|
|
+ {"ok x509 no sans", p, args{okNoSANs, p.ctl.Audiences.Sign}, &jwtPayload{
|
|
|
Claims: x509Claims,
|
|
|
}, false},
|
|
|
- {"ok ssh", p, args{okSSH, p.ctl.Audiences.SSHSign}, crt, &jwtPayload{
|
|
|
+ {"ok ssh", p, args{okSSH, p.ctl.Audiences.SSHSign}, &jwtPayload{
|
|
|
Claims: sshClaims,
|
|
|
Step: &stepPayload{
|
|
|
SSH: &SignSSHOptions{
|
|
|
@@ -906,16 +864,16 @@ func TestNebula_authorizeToken(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
}, false},
|
|
|
- {"ok ssh no principals", p, args{okSSHNoOptions, p.ctl.Audiences.SSHSign}, crt, &jwtPayload{
|
|
|
+ {"ok ssh no principals", p, args{okSSHNoOptions, p.ctl.Audiences.SSHSign}, &jwtPayload{
|
|
|
Claims: sshClaims,
|
|
|
}, false},
|
|
|
- {"fail parse", p, args{"bad.token", p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail header", p, args{simpleToken, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail verify", p2, args{ok, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims nbf", p, args{failNotBefore, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims iss", p, args{failIssuer, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims aud", p, args{failAudience, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims sub", p, args{failSubject, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
+ {"fail parse", p, args{"bad.token", p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail header", p, args{simpleToken, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail verify", p2, args{ok, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims nbf", p, args{failNotBefore, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims iss", p, args{failIssuer, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims aud", p, args{failAudience, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims sub", p, args{failSubject, p.ctl.Audiences.Sign}, nil, true},
|
|
|
}
|
|
|
for _, tt := range tests {
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
@@ -927,13 +885,13 @@ func TestNebula_authorizeToken(t *testing.T) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if got1 != nil && tt.want1 != nil {
|
|
|
- tt.want1.ID = got1.ID
|
|
|
+ if got1 != nil && tt.wantClaims != nil {
|
|
|
+ tt.wantClaims.ID = got1.ID
|
|
|
}
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
- assert.Equal(t, tt.want, got)
|
|
|
- assert.Equal(t, tt.want1, got1)
|
|
|
+ assert.NotNil(t, got)
|
|
|
+ assert.Equal(t, tt.wantClaims, got1)
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
@@ -941,7 +899,7 @@ func TestNebula_authorizeToken(t *testing.T) {
|
|
|
func TestNebula_authorizeToken_P256(t *testing.T) {
|
|
|
t1 := now()
|
|
|
p, ca, signer := mustNebulaP256Provisioner(t)
|
|
|
- crt, priv := mustNebulaP256Cert(t, "test.lan", mustNebulaIPNet(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
+ crt, priv := mustNebulaP256Cert(t, "test.lan", mustNebulaPrefix(t, "10.1.0.1/16"), []string{"test"}, ca, signer)
|
|
|
ok := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], t1, []string{"10.1.0.1"}, crt, priv, jose.ES256)
|
|
|
okNoSANs := mustNebulaToken(t, "test.lan", p.Name, p.ctl.Audiences.Sign[0], t1, nil, crt, priv, jose.ES256)
|
|
|
okSSH := mustNebulaSSHToken(t, "test.lan", p.Name, p.ctl.Audiences.SSHSign[0], t1, &SignSSHOptions{
|
|
|
@@ -990,21 +948,20 @@ func TestNebula_authorizeToken_P256(t *testing.T) {
|
|
|
audiences []string
|
|
|
}
|
|
|
tests := []struct {
|
|
|
- name string
|
|
|
- p *Nebula
|
|
|
- args args
|
|
|
- want *cert.NebulaCertificate
|
|
|
- want1 *jwtPayload
|
|
|
- wantErr bool
|
|
|
+ name string
|
|
|
+ p *Nebula
|
|
|
+ args args
|
|
|
+ wantClaims *jwtPayload
|
|
|
+ wantErr bool
|
|
|
}{
|
|
|
- {"ok x509", p, args{ok, p.ctl.Audiences.Sign}, crt, &jwtPayload{
|
|
|
+ {"ok x509", p, args{ok, p.ctl.Audiences.Sign}, &jwtPayload{
|
|
|
Claims: x509Claims,
|
|
|
SANs: []string{"10.1.0.1"},
|
|
|
}, false},
|
|
|
- {"ok x509 no sans", p, args{okNoSANs, p.ctl.Audiences.Sign}, crt, &jwtPayload{
|
|
|
+ {"ok x509 no sans", p, args{okNoSANs, p.ctl.Audiences.Sign}, &jwtPayload{
|
|
|
Claims: x509Claims,
|
|
|
}, false},
|
|
|
- {"ok ssh", p, args{okSSH, p.ctl.Audiences.SSHSign}, crt, &jwtPayload{
|
|
|
+ {"ok ssh", p, args{okSSH, p.ctl.Audiences.SSHSign}, &jwtPayload{
|
|
|
Claims: sshClaims,
|
|
|
Step: &stepPayload{
|
|
|
SSH: &SignSSHOptions{
|
|
|
@@ -1014,16 +971,16 @@ func TestNebula_authorizeToken_P256(t *testing.T) {
|
|
|
},
|
|
|
},
|
|
|
}, false},
|
|
|
- {"ok ssh no principals", p, args{okSSHNoOptions, p.ctl.Audiences.SSHSign}, crt, &jwtPayload{
|
|
|
+ {"ok ssh no principals", p, args{okSSHNoOptions, p.ctl.Audiences.SSHSign}, &jwtPayload{
|
|
|
Claims: sshClaims,
|
|
|
}, false},
|
|
|
- {"fail parse", p, args{"bad.token", p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail header", p, args{simpleToken, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail verify", p2, args{ok, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims nbf", p, args{failNotBefore, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims iss", p, args{failIssuer, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims aud", p, args{failAudience, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
- {"fail claims sub", p, args{failSubject, p.ctl.Audiences.Sign}, nil, nil, true},
|
|
|
+ {"fail parse", p, args{"bad.token", p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail header", p, args{simpleToken, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail verify", p2, args{ok, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims nbf", p, args{failNotBefore, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims iss", p, args{failIssuer, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims aud", p, args{failAudience, p.ctl.Audiences.Sign}, nil, true},
|
|
|
+ {"fail claims sub", p, args{failSubject, p.ctl.Audiences.Sign}, nil, true},
|
|
|
}
|
|
|
for _, tt := range tests {
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
@@ -1035,22 +992,22 @@ func TestNebula_authorizeToken_P256(t *testing.T) {
|
|
|
return
|
|
|
}
|
|
|
|
|
|
- if got1 != nil && tt.want1 != nil {
|
|
|
- tt.want1.ID = got1.ID
|
|
|
+ if got1 != nil && tt.wantClaims != nil {
|
|
|
+ tt.wantClaims.ID = got1.ID
|
|
|
}
|
|
|
|
|
|
assert.NoError(t, err)
|
|
|
- assert.Equal(t, tt.want, got)
|
|
|
- assert.Equal(t, tt.want1, got1)
|
|
|
+ assert.NotNil(t, got)
|
|
|
+ assert.Equal(t, tt.wantClaims, got1)
|
|
|
})
|
|
|
}
|
|
|
}
|
|
|
|
|
|
func Test_nebulaSANsValidator_Valid(t *testing.T) {
|
|
|
- ipNet := mustNebulaIPNet(t, "10.1.2.3/16")
|
|
|
+ prefix := mustNebulaPrefix(t, "10.1.2.3/16")
|
|
|
type fields struct {
|
|
|
- Name string
|
|
|
- IPs []*net.IPNet
|
|
|
+ Name string
|
|
|
+ Networks []netip.Prefix
|
|
|
}
|
|
|
type args struct {
|
|
|
req *x509.CertificateRequest
|
|
|
@@ -1061,47 +1018,47 @@ func Test_nebulaSANsValidator_Valid(t *testing.T) {
|
|
|
args args
|
|
|
wantErr bool
|
|
|
}{
|
|
|
- {"ok", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"ok", fields{"dns.name", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
DNSNames: []string{"dns.name"},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
|
|
|
}}, false},
|
|
|
- {"ok name only", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"ok name only", fields{"dns.name", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
DNSNames: []string{"dns.name"},
|
|
|
}}, false},
|
|
|
- {"ok ip only", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"ok ip only", fields{"dns.name", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
|
|
|
}}, false},
|
|
|
- {"ok email name", fields{"jane@doe.org", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"ok email name", fields{"jane@doe.org", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
EmailAddresses: []string{"jane@doe.org"},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
|
|
|
}}, false},
|
|
|
- {"ok uri name", fields{"urn:foobar", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"ok uri name", fields{"urn:foobar", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
URIs: []*url.URL{{Scheme: "urn", Opaque: "foobar"}},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
|
|
|
}}, false},
|
|
|
- {"ok ip name", fields{"127.0.0.1", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"ok ip name", fields{"127.0.0.1", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
IPAddresses: []net.IP{net.IPv4(127, 0, 0, 1), net.IPv4(10, 1, 2, 3)},
|
|
|
}}, false},
|
|
|
- {"ok multiple ips", fields{"dns.name", []*net.IPNet{ipNet, mustNebulaIPNet(t, "10.2.2.3/8")}}, args{&x509.CertificateRequest{
|
|
|
+ {"ok multiple ips", fields{"dns.name", []netip.Prefix{prefix, mustNebulaPrefix(t, "10.2.2.3/8")}}, args{&x509.CertificateRequest{
|
|
|
DNSNames: []string{"dns.name"},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3), net.IPv4(10, 2, 2, 3)},
|
|
|
}}, false},
|
|
|
- {"fail dns", fields{"fail.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"fail dns", fields{"fail.name", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
DNSNames: []string{"dns.name"},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
|
|
|
}}, true},
|
|
|
- {"fail email", fields{"fail@doe.org", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"fail email", fields{"fail@doe.org", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
EmailAddresses: []string{"jane@doe.org"},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
|
|
|
}}, true},
|
|
|
- {"fail uri", fields{"urn:barfoo", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"fail uri", fields{"urn:barfoo", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
URIs: []*url.URL{{Scheme: "urn", Opaque: "foobar"}},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 3)},
|
|
|
}}, true},
|
|
|
- {"fail ip", fields{"127.0.0.1", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"fail ip", fields{"127.0.0.1", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 1, 2, 1), net.IPv4(10, 1, 2, 3)},
|
|
|
}}, true},
|
|
|
- {"fail nebula ip", fields{"dns.name", []*net.IPNet{ipNet}}, args{&x509.CertificateRequest{
|
|
|
+ {"fail nebula ip", fields{"dns.name", []netip.Prefix{prefix}}, args{&x509.CertificateRequest{
|
|
|
DNSNames: []string{"dns.name"},
|
|
|
IPAddresses: []net.IP{net.IPv4(10, 2, 2, 3)},
|
|
|
}}, true},
|
|
|
@@ -1109,8 +1066,8 @@ func Test_nebulaSANsValidator_Valid(t *testing.T) {
|
|
|
for _, tt := range tests {
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
v := nebulaSANsValidator{
|
|
|
- Name: tt.fields.Name,
|
|
|
- IPs: tt.fields.IPs,
|
|
|
+ Name: tt.fields.Name,
|
|
|
+ Networks: tt.fields.Networks,
|
|
|
}
|
|
|
if err := v.Valid(tt.args.req); (err != nil) != tt.wantErr {
|
|
|
t.Errorf("nebulaSANsValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
|
|
|
@@ -1120,11 +1077,11 @@ func Test_nebulaSANsValidator_Valid(t *testing.T) {
|
|
|
}
|
|
|
|
|
|
func Test_nebulaPrincipalsValidator_Valid(t *testing.T) {
|
|
|
- ipNet := mustNebulaIPNet(t, "10.1.2.3/16")
|
|
|
+ prefix := mustNebulaPrefix(t, "10.1.2.3/16")
|
|
|
|
|
|
type fields struct {
|
|
|
- Name string
|
|
|
- IPs []*net.IPNet
|
|
|
+ Name string
|
|
|
+ Networks []netip.Prefix
|
|
|
}
|
|
|
type args struct {
|
|
|
got SignSSHOptions
|
|
|
@@ -1135,27 +1092,27 @@ func Test_nebulaPrincipalsValidator_Valid(t *testing.T) {
|
|
|
args args
|
|
|
wantErr bool
|
|
|
}{
|
|
|
- {"ok", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
|
|
|
+ {"ok", fields{"dns.name", []netip.Prefix{prefix}}, args{SignSSHOptions{
|
|
|
Principals: []string{"dns.name", "10.1.2.3"},
|
|
|
}}, false},
|
|
|
- {"ok name", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
|
|
|
+ {"ok name", fields{"dns.name", []netip.Prefix{prefix}}, args{SignSSHOptions{
|
|
|
Principals: []string{"dns.name"},
|
|
|
}}, false},
|
|
|
- {"ok ip", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
|
|
|
+ {"ok ip", fields{"dns.name", []netip.Prefix{prefix}}, args{SignSSHOptions{
|
|
|
Principals: []string{"10.1.2.3"},
|
|
|
}}, false},
|
|
|
- {"fail name", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
|
|
|
+ {"fail name", fields{"dns.name", []netip.Prefix{prefix}}, args{SignSSHOptions{
|
|
|
Principals: []string{"foo.name", "10.1.2.3"},
|
|
|
}}, true},
|
|
|
- {"fail ip", fields{"dns.name", []*net.IPNet{ipNet}}, args{SignSSHOptions{
|
|
|
+ {"fail ip", fields{"dns.name", []netip.Prefix{prefix}}, args{SignSSHOptions{
|
|
|
Principals: []string{"dns.name", "10.2.2.3"},
|
|
|
}}, true},
|
|
|
}
|
|
|
for _, tt := range tests {
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
v := nebulaPrincipalsValidator{
|
|
|
- Name: tt.fields.Name,
|
|
|
- IPs: tt.fields.IPs,
|
|
|
+ Name: tt.fields.Name,
|
|
|
+ Networks: tt.fields.Networks,
|
|
|
}
|
|
|
if err := v.Valid(tt.args.got); (err != nil) != tt.wantErr {
|
|
|
t.Errorf("nebulaPrincipalsValidator.Valid() error = %v, wantErr %v", err, tt.wantErr)
|