@@ -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.
7676var 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+
7882func 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+
151186func timeoutInterceptor () grpc.UnaryClientInterceptor {
152187 return func (
153188 ctx context.Context ,
0 commit comments