xds_certificate_provider_test.cc 29 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581
  1. //
  2. //
  3. // Copyright 2020 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 "src/core/ext/xds/xds_certificate_provider.h"
  19. #include <gmock/gmock.h>
  20. #include <gtest/gtest.h>
  21. #include "test/core/util/test_config.h"
  22. #include "test/core/util/tls_utils.h"
  23. namespace grpc_core {
  24. namespace testing {
  25. namespace {
  26. constexpr const char* kRootCert1 = "root_cert_1_contents";
  27. constexpr const char* kRootCert2 = "root_cert_2_contents";
  28. constexpr const char* kIdentityCert1PrivateKey = "identity_private_key_1";
  29. constexpr const char* kIdentityCert1 = "identity_cert_1_contents";
  30. constexpr const char* kIdentityCert2PrivateKey = "identity_private_key_2";
  31. constexpr const char* kIdentityCert2 = "identity_cert_2_contents";
  32. constexpr const char* kRootErrorMessage = "root_error_message";
  33. constexpr const char* kIdentityErrorMessage = "identity_error_message";
  34. PemKeyCertPairList MakeKeyCertPairsType1() {
  35. return MakeCertKeyPairs(kIdentityCert1PrivateKey, kIdentityCert1);
  36. }
  37. PemKeyCertPairList MakeKeyCertPairsType2() {
  38. return MakeCertKeyPairs(kIdentityCert2PrivateKey, kIdentityCert2);
  39. }
  40. class TestCertificatesWatcher
  41. : public grpc_tls_certificate_distributor::TlsCertificatesWatcherInterface {
  42. public:
  43. ~TestCertificatesWatcher() override {
  44. GRPC_ERROR_UNREF(root_cert_error_);
  45. GRPC_ERROR_UNREF(identity_cert_error_);
  46. }
  47. void OnCertificatesChanged(
  48. absl::optional<absl::string_view> root_certs,
  49. absl::optional<PemKeyCertPairList> key_cert_pairs) override {
  50. if (root_certs.has_value()) {
  51. if (!root_certs_.has_value() ||
  52. (root_certs_.has_value() &&
  53. std::string(root_certs.value()) != root_certs_.value())) {
  54. GRPC_ERROR_UNREF(root_cert_error_);
  55. root_cert_error_ = GRPC_ERROR_NONE;
  56. }
  57. root_certs_.emplace(std::string(root_certs.value()));
  58. }
  59. if (key_cert_pairs.has_value()) {
  60. if (key_cert_pairs != key_cert_pairs_) {
  61. GRPC_ERROR_UNREF(identity_cert_error_);
  62. identity_cert_error_ = GRPC_ERROR_NONE;
  63. key_cert_pairs_ = key_cert_pairs;
  64. }
  65. }
  66. }
  67. void OnError(grpc_error_handle root_cert_error,
  68. grpc_error_handle identity_cert_error) override {
  69. GRPC_ERROR_UNREF(root_cert_error_);
  70. root_cert_error_ = root_cert_error;
  71. GRPC_ERROR_UNREF(identity_cert_error_);
  72. identity_cert_error_ = identity_cert_error;
  73. }
  74. const absl::optional<std::string>& root_certs() const { return root_certs_; }
  75. const absl::optional<PemKeyCertPairList>& key_cert_pairs() const {
  76. return key_cert_pairs_;
  77. }
  78. grpc_error_handle root_cert_error() const { return root_cert_error_; }
  79. grpc_error_handle identity_cert_error() const { return identity_cert_error_; }
  80. private:
  81. absl::optional<std::string> root_certs_;
  82. absl::optional<PemKeyCertPairList> key_cert_pairs_;
  83. grpc_error_handle root_cert_error_ = GRPC_ERROR_NONE;
  84. grpc_error_handle identity_cert_error_ = GRPC_ERROR_NONE;
  85. };
  86. TEST(
  87. XdsCertificateProviderTest,
  88. RootCertDistributorDifferentFromIdentityCertDistributorDifferentCertNames) {
  89. auto root_cert_distributor =
  90. MakeRefCounted<grpc_tls_certificate_distributor>();
  91. auto identity_cert_distributor =
  92. MakeRefCounted<grpc_tls_certificate_distributor>();
  93. XdsCertificateProvider provider;
  94. provider.UpdateRootCertNameAndDistributor("", "root", root_cert_distributor);
  95. provider.UpdateIdentityCertNameAndDistributor("", "identity",
  96. identity_cert_distributor);
  97. auto* watcher = new TestCertificatesWatcher;
  98. provider.distributor()->WatchTlsCertificates(
  99. std::unique_ptr<TestCertificatesWatcher>(watcher), "", "");
  100. EXPECT_EQ(watcher->root_certs(), absl::nullopt);
  101. EXPECT_EQ(watcher->key_cert_pairs(), absl::nullopt);
  102. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  103. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  104. // Update both root certs and identity certs
  105. root_cert_distributor->SetKeyMaterials("root", kRootCert1, absl::nullopt);
  106. identity_cert_distributor->SetKeyMaterials("identity", absl::nullopt,
  107. MakeKeyCertPairsType1());
  108. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  109. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  110. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  111. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  112. // Second update for just root certs
  113. root_cert_distributor->SetKeyMaterials(
  114. "root", kRootCert2,
  115. MakeKeyCertPairsType2() /* does not have an effect */);
  116. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  117. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  118. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  119. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  120. // Second update for identity certs
  121. identity_cert_distributor->SetKeyMaterials(
  122. "identity", kRootCert1 /* does not have an effect */,
  123. MakeKeyCertPairsType2());
  124. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  125. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  126. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  127. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  128. // Set error for both root and identity
  129. root_cert_distributor->SetErrorForCert(
  130. "root", GRPC_ERROR_CREATE_FROM_STATIC_STRING(kRootErrorMessage),
  131. absl::nullopt);
  132. identity_cert_distributor->SetErrorForCert(
  133. "identity", absl::nullopt,
  134. GRPC_ERROR_CREATE_FROM_STATIC_STRING(kIdentityErrorMessage));
  135. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  136. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  137. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  138. ::testing::HasSubstr(kRootErrorMessage));
  139. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  140. ::testing::HasSubstr(kIdentityErrorMessage));
  141. // Send an update for root certs. Test that the root cert error is reset.
  142. root_cert_distributor->SetKeyMaterials("root", kRootCert1, absl::nullopt);
  143. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  144. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  145. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  146. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  147. ::testing::HasSubstr(kIdentityErrorMessage));
  148. // Send an update for identity certs. Test that the identity cert error is
  149. // reset.
  150. identity_cert_distributor->SetKeyMaterials("identity", absl::nullopt,
  151. MakeKeyCertPairsType1());
  152. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  153. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  154. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  155. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  156. }
  157. TEST(XdsCertificateProviderTest,
  158. RootCertDistributorDifferentFromIdentityCertDistributorSameCertNames) {
  159. auto root_cert_distributor =
  160. MakeRefCounted<grpc_tls_certificate_distributor>();
  161. auto identity_cert_distributor =
  162. MakeRefCounted<grpc_tls_certificate_distributor>();
  163. XdsCertificateProvider provider;
  164. provider.UpdateRootCertNameAndDistributor("", "test", root_cert_distributor);
  165. provider.UpdateIdentityCertNameAndDistributor("", "test",
  166. identity_cert_distributor);
  167. auto* watcher = new TestCertificatesWatcher;
  168. provider.distributor()->WatchTlsCertificates(
  169. std::unique_ptr<TestCertificatesWatcher>(watcher), "", "");
  170. EXPECT_EQ(watcher->root_certs(), absl::nullopt);
  171. EXPECT_EQ(watcher->key_cert_pairs(), absl::nullopt);
  172. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  173. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  174. // Update both root certs and identity certs
  175. root_cert_distributor->SetKeyMaterials("test", kRootCert1, absl::nullopt);
  176. identity_cert_distributor->SetKeyMaterials("test", absl::nullopt,
  177. MakeKeyCertPairsType1());
  178. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  179. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  180. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  181. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  182. // Second update for just root certs
  183. root_cert_distributor->SetKeyMaterials("test", kRootCert2, absl::nullopt);
  184. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  185. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  186. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  187. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  188. // Second update for identity certs
  189. identity_cert_distributor->SetKeyMaterials("test", absl::nullopt,
  190. MakeKeyCertPairsType2());
  191. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  192. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  193. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  194. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  195. // Set error for both root and identity
  196. root_cert_distributor->SetErrorForCert(
  197. "test", GRPC_ERROR_CREATE_FROM_STATIC_STRING(kRootErrorMessage),
  198. absl::nullopt);
  199. identity_cert_distributor->SetErrorForCert(
  200. "test", absl::nullopt,
  201. GRPC_ERROR_CREATE_FROM_STATIC_STRING(kIdentityErrorMessage));
  202. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  203. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  204. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  205. ::testing::HasSubstr(kRootErrorMessage));
  206. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  207. ::testing::HasSubstr(kIdentityErrorMessage));
  208. // Send an update for root certs. Test that the root cert error is reset.
  209. root_cert_distributor->SetKeyMaterials("test", kRootCert1, absl::nullopt);
  210. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  211. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  212. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  213. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  214. ::testing::HasSubstr(kIdentityErrorMessage));
  215. // Send an update for identity certs. Test that the identity cert error is
  216. // reset.
  217. identity_cert_distributor->SetKeyMaterials("test", absl::nullopt,
  218. MakeKeyCertPairsType1());
  219. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  220. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  221. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  222. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  223. // Test update on unwatched cert name
  224. identity_cert_distributor->SetKeyMaterials("identity", kRootCert2,
  225. MakeKeyCertPairsType2());
  226. root_cert_distributor->SetKeyMaterials("root", kRootCert1,
  227. MakeKeyCertPairsType1());
  228. }
  229. TEST(XdsCertificateProviderTest,
  230. RootCertDistributorSameAsIdentityCertDistributorDifferentCertNames) {
  231. auto distributor = MakeRefCounted<grpc_tls_certificate_distributor>();
  232. XdsCertificateProvider provider;
  233. provider.UpdateRootCertNameAndDistributor("", "root", distributor);
  234. provider.UpdateIdentityCertNameAndDistributor("", "identity", distributor);
  235. auto* watcher = new TestCertificatesWatcher;
  236. provider.distributor()->WatchTlsCertificates(
  237. std::unique_ptr<TestCertificatesWatcher>(watcher), "", "");
  238. EXPECT_EQ(watcher->root_certs(), absl::nullopt);
  239. EXPECT_EQ(watcher->key_cert_pairs(), absl::nullopt);
  240. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  241. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  242. // Update both root certs and identity certs
  243. distributor->SetKeyMaterials("root", kRootCert1, MakeKeyCertPairsType2());
  244. distributor->SetKeyMaterials("identity", kRootCert2, MakeKeyCertPairsType1());
  245. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  246. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  247. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  248. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  249. // Second update for just root certs
  250. distributor->SetKeyMaterials("root", kRootCert2, MakeKeyCertPairsType2());
  251. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  252. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  253. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  254. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  255. // Second update for identity certs
  256. distributor->SetKeyMaterials("identity", kRootCert1, MakeKeyCertPairsType2());
  257. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  258. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  259. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  260. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  261. // Set error for root
  262. distributor->SetErrorForCert(
  263. "root", GRPC_ERROR_CREATE_FROM_STATIC_STRING(kRootErrorMessage),
  264. GRPC_ERROR_CREATE_FROM_STATIC_STRING(kRootErrorMessage));
  265. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  266. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  267. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  268. ::testing::HasSubstr(kRootErrorMessage));
  269. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  270. distributor->SetErrorForCert(
  271. "identity", GRPC_ERROR_CREATE_FROM_STATIC_STRING(kIdentityErrorMessage),
  272. GRPC_ERROR_CREATE_FROM_STATIC_STRING(kIdentityErrorMessage));
  273. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  274. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  275. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  276. ::testing::HasSubstr(kRootErrorMessage));
  277. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  278. ::testing::HasSubstr(kIdentityErrorMessage));
  279. // Send an update for root
  280. distributor->SetKeyMaterials("root", kRootCert1, MakeKeyCertPairsType1());
  281. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  282. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  283. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  284. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  285. ::testing::HasSubstr(kIdentityErrorMessage));
  286. // Send an update for identity
  287. distributor->SetKeyMaterials("identity", kRootCert2, MakeKeyCertPairsType1());
  288. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  289. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  290. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  291. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  292. }
  293. TEST(XdsCertificateProviderTest,
  294. RootCertDistributorSameAsIdentityCertDistributorSameCertNames) {
  295. auto distributor = MakeRefCounted<grpc_tls_certificate_distributor>();
  296. XdsCertificateProvider provider;
  297. provider.UpdateRootCertNameAndDistributor("", "", distributor);
  298. provider.UpdateIdentityCertNameAndDistributor("", "", distributor);
  299. auto* watcher = new TestCertificatesWatcher;
  300. provider.distributor()->WatchTlsCertificates(
  301. std::unique_ptr<TestCertificatesWatcher>(watcher), "", "");
  302. EXPECT_EQ(watcher->root_certs(), absl::nullopt);
  303. EXPECT_EQ(watcher->key_cert_pairs(), absl::nullopt);
  304. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  305. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  306. // Update both root certs and identity certs
  307. distributor->SetKeyMaterials("", kRootCert1, MakeKeyCertPairsType1());
  308. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  309. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  310. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  311. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  312. // Second update for just root certs
  313. distributor->SetKeyMaterials("", kRootCert2, absl::nullopt);
  314. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  315. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  316. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  317. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  318. // Second update for identity certs
  319. distributor->SetKeyMaterials("", absl::nullopt, MakeKeyCertPairsType2());
  320. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  321. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  322. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  323. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  324. // Set error for root
  325. distributor->SetErrorForCert(
  326. "", GRPC_ERROR_CREATE_FROM_STATIC_STRING(kRootErrorMessage),
  327. absl::nullopt);
  328. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  329. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  330. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  331. ::testing::HasSubstr(kRootErrorMessage));
  332. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  333. // Set error for identity
  334. distributor->SetErrorForCert(
  335. "", absl::nullopt,
  336. GRPC_ERROR_CREATE_FROM_STATIC_STRING(kIdentityErrorMessage));
  337. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  338. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  339. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  340. ::testing::HasSubstr(kRootErrorMessage));
  341. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  342. ::testing::HasSubstr(kIdentityErrorMessage));
  343. // Send an update for root
  344. distributor->SetKeyMaterials("", kRootCert1, absl::nullopt);
  345. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  346. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  347. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  348. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  349. ::testing::HasSubstr(kIdentityErrorMessage));
  350. // Send an update for identity
  351. distributor->SetKeyMaterials("", absl::nullopt, MakeKeyCertPairsType1());
  352. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  353. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  354. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  355. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  356. }
  357. TEST(XdsCertificateProviderTest, SwapOutDistributorsMultipleTimes) {
  358. auto distributor = MakeRefCounted<grpc_tls_certificate_distributor>();
  359. distributor->SetKeyMaterials("", kRootCert1, MakeKeyCertPairsType1());
  360. XdsCertificateProvider provider;
  361. auto* watcher = new TestCertificatesWatcher;
  362. provider.distributor()->WatchTlsCertificates(
  363. std::unique_ptr<TestCertificatesWatcher>(watcher), "", "");
  364. // Initially there are no certificate providers.
  365. EXPECT_EQ(watcher->root_certs(), absl::nullopt);
  366. EXPECT_EQ(watcher->key_cert_pairs(), absl::nullopt);
  367. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  368. ::testing::HasSubstr(
  369. "No certificate provider available for root certificates"));
  370. EXPECT_THAT(
  371. grpc_error_std_string(watcher->identity_cert_error()),
  372. ::testing::HasSubstr(
  373. "No certificate provider available for identity certificates"));
  374. // Update root cert distributor.
  375. provider.UpdateRootCertNameAndDistributor("", "", distributor);
  376. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  377. EXPECT_EQ(watcher->key_cert_pairs(), absl::nullopt);
  378. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  379. EXPECT_THAT(
  380. grpc_error_std_string(watcher->identity_cert_error()),
  381. ::testing::HasSubstr(
  382. "No certificate provider available for identity certificates"));
  383. // Update identity cert distributor
  384. provider.UpdateIdentityCertNameAndDistributor("", "", distributor);
  385. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  386. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  387. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  388. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  389. // Update both root and identity certs
  390. distributor->SetKeyMaterials("", kRootCert2, MakeKeyCertPairsType2());
  391. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  392. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  393. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  394. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  395. // Set error for both root and identity
  396. distributor->SetErrorForCert(
  397. "", GRPC_ERROR_CREATE_FROM_STATIC_STRING(kRootErrorMessage),
  398. GRPC_ERROR_CREATE_FROM_STATIC_STRING(kIdentityErrorMessage));
  399. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  400. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  401. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  402. ::testing::HasSubstr(kRootErrorMessage));
  403. EXPECT_THAT(grpc_error_std_string(watcher->identity_cert_error()),
  404. ::testing::HasSubstr(kIdentityErrorMessage));
  405. // Send an update again
  406. distributor->SetKeyMaterials("", kRootCert1, MakeKeyCertPairsType1());
  407. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  408. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  409. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  410. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  411. // Remove root cert provider
  412. provider.UpdateRootCertNameAndDistributor("", "", nullptr);
  413. distributor->SetKeyMaterials("", kRootCert2, MakeKeyCertPairsType2());
  414. EXPECT_EQ(watcher->root_certs(), kRootCert1); // not updated
  415. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  416. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  417. ::testing::HasSubstr(
  418. "No certificate provider available for root certificates"));
  419. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  420. // Remove identity cert provider too
  421. provider.UpdateIdentityCertNameAndDistributor("", "", nullptr);
  422. distributor->SetKeyMaterials("", kRootCert1, MakeKeyCertPairsType1());
  423. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  424. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2()); // not updated
  425. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  426. ::testing::HasSubstr(
  427. "No certificate provider available for root certificates"));
  428. EXPECT_THAT(
  429. grpc_error_std_string(watcher->identity_cert_error()),
  430. ::testing::HasSubstr(
  431. "No certificate provider available for identity certificates"));
  432. // Change certificate names being watched, without any certificate updates.
  433. provider.UpdateRootCertNameAndDistributor("", "root", distributor);
  434. provider.UpdateIdentityCertNameAndDistributor("", "identity", distributor);
  435. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  436. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  437. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  438. ::testing::HasSubstr(
  439. "No certificate provider available for root certificates"));
  440. EXPECT_THAT(
  441. grpc_error_std_string(watcher->identity_cert_error()),
  442. ::testing::HasSubstr(
  443. "No certificate provider available for identity certificates"));
  444. // Send out certificate updates.
  445. distributor->SetKeyMaterials("root", kRootCert2, absl::nullopt);
  446. distributor->SetKeyMaterials("identity", absl::nullopt,
  447. MakeKeyCertPairsType1());
  448. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  449. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  450. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  451. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  452. // Swap in new certificate distributors with different certificate names and
  453. // existing updates.
  454. auto root_cert_distributor =
  455. MakeRefCounted<grpc_tls_certificate_distributor>();
  456. auto identity_cert_distributor =
  457. MakeRefCounted<grpc_tls_certificate_distributor>();
  458. provider.UpdateRootCertNameAndDistributor("", "root", root_cert_distributor);
  459. provider.UpdateIdentityCertNameAndDistributor("", "identity",
  460. identity_cert_distributor);
  461. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  462. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  463. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  464. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  465. // Change certificate names without any certificate updates.
  466. provider.UpdateRootCertNameAndDistributor("", "test", root_cert_distributor);
  467. provider.UpdateIdentityCertNameAndDistributor("", "test",
  468. identity_cert_distributor);
  469. EXPECT_EQ(watcher->root_certs(), kRootCert2);
  470. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType1());
  471. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  472. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  473. // Send out certificate updates.
  474. root_cert_distributor->SetKeyMaterials("test", kRootCert1,
  475. MakeKeyCertPairsType1());
  476. identity_cert_distributor->SetKeyMaterials("test", kRootCert2,
  477. MakeKeyCertPairsType2());
  478. EXPECT_EQ(watcher->root_certs(), kRootCert1);
  479. EXPECT_EQ(watcher->key_cert_pairs(), MakeKeyCertPairsType2());
  480. EXPECT_EQ(watcher->root_cert_error(), GRPC_ERROR_NONE);
  481. EXPECT_EQ(watcher->identity_cert_error(), GRPC_ERROR_NONE);
  482. }
  483. TEST(XdsCertificateProviderTest, MultipleCertNames) {
  484. XdsCertificateProvider provider;
  485. // Start watch for "test1". There are no underlying distributors for
  486. // that cert name, so it will return an error.
  487. auto* watcher1 = new TestCertificatesWatcher;
  488. provider.distributor()->WatchTlsCertificates(
  489. std::unique_ptr<TestCertificatesWatcher>(watcher1), "test1", "test1");
  490. EXPECT_EQ(watcher1->root_certs(), absl::nullopt);
  491. EXPECT_EQ(watcher1->key_cert_pairs(), absl::nullopt);
  492. EXPECT_THAT(grpc_error_std_string(watcher1->root_cert_error()),
  493. ::testing::HasSubstr(
  494. "No certificate provider available for root certificates"));
  495. EXPECT_THAT(
  496. grpc_error_std_string(watcher1->identity_cert_error()),
  497. ::testing::HasSubstr(
  498. "No certificate provider available for identity certificates"));
  499. // Add distributor for "test1". This will return data to the watcher.
  500. auto cert_distributor1 = MakeRefCounted<grpc_tls_certificate_distributor>();
  501. cert_distributor1->SetKeyMaterials("root", kRootCert1, absl::nullopt);
  502. cert_distributor1->SetKeyMaterials("identity", absl::nullopt,
  503. MakeKeyCertPairsType1());
  504. provider.UpdateRootCertNameAndDistributor("test1", "root", cert_distributor1);
  505. provider.UpdateIdentityCertNameAndDistributor("test1", "identity",
  506. cert_distributor1);
  507. EXPECT_EQ(watcher1->root_certs(), kRootCert1);
  508. EXPECT_EQ(watcher1->key_cert_pairs(), MakeKeyCertPairsType1());
  509. EXPECT_EQ(watcher1->root_cert_error(), GRPC_ERROR_NONE);
  510. EXPECT_EQ(watcher1->identity_cert_error(), GRPC_ERROR_NONE);
  511. // Add distributor for "test2".
  512. auto cert_distributor2 = MakeRefCounted<grpc_tls_certificate_distributor>();
  513. cert_distributor2->SetKeyMaterials("root2", kRootCert2, absl::nullopt);
  514. cert_distributor2->SetKeyMaterials("identity2", absl::nullopt,
  515. MakeKeyCertPairsType2());
  516. provider.UpdateRootCertNameAndDistributor("test2", "root2",
  517. cert_distributor2);
  518. provider.UpdateIdentityCertNameAndDistributor("test2", "identity2",
  519. cert_distributor2);
  520. // Add watcher for "test2". This one should return data immediately.
  521. auto* watcher2 = new TestCertificatesWatcher;
  522. provider.distributor()->WatchTlsCertificates(
  523. std::unique_ptr<TestCertificatesWatcher>(watcher2), "test2", "test2");
  524. EXPECT_EQ(watcher2->root_certs(), kRootCert2);
  525. EXPECT_EQ(watcher2->key_cert_pairs(), MakeKeyCertPairsType2());
  526. EXPECT_EQ(watcher2->root_cert_error(), GRPC_ERROR_NONE);
  527. EXPECT_EQ(watcher2->identity_cert_error(), GRPC_ERROR_NONE);
  528. // The presence of "test2" should not affect "test1".
  529. EXPECT_EQ(watcher1->root_certs(), kRootCert1);
  530. EXPECT_EQ(watcher1->key_cert_pairs(), MakeKeyCertPairsType1());
  531. EXPECT_EQ(watcher1->root_cert_error(), GRPC_ERROR_NONE);
  532. EXPECT_EQ(watcher1->identity_cert_error(), GRPC_ERROR_NONE);
  533. }
  534. TEST(XdsCertificateProviderTest, UnknownCertName) {
  535. XdsCertificateProvider provider;
  536. auto* watcher = new TestCertificatesWatcher;
  537. provider.distributor()->WatchTlsCertificates(
  538. std::unique_ptr<TestCertificatesWatcher>(watcher), "test", "test");
  539. EXPECT_THAT(grpc_error_std_string(watcher->root_cert_error()),
  540. ::testing::HasSubstr(
  541. "No certificate provider available for root certificates"));
  542. EXPECT_THAT(
  543. grpc_error_std_string(watcher->identity_cert_error()),
  544. ::testing::HasSubstr(
  545. "No certificate provider available for identity certificates"));
  546. }
  547. } // namespace
  548. } // namespace testing
  549. } // namespace grpc_core
  550. int main(int argc, char** argv) {
  551. ::testing::InitGoogleTest(&argc, argv);
  552. grpc::testing::TestEnvironment env(argc, argv);
  553. grpc_init();
  554. auto result = RUN_ALL_TESTS();
  555. grpc_shutdown();
  556. return result;
  557. }