Skip to content

Commit 26da11e

Browse files
committed
fix api token initialization
Signed-off-by: Fabian Martinez <[email protected]>
1 parent a3df75f commit 26da11e

File tree

1 file changed

+15
-8
lines changed

1 file changed

+15
-8
lines changed

client/client.go

+15-8
Original file line numberDiff line numberDiff line change
@@ -355,7 +355,7 @@ func NewClientWithAddressContext(ctx context.Context, address string) (client Cl
355355
return nil, fmt.Errorf("error parsing address '%s': %w", address, err)
356356
}
357357

358-
at := &authToken{}
358+
at := newAuthToken()
359359

360360
opts := []grpc.DialOption{
361361
grpc.WithUserAgent(userAgent()),
@@ -404,7 +404,7 @@ func NewClientWithSocket(socket string) (client Client, err error) {
404404
if socket == "" {
405405
return nil, errors.New("nil socket")
406406
}
407-
at := &authToken{}
407+
at := newAuthToken()
408408
logger.Printf("dapr client initializing for: %s", socket)
409409
addr := "unix://" + socket
410410
conn, err := grpc.Dial( //nolint:staticcheck
@@ -421,11 +421,6 @@ func NewClientWithSocket(socket string) (client Client, err error) {
421421
}
422422

423423
func newClientWithConnection(conn *grpc.ClientConn, authToken *authToken) Client {
424-
apiToken := os.Getenv(apiTokenEnvVarName)
425-
if apiToken != "" {
426-
logger.Println("client uses API token")
427-
authToken.set(apiToken)
428-
}
429424
return &GRPCClient{
430425
connection: conn,
431426
protoClient: pb.NewDaprClient(conn),
@@ -435,14 +430,26 @@ func newClientWithConnection(conn *grpc.ClientConn, authToken *authToken) Client
435430

436431
// NewClientWithConnection instantiates Dapr client using specific connection.
437432
func NewClientWithConnection(conn *grpc.ClientConn) Client {
438-
return newClientWithConnection(conn, &authToken{})
433+
return newClientWithConnection(conn, newAuthToken())
439434
}
440435

436+
// NOTE: authToken must be created using newAuthToken()
437+
// it is crucial to correctly initialize the dapr client with the API token from the environment variable
441438
type authToken struct {
442439
mu sync.RWMutex
443440
authToken string
444441
}
445442

443+
func newAuthToken() *authToken {
444+
apiToken := os.Getenv(apiTokenEnvVarName)
445+
if apiToken != "" {
446+
logger.Println("client uses API token")
447+
}
448+
return &authToken{
449+
authToken: apiToken,
450+
}
451+
}
452+
446453
func (a *authToken) get() string {
447454
a.mu.RLock()
448455
defer a.mu.RUnlock()

0 commit comments

Comments
 (0)