Repository navigation
Expand file tree
/
Copy pathca_setup_test.go
More file actions
85 lines (77 loc) · 2.74 KB
/
Copy pathca_setup_test.go
File metadata and controls
85 lines (77 loc) · 2.74 KB
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
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
package tests
import (
"context"
"testing"
"time"
v1 "github.com/stackrox/rox/generated/api/v1"
"github.com/stackrox/rox/generated/internalapi/central"
"github.com/stackrox/rox/pkg/testutils"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
)
func centralIsReleaseBuild(conn *grpc.ClientConn, t *testing.T) bool {
client := v1.NewMetadataServiceClient(conn)
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
metadata, err := client.GetMetadata(ctx, &v1.Empty{})
require.NoError(t, err)
return metadata.ReleaseBuild
}
func TestCASetup(t *testing.T) {
t.Parallel()
conn := testutils.GRPCConnectionToCentral(t)
service := central.NewDevelopmentServiceClient(conn)
isReleaseBuild := centralIsReleaseBuild(conn, t)
// Can't run these tests on a release build. But also let's assert
// that the development service is not available.
if isReleaseBuild {
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
resp, err := service.URLHasValidCert(ctx, ¢ral.URLHasValidCertRequest{})
require.Nil(t, resp)
require.Error(t, err)
require.Equal(t, codes.Unimplemented, status.Code(err))
return
}
cases := []struct {
url string
expectedResp central.URLHasValidCertResponse_URLResult
additionalMessage string
}{
{
url: "https://untrusted-root.badssl.com",
// This should succeed because, even though it's a bad cert, we have configured Central to trust its root
// on startup.
expectedResp: central.URLHasValidCertResponse_REQUEST_SUCCEEDED,
additionalMessage: "This failure likely means that setting up trusted CAs with Central is broken. Look at the TRUSTED_CA_FILE being exported in the deploy scripts",
},
{
url: "https://self-signed.badssl.com",
expectedResp: central.URLHasValidCertResponse_CERT_SIGNED_BY_UNKNOWN_AUTHORITY,
},
{
url: "https://expired.badssl.com",
expectedResp: central.URLHasValidCertResponse_CERT_SIGNING_AUTHORITY_VALID_BUT_OTHER_ERROR,
},
{
url: "https://google.com",
expectedResp: central.URLHasValidCertResponse_REQUEST_SUCCEEDED,
},
{
url: "https://doesnotexist123.com",
expectedResp: central.URLHasValidCertResponse_OTHER_GET_ERROR,
},
}
for _, c := range cases {
t.Run(c.url, func(t *testing.T) {
ctx, cancel := context.WithTimeout(context.Background(), 20*time.Second)
defer cancel()
resp, err := service.URLHasValidCert(ctx, ¢ral.URLHasValidCertRequest{Url: c.url})
require.NoError(t, err)
assert.Equal(t, c.expectedResp, resp.GetResult(), "received resp: %+v. %s", resp, c.additionalMessage)
})
}
}