Skip to content

Commit 90385ad

Browse files
committed
Add ut
Signed-off-by: Jian Qiu <[email protected]>
1 parent c5ce015 commit 90385ad

File tree

4 files changed

+187
-13
lines changed

4 files changed

+187
-13
lines changed
Lines changed: 65 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,65 @@
1+
package authn
2+
3+
import (
4+
"context"
5+
"crypto/tls"
6+
"crypto/x509"
7+
"crypto/x509/pkix"
8+
"google.golang.org/grpc/credentials"
9+
"google.golang.org/grpc/peer"
10+
"testing"
11+
)
12+
13+
func TestMtlsAuthenticator(t *testing.T) {
14+
tests := []struct {
15+
name string
16+
authInfo credentials.TLSInfo
17+
valid bool
18+
}{
19+
{
20+
name: "no info",
21+
authInfo: credentials.TLSInfo{},
22+
valid: false,
23+
},
24+
{
25+
name: "nil chain",
26+
authInfo: credentials.TLSInfo{
27+
State: tls.ConnectionState{
28+
VerifiedChains: [][]*x509.Certificate{nil},
29+
},
30+
},
31+
valid: false,
32+
},
33+
{
34+
name: "valid chain",
35+
authInfo: credentials.TLSInfo{
36+
State: tls.ConnectionState{
37+
VerifiedChains: [][]*x509.Certificate{
38+
{
39+
{
40+
Subject: pkix.Name{},
41+
},
42+
},
43+
},
44+
},
45+
},
46+
valid: true,
47+
},
48+
}
49+
50+
for _, test := range tests {
51+
t.Run(test.name, func(t *testing.T) {
52+
p := &peer.Peer{
53+
AuthInfo: test.authInfo,
54+
}
55+
ctx := peer.NewContext(context.Background(), p)
56+
authenticator := MtlsAuthenticator{}
57+
_, err := authenticator.Authenticate(ctx)
58+
if test.valid && err != nil {
59+
t.Errorf("authenticator.Authenticate() = %v", err)
60+
} else if !test.valid && err == nil {
61+
t.Errorf("authenticator.Authenticate() = %v, wanted error", err)
62+
}
63+
})
64+
}
65+
}
Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
package authn
2+
3+
import (
4+
"context"
5+
"fmt"
6+
"google.golang.org/grpc/metadata"
7+
authenticationv1 "k8s.io/api/authentication/v1"
8+
"k8s.io/apimachinery/pkg/runtime"
9+
"k8s.io/client-go/kubernetes/fake"
10+
clienttesting "k8s.io/client-go/testing"
11+
"testing"
12+
)
13+
14+
func TestTokenAuthenticator(t *testing.T) {
15+
tests := []struct {
16+
name string
17+
metadata metadata.MD
18+
token string
19+
valid bool
20+
}{
21+
{
22+
name: "no authorization field",
23+
metadata: metadata.MD{},
24+
valid: false,
25+
},
26+
{
27+
name: "token is not correct",
28+
metadata: metadata.MD{
29+
"Authorization": []string{"Bearer foo"},
30+
},
31+
token: "bar",
32+
valid: false,
33+
},
34+
{
35+
name: "authorization header is set",
36+
metadata: metadata.MD{
37+
"Authorization": []string{"Bearer foo"},
38+
},
39+
token: "foo",
40+
valid: true,
41+
},
42+
}
43+
44+
for _, test := range tests {
45+
t.Run(test.name, func(t *testing.T) {
46+
ctx := metadata.NewIncomingContext(context.Background(), test.metadata)
47+
client := fake.NewClientset()
48+
client.PrependReactor("create", "tokenreviews", func(action clienttesting.Action) (handled bool, ret runtime.Object, err error) {
49+
createAction := action.(clienttesting.CreateAction)
50+
tr, ok := createAction.GetObject().(*authenticationv1.TokenReview)
51+
if !ok {
52+
return false, nil, fmt.Errorf("not a TokenReview")
53+
}
54+
if tr.Spec.Token != test.token {
55+
return false, nil, fmt.Errorf("invalid token")
56+
}
57+
tr.Status = authenticationv1.TokenReviewStatus{Authenticated: true}
58+
return true, tr, nil
59+
})
60+
authenticator := NewTokenAuthenticator(client)
61+
_, err := authenticator.Authenticate(ctx)
62+
if test.valid {
63+
if err != nil {
64+
t.Errorf("authenticator.Authenticate() = %v", err)
65+
}
66+
67+
}
68+
if !test.valid && err == nil {
69+
t.Errorf("authenticator.Authenticate() = %v, wanted error", err)
70+
}
71+
})
72+
}
73+
}

pkg/cloudevents/server/grpc/options/optoins.go renamed to pkg/cloudevents/server/grpc/options/options.go

Lines changed: 25 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -25,22 +25,34 @@ type GRPCServerOptions struct {
2525
}
2626

2727
func NewGRPCServerOptions() *GRPCServerOptions {
28-
return &GRPCServerOptions{}
28+
return &GRPCServerOptions{
29+
ServerBindPort: "8090",
30+
MaxConcurrentStreams: math.MaxUint32,
31+
MaxReceiveMessageSize: 1024 * 1024 * 4,
32+
MaxSendMessageSize: math.MaxInt32,
33+
ConnectionTimeout: 120 * time.Second,
34+
MaxConnectionAge: time.Duration(math.MaxInt64),
35+
ClientMinPingInterval: 5 * time.Second,
36+
ServerPingInterval: 30 * time.Second,
37+
ServerPingTimeout: 10 * time.Second,
38+
WriteBufferSize: 32 * 1024,
39+
ReadBufferSize: 32 * 1024,
40+
}
2941
}
3042

3143
func (o *GRPCServerOptions) AddFlags(flags *pflag.FlagSet) {
32-
flags.StringVar(&o.ServerBindPort, "grpc-server-bindport", "8090", "gPRC server bind port")
33-
flags.Uint32Var(&o.MaxConcurrentStreams, "grpc-max-concurrent-streams", math.MaxUint32, "gPRC max concurrent streams")
34-
flags.IntVar(&o.MaxReceiveMessageSize, "grpc-max-receive-message-size", 1024*1024*4, "gPRC max receive message size")
35-
flags.IntVar(&o.MaxSendMessageSize, "grpc-max-send-message-size", math.MaxInt32, "gPRC max send message size")
36-
flags.DurationVar(&o.ConnectionTimeout, "grpc-connection-timeout", 120*time.Second, "gPRC connection timeout")
37-
flags.DurationVar(&o.MaxConnectionAge, "grpc-max-connection-age", time.Duration(math.MaxInt64), "A duration for the maximum amount of time connection may exist before closing")
38-
flags.DurationVar(&o.ClientMinPingInterval, "grpc-client-min-ping-interval", 5*time.Second, "Server will terminate the connection if the client pings more than once within this duration")
39-
flags.DurationVar(&o.ServerPingInterval, "grpc-server-ping-interval", 30*time.Second, "Duration after which the server pings the client if no activity is detected")
40-
flags.DurationVar(&o.ServerPingTimeout, "grpc-server-ping-timeout", 10*time.Second, "Duration the client waits for a response after sending a keepalive ping")
41-
flags.BoolVar(&o.PermitPingWithoutStream, "permit-ping-without-stream", false, "Allow keepalive pings even when there are no active streams")
42-
flags.IntVar(&o.WriteBufferSize, "grpc-write-buffer-size", 32*1024, "gPRC write buffer size")
43-
flags.IntVar(&o.ReadBufferSize, "grpc-read-buffer-size", 32*1024, "gPRC read buffer size")
44+
flags.StringVar(&o.ServerBindPort, "grpc-server-bindport", o.ServerBindPort, "gPRC server bind port")
45+
flags.Uint32Var(&o.MaxConcurrentStreams, "grpc-max-concurrent-streams", o.MaxConcurrentStreams, "gPRC max concurrent streams")
46+
flags.IntVar(&o.MaxReceiveMessageSize, "grpc-max-receive-message-size", o.MaxReceiveMessageSize, "gPRC max receive message size")
47+
flags.IntVar(&o.MaxSendMessageSize, "grpc-max-send-message-size", o.MaxSendMessageSize, "gPRC max send message size")
48+
flags.DurationVar(&o.ConnectionTimeout, "grpc-connection-timeout", o.ConnectionTimeout, "gPRC connection timeout")
49+
flags.DurationVar(&o.MaxConnectionAge, "grpc-max-connection-age", o.MaxConnectionAge, "A duration for the maximum amount of time connection may exist before closing")
50+
flags.DurationVar(&o.ClientMinPingInterval, "grpc-client-min-ping-interval", o.ClientMinPingInterval, "Server will terminate the connection if the client pings more than once within this duration")
51+
flags.DurationVar(&o.ServerPingInterval, "grpc-server-ping-interval", o.ServerPingInterval, "Duration after which the server pings the client if no activity is detected")
52+
flags.DurationVar(&o.ServerPingTimeout, "grpc-server-ping-timeout", o.ServerPingTimeout, "Duration the client waits for a response after sending a keepalive ping")
53+
flags.BoolVar(&o.PermitPingWithoutStream, "permit-ping-without-stream", o.PermitPingWithoutStream, "Allow keepalive pings even when there are no active streams")
54+
flags.IntVar(&o.WriteBufferSize, "grpc-write-buffer-size", o.WriteBufferSize, "gPRC write buffer size")
55+
flags.IntVar(&o.ReadBufferSize, "grpc-read-buffer-size", o.ReadBufferSize, "gPRC read buffer size")
4456
flags.StringVar(&o.TLSCertFile, "grpc-tls-cert-file", "", "The path to the tls.crt file")
4557
flags.StringVar(&o.TLSKeyFile, "grpc-tls-key-file", "", "The path to the tls.key file")
4658
flags.StringVar(&o.ClientCAFile, "grpc-client-ca-file", "", "The path to the client ca file, must specify if using mtls authentication type")
Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package options
2+
3+
import (
4+
"context"
5+
"open-cluster-management.io/sdk-go/pkg/cloudevents/server/grpc/authn"
6+
"testing"
7+
)
8+
9+
type testHook struct{}
10+
11+
func (h *testHook) Run(ctx context.Context) {}
12+
13+
func TestRunServer(t *testing.T) {
14+
opt := NewGRPCServerOptions()
15+
server := NewServer(opt).WithAuthenticator(authn.NewMtlsAuthenticator()).WithPreStartHooks(&testHook{})
16+
ctx, cancel := context.WithCancel(context.Background())
17+
go func() {
18+
err := server.Run(ctx)
19+
if err != nil {
20+
t.Errorf("server run error: %v", err)
21+
}
22+
cancel()
23+
}()
24+
}

0 commit comments

Comments
 (0)