Skip to content

Commit c86da65

Browse files
authored
chore: Add basic auth tests (#70)
1 parent f8021ca commit c86da65

File tree

5 files changed

+140
-12
lines changed

5 files changed

+140
-12
lines changed

go.mod

+4-4
Original file line numberDiff line numberDiff line change
@@ -3,12 +3,9 @@ module github.com/warehouse-13/hammertime
33
go 1.18
44

55
require (
6-
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0
7-
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0
8-
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d
96
github.com/onsi/ginkgo/v2 v2.4.0
107
github.com/urfave/cli/v2 v2.10.2
11-
github.com/warehouse-13/safety v0.0.0-20221207164058-27f5985eb045
8+
github.com/warehouse-13/safety v0.0.0-20230120170710-60c7451457c5
129
github.com/weaveworks-liquidmetal/flintlock/api v0.0.0-20221117153111-bd29de31356f
1310
github.com/weaveworks-liquidmetal/flintlock/client v0.0.0-20220722132608-982d429ba641
1411
google.golang.org/grpc v1.51.0
@@ -23,7 +20,10 @@ require (
2320
github.com/cespare/xxhash/v2 v2.1.2 // indirect
2421
github.com/go-logr/logr v1.2.3 // indirect
2522
github.com/google/go-cmp v0.5.8 // indirect
23+
github.com/grpc-ecosystem/go-grpc-middleware v1.3.0 // indirect
24+
github.com/grpc-ecosystem/go-grpc-prometheus v1.2.0 // indirect
2625
github.com/matttproud/golang_protobuf_extensions v1.0.1 // indirect
26+
github.com/nu7hatch/gouuid v0.0.0-20131221200532-179d4d0c4d8d // indirect
2727
github.com/prometheus/client_golang v1.13.1 // indirect
2828
github.com/prometheus/client_model v0.2.0 // indirect
2929
github.com/prometheus/common v0.37.0 // indirect

go.sum

+2-2
Original file line numberDiff line numberDiff line change
@@ -245,8 +245,8 @@ github.com/stretchr/testify v1.7.0 h1:nwc3DEeHmmLAfoZucVR881uASk0Mfjw8xYJ99tb5Cc
245245
github.com/stretchr/testify v1.7.0/go.mod h1:6Fq8oRcR53rry900zMqJjRRixrwX3KX962/h/Wwjteg=
246246
github.com/urfave/cli/v2 v2.10.2 h1:x3p8awjp/2arX+Nl/G2040AZpOCHS/eMJJ1/a+mye4Y=
247247
github.com/urfave/cli/v2 v2.10.2/go.mod h1:f8iq5LtQ/bLxafbdBSLPPNsgaW0l/2fYYEHhAyPlwvo=
248-
github.com/warehouse-13/safety v0.0.0-20221207164058-27f5985eb045 h1:X5PZayrpXPpuhq6vOBCXUbIsdxv56L471p32RBqWDUc=
249-
github.com/warehouse-13/safety v0.0.0-20221207164058-27f5985eb045/go.mod h1:hTlI1j1CXNEYrNoGbw7bAPj1/maOkuvGgoi63donJ5I=
248+
github.com/warehouse-13/safety v0.0.0-20230120170710-60c7451457c5 h1:1uTkby9abQ0d0RJIYrKxZsfDZoWdWEb/KoaCnDqImtI=
249+
github.com/warehouse-13/safety v0.0.0-20230120170710-60c7451457c5/go.mod h1:hTlI1j1CXNEYrNoGbw7bAPj1/maOkuvGgoi63donJ5I=
250250
github.com/weaveworks-liquidmetal/flintlock/api v0.0.0-20221117153111-bd29de31356f h1:xHtKo4eiR1pRZUrXxlGF3KDuoa6xYjBGuPP47M9eVWM=
251251
github.com/weaveworks-liquidmetal/flintlock/api v0.0.0-20221117153111-bd29de31356f/go.mod h1:JPML9O56MoPKGX97jfj++BtuFFS84jm4T+jWQBjO5Uc=
252252
github.com/weaveworks-liquidmetal/flintlock/client v0.0.0-20220722132608-982d429ba641 h1:c5GlWX7UuBOQwx3Xfw4Jnd3VCHvChpkWaSVJhEbQcdo=

pkg/client/client.go

+3-3
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import (
1717
// Client is a wrapper around a v1alpha1.MicroVMClient.
1818
type Client struct {
1919
v1alpha1.MicroVMClient
20-
conn *grpc.ClientConn
20+
Conn *grpc.ClientConn
2121
}
2222

2323
//counterfeiter:generate -o fakeclient/ . FlintlockClient
@@ -31,7 +31,7 @@ type FlintlockClient interface {
3131

3232
// New returns a new flintlock Client.
3333
func New(address, basicAuthToken string) (FlintlockClient, error) {
34-
conn, err := dialler.New(address, basicAuthToken)
34+
conn, err := dialler.New(address, basicAuthToken, nil)
3535
if err != nil {
3636
return nil, err
3737
}
@@ -40,7 +40,7 @@ func New(address, basicAuthToken string) (FlintlockClient, error) {
4040
}
4141

4242
func (c *Client) Close() error {
43-
return c.conn.Close()
43+
return c.Conn.Close()
4444
}
4545

4646
// Create creates a new Microvm with the MicroVMClient.

pkg/command/app_test.go

+125
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,125 @@
1+
package command_test
2+
3+
import (
4+
"bytes"
5+
"context"
6+
"encoding/json"
7+
"net"
8+
"testing"
9+
10+
. "github.com/onsi/gomega"
11+
"github.com/warehouse-13/safety"
12+
"github.com/weaveworks-liquidmetal/flintlock/api/services/microvm/v1alpha1"
13+
"google.golang.org/grpc"
14+
15+
"github.com/warehouse-13/hammertime/pkg/client"
16+
"github.com/warehouse-13/hammertime/pkg/command"
17+
"github.com/warehouse-13/hammertime/pkg/config"
18+
"github.com/warehouse-13/hammertime/pkg/dialler"
19+
"github.com/warehouse-13/hammertime/pkg/utils"
20+
)
21+
22+
func Test_CRUD_noBasicAuth_noTLS(t *testing.T) {
23+
g := NewWithT(t)
24+
25+
fakeserver := safety.New()
26+
dialer := fakeserver.StartBuf("")
27+
28+
t.Cleanup(func() {
29+
fakeserver.Stop()
30+
})
31+
32+
cfg := &config.Config{
33+
ClientConfig: config.ClientConfig{
34+
ClientBuilderFunc: cl(dialer),
35+
},
36+
}
37+
38+
buf := &bytes.Buffer{}
39+
w := utils.NewWriter(buf)
40+
41+
g.Expect(command.CreateFn(w, cfg)).To(Succeed())
42+
43+
out := &v1alpha1.CreateMicroVMResponse{}
44+
g.Expect(json.Unmarshal(buf.Bytes(), out)).To(Succeed())
45+
46+
cfg.UUID = *out.Microvm.Spec.Uid
47+
48+
g.Expect(command.GetFn(w, cfg)).To(Succeed())
49+
g.Expect(command.ListFn(w, cfg)).To(Succeed())
50+
g.Expect(command.DeleteFn(w, cfg)).To(Succeed())
51+
}
52+
53+
func Test_CRUD_basicAuth_noTLS(t *testing.T) {
54+
g := NewWithT(t)
55+
56+
basicAuthToken := "secret"
57+
58+
fakeserver := safety.New()
59+
dialer := fakeserver.StartBuf(basicAuthToken)
60+
61+
t.Cleanup(func() {
62+
fakeserver.Stop()
63+
})
64+
65+
cfg := &config.Config{
66+
ClientConfig: config.ClientConfig{
67+
ClientBuilderFunc: cl(dialer),
68+
},
69+
Token: basicAuthToken,
70+
}
71+
72+
buf := &bytes.Buffer{}
73+
w := utils.NewWriter(buf)
74+
75+
g.Expect(command.CreateFn(w, cfg)).To(Succeed())
76+
77+
out := &v1alpha1.CreateMicroVMResponse{}
78+
g.Expect(json.Unmarshal(buf.Bytes(), out)).To(Succeed())
79+
80+
cfg.UUID = *out.Microvm.Spec.Uid
81+
82+
g.Expect(command.GetFn(w, cfg)).To(Succeed())
83+
g.Expect(command.ListFn(w, cfg)).To(Succeed())
84+
g.Expect(command.DeleteFn(w, cfg)).To(Succeed())
85+
}
86+
87+
func Test_basicAuth_failsWithNoClientToken(t *testing.T) {
88+
g := NewWithT(t)
89+
90+
basicAuthToken := "secret"
91+
92+
fakeserver := safety.New()
93+
dialer := fakeserver.StartBuf(basicAuthToken)
94+
95+
t.Cleanup(func() {
96+
fakeserver.Stop()
97+
})
98+
99+
cfg := &config.Config{
100+
ClientConfig: config.ClientConfig{
101+
ClientBuilderFunc: cl(dialer),
102+
},
103+
Token: "",
104+
}
105+
106+
buf := &bytes.Buffer{}
107+
w := utils.NewWriter(buf)
108+
109+
g.Expect(command.CreateFn(w, cfg)).To(MatchError(ContainSubstring("unauthenticated")))
110+
}
111+
112+
func cl(dialer func(context.Context, string) (net.Conn, error)) func(string, string) (client.FlintlockClient, error) {
113+
return func(string, token string) (client.FlintlockClient, error) {
114+
opt := []grpc.DialOption{grpc.WithContextDialer(dialer)}
115+
conn, err := dialler.New("bufnet", token, opt)
116+
if err != nil {
117+
return nil, err
118+
}
119+
120+
return &client.Client{
121+
MicroVMClient: v1alpha1.NewMicroVMClient(conn),
122+
Conn: conn,
123+
}, nil
124+
}
125+
}

pkg/dialler/dialler.go

+6-3
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,13 @@ import (
77

88
// New process the dial config and returns a grpc.ClientConn. The caller is
99
// responsible for closing the connection.
10-
func New(address, basicAuthToken string) (*grpc.ClientConn, error) {
11-
dialOpts := []grpc.DialOption{
10+
func New(address, basicAuthToken string, opts []grpc.DialOption) (*grpc.ClientConn, error) {
11+
// TODO this needs to be tidied up when adding TLS #47
12+
dialOpts := opts
13+
14+
dialOpts = append(dialOpts,
1215
grpc.WithTransportCredentials(insecure.NewCredentials()),
13-
}
16+
)
1417

1518
if basicAuthToken != "" {
1619
dialOpts = append(dialOpts, grpc.WithPerRPCCredentials(

0 commit comments

Comments
 (0)