Debian Codegraph @

test/core/xds/xds_certificate_provider_test.cc

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