diff --git a/pkg/certificates/cli.go b/pkg/certificates/cli.go index 4e86b9e34..49dd16c06 100644 --- a/pkg/certificates/cli.go +++ b/pkg/certificates/cli.go @@ -14,6 +14,7 @@ import ( "github.com/ghjm/cmdline" ) +// InitCA Initialize Certificate Authority. func InitCA(opts *CertOptions, certOut, keyOut string) error { ca, err := CreateCA(opts, &RsaWrapper{}) if err == nil { @@ -56,6 +57,7 @@ func (ica initCA) Run() (err error) { return InitCA(opts, ica.OutCert, ica.OutKey) } +// MakeReq Create Certificate Request. func MakeReq(opts *CertOptions, keyIn, keyOut, reqOut string) error { var req *x509.CertificateRequest var key *rsa.PrivateKey @@ -151,6 +153,7 @@ func (mr makeReq) Run() error { return MakeReq(opts, mr.InKey, mr.OutKey, mr.OutReq) } +// SignReq Sign Certificate Request. func SignReq(opts *CertOptions, caCrtPath, caKeyPath, reqPath, certOut string, verify bool) error { ca := &CA{} var err error diff --git a/pkg/certificates/cli_test.go b/pkg/certificates/cli_test.go index 71d51f240..663873e74 100644 --- a/pkg/certificates/cli_test.go +++ b/pkg/certificates/cli_test.go @@ -24,11 +24,11 @@ func TestInitCA(t *testing.T) { name: "Positive test", args: args{ opts: &certificates.CertOptions{ - CommonName: "Ansible Automation Controller Nodes Mesh", Bits: 8192, + CommonName: "Ansible Automation Controller Nodes Mesh", }, - certOut: "receptor_cert.pem", - keyOut: "receptor_key.pem", + certOut: "/tmp/receptor_cert.pem", + keyOut: "/tmp/receptor_key.pem", }, wantErr: false, }, @@ -39,8 +39,8 @@ func TestInitCA(t *testing.T) { Bits: -1, CommonName: "Ansible Automation Controller Nodes Mesh", }, - certOut: "receptor_cert.pem", - keyOut: "receptor_key.pem", + certOut: "/tmp/receptor_cert.pem", + keyOut: "/tmp/receptor_key.pem", }, wantErr: true, }, @@ -54,3 +54,51 @@ func TestInitCA(t *testing.T) { }) } } + +func TestMakeReq(t *testing.T) { + type args struct { + opts *certificates.CertOptions + keyIn string + keyOut string + reqOut string + } + tests := []struct { + name string + args args + wantErr bool + }{ + { + name: "Positive test", + args: args{ + opts: &certificates.CertOptions{ + Bits: 8192, + CommonName: "Ansible Automation Controller Nodes Mesh", + }, + keyIn: "/tmp/receptor_key.pem", + keyOut: "/tmp/receptor_key_out.pem", + reqOut: "/tmp/receptor_request_out.pem", + }, + wantErr: false, + }, + { + name: "Negative test", + args: args{ + opts: &certificates.CertOptions{ + Bits: -1, + CommonName: "Ansible Automation Controller Nodes Mesh", + }, + keyIn: "/tmp/", + keyOut: "/tmp/receptor_key_out.pem", + reqOut: "/tmp/receptor_request_out.pem", + }, + wantErr: true, + }, + } + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + if err := certificates.MakeReq(tt.args.opts, tt.args.keyIn, tt.args.keyOut, tt.args.reqOut); (err != nil) != tt.wantErr { + t.Errorf("MakeReq() error = %v, wantErr %v", err, tt.wantErr) + } + }) + } +}