-
Notifications
You must be signed in to change notification settings - Fork 82
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
dab86f4
commit 94cf499
Showing
7 changed files
with
365 additions
and
6 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,6 @@ | ||
[allowlist] | ||
description = "Global Allowlist" | ||
paths = [ | ||
'''pkg/certificates/ca_test.go''', | ||
'''pkg/netceptor/tlsconfig_test.go''', | ||
] |
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,286 @@ | ||
//go:build !no_cert_auth | ||
// +build !no_cert_auth | ||
|
||
package certificates_test | ||
|
||
import ( | ||
"crypto/rsa" | ||
"crypto/x509" | ||
"encoding/pem" | ||
"fmt" | ||
"io" | ||
"reflect" | ||
"testing" | ||
"time" | ||
|
||
"github.com/ansible/receptor/pkg/certificates" | ||
"github.com/ansible/receptor/pkg/certificates/mock_certificates" | ||
"go.uber.org/mock/gomock" | ||
) | ||
|
||
func TestCreateCAValid(t *testing.T) { | ||
type args struct { | ||
opts *certificates.CertOptions | ||
} | ||
goodCaBlock, _ := pem.Decode([]byte(` | ||
-----BEGIN CERTIFICATE----- | ||
MIIFVTCCAz2gAwIBAgIEYdeDaTANBgkqhkiG9w0BAQsFADA7MTkwNwYDVQQDEzBB | ||
bnNpYmxlIEF1dG9tYXRpb24gQ29udHJvb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7/NfMA0GCSqGSIb3DQEBCwUAA4ICAQCWCP/O6YQ9 | ||
jhae2/BeUeeKsnoxf90prg3o6/QHbelF6yL+MvGg5ZbPSlt+ywLDNR2CYvXk/4SD | ||
5To7CPKhSPBwwUJpafvQfAOZijU30fvXvp5yZEFoOzOyvBP58NfzL5qH6Pf5A6i3 | ||
rHvtR1v7DgS7u2qWWcSimIM0UPoV3JubLTEORjOR6FIyNkIxdjhrP3SxyZ54xxde | ||
G3bchKaRcGVNoFYSDN4bAA22JAjlD8kXNYKzIS/0cOR/9SnHd1wMIQ2trx0+TfyG | ||
FAA1mW1mjzQd+h5SGBVeCz2W2XttNSIfQDndJCsyACxmIaOK99AQxdhZsWfHtGO1 | ||
3TjnyoiHjf8rozJbAVYqrIdB6GDf6fUlxwhUXT0qkgOvvAzjNnLoOBUkE4TWqXHl | ||
38a+ITDNVzaUlrTd63eexS69V6kHe7mrqjywNQ9EXF9kaVeoNTzRf/ztT/DEVAl+ | ||
rKshMt4IOKQf1ScE+EJe1njpREHV+fa+kYvQB6cRuxW9a8sOSeQNaSL73Zv54elZ | ||
xffYhMv6yXvVxVnJHEsG3kM/CsvsU364BBd9kDcZbHpjNcDHMu+XxECJjD2atVtu | ||
FdaOLykGKfMCYVBP+xs97IJO8En/5N9QQwc+N4cfCg9/BWoZKHPbRx/V+57VEj0m | ||
69EpJXbL15ZQLCPsaIcqJqpK23VyJKc8fA== | ||
-----END CERTIFICATE----- | ||
`)) | ||
|
||
goodCaCertificate, err := x509.ParseCertificate(goodCaBlock.Bytes) | ||
if err != nil { | ||
t.Errorf("Error parsing certificate: %v", err) | ||
} | ||
|
||
goodCaTimeAfterString := "2032-01-07T00:03:51Z" | ||
goodCaTimeAfter, err := time.Parse(time.RFC3339, goodCaTimeAfterString) | ||
if err != nil { | ||
t.Errorf("Error parsing time %s: %v", goodCaTimeAfterString, err) | ||
} | ||
goodCaTimeBeforeString := "2022-01-07T00:03:51Z" | ||
goodCaTimeBefore, err := time.Parse(time.RFC3339, goodCaTimeBeforeString) | ||
if err != nil { | ||
t.Errorf("Error parsing time %s: %v", goodCaTimeBeforeString, err) | ||
} | ||
|
||
goodCertOptions := certificates.CertOptions{ | ||
Bits: 4096, | ||
CommonName: "Ansible Automation Controller Nodes Mesh ROOT CA", | ||
NotAfter: goodCaTimeAfter, | ||
NotBefore: goodCaTimeBefore, | ||
} | ||
|
||
goodCaPrivateKeyBlock, _ := pem.Decode([]byte(`-----BEGIN RSA PRIVATE KEY----- | ||
MIIJKAIBAAKCAgEAp17EU0yKFdjqoec7zSc0AWDmT6cZpys8C74HqKeOArJPvswE | ||
b4OVyKZj20hFNj2N6TRry0x+pw+eP2XziEc0jdIqb33K6SbZKyezmKNYF+0TlzN9 | ||
Md249inCf3DIDImTEC6j3oCobTByxs9E1/tDHyeY6k5aMFY0gMlISuqTLX9iEqR6 | ||
jgOrr5i4VIZK7lK1JBzJ28FjE86zvEAzGnS71foYlmTWRWn+l7d5TQUWPsq17khu | ||
2TnP+lLFg2+DVQCy9ZidCI30noiufEn/FR1GODBI8vFVtpXwEVP5nDZMa1GNQwTa | ||
ec3BzIcKC5CyHfdD8hcs1zAwr2cR6xhMLWdt1AGGP8AL8NV1puVyQYi82i9dnUrb | ||
h3mYLQFDrnEB7xDoJbz4pVOryn+TxXcaqChDsF7YC1E5cOKLZtm1seadiz5cZDwK | ||
WwL+1GsYk23KbiDIfFk00pPxFIKchFI6YYjdLqp6dnx/TJsp/IYEgfyn+hYSGRZd | ||
1TDTesfFU5Ki5M1RvFHePIBR362lUF72i3Awwi8U3nWuk4erk8Nswonwc121sWSo | ||
5Yp8fDBDP5CANcHv8JcLGMKUDYZGzqK0d3iehMXZdQK/Jd4x6dvd4Qr8VDbsxuWf | ||
aDwzEOjEpvMcawTdqWGTS9wwlmidJ47jY2HjUe5e7PvYm1+UQ/rgEoguoTsCAwEA | ||
AQKCAgApCj3Nxyjc7pGqHY82YPSJmf8fbPQHX7ybjH9IRb22v456VICJ75Qc3WAC | ||
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BABmtcLim8rEIo82NERAUvVHR7MxR | ||
hXKx9g3bm1O0w7kJ16jyf5uyJ85JNi1XF2/AomSNWH6ikHuX5Xj6vOdL4Ki9jPDq | ||
TOlmvys2LtCAMOM3e3NvzIfTnrQEurGusCQKxCbnlRk2W13j3uc2gVFgB3T1+w2H | ||
lSEhzuFpDrxKrsE9QcCf7/Cju+2ir9h3FsPDRKoxfRJ2/onsgQ/Q7NODRRQGjwxw | ||
P/Hli/j17jC7TdgC26JhtVHH7K5xC6iNL03Pf3GTSvwN1vK1BY2reoz1FtQrGZvM | ||
rydzkVNNVeMVX2TER9yc8AdFqkRlaBWHmO61rYmV+N1quLM0uMVsu55ZNCY= | ||
-----END RSA PRIVATE KEY-----`)) | ||
|
||
goodCaPrivateKey, err := x509.ParsePKCS1PrivateKey(goodCaPrivateKeyBlock.Bytes) | ||
if err != nil { | ||
t.Errorf("Error parsing Private Key: %v", err) | ||
} | ||
|
||
tests := []struct { | ||
name string | ||
args args | ||
want *certificates.CA | ||
}{ | ||
{ | ||
name: "Positive test", | ||
args: args{ | ||
opts: &goodCertOptions, | ||
}, | ||
want: &certificates.CA{ | ||
Certificate: goodCaCertificate, | ||
PrivateKey: goodCaPrivateKey, | ||
}, | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
ctrl := gomock.NewController(t) | ||
defer ctrl.Finish() | ||
|
||
mockRsa := mock_certificates.NewMockRsaer(ctrl) | ||
|
||
mockRsa.EXPECT().GenerateKey(gomock.Any(), gomock.Any()).DoAndReturn( | ||
func(random io.Reader, bits int) (*rsa.PrivateKey, error) { | ||
return goodCaPrivateKey, nil | ||
}, | ||
) | ||
|
||
got, _ := certificates.CreateCA(tt.args.opts, mockRsa) | ||
if !reflect.DeepEqual(got.PrivateKey, tt.want.PrivateKey) { | ||
t.Errorf("CreateCA() Private Key got = %v, want = %v", got.PrivateKey, tt.want.PrivateKey) | ||
|
||
return | ||
} | ||
|
||
certWant := tt.want.Certificate | ||
certGot := got.Certificate | ||
if certGot.BasicConstraintsValid != true { | ||
t.Errorf("CreateCA() Certificate BasicConstraintsValid got = %v, want = %v", certGot.BasicConstraintsValid, true) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.ExtraExtensions, certWant.ExtraExtensions) { | ||
t.Errorf("CreateCA() Certificate ExtraExtensions got = %v, want = %v", certGot.ExtraExtensions, certWant.ExtraExtensions) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.ExtKeyUsage, certWant.ExtKeyUsage) { | ||
t.Errorf("CreateCA() Certificate ExtKeyUsage got = %v, want = %v", certGot.ExtKeyUsage, certWant.ExtKeyUsage) | ||
|
||
return | ||
} | ||
|
||
if certGot.IsCA != true { | ||
t.Errorf("CreateCA() Certificate IsCA got = %v, want = %v", certGot.IsCA, true) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.Issuer, certWant.Issuer) { | ||
t.Errorf("CreateCA() Certificate Issuer got = %v, want = %v", certGot.Issuer, certWant.Issuer) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.KeyUsage, certWant.KeyUsage) { | ||
t.Errorf("CreateCA() Certificate KeyUsage got = %v, want = %v", certGot.KeyUsage, certWant.KeyUsage) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.NotAfter, certWant.NotAfter) { | ||
t.Errorf("CreateCA() Certificate NotAfter got = %v, want = %v", certGot.NotAfter, certWant.NotAfter) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.NotBefore, certWant.NotBefore) { | ||
t.Errorf("CreateCA() Certificate NotBefore got = %v, want = %v", certGot.NotBefore, certWant.NotBefore) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.PublicKeyAlgorithm, certWant.PublicKeyAlgorithm) { | ||
t.Errorf("CreateCA() Certificate PublicKeyAlgorithm got = %v, want = %v", certGot.PublicKeyAlgorithm, certWant.PublicKeyAlgorithm) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.SignatureAlgorithm, certWant.SignatureAlgorithm) { | ||
t.Errorf("CreateCA() Certificate SignatureAlgorithm got = %v, want = %v", certGot.SignatureAlgorithm, certWant.SignatureAlgorithm) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.Subject, certWant.Subject) { | ||
t.Errorf("CreateCA() Certificate Subject got = %v, want = %v", certGot.Subject, certWant.Subject) | ||
|
||
return | ||
} | ||
|
||
if !reflect.DeepEqual(certGot.Version, certWant.Version) { | ||
t.Errorf("CreateCA() Certificate Version got = %v, want = %v", certGot.Version, certWant.Version) | ||
|
||
return | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestCreateCANegative(t *testing.T) { | ||
type args struct { | ||
opts *certificates.CertOptions | ||
} | ||
badCertOptions := certificates.CertOptions{ | ||
Bits: -1, | ||
} | ||
tests := []struct { | ||
name string | ||
args args | ||
want *certificates.CA | ||
wantErr error | ||
}{ | ||
{ | ||
name: "Negative test for Common Name", | ||
args: args{ | ||
opts: &badCertOptions, | ||
}, | ||
want: nil, | ||
wantErr: fmt.Errorf("must provide CommonName"), | ||
}, | ||
} | ||
|
||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
_, gotErr := certificates.CreateCA(tt.args.opts, &certificates.RsaWrapper{}) | ||
if gotErr == nil || gotErr.Error() != tt.wantErr.Error() { | ||
t.Errorf("CreateCA() error = %v, wantErr = %v", gotErr, tt.wantErr) | ||
|
||
return | ||
} | ||
}) | ||
} | ||
} |
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
Oops, something went wrong.