forked from stackrox/stackrox
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcert_test.go
63 lines (49 loc) · 1.46 KB
/
cert_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
package tests
import (
"crypto/tls"
"crypto/x509"
"os"
"testing"
"github.com/stackrox/rox/pkg/mtls"
"github.com/stackrox/rox/pkg/testutils/centralgrpc"
"github.com/stackrox/rox/pkg/utils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestInternalCert(t *testing.T) {
t.Parallel()
tlsConf := &tls.Config{
InsecureSkipVerify: true,
ServerName: "central.stackrox",
}
conn, err := tls.Dial("tcp", centralgrpc.RoxAPIEndpoint(t), tlsConf)
require.NoError(t, err)
defer utils.IgnoreError(conn.Close)
certs := conn.ConnectionState().PeerCertificates
require.NotEmpty(t, certs)
leaf := certs[0]
subj := mtls.SubjectFromCommonName(leaf.Subject.CommonName)
assert.Equal(t, mtls.CentralSubject, subj)
}
func TestCustomCert(t *testing.T) {
t.Parallel()
testCentralCertCAPEM := os.Getenv("ROX_TEST_CA_PEM")
if testCentralCertCAPEM == "" {
t.Skip("No test CA pem specified")
}
centralCN := os.Getenv("ROX_TEST_CENTRAL_CN")
require.NotEmpty(t, centralCN)
trustPool := x509.NewCertPool()
ok := trustPool.AppendCertsFromPEM([]byte(testCentralCertCAPEM))
require.True(t, ok)
tlsConf := &tls.Config{
InsecureSkipVerify: false,
ServerName: centralCN,
RootCAs: trustPool,
}
conn, err := tls.Dial("tcp", centralgrpc.RoxAPIEndpoint(t), tlsConf)
require.NoError(t, err)
defer utils.IgnoreError(conn.Close)
certChains := conn.ConnectionState().VerifiedChains
require.Len(t, certChains, 1)
}