@@ -170,7 +170,6 @@ type Dialer struct {
170170	// network. By default it is golang.org/x/net/proxy#Dial. 
171171	dialFunc  func (cxt  context.Context , network , addr  string ) (net.Conn , error )
172172
173- 	useIAMAuthN     bool 
174173	iamTokenSource  oauth2.TokenSource 
175174	userAgent       string 
176175
@@ -201,6 +200,7 @@ func NewDialer(ctx context.Context, opts ...Option) (*Dialer, error) {
201200	dialCfg  :=  dialCfg {
202201		ipType :       alloydb .PrivateIP ,
203202		tcpKeepAlive : defaultTCPKeepAlive ,
203+ 		iamAuthN :     cfg .useIAMAuthN , // Use dialer configuration as default 
204204	}
205205	for  _ , opt  :=  range  cfg .dialOpts  {
206206		opt (& dialCfg )
@@ -233,7 +233,6 @@ func NewDialer(ctx context.Context, opts ...Option) (*Dialer, error) {
233233		dialerID :                dialerID ,
234234		metricRecorders :         map [alloydb.InstanceURI ]telv2.MetricRecorder {},
235235		dialFunc :                cfg .dialFunc ,
236- 		useIAMAuthN :             cfg .useIAMAuthN ,
237236		iamTokenSource :          cfg .tokenSource ,
238237		userAgent :               userAgent ,
239238		buffer :                  newBuffer (),
@@ -271,6 +270,10 @@ func (d *Dialer) Dial(ctx context.Context, instance string, opts ...DialOption)
271270		return  nil , ErrDialerClosed 
272271	default :
273272	}
273+ 	cfg  :=  d .defaultDialCfg 
274+ 	for  _ , opt  :=  range  opts  {
275+ 		opt (& cfg )
276+ 	}
274277
275278	inst , err  :=  alloydb .ParseInstURI (instance )
276279	if  err  !=  nil  {
@@ -282,7 +285,7 @@ func (d *Dialer) Dial(ctx context.Context, instance string, opts ...DialOption)
282285		startTime  =  time .Now ()
283286		endDial    tel.EndSpanFunc 
284287		attrs      =  telv2.Attributes {
285- 			IAMAuthN :    d . useIAMAuthN ,
288+ 			IAMAuthN :    cfg . iamAuthN ,
286289			UserAgent :   d .userAgent ,
287290			RefreshType : telv2 .RefreshAheadType ,
288291		}
@@ -300,11 +303,6 @@ func (d *Dialer) Dial(ctx context.Context, instance string, opts ...DialOption)
300303		endDial (err )
301304	}()
302305
303- 	cfg  :=  d .defaultDialCfg 
304- 	for  _ , opt  :=  range  opts  {
305- 		opt (& cfg )
306- 	}
307- 
308306	var  endInfo  tel.EndSpanFunc 
309307	ctx , endInfo  =  tel .StartSpan (ctx , "cloud.google.com/go/alloydbconn/internal.InstanceInfo" )
310308	cache , cacheHit , err  :=  d .connectionInfoCache (ctx , inst , mr )
@@ -400,7 +398,7 @@ func (d *Dialer) Dial(ctx context.Context, instance string, opts ...DialOption)
400398	if  ! d .disableMetadataExchange  {
401399		// The metadata exchange must occur after the TLS connection is established 
402400		// to avoid leaking sensitive information. 
403- 		err  =  d .metadataExchange (tlsConn )
401+ 		err  =  d .metadataExchange (tlsConn ,  cfg . iamAuthN )
404402		if  err  !=  nil  {
405403			_  =  tlsConn .Close () // best effort close attempt 
406404			attrs .DialStatus  =  telv2 .DialMDXError 
@@ -480,13 +478,13 @@ func invalidClientCert(
480478//     metadata exchange has succeeded and the connection is complete. 
481479// 
482480// Subsequent interactions with the server use the database protocol. 
483- func  (d  * Dialer ) metadataExchange (conn  net.Conn ) error  {
481+ func  (d  * Dialer ) metadataExchange (conn  net.Conn ,  useIAMAuthN   bool ) error  {
484482	tok , err  :=  d .iamTokenSource .Token ()
485483	if  err  !=  nil  {
486484		return  err 
487485	}
488486	authType  :=  connectorspb .MetadataExchangeRequest_DB_NATIVE 
489- 	if  d . useIAMAuthN  {
487+ 	if  useIAMAuthN  {
490488		authType  =  connectorspb .MetadataExchangeRequest_AUTO_IAM 
491489	}
492490	req  :=  & connectorspb.MetadataExchangeRequest {
0 commit comments