Add tls support, remove reminder rename tests (#470)

* add tls support, remove reminder rename tests

Signed-off-by: yaron2 <schneider.yaron@live.com>

* update sdk version

Signed-off-by: yaron2 <schneider.yaron@live.com>

---------

Signed-off-by: yaron2 <schneider.yaron@live.com>
This commit is contained in:
Yaron Schneider 2023-11-02 08:07:15 -07:00 committed by GitHub
parent aa7abc06d9
commit e920fd9dd9
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 46 additions and 67 deletions

View File

@ -257,65 +257,6 @@ func TestUnregisterActorReminder(t *testing.T) {
})
}
func TestRenameActorReminder(t *testing.T) {
ctx := context.Background()
registerReminderReq := &RegisterActorReminderRequest{
ActorID: "fn",
Data: []byte(`{hello}`),
ActorType: testActorType,
Name: "oldName",
Period: "2s",
DueTime: "4s",
TTL: "20s",
}
testClient.RegisterActorReminder(ctx, registerReminderReq)
renameReminderReq := &RenameActorReminderRequest{
ActorID: "fn",
ActorType: testActorType,
OldName: "oldName",
NewName: "newName",
}
t.Run("invoke rename actor reminder without actorType", func(t *testing.T) {
renameReminderReq.ActorType = ""
err := testClient.RenameActorReminder(ctx, renameReminderReq)
renameReminderReq.ActorType = testActorType
assert.Error(t, err)
})
t.Run("invoke rename actor reminder without id ", func(t *testing.T) {
renameReminderReq.ActorID = ""
err := testClient.RenameActorReminder(ctx, renameReminderReq)
renameReminderReq.ActorID = "fn"
assert.Error(t, err)
})
t.Run("invoke rename actor reminder without oldName ", func(t *testing.T) {
renameReminderReq.OldName = ""
err := testClient.RenameActorReminder(ctx, renameReminderReq)
renameReminderReq.OldName = "oldName"
assert.Error(t, err)
})
t.Run("invoke rename actor reminder without newName ", func(t *testing.T) {
renameReminderReq.NewName = ""
err := testClient.RenameActorReminder(ctx, renameReminderReq)
renameReminderReq.NewName = "newName"
assert.Error(t, err)
})
t.Run("invoke rename actor reminder ", func(t *testing.T) {
assert.NoError(t, testClient.RenameActorReminder(ctx, renameReminderReq))
})
t.Run("invoke rename actor reminder with empty param", func(t *testing.T) {
assert.Error(t, testClient.RenameActorReminder(ctx, nil))
})
}
func TestUnregisterActorTimer(t *testing.T) {
ctx := context.Background()
in := &UnregisterActorTimerRequest{

View File

@ -15,6 +15,7 @@ package client
import (
"context"
"crypto/tls"
"errors"
"fmt"
"io"
@ -31,6 +32,7 @@ import (
"github.com/dapr/go-sdk/version"
"google.golang.org/grpc"
"google.golang.org/grpc/credentials"
"google.golang.org/grpc/credentials/insecure"
"google.golang.org/grpc/metadata"
"google.golang.org/protobuf/types/known/emptypb"
@ -44,6 +46,7 @@ import (
const (
daprPortDefault = "50001"
daprPortEnvVarName = "DAPR_GRPC_PORT" /* #nosec */
daprGRPCEndpointEnvVarName = "DAPR_GRPC_ENDPOINT"
traceparentKey = "traceparent"
apiTokenKey = "dapr-api-token" /* #nosec */
apiTokenEnvVarName = "DAPR_API_TOKEN" /* #nosec */
@ -192,9 +195,6 @@ type Client interface {
// UnregisterActorReminder unregisters an actor reminder.
UnregisterActorReminder(ctx context.Context, req *UnregisterActorReminderRequest) error
// RenameActorReminder rename an actor reminder.
RenameActorReminder(ctx context.Context, req *RenameActorReminderRequest) error
// InvokeActor calls a method on an actor.
InvokeActor(ctx context.Context, req *InvokeActorRequest) (*InvokeActorResponse, error)
@ -247,7 +247,13 @@ func NewClientWithPort(port string) (client Client, err error) {
if port == "" {
return nil, errors.New("nil port")
}
return NewClientWithAddress(net.JoinHostPort("127.0.0.1", port))
address := os.Getenv(daprGRPCEndpointEnvVarName)
if address == "" {
address = "127.0.0.1"
}
return NewClientWithAddress(net.JoinHostPort(address, port))
}
// NewClientWithAddress instantiates Dapr using specific address (including port).
@ -258,7 +264,7 @@ func NewClientWithAddress(address string) (client Client, err error) {
// NewClientWithAddressContext instantiates Dapr using specific address (including port).
// Uses the provided context to create the connection.
func NewClientWithAddressContext(ctx context.Context, address string) (client Client, err error) {
func NewClientWithAddressContext(ctx context.Context, address string, opts ...ClientOption) (client Client, err error) {
if address == "" {
return nil, errors.New("empty address")
}
@ -268,13 +274,26 @@ func NewClientWithAddressContext(ctx context.Context, address string) (client Cl
if err != nil {
return nil, err
}
var option grpc.DialOption
cOpts := clientOptions{}
for _, opt := range opts {
opt(&cOpts)
}
if cOpts.useTLS || strings.Contains(address, "https://") {
option = grpc.WithTransportCredentials(credentials.NewTLS(&tls.Config{}))
} else {
option = grpc.WithTransportCredentials(insecure.NewCredentials())
}
ctx, cancel := context.WithTimeout(ctx, time.Duration(timeoutSeconds)*time.Second)
conn, err := grpc.DialContext(
ctx,
address,
grpc.WithTransportCredentials(insecure.NewCredentials()),
option,
grpc.WithUserAgent(userAgent()),
grpc.WithBlock(),
)
cancel()
if err != nil {
@ -353,6 +372,19 @@ func (c *GRPCClient) WithAuthToken(token string) {
c.authToken = token
}
type clientOptions struct {
useTLS bool
}
type ClientOption func(*clientOptions)
// WithTLS sets gRPC TLS transport credentials on the connection.
func WithTLS() ClientOption {
return func(co *clientOptions) {
co.useTLS = true
}
}
// WithTraceID adds existing trace ID to the outgoing context.
func (c *GRPCClient) WithTraceID(ctx context.Context, id string) context.Context {
if id == "" {

View File

@ -133,6 +133,12 @@ func TestShutdown(t *testing.T) {
})
}
func TestWithTLS(t *testing.T) {
c, err := NewClientWithAddressContext(context.Background(), "127.0.0.1", WithTLS())
require.NoError(t, err)
defer c.Close()
}
func getTestClient(ctx context.Context) (client Client, closer func()) {
s := grpc.NewServer()
pb.RegisterDaprServer(s, &testDaprServer{

View File

@ -1 +1 @@
v1.9.0
v1.9.1