Skip to content

Commit 99430b6

Browse files
committed
refactor interop client
1 parent b1f860b commit 99430b6

File tree

1 file changed

+49
-22
lines changed

1 file changed

+49
-22
lines changed

Diff for: interop/client/client.go

+49-22
Original file line numberDiff line numberDiff line change
@@ -41,10 +41,9 @@ const (
4141
)
4242

4343
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)")
4847
customCredentialsType = flag.String("custom_credentials_type", "", "Custom creds to use, excluding TLS or ALTS")
4948
altsHSAddr = flag.String("alts_handshaker_service_address", "", "ALTS handshaker gRPC service address")
5049
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 (
7776
unimplemented_service: client attempts to call unimplemented service.`)
7877
)
7978

79+
type credsMode uint8
80+
81+
const (
82+
credsNone credsMode = iota
83+
credsTLS
84+
credsALTS
85+
credsGoogleDefaultCreds
86+
)
87+
8088
func main() {
8189
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
8596
}
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")
86112
serverAddr := net.JoinHostPort(*serverHost, strconv.Itoa(*serverPort))
87113
var opts []grpc.DialOption
88-
if *useTLS {
114+
switch credsChosen {
115+
case credsTLS:
89116
var sn string
90117
if *tlsServerName != "" {
91118
sn = *tlsServerName
@@ -104,19 +131,19 @@ func main() {
104131
creds = credentials.NewClientTLSFromCert(nil, sn)
105132
}
106133
opts = append(opts, grpc.WithTransportCredentials(creds))
107-
} else if *useALTS {
134+
case credsALTS:
108135
altsOpts := alts.DefaultClientOptions()
109136
if *altsHSAddr != "" {
110137
altsOpts.HandshakerServiceAddress = *altsHSAddr
111138
}
112139
altsTC := alts.NewClientCreds(altsOpts)
113140
opts = append(opts, grpc.WithTransportCredentials(altsTC))
114-
} else if *customCredentialsType == googleDefaultCredsName {
141+
case credsGoogleDefaultCreds:
115142
opts = append(opts, grpc.WithCredentialsBundle(google.NewDefaultCredentials()))
116-
} else {
143+
default:
117144
opts = append(opts, grpc.WithInsecure())
118145
}
119-
if *useTLS || *useALTS {
146+
if credsChosen == credsTLS || credsChosen == credsALTS {
120147
if *testCase == "compute_engine_creds" {
121148
opts = append(opts, grpc.WithPerRPCCredentials(oauth.NewComputeEngine()))
122149
} else if *testCase == "service_account_creds" {
@@ -165,32 +192,32 @@ func main() {
165192
interop.DoTimeoutOnSleepingServer(tc)
166193
grpclog.Infoln("TimeoutOnSleepingServer done")
167194
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.")
170197
}
171198
interop.DoComputeEngineCreds(tc, *defaultServiceAccount, *oauthScope)
172199
grpclog.Infoln("ComputeEngineCreds done")
173200
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.")
176203
}
177204
interop.DoServiceAccountCreds(tc, *serviceAccountKeyFile, *oauthScope)
178205
grpclog.Infoln("ServiceAccountCreds done")
179206
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.")
182209
}
183210
interop.DoJWTTokenCreds(tc, *serviceAccountKeyFile)
184211
grpclog.Infoln("JWTtokenCreds done")
185212
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.")
188215
}
189216
interop.DoPerRPCCreds(tc, *serviceAccountKeyFile, *oauthScope)
190217
grpclog.Infoln("PerRPCCreds done")
191218
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.")
194221
}
195222
interop.DoOauth2TokenCreds(tc, *serviceAccountKeyFile, *oauthScope)
196223
grpclog.Infoln("Oauth2TokenCreds done")

0 commit comments

Comments
 (0)