@@ -138,14 +138,16 @@ func DefaultServerHandshakerOptions() *ServerHandshakerOptions {
138
138
// and server options (server options struct does not exist now. When
139
139
// caller can provide endpoints, it should be created.
140
140
141
- // altsHandshaker is used to complete a ALTS handshaking between client and
141
+ // altsHandshaker is used to complete an ALTS handshake between client and
142
142
// server. This handshaker talks to the ALTS handshaker service in the metadata
143
143
// server.
144
144
type altsHandshaker struct {
145
145
// RPC stream used to access the ALTS Handshaker service.
146
146
stream altsgrpc.HandshakerService_DoHandshakeClient
147
147
// the connection to the peer.
148
148
conn net.Conn
149
+ // a virtual connection to the ALTS handshaker service.
150
+ clientConn * grpc.ClientConn
149
151
// client handshake options.
150
152
clientOpts * ClientHandshakerOptions
151
153
// server handshake options.
@@ -154,39 +156,33 @@ type altsHandshaker struct {
154
156
side core.Side
155
157
}
156
158
157
- // NewClientHandshaker creates a ALTS handshaker for GCP which contains an RPC
158
- // stub created using the passed conn and used to talk to the ALTS Handshaker
159
+ // NewClientHandshaker creates a core.Handshaker that performs a client-side
160
+ // ALTS handshake by acting as a proxy between the peer and the ALTS handshaker
159
161
// service in the metadata server.
160
162
func NewClientHandshaker (ctx context.Context , conn * grpc.ClientConn , c net.Conn , opts * ClientHandshakerOptions ) (core.Handshaker , error ) {
161
- stream , err := altsgrpc .NewHandshakerServiceClient (conn ).DoHandshake (ctx )
162
- if err != nil {
163
- return nil , err
164
- }
165
163
return & altsHandshaker {
166
- stream : stream ,
164
+ stream : nil ,
167
165
conn : c ,
166
+ clientConn : conn ,
168
167
clientOpts : opts ,
169
168
side : core .ClientSide ,
170
169
}, nil
171
170
}
172
171
173
- // NewServerHandshaker creates a ALTS handshaker for GCP which contains an RPC
174
- // stub created using the passed conn and used to talk to the ALTS Handshaker
172
+ // NewServerHandshaker creates a core.Handshaker that performs a server-side
173
+ // ALTS handshake by acting as a proxy between the peer and the ALTS handshaker
175
174
// service in the metadata server.
176
175
func NewServerHandshaker (ctx context.Context , conn * grpc.ClientConn , c net.Conn , opts * ServerHandshakerOptions ) (core.Handshaker , error ) {
177
- stream , err := altsgrpc .NewHandshakerServiceClient (conn ).DoHandshake (ctx )
178
- if err != nil {
179
- return nil , err
180
- }
181
176
return & altsHandshaker {
182
- stream : stream ,
177
+ stream : nil ,
183
178
conn : c ,
179
+ clientConn : conn ,
184
180
serverOpts : opts ,
185
181
side : core .ServerSide ,
186
182
}, nil
187
183
}
188
184
189
- // ClientHandshake starts and completes a client ALTS handshaking for GCP. Once
185
+ // ClientHandshake starts and completes a client ALTS handshake for GCP. Once
190
186
// done, ClientHandshake returns a secure connection.
191
187
func (h * altsHandshaker ) ClientHandshake (ctx context.Context ) (net.Conn , credentials.AuthInfo , error ) {
192
188
if ! acquire () {
@@ -198,6 +194,16 @@ func (h *altsHandshaker) ClientHandshake(ctx context.Context) (net.Conn, credent
198
194
return nil , nil , errors .New ("only handshakers created using NewClientHandshaker can perform a client handshaker" )
199
195
}
200
196
197
+ // TODO(matthewstevenson88): Change unit tests to use public APIs so
198
+ // that h.stream can unconditionally be set based on h.clientConn.
199
+ if h .stream == nil {
200
+ stream , err := altsgrpc .NewHandshakerServiceClient (h .clientConn ).DoHandshake (ctx )
201
+ if err != nil {
202
+ return nil , nil , fmt .Errorf ("failed to establish stream to ALTS handshaker service: %v" , err )
203
+ }
204
+ h .stream = stream
205
+ }
206
+
201
207
// Create target identities from service account list.
202
208
targetIdentities := make ([]* altspb.Identity , 0 , len (h .clientOpts .TargetServiceAccounts ))
203
209
for _ , account := range h .clientOpts .TargetServiceAccounts {
@@ -229,7 +235,7 @@ func (h *altsHandshaker) ClientHandshake(ctx context.Context) (net.Conn, credent
229
235
return conn , authInfo , nil
230
236
}
231
237
232
- // ServerHandshake starts and completes a server ALTS handshaking for GCP. Once
238
+ // ServerHandshake starts and completes a server ALTS handshake for GCP. Once
233
239
// done, ServerHandshake returns a secure connection.
234
240
func (h * altsHandshaker ) ServerHandshake (ctx context.Context ) (net.Conn , credentials.AuthInfo , error ) {
235
241
if ! acquire () {
@@ -241,6 +247,16 @@ func (h *altsHandshaker) ServerHandshake(ctx context.Context) (net.Conn, credent
241
247
return nil , nil , errors .New ("only handshakers created using NewServerHandshaker can perform a server handshaker" )
242
248
}
243
249
250
+ // TODO(matthewstevenson88): Change unit tests to use public APIs so
251
+ // that h.stream can unconditionally be set based on h.clientConn.
252
+ if h .stream == nil {
253
+ stream , err := altsgrpc .NewHandshakerServiceClient (h .clientConn ).DoHandshake (ctx )
254
+ if err != nil {
255
+ return nil , nil , fmt .Errorf ("failed to establish stream to ALTS handshaker service: %v" , err )
256
+ }
257
+ h .stream = stream
258
+ }
259
+
244
260
p := make ([]byte , frameLimit )
245
261
n , err := h .conn .Read (p )
246
262
if err != nil {
0 commit comments