Files

148 lines
3.8 KiB
Go

package cli
import (
"path/filepath"
"testing"
"uuid"
"github.com/effective-security/xpki/certutil"
"github.com/effective-security/xpki/internal/testenv"
"github.com/stretchr/testify/suite"
)
type csrSuite struct {
testSuite
}
// TestCsrSuite needs the local-kms emulator behind aws-dev-kms.json, the
// default provider on :14555 (XPKI-100); the extra provider from
// aws-dev-kms.yaml (:14556) is loaded lazily and never called. It skips
// when the emulator is unreachable, unless XPKI_INTEGRATION=required.
func TestCsrSuite(t *testing.T) {
testenv.RequireTCP(t, "local-kms", "localhost:14555")
s := new(csrSuite)
s.appFlags = []string{
"--cfg", "../../../cryptoprov/awskmscrypto/testdata/aws-dev-kms.json",
"--crypto", "../../../cryptoprov/awskmscrypto/testdata/aws-dev-kms.yaml",
}
suite.Run(t, s)
}
func (s *csrSuite) TestCreate() {
s.createRootCA()
label := "cert" + uuid.NewV7().String()
cmd := CsrCreateCmd{
CsrProfile: "../../../authority/testdata/csrprofiles/trusty_client.yaml",
KeyLabel: label,
}
err := cmd.Run(s.ctl)
s.Require().NoError(err)
cmd.Output = filepath.Join(s.tmpdir, label)
err = cmd.Run(s.ctl)
s.Require().NoError(err)
s.HasTextInFile(cmd.Output+".csr", "REQUEST")
s.HasTextInFile(cmd.Output+".key", "private")
}
func (s *csrSuite) TestGenCert() {
s.createRootCA()
cmd := GenCertCmd{
CACert: s.rootCert,
CAKey: s.rootKey,
CAConfig: "../../../authority/testdata/ca-config.bootstrap.yaml",
CsrProfile: "../../../authority/testdata/csrprofiles/trusty_server.yaml",
KeyLabel: "server" + uuid.NewV7().String(),
San: []string{"ekspand.com", "[email protected]", "10.1.1.12"},
Profile: "server",
}
// to stdout
err := cmd.Run(s.ctl)
s.Require().NoError(err)
// to file
cmd.Output = filepath.Join(s.tmpdir, cmd.KeyLabel)
err = cmd.Run(s.ctl)
s.Require().NoError(err)
s.Require().NoError(err)
s.HasTextInFile(cmd.Output+".pem", "CERTIFICATE")
s.HasTextInFile(cmd.Output+".key", "private")
}
func (s *csrSuite) TestSignCert() {
s.createRootCA()
createCSR := CsrCreateCmd{
CsrProfile: "../../../authority/testdata/csrprofiles/delegated_l1_ca.yaml",
KeyLabel: "*",
Output: filepath.Join(s.tmpdir, "server"+uuid.NewV7().String()),
}
err := createCSR.Run(s.ctl)
s.Require().NoError(err)
req := createCSR.Output + ".csr"
s.HasTextInFile(req, "REQUEST")
// to file
cmd := CsrSignCmd{
CACert: s.rootCert,
CAKey: s.rootKey,
CAConfig: "../../../authority/testdata/ca-config.dev.yaml",
Csr: req,
San: []string{"ekspand.com", "[email protected]", "10.1.1.12"},
Profile: "DELEGATED_L1_CA",
Output: createCSR.Output,
}
err = cmd.Run(s.ctl)
s.Require().NoError(err)
s.Require().NoError(err)
s.HasTextInFile(cmd.Output+".pem", "CERTIFICATE")
s.HasTextInFile(cmd.Output+".key", "private")
// to stdout
cmd.Output = ""
err = cmd.Run(s.ctl)
s.Require().NoError(err)
}
func (s *testSuite) createRootCA() {
if s.rootCert != "" {
return
}
label := "root" + uuid.NewV7().String()
output := filepath.Join(s.tmpdir, label)
cmd := GenCertCmd{
SelfSign: true,
CAConfig: "../../../authority/testdata/ca-config.bootstrap.yaml",
CsrProfile: "../../../authority/testdata/csrprofiles/root_ca.yaml",
KeyLabel: label,
Profile: "ROOT",
Output: output,
}
err := cmd.Run(s.ctl)
s.Require().NoError(err)
s.rootCert = output + ".pem"
s.rootKey = output + ".key"
s.HasTextInFile(s.rootCert, "CERTIFICATE")
s.HasTextInFile(s.rootKey, "private")
// subject names from csrprofiles/root_ca.yaml must be applied
crt, err := certutil.LoadFromPEM(s.rootCert)
s.Require().NoError(err)
s.Equal("[TEST] One Root CA", crt.Subject.CommonName)
s.Equal([]string{"US"}, crt.Subject.Country)
s.Equal([]string{"WA"}, crt.Subject.Locality)
s.Equal([]string{"trusty.com"}, crt.Subject.Organization)
}