21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
36 #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
37 #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
38 #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
39 #define CRL_DIR_PATH "test/core/tsi/test_creds/crl_data"
44 constexpr
const char* kRootCertContents =
"root_cert_contents";
46 constexpr
const char* kIdentityCertPrivateKey =
"identity_private_key";
47 constexpr
const char* kIdentityCertContents =
"identity_cert_contents";
49 using ::grpc::experimental::ExternalCertificateVerifier;
50 using ::grpc::experimental::FileWatcherCertificateProvider;
51 using ::grpc::experimental::HostNameCertificateVerifier;
52 using ::grpc::experimental::StaticDataCertificateProvider;
60 TEST(CredentialsTest, InvalidGoogleRefreshToken) {
65 TEST(CredentialsTest, DefaultCredentials) {
72 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
73 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
74 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
75 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
76 "token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/"
77 "generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":"
78 "\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":"
79 "\"access_token\"}},\"quota_project_id\":\"quota_"
80 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
83 {
"scope1",
"scope2"});
87 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
88 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
89 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
90 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
91 "token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"},"
92 "\"quota_project_id\":\"quota_"
93 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
96 {
"scope1",
"scope2"});
100 "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
101 "token_type\":\"subject_token_type\",\"service_account_impersonation_"
102 "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
103 "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
104 "token_info\",\"credential_source\":{\"environment_id\":\"aws1\","
105 "\"region_url\":\"https://foo.com:5555/region_url\",\"url\":\"https://"
106 "foo.com:5555/url\",\"regional_cred_verification_url\":\"https://"
107 "foo.com:5555/regional_cred_verification_url_{region}\"},"
108 "\"quota_project_id\":\"quota_"
109 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
112 {
"scope1",
"scope2"});
116 TEST(CredentialsTest, StsCredentialsOptionsCppToCore) {
118 options.token_exchange_service_uri =
"https://foo.com/exchange";
123 options.subject_token_path =
"/foo/bar";
124 options.subject_token_type =
"nice_token_type";
125 options.actor_token_path =
"/foo/baz";
126 options.actor_token_type =
"even_nicer_token_type";
141 TEST(CredentialsTest, StsCredentialsOptionsJson) {
142 const char valid_json[] = R
"(
144 "token_exchange_service_uri": "https://foo/exchange",
145 "resource": "resource",
146 "audience": "audience",
148 "requested_token_type": "requested_token_type",
149 "subject_token_path": "subject_token_path",
150 "subject_token_type": "subject_token_type",
151 "actor_token_path": "actor_token_path",
152 "actor_token_type": "actor_token_type"
168 const char minimum_valid_json[] = R
"(
170 "token_exchange_service_uri": "https://foo/exchange",
171 "subject_token_path": "subject_token_path",
172 "subject_token_type": "subject_token_type"
188 I'm not a valid JSON.
195 const char invalid_json_missing_subject_token_type[] = R
"(
197 "token_exchange_service_uri": "https://foo/exchange",
198 "subject_token_path": "subject_token_path"
201 invalid_json_missing_subject_token_type, &
options);
208 "token_exchange_service_uri": "https://foo/exchange",
209 "subject_token_type": "subject_token_type"
219 "subject_token_path": "subject_token_path",
220 "subject_token_type": "subject_token_type"
237 const char valid_json[] = R
"(
239 "token_exchange_service_uri": "https://foo/exchange",
240 "subject_token_path": "subject_token_path",
241 "subject_token_type": "subject_token_type"
248 fwrite(valid_json, 1,
sizeof(valid_json),
creds_file));
268 TEST(CredentialsTest, TlsChannelCredentialsWithDefaultRootsAndDefaultVerifier) {
270 options.set_verify_server_certs(
true);
272 GPR_ASSERT(channel_credentials.get() !=
nullptr);
277 TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity) {
278 experimental::IdentityKeyCertPair key_cert_pair;
279 key_cert_pair.private_key = kIdentityCertPrivateKey;
280 key_cert_pair.certificate_chain = kIdentityCertContents;
283 auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
286 options.set_certificate_provider(certificate_provider);
289 options.watch_identity_key_cert_pairs();
292 GPR_ASSERT(channel_credentials.get() !=
nullptr);
296 TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootOnly) {
297 auto certificate_provider =
298 std::make_shared<StaticDataCertificateProvider>(kRootCertContents);
300 GPR_ASSERT(certificate_provider->c_provider() !=
nullptr);
302 options.set_certificate_provider(certificate_provider);
306 GPR_ASSERT(channel_credentials.get() !=
nullptr);
311 TlsChannelCredentialsWithDefaultRootsAndStaticDataCertificateProviderLoadingIdentityOnly) {
312 experimental::IdentityKeyCertPair key_cert_pair;
313 key_cert_pair.private_key = kIdentityCertPrivateKey;
314 key_cert_pair.certificate_chain = kIdentityCertContents;
317 auto certificate_provider =
320 options.set_certificate_provider(certificate_provider);
321 options.watch_identity_key_cert_pairs();
324 GPR_ASSERT(channel_credentials.get() !=
nullptr);
329 TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootAndIdentity) {
330 auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
333 options.set_certificate_provider(certificate_provider);
336 options.watch_identity_key_cert_pairs();
339 GPR_ASSERT(channel_credentials.get() !=
nullptr);
343 TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootOnly) {
344 auto certificate_provider =
345 std::make_shared<FileWatcherCertificateProvider>(
CA_CERT_PATH, 1);
347 options.set_certificate_provider(certificate_provider);
351 GPR_ASSERT(channel_credentials.get() !=
nullptr);
354 TEST(CredentialsTest, TlsChannelCredentialsWithHostNameVerifier) {
355 auto verifier = std::make_shared<HostNameCertificateVerifier>();
357 options.set_verify_server_certs(
true);
360 GPR_ASSERT(channel_credentials.get() !=
nullptr);
363 TEST(CredentialsTest, TlsChannelCredentialsWithSyncExternalVerifier) {
365 ExternalCertificateVerifier::Create<SyncCertificateVerifier>(
true);
367 options.set_verify_server_certs(
true);
369 options.set_check_call_host(
false);
371 GPR_ASSERT(channel_credentials.get() !=
nullptr);
374 TEST(CredentialsTest, TlsChannelCredentialsWithAsyncExternalVerifier) {
376 ExternalCertificateVerifier::Create<AsyncCertificateVerifier>(
true);
378 options.set_verify_server_certs(
true);
380 options.set_check_call_host(
false);
382 GPR_ASSERT(channel_credentials.get() !=
nullptr);
385 TEST(CredentialsTest, TlsChannelCredentialsWithCrlDirectory) {
386 auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
389 options.set_certificate_provider(certificate_provider);
392 options.watch_identity_key_cert_pairs();
396 GPR_ASSERT(channel_credentials.get() !=
nullptr);
403 int main(
int argc,
char** argv) {