Skip to content

Commit f1f43a9

Browse files
committed
Handle authtokens in the go code same as ts
1 parent 90673b6 commit f1f43a9

File tree

3 files changed

+39
-13
lines changed

3 files changed

+39
-13
lines changed

modal-go/client.go

Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ var client pb.ModalClientClient
7575
// The us-east client talks to the control plane; all other clients talk to input planes.
7676
var clients = map[string]pb.ModalClientClient{}
7777

78+
// authToken is the auth token received from the control plane on the first request, and sent with all
79+
// subsequent requests to both the control plane and the input plane.
80+
var authToken string
81+
7882
func init() {
7983
var err error
8084
defaultConfig, _ = readConfigFile()
@@ -126,6 +130,7 @@ func createClient(serverURL string) (*grpc.ClientConn, pb.ModalClientClient, err
126130
grpc.MaxCallSendMsgSize(maxMessageSize),
127131
),
128132
grpc.WithChainUnaryInterceptor(
133+
authTokenInterceptor(),
129134
retryInterceptor(),
130135
timeoutInterceptor(),
131136
),
@@ -148,6 +153,36 @@ func clientContext(ctx context.Context) context.Context {
148153
)
149154
}
150155

156+
// We receive an auth token from the control plane on our first request. We then include that auth token in every
157+
// subsequent request to both the control plane and the input plane.
158+
func authTokenInterceptor() grpc.UnaryClientInterceptor {
159+
return func(
160+
ctx context.Context,
161+
method string,
162+
req, reply any,
163+
cc *grpc.ClientConn,
164+
inv grpc.UnaryInvoker,
165+
opts ...grpc.CallOption,
166+
) error {
167+
var headers, trailers metadata.MD
168+
// Add authToken to outgoing context if it's set
169+
if authToken != "" {
170+
ctx = metadata.AppendToOutgoingContext(ctx, "x-modal-auth-token", authToken)
171+
}
172+
opts = append(opts, grpc.Header(&headers), grpc.Trailer(&trailers))
173+
err := inv(ctx, method, req, reply, cc, opts...)
174+
// If we're talking to the control plane, and no auth token was sent, it will return one.
175+
// The python server returns it in the trailers, the worker returns it in the headers.
176+
if val, ok := headers["x-modal-auth-token"]; ok {
177+
authToken = val[0]
178+
} else if val, ok := trailers["x-modal-auth-token"]; ok {
179+
authToken = val[0]
180+
}
181+
182+
return err
183+
}
184+
}
185+
151186
func timeoutInterceptor() grpc.UnaryClientInterceptor {
152187
return func(
153188
ctx context.Context,

modal-go/function.go

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,6 @@ import (
1010
"encoding/base64"
1111
"errors"
1212
"fmt"
13-
"google.golang.org/grpc"
14-
"google.golang.org/grpc/metadata"
1513
"net/http"
1614
"time"
1715

@@ -49,13 +47,12 @@ func FunctionLookup(ctx context.Context, appName string, name string, options *L
4947
}
5048
ctx = clientContext(ctx)
5149

52-
var header, trailer metadata.MD
5350
resp, err := client.FunctionGet(ctx, pb.FunctionGetRequest_builder{
5451
AppName: appName,
5552
ObjectTag: name,
5653
Namespace: pb.DeploymentNamespace_DEPLOYMENT_NAMESPACE_WORKSPACE,
5754
EnvironmentName: environmentName(options.Environment),
58-
}.Build(), grpc.Header(&header), grpc.Trailer(&trailer))
55+
}.Build())
5956

6057
if status, ok := status.FromError(err); ok && status.Code() == codes.NotFound {
6158
return nil, NotFoundError{fmt.Sprintf("function '%s/%s' not found", appName, name)}
@@ -64,15 +61,6 @@ func FunctionLookup(ctx context.Context, appName string, name string, options *L
6461
return nil, err
6562
}
6663

67-
// Attach x-modal-auth-token to all future requests.
68-
authTokenArray := header.Get("x-modal-auth-token")
69-
if len(authTokenArray) == 0 {
70-
authTokenArray = trailer.Get("x-modal-auth-token")
71-
}
72-
if len(authTokenArray) > 0 {
73-
authToken := authTokenArray[0]
74-
ctx = metadata.AppendToOutgoingContext(ctx, "x-modal-auth-token", authToken)
75-
}
7664
var inputPlaneUrl *string
7765
if meta := resp.GetHandleMetadata(); meta != nil {
7866
if url := meta.GetInputPlaneUrl(); url != "" {

modal-js/src/client.ts

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,9 @@ function authMiddleware(profile: Profile): ClientMiddleware {
3333
options.metadata.set("x-modal-auth-token", modalAuthToken);
3434
}
3535

36+
// We receive an auth token from the control plane on our first request. We then include that auth token in every
37+
// subsequent request to both the control plane and the input plane. The python server returns it in the trailers,
38+
// the worker returns it in the headers.
3639
const prevOnHeader = options.onHeader;
3740
options.onHeader = (header) => {
3841
const token = header.get("x-modal-auth-token");

0 commit comments

Comments
 (0)