credentials_test.cc 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407
  1. /*
  2. *
  3. * Copyright 2015 gRPC authors.
  4. *
  5. * Licensed under the Apache License, Version 2.0 (the "License");
  6. * you may not use this file except in compliance with the License.
  7. * You may obtain a copy of the License at
  8. *
  9. * http://www.apache.org/licenses/LICENSE-2.0
  10. *
  11. * Unless required by applicable law or agreed to in writing, software
  12. * distributed under the License is distributed on an "AS IS" BASIS,
  13. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. * See the License for the specific language governing permissions and
  15. * limitations under the License.
  16. *
  17. */
  18. #include <memory>
  19. #include <gmock/gmock.h>
  20. #include <gtest/gtest.h>
  21. #include <grpc/grpc.h>
  22. #include <grpc/grpc_security.h>
  23. #include <grpcpp/security/credentials.h>
  24. #include <grpcpp/security/server_credentials.h>
  25. #include <grpcpp/security/tls_credentials_options.h>
  26. #include <grpcpp/server_builder.h>
  27. #include "src/core/lib/gpr/env.h"
  28. #include "src/core/lib/gpr/tmpfile.h"
  29. #include "src/cpp/client/secure_credentials.h"
  30. #include "test/cpp/util/tls_test_utils.h"
  31. #define CA_CERT_PATH "src/core/tsi/test_creds/ca.pem"
  32. #define SERVER_CERT_PATH "src/core/tsi/test_creds/server1.pem"
  33. #define SERVER_KEY_PATH "src/core/tsi/test_creds/server1.key"
  34. #define CRL_DIR_PATH "test/core/tsi/test_creds/crl_data"
  35. namespace {
  36. constexpr const char* kRootCertName = "root_cert_name";
  37. constexpr const char* kRootCertContents = "root_cert_contents";
  38. constexpr const char* kIdentityCertName = "identity_cert_name";
  39. constexpr const char* kIdentityCertPrivateKey = "identity_private_key";
  40. constexpr const char* kIdentityCertContents = "identity_cert_contents";
  41. using ::grpc::experimental::ExternalCertificateVerifier;
  42. using ::grpc::experimental::FileWatcherCertificateProvider;
  43. using ::grpc::experimental::HostNameCertificateVerifier;
  44. using ::grpc::experimental::StaticDataCertificateProvider;
  45. } // namespace
  46. namespace grpc {
  47. namespace testing {
  48. namespace {
  49. TEST(CredentialsTest, InvalidGoogleRefreshToken) {
  50. std::shared_ptr<CallCredentials> bad1 = GoogleRefreshTokenCredentials("");
  51. EXPECT_EQ(static_cast<CallCredentials*>(nullptr), bad1.get());
  52. }
  53. TEST(CredentialsTest, DefaultCredentials) {
  54. auto creds = GoogleDefaultCredentials();
  55. }
  56. TEST(CredentialsTest, ExternalAccountCredentials) {
  57. // url credentials
  58. std::string url_options_string(
  59. "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
  60. "token_type\":\"subject_token_type\",\"service_account_impersonation_"
  61. "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
  62. "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
  63. "token_info\",\"credential_source\":{\"url\":\"https://foo.com:5555/"
  64. "generate_subject_token_format_json\",\"headers\":{\"Metadata-Flavor\":"
  65. "\"Google\"},\"format\":{\"type\":\"json\",\"subject_token_field_name\":"
  66. "\"access_token\"}},\"quota_project_id\":\"quota_"
  67. "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
  68. "secret\"}");
  69. auto url_creds = grpc::ExternalAccountCredentials(url_options_string,
  70. {"scope1", "scope2"});
  71. EXPECT_TRUE(url_creds != nullptr);
  72. // file credentials
  73. std::string file_options_string(
  74. "{\"type\":\"external_account\",\"audience\":\"audience\",\"subject_"
  75. "token_type\":\"subject_token_type\",\"service_account_impersonation_"
  76. "url\":\"service_account_impersonation_url\",\"token_url\":\"https://"
  77. "foo.com:5555/token\",\"token_info_url\":\"https://foo.com:5555/"
  78. "token_info\",\"credential_source\":{\"file\":\"credentials_file_path\"},"
  79. "\"quota_project_id\":\"quota_"
  80. "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
  81. "secret\"}");
  82. auto file_creds = grpc::ExternalAccountCredentials(file_options_string,
  83. {"scope1", "scope2"});
  84. EXPECT_TRUE(file_creds != nullptr);
  85. // aws credentials
  86. std::string aws_options_string(
  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\":{\"environment_id\":\"aws1\","
  92. "\"region_url\":\"https://foo.com:5555/region_url\",\"url\":\"https://"
  93. "foo.com:5555/url\",\"regional_cred_verification_url\":\"https://"
  94. "foo.com:5555/regional_cred_verification_url_{region}\"},"
  95. "\"quota_project_id\":\"quota_"
  96. "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
  97. "secret\"}");
  98. auto aws_creds = grpc::ExternalAccountCredentials(aws_options_string,
  99. {"scope1", "scope2"});
  100. EXPECT_TRUE(aws_creds != nullptr);
  101. }
  102. TEST(CredentialsTest, StsCredentialsOptionsCppToCore) {
  103. grpc::experimental::StsCredentialsOptions options;
  104. options.token_exchange_service_uri = "https://foo.com/exchange";
  105. options.resource = "resource";
  106. options.audience = "audience";
  107. options.scope = "scope";
  108. // options.requested_token_type explicitly not set.
  109. options.subject_token_path = "/foo/bar";
  110. options.subject_token_type = "nice_token_type";
  111. options.actor_token_path = "/foo/baz";
  112. options.actor_token_type = "even_nicer_token_type";
  113. grpc_sts_credentials_options core_opts =
  114. grpc::experimental::StsCredentialsCppToCoreOptions(options);
  115. EXPECT_EQ(options.token_exchange_service_uri,
  116. core_opts.token_exchange_service_uri);
  117. EXPECT_EQ(options.resource, core_opts.resource);
  118. EXPECT_EQ(options.audience, core_opts.audience);
  119. EXPECT_EQ(options.scope, core_opts.scope);
  120. EXPECT_EQ(options.requested_token_type, core_opts.requested_token_type);
  121. EXPECT_EQ(options.subject_token_path, core_opts.subject_token_path);
  122. EXPECT_EQ(options.subject_token_type, core_opts.subject_token_type);
  123. EXPECT_EQ(options.actor_token_path, core_opts.actor_token_path);
  124. EXPECT_EQ(options.actor_token_type, core_opts.actor_token_type);
  125. }
  126. TEST(CredentialsTest, StsCredentialsOptionsJson) {
  127. const char valid_json[] = R"(
  128. {
  129. "token_exchange_service_uri": "https://foo/exchange",
  130. "resource": "resource",
  131. "audience": "audience",
  132. "scope": "scope",
  133. "requested_token_type": "requested_token_type",
  134. "subject_token_path": "subject_token_path",
  135. "subject_token_type": "subject_token_type",
  136. "actor_token_path": "actor_token_path",
  137. "actor_token_type": "actor_token_type"
  138. })";
  139. grpc::experimental::StsCredentialsOptions options;
  140. EXPECT_TRUE(
  141. grpc::experimental::StsCredentialsOptionsFromJson(valid_json, &options)
  142. .ok());
  143. EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
  144. EXPECT_EQ(options.resource, "resource");
  145. EXPECT_EQ(options.audience, "audience");
  146. EXPECT_EQ(options.scope, "scope");
  147. EXPECT_EQ(options.requested_token_type, "requested_token_type");
  148. EXPECT_EQ(options.subject_token_path, "subject_token_path");
  149. EXPECT_EQ(options.subject_token_type, "subject_token_type");
  150. EXPECT_EQ(options.actor_token_path, "actor_token_path");
  151. EXPECT_EQ(options.actor_token_type, "actor_token_type");
  152. const char minimum_valid_json[] = R"(
  153. {
  154. "token_exchange_service_uri": "https://foo/exchange",
  155. "subject_token_path": "subject_token_path",
  156. "subject_token_type": "subject_token_type"
  157. })";
  158. EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(
  159. minimum_valid_json, &options)
  160. .ok());
  161. EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
  162. EXPECT_EQ(options.resource, "");
  163. EXPECT_EQ(options.audience, "");
  164. EXPECT_EQ(options.scope, "");
  165. EXPECT_EQ(options.requested_token_type, "");
  166. EXPECT_EQ(options.subject_token_path, "subject_token_path");
  167. EXPECT_EQ(options.subject_token_type, "subject_token_type");
  168. EXPECT_EQ(options.actor_token_path, "");
  169. EXPECT_EQ(options.actor_token_type, "");
  170. const char invalid_json[] = R"(
  171. I'm not a valid JSON.
  172. )";
  173. EXPECT_EQ(
  174. grpc::StatusCode::INVALID_ARGUMENT,
  175. grpc::experimental::StsCredentialsOptionsFromJson(invalid_json, &options)
  176. .error_code());
  177. const char invalid_json_missing_subject_token_type[] = R"(
  178. {
  179. "token_exchange_service_uri": "https://foo/exchange",
  180. "subject_token_path": "subject_token_path"
  181. })";
  182. auto status = grpc::experimental::StsCredentialsOptionsFromJson(
  183. invalid_json_missing_subject_token_type, &options);
  184. EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
  185. EXPECT_THAT(status.error_message(),
  186. ::testing::HasSubstr("subject_token_type"));
  187. const char invalid_json_missing_subject_token_path[] = R"(
  188. {
  189. "token_exchange_service_uri": "https://foo/exchange",
  190. "subject_token_type": "subject_token_type"
  191. })";
  192. status = grpc::experimental::StsCredentialsOptionsFromJson(
  193. invalid_json_missing_subject_token_path, &options);
  194. EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
  195. EXPECT_THAT(status.error_message(),
  196. ::testing::HasSubstr("subject_token_path"));
  197. const char invalid_json_missing_token_exchange_uri[] = R"(
  198. {
  199. "subject_token_path": "subject_token_path",
  200. "subject_token_type": "subject_token_type"
  201. })";
  202. status = grpc::experimental::StsCredentialsOptionsFromJson(
  203. invalid_json_missing_token_exchange_uri, &options);
  204. EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
  205. EXPECT_THAT(status.error_message(),
  206. ::testing::HasSubstr("token_exchange_service_uri"));
  207. }
  208. TEST(CredentialsTest, StsCredentialsOptionsFromEnv) {
  209. // Unset env and check expected failure.
  210. gpr_unsetenv("STS_CREDENTIALS");
  211. grpc::experimental::StsCredentialsOptions options;
  212. auto status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
  213. EXPECT_EQ(grpc::StatusCode::NOT_FOUND, status.error_code());
  214. // Set env and check for success.
  215. const char valid_json[] = R"(
  216. {
  217. "token_exchange_service_uri": "https://foo/exchange",
  218. "subject_token_path": "subject_token_path",
  219. "subject_token_type": "subject_token_type"
  220. })";
  221. char* creds_file_name;
  222. FILE* creds_file = gpr_tmpfile("sts_creds_options", &creds_file_name);
  223. ASSERT_NE(creds_file_name, nullptr);
  224. ASSERT_NE(creds_file, nullptr);
  225. ASSERT_EQ(sizeof(valid_json),
  226. fwrite(valid_json, 1, sizeof(valid_json), creds_file));
  227. fclose(creds_file);
  228. gpr_setenv("STS_CREDENTIALS", creds_file_name);
  229. gpr_free(creds_file_name);
  230. status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
  231. EXPECT_TRUE(status.ok());
  232. EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
  233. EXPECT_EQ(options.resource, "");
  234. EXPECT_EQ(options.audience, "");
  235. EXPECT_EQ(options.scope, "");
  236. EXPECT_EQ(options.requested_token_type, "");
  237. EXPECT_EQ(options.subject_token_path, "subject_token_path");
  238. EXPECT_EQ(options.subject_token_type, "subject_token_type");
  239. EXPECT_EQ(options.actor_token_path, "");
  240. EXPECT_EQ(options.actor_token_type, "");
  241. // Cleanup.
  242. gpr_unsetenv("STS_CREDENTIALS");
  243. }
  244. TEST(CredentialsTest, TlsChannelCredentialsWithDefaultRootsAndDefaultVerifier) {
  245. grpc::experimental::TlsChannelCredentialsOptions options;
  246. options.set_verify_server_certs(true);
  247. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  248. GPR_ASSERT(channel_credentials.get() != nullptr);
  249. }
  250. TEST(
  251. CredentialsTest,
  252. TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity) {
  253. experimental::IdentityKeyCertPair key_cert_pair;
  254. key_cert_pair.private_key = kIdentityCertPrivateKey;
  255. key_cert_pair.certificate_chain = kIdentityCertContents;
  256. std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
  257. identity_key_cert_pairs.emplace_back(key_cert_pair);
  258. auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
  259. kRootCertContents, identity_key_cert_pairs);
  260. grpc::experimental::TlsChannelCredentialsOptions options;
  261. options.set_certificate_provider(certificate_provider);
  262. options.watch_root_certs();
  263. options.set_root_cert_name(kRootCertName);
  264. options.watch_identity_key_cert_pairs();
  265. options.set_identity_cert_name(kIdentityCertName);
  266. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  267. GPR_ASSERT(channel_credentials.get() != nullptr);
  268. }
  269. TEST(CredentialsTest,
  270. TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootOnly) {
  271. auto certificate_provider =
  272. std::make_shared<StaticDataCertificateProvider>(kRootCertContents);
  273. GPR_ASSERT(certificate_provider != nullptr);
  274. GPR_ASSERT(certificate_provider->c_provider() != nullptr);
  275. grpc::experimental::TlsChannelCredentialsOptions options;
  276. options.set_certificate_provider(certificate_provider);
  277. options.watch_root_certs();
  278. options.set_root_cert_name(kRootCertName);
  279. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  280. GPR_ASSERT(channel_credentials.get() != nullptr);
  281. }
  282. TEST(
  283. CredentialsTest,
  284. TlsChannelCredentialsWithDefaultRootsAndStaticDataCertificateProviderLoadingIdentityOnly) {
  285. experimental::IdentityKeyCertPair key_cert_pair;
  286. key_cert_pair.private_key = kIdentityCertPrivateKey;
  287. key_cert_pair.certificate_chain = kIdentityCertContents;
  288. std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
  289. identity_key_cert_pairs.emplace_back(key_cert_pair);
  290. auto certificate_provider =
  291. std::make_shared<StaticDataCertificateProvider>(identity_key_cert_pairs);
  292. grpc::experimental::TlsChannelCredentialsOptions options;
  293. options.set_certificate_provider(certificate_provider);
  294. options.watch_identity_key_cert_pairs();
  295. options.set_identity_cert_name(kIdentityCertName);
  296. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  297. GPR_ASSERT(channel_credentials.get() != nullptr);
  298. }
  299. TEST(
  300. CredentialsTest,
  301. TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootAndIdentity) {
  302. auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
  303. SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1);
  304. grpc::experimental::TlsChannelCredentialsOptions options;
  305. options.set_certificate_provider(certificate_provider);
  306. options.watch_root_certs();
  307. options.set_root_cert_name(kRootCertName);
  308. options.watch_identity_key_cert_pairs();
  309. options.set_identity_cert_name(kIdentityCertName);
  310. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  311. GPR_ASSERT(channel_credentials.get() != nullptr);
  312. }
  313. TEST(CredentialsTest,
  314. TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootOnly) {
  315. auto certificate_provider =
  316. std::make_shared<FileWatcherCertificateProvider>(CA_CERT_PATH, 1);
  317. grpc::experimental::TlsChannelCredentialsOptions options;
  318. options.set_certificate_provider(certificate_provider);
  319. options.watch_root_certs();
  320. options.set_root_cert_name(kRootCertName);
  321. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  322. GPR_ASSERT(channel_credentials.get() != nullptr);
  323. }
  324. TEST(CredentialsTest, TlsChannelCredentialsWithHostNameVerifier) {
  325. auto verifier = std::make_shared<HostNameCertificateVerifier>();
  326. grpc::experimental::TlsChannelCredentialsOptions options;
  327. options.set_verify_server_certs(true);
  328. options.set_certificate_verifier(verifier);
  329. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  330. GPR_ASSERT(channel_credentials.get() != nullptr);
  331. }
  332. TEST(CredentialsTest, TlsChannelCredentialsWithSyncExternalVerifier) {
  333. auto verifier =
  334. ExternalCertificateVerifier::Create<SyncCertificateVerifier>(true);
  335. grpc::experimental::TlsChannelCredentialsOptions options;
  336. options.set_verify_server_certs(true);
  337. options.set_certificate_verifier(verifier);
  338. options.set_check_call_host(false);
  339. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  340. GPR_ASSERT(channel_credentials.get() != nullptr);
  341. }
  342. TEST(CredentialsTest, TlsChannelCredentialsWithAsyncExternalVerifier) {
  343. auto verifier =
  344. ExternalCertificateVerifier::Create<AsyncCertificateVerifier>(true);
  345. grpc::experimental::TlsChannelCredentialsOptions options;
  346. options.set_verify_server_certs(true);
  347. options.set_certificate_verifier(verifier);
  348. options.set_check_call_host(false);
  349. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  350. GPR_ASSERT(channel_credentials.get() != nullptr);
  351. }
  352. TEST(CredentialsTest, TlsChannelCredentialsWithCrlDirectory) {
  353. auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
  354. SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1);
  355. grpc::experimental::TlsChannelCredentialsOptions options;
  356. options.set_certificate_provider(certificate_provider);
  357. options.watch_root_certs();
  358. options.set_root_cert_name(kRootCertName);
  359. options.watch_identity_key_cert_pairs();
  360. options.set_identity_cert_name(kIdentityCertName);
  361. options.set_crl_directory(CRL_DIR_PATH);
  362. auto channel_credentials = grpc::experimental::TlsCredentials(options);
  363. GPR_ASSERT(channel_credentials.get() != nullptr);
  364. }
  365. } // namespace
  366. } // namespace testing
  367. } // namespace grpc
  368. int main(int argc, char** argv) {
  369. ::testing::InitGoogleTest(&argc, argv);
  370. int ret = RUN_ALL_TESTS();
  371. return ret;
  372. }