2023-02-07 17:58:00 +00:00
|
|
|
package troubleshoot
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
envoy_admin_v3 "github.com/envoyproxy/go-control-plane/envoy/admin/v3"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
"google.golang.org/protobuf/types/known/timestamppb"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestValidateCerts(t *testing.T) {
|
|
|
|
|
|
|
|
t.Parallel()
|
|
|
|
|
|
|
|
anHourAgo := timestamppb.New(time.Now().Add(-1 * time.Hour))
|
|
|
|
|
2023-02-08 21:03:15 +00:00
|
|
|
cases := map[string]struct {
|
2023-02-07 17:58:00 +00:00
|
|
|
certs *envoy_admin_v3.Certificates
|
|
|
|
expectedError string
|
|
|
|
}{
|
2023-02-08 21:03:15 +00:00
|
|
|
"cert is nil": {
|
2023-02-07 17:58:00 +00:00
|
|
|
certs: nil,
|
2023-02-08 21:03:15 +00:00
|
|
|
expectedError: "certificate object is nil in the proxy configuration",
|
2023-02-07 17:58:00 +00:00
|
|
|
},
|
2023-02-08 21:03:15 +00:00
|
|
|
"no certificates": {
|
2023-02-07 17:58:00 +00:00
|
|
|
certs: &envoy_admin_v3.Certificates{
|
|
|
|
Certificates: []*envoy_admin_v3.Certificate{},
|
|
|
|
},
|
2023-02-08 21:03:15 +00:00
|
|
|
expectedError: "no certificates found",
|
2023-02-07 17:58:00 +00:00
|
|
|
},
|
2023-02-08 21:03:15 +00:00
|
|
|
"ca expired": {
|
2023-02-07 17:58:00 +00:00
|
|
|
certs: &envoy_admin_v3.Certificates{
|
|
|
|
Certificates: []*envoy_admin_v3.Certificate{
|
|
|
|
{
|
|
|
|
CaCert: []*envoy_admin_v3.CertificateDetails{
|
|
|
|
{
|
|
|
|
ExpirationTime: anHourAgo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-08 21:03:15 +00:00
|
|
|
expectedError: "ca certificate is expired",
|
2023-02-07 17:58:00 +00:00
|
|
|
},
|
2023-02-08 21:03:15 +00:00
|
|
|
"cert expired": {
|
2023-02-07 17:58:00 +00:00
|
|
|
certs: &envoy_admin_v3.Certificates{
|
|
|
|
Certificates: []*envoy_admin_v3.Certificate{
|
|
|
|
{
|
|
|
|
CertChain: []*envoy_admin_v3.CertificateDetails{
|
|
|
|
{
|
|
|
|
ExpirationTime: anHourAgo,
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
|
|
|
},
|
2023-02-08 21:03:15 +00:00
|
|
|
expectedError: "certificate chain is expired",
|
2023-02-07 17:58:00 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
ts := Troubleshoot{}
|
2023-02-08 21:03:15 +00:00
|
|
|
for name, tc := range cases {
|
|
|
|
t.Run(name, func(t *testing.T) {
|
|
|
|
messages := ts.validateCerts(tc.certs)
|
|
|
|
|
|
|
|
var outputErrors string
|
|
|
|
for _, msgError := range messages.Errors() {
|
|
|
|
outputErrors += msgError.Message
|
|
|
|
outputErrors += msgError.PossibleActions
|
|
|
|
}
|
|
|
|
if tc.expectedError == "" {
|
|
|
|
require.True(t, messages.Success())
|
|
|
|
} else {
|
|
|
|
require.Contains(t, outputErrors, tc.expectedError)
|
|
|
|
}
|
|
|
|
|
|
|
|
})
|
2023-02-07 17:58:00 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
}
|