Skip to content

Commit 375f3fb

Browse files
author
Matthew Mark Miller
committed
Trying to fix tests
1 parent 7e2429f commit 375f3fb

13 files changed

+590
-5
lines changed

Makefile

+1
Original file line numberDiff line numberDiff line change
@@ -30,6 +30,7 @@ clean:
3030
rm -rf vendor-ansible/out
3131
rm -rf vendor-cfssl/out
3232
rm -rf vendor-provision/out
33+
rm -rf integration/vendor
3334

3435
test: vendor
3536
go test ./cmd/... ./pkg/... $(TEST_OPTS)

integration/aws/client.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import (
66
"strconv"
77
"time"
88

9-
"github.com/apprenda/kismatic/pkg/retry"
9+
"github.com/apprenda/kismatic/integration/retry"
1010
"github.com/aws/aws-sdk-go/aws"
1111
"github.com/aws/aws-sdk-go/aws/credentials"
1212
"github.com/aws/aws-sdk-go/aws/session"

integration/glide.lock

+131
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

integration/install.go

+1-1
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ import (
1212
"path/filepath"
1313
"time"
1414

15-
"github.com/apprenda/kismatic/pkg/retry"
15+
"github.com/apprenda/kismatic/integration/retry"
1616
homedir "github.com/mitchellh/go-homedir"
1717
. "github.com/onsi/ginkgo"
1818
)

integration/prepare.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -12,8 +12,8 @@ import (
1212
"strings"
1313
"time"
1414

15-
"github.com/apprenda/kismatic/pkg/retry"
16-
"github.com/apprenda/kismatic/pkg/tls"
15+
"github.com/apprenda/kismatic/integration/retry"
16+
"github.com/apprenda/kismatic/integration/tls"
1717
"github.com/cloudflare/cfssl/csr"
1818
. "github.com/onsi/ginkgo"
1919
)

integration/provision.go

+2-1
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import (
99

1010
"github.com/apprenda/kismatic/integration/aws"
1111
"github.com/apprenda/kismatic/integration/packet"
12-
"github.com/apprenda/kismatic/pkg/retry"
12+
"github.com/apprenda/kismatic/integration/retry"
13+
1314
homedir "github.com/mitchellh/go-homedir"
1415
)
1516

integration/retry/retry.go

+21
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package retry
2+
3+
import "time"
4+
5+
// WithBackoff will retry a function specified number of times
6+
func WithBackoff(fn func() error, retries uint) error {
7+
var attempts uint
8+
var err error
9+
for {
10+
err = fn()
11+
if err == nil {
12+
break
13+
}
14+
if attempts == retries {
15+
break
16+
}
17+
time.Sleep((1 << attempts) * time.Second)
18+
attempts++
19+
}
20+
return err
21+
}

integration/tls/ca.go

+77
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,77 @@
1+
package tls
2+
3+
import (
4+
"encoding/json"
5+
"fmt"
6+
"io/ioutil"
7+
"os"
8+
"path/filepath"
9+
10+
"github.com/cloudflare/cfssl/csr"
11+
"github.com/cloudflare/cfssl/initca"
12+
"github.com/cloudflare/cfssl/log"
13+
)
14+
15+
func init() {
16+
log.Level = log.LevelError
17+
}
18+
19+
// The Subject contains the fields of the X.509 Subject
20+
type Subject struct {
21+
Country string
22+
State string
23+
Locality string
24+
Organization string
25+
OrganizationalUnit string
26+
}
27+
28+
// NewCACert creates a new Certificate Authority and returns it's private key and public certificate.
29+
func NewCACert(csrFile string, commonName string, subject Subject) (key, cert []byte, err error) {
30+
// Open CSR file
31+
f, err := os.Open(csrFile)
32+
if os.IsNotExist(err) {
33+
return nil, nil, fmt.Errorf("%q does not exist", csrFile)
34+
}
35+
if err != nil {
36+
return nil, nil, fmt.Errorf("error opening %q", csrFile)
37+
}
38+
// Create CSR struct
39+
caCSR := &csr.CertificateRequest{
40+
KeyRequest: csr.NewBasicKeyRequest(),
41+
}
42+
err = json.NewDecoder(f).Decode(caCSR)
43+
if err != nil {
44+
return nil, nil, fmt.Errorf("error decoding CSR: %v", err)
45+
}
46+
// Set the subject information
47+
name := csr.Name{
48+
C: subject.Country,
49+
ST: subject.State,
50+
L: subject.Locality,
51+
O: subject.Organization,
52+
OU: subject.OrganizationalUnit,
53+
}
54+
caCSR.Names = []csr.Name{name}
55+
caCSR.CN = commonName
56+
// Generate CA Cert according to CSR
57+
cert, _, key, err = initca.New(caCSR)
58+
if err != nil {
59+
return nil, nil, fmt.Errorf("error creating CA cert: %v", err)
60+
}
61+
return key, cert, nil
62+
}
63+
64+
// ReadCACert read CA file
65+
func ReadCACert(name, dir string) (key, cert []byte, err error) {
66+
dest := filepath.Join(dir, keyName(name))
67+
key, errKey := ioutil.ReadFile(dest)
68+
if errKey != nil {
69+
return nil, nil, fmt.Errorf("error reading private key: %v", errKey)
70+
}
71+
dest = filepath.Join(dir, certName(name))
72+
cert, errCert := ioutil.ReadFile(dest)
73+
if errCert != nil {
74+
return nil, nil, fmt.Errorf("error reading certificate: %v", errKey)
75+
}
76+
return key, cert, nil
77+
}

integration/tls/ca_test.go

+63
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
package tls
2+
3+
import (
4+
"reflect"
5+
"testing"
6+
"time"
7+
8+
"github.com/cloudflare/cfssl/helpers"
9+
)
10+
11+
func TestNewCACert(t *testing.T) {
12+
subject := Subject{
13+
Organization: "someOrg",
14+
OrganizationalUnit: "someOrgUnit",
15+
}
16+
_, cert, err := NewCACert("test/ca-csr.json", "someCommonName", subject)
17+
if err != nil {
18+
t.Fatalf("error creating CA cert: %v", err)
19+
}
20+
21+
parsedCert, err := helpers.ParseCertificatePEM(cert)
22+
if err != nil {
23+
t.Fatalf("error parsing certificate: %v", err)
24+
}
25+
26+
if !parsedCert.IsCA {
27+
t.Errorf("Genereated CA cert is not CA")
28+
}
29+
30+
expectedCN := "someCommonName"
31+
if parsedCert.Subject.CommonName != expectedCN {
32+
t.Errorf("CN mismatch: expected %q, found %q", expectedCN, parsedCert.Subject.CommonName)
33+
}
34+
35+
if parsedCert.Subject.Organization[0] != subject.Organization {
36+
t.Errorf("Organization mismatch: expected %q, found %q", subject.Organization, parsedCert.Subject.Organization[0])
37+
}
38+
39+
if parsedCert.Subject.OrganizationalUnit[0] != subject.OrganizationalUnit {
40+
t.Errorf("OrganizationalUnit mismatch: expected %q, found %q", subject.OrganizationalUnit, parsedCert.Subject.OrganizationalUnit[0])
41+
}
42+
43+
if !reflect.DeepEqual(parsedCert.Issuer, parsedCert.Subject) {
44+
t.Errorf("cert issuer is not equal to the CA's subject")
45+
}
46+
47+
// You might be tempted to test for this, but it seems like the AuthKeyID doesn't have to be set
48+
// for self-signed certificates. https://go.googlesource.com/go/+/b623b71509b2d24df915d5bc68602e1c6edf38ca
49+
// if !bytes.Equal(parsedCert.AuthorityKeyId, parsedCert.SubjectKeyId) {
50+
// t.Errorf("certificate auth key ID %q is not the subject key ID of the CA %q", string(parsedCert.AuthorityKeyId), string(parsedCert.SubjectKeyId))
51+
// }
52+
53+
// Verify expiration
54+
now := time.Now().UTC()
55+
d, err := time.ParseDuration("8760h")
56+
if err != nil {
57+
t.Fatalf("error parsing duration: %v", err)
58+
}
59+
expectedExpiration := now.Add(d)
60+
if expectedExpiration.Year() != parsedCert.NotAfter.Year() || expectedExpiration.YearDay() != parsedCert.NotAfter.YearDay() {
61+
t.Errorf("expected expiration date %q, got %q", expectedExpiration, parsedCert.NotAfter)
62+
}
63+
}

0 commit comments

Comments
 (0)