add cert tests (#16192)

This commit is contained in:
malizz 2023-02-07 09:58:00 -08:00 committed by GitHub
parent 00468d72df
commit 1777e9ec8f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 85 additions and 0 deletions

View File

@ -1,6 +1,7 @@
package troubleshoot
import (
"errors"
"fmt"
"time"
@ -15,6 +16,14 @@ func (t *Troubleshoot) validateCerts(certs *envoy_admin_v3.Certificates) error {
var resultErr error
now := time.Now()
if certs == nil {
return errors.New("certs object is nil")
}
if len(certs.GetCertificates()) == 0 {
return errors.New("no certificates provided")
}
for _, cert := range certs.GetCertificates() {
for _, cacert := range cert.GetCaCert() {
if now.After(cacert.GetExpirationTime().AsTime()) {

View File

@ -0,0 +1,71 @@
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))
x := []struct {
certs *envoy_admin_v3.Certificates
expectedError string
}{
{
certs: nil,
expectedError: "certs object is nil",
},
{
certs: &envoy_admin_v3.Certificates{
Certificates: []*envoy_admin_v3.Certificate{},
},
expectedError: "no certificates provided",
},
{
certs: &envoy_admin_v3.Certificates{
Certificates: []*envoy_admin_v3.Certificate{
{
CaCert: []*envoy_admin_v3.CertificateDetails{
{
ExpirationTime: anHourAgo,
},
},
},
},
},
expectedError: "Ca cert is expired",
},
{
certs: &envoy_admin_v3.Certificates{
Certificates: []*envoy_admin_v3.Certificate{
{
CertChain: []*envoy_admin_v3.CertificateDetails{
{
ExpirationTime: anHourAgo,
},
},
},
},
},
expectedError: "cert chain is expired",
},
}
ts := Troubleshoot{}
for _, tc := range x {
err := ts.validateCerts(tc.certs)
if tc.expectedError != "" {
require.Error(t, err)
require.Contains(t, err.Error(), tc.expectedError)
}
}
}

View File

@ -39,6 +39,11 @@ func NewTroubleshoot(envoyIP *net.IPAddr, envoyPort string) (*Troubleshoot, erro
if err != nil {
return nil, err
}
if envoyIP == nil {
return nil, fmt.Errorf("envoy address is empty")
}
return &Troubleshoot{
client: c,
envoyAddr: *envoyIP,