@@ -41,10 +41,9 @@ const (
41
41
)
42
42
43
43
var (
44
- caFile = flag .String ("ca_file" , "" , "The file containning the CA root cert file" )
45
- useTLS = flag .Bool ("use_tls" , false , "Connection uses TLS if true" )
46
- useALTS = flag .Bool ("use_alts" , false , "Connection uses ALTS if true (this option can only be used on GCP)" )
47
- // useGoogleDefaultCreds = flag.Bool("use_google_default_creds", false, "Uses google default creds if true")
44
+ caFile = flag .String ("ca_file" , "" , "The file containning the CA root cert file" )
45
+ useTLS = flag .Bool ("use_tls" , false , "Connection uses TLS if true" )
46
+ useALTS = flag .Bool ("use_alts" , false , "Connection uses ALTS if true (this option can only be used on GCP)" )
48
47
customCredentialsType = flag .String ("custom_credentials_type" , "" , "Custom creds to use, excluding TLS or ALTS" )
49
48
altsHSAddr = flag .String ("alts_handshaker_service_address" , "" , "ALTS handshaker gRPC service address" )
50
49
testCA = flag .Bool ("use_test_ca" , false , "Whether to replace platform root CAs with test CA as the CA root" )
@@ -77,15 +76,43 @@ var (
77
76
unimplemented_service: client attempts to call unimplemented service.` )
78
77
)
79
78
79
+ type credsMode uint8
80
+
81
+ const (
82
+ credsNone credsMode = iota
83
+ credsTLS
84
+ credsALTS
85
+ credsGoogleDefaultCreds
86
+ )
87
+
80
88
func main () {
81
89
flag .Parse ()
82
- resolver .SetDefaultScheme ("dns" )
83
- if * useTLS && * useALTS && * customCredentialsType != googleDefaultCredsName {
84
- grpclog .Fatalf ("use_tls, use_alts and use_google_default_creds cannot be all set to true" )
90
+ var useGDC bool // use google default creds
91
+ if * customCredentialsType != "" {
92
+ if * customCredentialsType != googleDefaultCredsName {
93
+ grpclog .Fatalf ("custom_credentials_type can only be set to %v or not set" , googleDefaultCredsName )
94
+ }
95
+ useGDC = true
85
96
}
97
+ if (* useTLS && * useALTS ) || (* useTLS && useGDC ) || (* useALTS && useGDC ) {
98
+ grpclog .Fatalf ("only one of TLS, ALTS and google default creds can be used" )
99
+ }
100
+
101
+ var credsChosen credsMode
102
+ switch {
103
+ case * useTLS :
104
+ credsChosen = credsTLS
105
+ case * useALTS :
106
+ credsChosen = credsALTS
107
+ case useGDC :
108
+ credsChosen = credsGoogleDefaultCreds
109
+ }
110
+
111
+ resolver .SetDefaultScheme ("dns" )
86
112
serverAddr := net .JoinHostPort (* serverHost , strconv .Itoa (* serverPort ))
87
113
var opts []grpc.DialOption
88
- if * useTLS {
114
+ switch credsChosen {
115
+ case credsTLS :
89
116
var sn string
90
117
if * tlsServerName != "" {
91
118
sn = * tlsServerName
@@ -104,19 +131,19 @@ func main() {
104
131
creds = credentials .NewClientTLSFromCert (nil , sn )
105
132
}
106
133
opts = append (opts , grpc .WithTransportCredentials (creds ))
107
- } else if * useALTS {
134
+ case credsALTS :
108
135
altsOpts := alts .DefaultClientOptions ()
109
136
if * altsHSAddr != "" {
110
137
altsOpts .HandshakerServiceAddress = * altsHSAddr
111
138
}
112
139
altsTC := alts .NewClientCreds (altsOpts )
113
140
opts = append (opts , grpc .WithTransportCredentials (altsTC ))
114
- } else if * customCredentialsType == googleDefaultCredsName {
141
+ case credsGoogleDefaultCreds :
115
142
opts = append (opts , grpc .WithCredentialsBundle (google .NewDefaultCredentials ()))
116
- } else {
143
+ default :
117
144
opts = append (opts , grpc .WithInsecure ())
118
145
}
119
- if * useTLS || * useALTS {
146
+ if credsChosen == credsTLS || credsChosen == credsALTS {
120
147
if * testCase == "compute_engine_creds" {
121
148
opts = append (opts , grpc .WithPerRPCCredentials (oauth .NewComputeEngine ()))
122
149
} else if * testCase == "service_account_creds" {
@@ -165,32 +192,32 @@ func main() {
165
192
interop .DoTimeoutOnSleepingServer (tc )
166
193
grpclog .Infoln ("TimeoutOnSleepingServer done" )
167
194
case "compute_engine_creds" :
168
- if ! * useTLS && ! * useALTS && * customCredentialsType != googleDefaultCredsName {
169
- grpclog .Fatalf ("Neither TLS or ALTS are enabled. TLS or ALTS is required to execute compute_engine_creds test case." )
195
+ if credsChosen == credsNone {
196
+ grpclog .Fatalf ("Credentials ( TLS, ALTS or google default creds) need to be set for compute_engine_creds test case." )
170
197
}
171
198
interop .DoComputeEngineCreds (tc , * defaultServiceAccount , * oauthScope )
172
199
grpclog .Infoln ("ComputeEngineCreds done" )
173
200
case "service_account_creds" :
174
- if ! * useTLS && ! * useALTS && * customCredentialsType != googleDefaultCredsName {
175
- grpclog .Fatalf ("Neither TLS or ALTS are enabled. TLS or ALTS is required to execute service_account_creds test case." )
201
+ if credsChosen == credsNone {
202
+ grpclog .Fatalf ("Credentials ( TLS, ALTS or google default creds) need to be set for service_account_creds test case." )
176
203
}
177
204
interop .DoServiceAccountCreds (tc , * serviceAccountKeyFile , * oauthScope )
178
205
grpclog .Infoln ("ServiceAccountCreds done" )
179
206
case "jwt_token_creds" :
180
- if ! * useTLS && ! * useALTS && * customCredentialsType != googleDefaultCredsName {
181
- grpclog .Fatalf ("Neither TLS or ALTS are enabled. TLS or ALTS is required to execute jwt_token_creds test case." )
207
+ if credsChosen == credsNone {
208
+ grpclog .Fatalf ("Credentials ( TLS, ALTS or google default creds) need to be set for jwt_token_creds test case." )
182
209
}
183
210
interop .DoJWTTokenCreds (tc , * serviceAccountKeyFile )
184
211
grpclog .Infoln ("JWTtokenCreds done" )
185
212
case "per_rpc_creds" :
186
- if ! * useTLS && ! * useALTS && * customCredentialsType != googleDefaultCredsName {
187
- grpclog .Fatalf ("Neither TLS or ALTS are enabled. TLS or ALTS is required to execute per_rpc_creds test case." )
213
+ if credsChosen == credsNone {
214
+ grpclog .Fatalf ("Credentials ( TLS, ALTS or google default creds) need to be set for per_rpc_creds test case." )
188
215
}
189
216
interop .DoPerRPCCreds (tc , * serviceAccountKeyFile , * oauthScope )
190
217
grpclog .Infoln ("PerRPCCreds done" )
191
218
case "oauth2_auth_token" :
192
- if ! * useTLS && ! * useALTS && * customCredentialsType != googleDefaultCredsName {
193
- grpclog .Fatalf ("Neither TLS or ALTS are enabled. TLS or ALTS is required to execute oauth2_auth_token test case." )
219
+ if credsChosen == credsNone {
220
+ grpclog .Fatalf ("Credentials ( TLS, ALTS or google default creds) need to be set for oauth2_auth_token test case." )
194
221
}
195
222
interop .DoOauth2TokenCreds (tc , * serviceAccountKeyFile , * oauthScope )
196
223
grpclog .Infoln ("Oauth2TokenCreds done" )
0 commit comments