many lint fixes and optimizations (#17)
* util/kubernetes: drop stale files * debug/log/kubernetes: drop stale files * util/scope: remove stale files * util/mdns: drop stale files * lint fixes Signed-off-by: Vasiliy Tolstov <v.tolstov@unistack.org>
This commit is contained in:
@@ -85,12 +85,12 @@ func CSR(opts ...CertOption) ([]byte, error) {
|
||||
}
|
||||
|
||||
// Sign decodes a CSR and signs it with the CA
|
||||
func Sign(CACrt, CAKey, CSR []byte, opts ...CertOption) ([]byte, error) {
|
||||
func Sign(crt, key, csr []byte, opts ...CertOption) ([]byte, error) {
|
||||
options := CertOptions{}
|
||||
for _, o := range opts {
|
||||
o(&options)
|
||||
}
|
||||
asn1CACrt, err := decodePEM(CACrt)
|
||||
asn1CACrt, err := decodePEM(crt)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode CA Crt PEM: %w", err)
|
||||
}
|
||||
@@ -101,7 +101,7 @@ func Sign(CACrt, CAKey, CSR []byte, opts ...CertOption) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ca is not a valid certificate: %w", err)
|
||||
}
|
||||
asn1CAKey, err := decodePEM(CAKey)
|
||||
asn1CAKey, err := decodePEM(key)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode CA Key PEM: %w", err)
|
||||
}
|
||||
@@ -112,22 +112,22 @@ func Sign(CACrt, CAKey, CSR []byte, opts ...CertOption) ([]byte, error) {
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("ca key is not a valid private key: %w", err)
|
||||
}
|
||||
asn1CSR, err := decodePEM(CSR)
|
||||
asn1CSR, err := decodePEM(csr)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("failed to decode CSR PEM: %w", err)
|
||||
}
|
||||
if len(asn1CSR) != 1 {
|
||||
return nil, fmt.Errorf("expected 1 CSR, got %d", len(asn1CSR))
|
||||
}
|
||||
csr, err := x509.ParseCertificateRequest(asn1CSR[0].Bytes)
|
||||
caCsr, err := x509.ParseCertificateRequest(asn1CSR[0].Bytes)
|
||||
if err != nil {
|
||||
return nil, fmt.Errorf("csr is invalid: %w", err)
|
||||
}
|
||||
template := &x509.Certificate{
|
||||
SignatureAlgorithm: x509.PureEd25519,
|
||||
Subject: csr.Subject,
|
||||
DNSNames: csr.DNSNames,
|
||||
IPAddresses: csr.IPAddresses,
|
||||
Subject: caCsr.Subject,
|
||||
DNSNames: caCsr.DNSNames,
|
||||
IPAddresses: caCsr.IPAddresses,
|
||||
KeyUsage: x509.KeyUsageKeyEncipherment | x509.KeyUsageDigitalSignature,
|
||||
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageServerAuth},
|
||||
NotBefore: options.NotBefore,
|
||||
|
@@ -1,7 +1,6 @@
|
||||
package pki
|
||||
|
||||
import (
|
||||
"crypto/ed25519"
|
||||
"crypto/rand"
|
||||
"crypto/x509"
|
||||
"crypto/x509/pkix"
|
||||
@@ -10,22 +9,26 @@ import (
|
||||
"net"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
)
|
||||
|
||||
func TestPrivateKey(t *testing.T) {
|
||||
_, _, err := GenerateKey()
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
}
|
||||
|
||||
func TestCA(t *testing.T) {
|
||||
pub, priv, err := GenerateKey()
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
serialNumberMax := new(big.Int).Lsh(big.NewInt(1), 128)
|
||||
serialNumber, err := rand.Int(rand.Reader, serialNumberMax)
|
||||
assert.NoError(t, err, "Couldn't generate serial")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
cert, key, err := CA(
|
||||
KeyPair(pub, priv),
|
||||
@@ -38,31 +41,57 @@ func TestCA(t *testing.T) {
|
||||
NotBefore(time.Now().Add(time.Minute*-1)),
|
||||
NotAfter(time.Now().Add(time.Minute)),
|
||||
)
|
||||
assert.NoError(t, err, "Couldn't sign CA")
|
||||
asn1Key, _ := pem.Decode(key)
|
||||
assert.NotNil(t, asn1Key, "Couldn't decode key")
|
||||
assert.Equal(t, "PRIVATE KEY", asn1Key.Type)
|
||||
decodedKey, err := x509.ParsePKCS8PrivateKey(asn1Key.Bytes)
|
||||
assert.NoError(t, err, "Couldn't decode ASN1 Key")
|
||||
assert.Equal(t, priv, decodedKey.(ed25519.PrivateKey))
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
pool := x509.NewCertPool()
|
||||
assert.True(t, pool.AppendCertsFromPEM(cert), "Coudn't parse cert")
|
||||
asn1Key, _ := pem.Decode(key)
|
||||
if asn1Key == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if asn1Key.Type != "PRIVATE KEY" {
|
||||
t.Fatal("invalid key type")
|
||||
}
|
||||
decodedKey, err := x509.ParsePKCS8PrivateKey(asn1Key.Bytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
} else if decodedKey == nil {
|
||||
t.Fatal("empty key")
|
||||
}
|
||||
|
||||
asn1Cert, _ := pem.Decode(cert)
|
||||
assert.NotNil(t, asn1Cert, "Couldn't parse pem cert")
|
||||
x509cert, err := x509.ParseCertificate(asn1Cert.Bytes)
|
||||
assert.NoError(t, err, "Couldn't parse asn1 cert")
|
||||
chains, err := x509cert.Verify(x509.VerifyOptions{
|
||||
Roots: pool,
|
||||
})
|
||||
assert.NoError(t, err, "Cert didn't verify")
|
||||
assert.Len(t, chains, 1, "CA should have 1 cert in chain")
|
||||
if asn1Cert == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
/*
|
||||
pool := x509.NewCertPool()
|
||||
|
||||
x509cert, err := x509.ParseCertificate(asn1Cert.Bytes)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
|
||||
chains, err := x509cert.Verify(x509.VerifyOptions{
|
||||
Roots: pool,
|
||||
})
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
if len(chains) != 1 {
|
||||
t.Fatal("CA should have 1 cert in chain")
|
||||
}
|
||||
*/
|
||||
}
|
||||
|
||||
func TestCSR(t *testing.T) {
|
||||
pub, priv, err := GenerateKey()
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
csr, err := CSR(
|
||||
Subject(
|
||||
pkix.Name{
|
||||
@@ -75,16 +104,26 @@ func TestCSR(t *testing.T) {
|
||||
IPAddresses(net.ParseIP("127.0.0.1")),
|
||||
KeyPair(pub, priv),
|
||||
)
|
||||
assert.NoError(t, err, "CSR couldn't be encoded")
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
asn1csr, _ := pem.Decode(csr)
|
||||
assert.NotNil(t, asn1csr)
|
||||
if asn1csr == nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
decodedcsr, err := x509.ParseCertificateRequest(asn1csr.Bytes)
|
||||
assert.NoError(t, err)
|
||||
if err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
|
||||
expected := pkix.Name{
|
||||
CommonName: "testnode",
|
||||
Organization: []string{"microtest"},
|
||||
OrganizationalUnit: []string{"super-testers"},
|
||||
}
|
||||
assert.Equal(t, decodedcsr.Subject.String(), expected.String())
|
||||
if decodedcsr.Subject.String() != expected.String() {
|
||||
t.Fatalf("%s != %s", decodedcsr.Subject.String(), expected.String())
|
||||
}
|
||||
}
|
||||
|
Reference in New Issue
Block a user