Debian Codegraph @

test/cpp/client/credentials_test.cc

raw
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
19 #include <memory>
20
21 #include <gmock/gmock.h>
22 #include <gtest/gtest.h>
23
24 #include <grpc/grpc.h>
25 #include <grpc/grpc_security.h>
26 #include <grpcpp/security/credentials.h>
27 #include <grpcpp/security/server_credentials.h>
28 #include <grpcpp/security/tls_credentials_options.h>
29 #include <grpcpp/server_builder.h>
30
31 #include "src/core/lib/gpr/tmpfile.h"
32 #include "src/core/lib/gprpp/env.h"
33 #include "src/cpp/client/secure_credentials.h"
34 #include "test/cpp/util/tls_test_utils.h"
35
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"
40
41 namespace {
42
43 constexpr const char* kRootCertName = "root_cert_name";
44 constexpr const char* kRootCertContents = "root_cert_contents";
45 constexpr const char* kIdentityCertName = "identity_cert_name";
46 constexpr const char* kIdentityCertPrivateKey = "identity_private_key";
47 constexpr const char* kIdentityCertContents = "identity_cert_contents";
48
49 using ::grpc::experimental::ExternalCertificateVerifier;
50 using ::grpc::experimental::FileWatcherCertificateProvider;
51 using ::grpc::experimental::HostNameCertificateVerifier;
52 using ::grpc::experimental::StaticDataCertificateProvider;
53
54 } // namespace
55
56 namespace grpc {
57 namespace testing {
58 namespace {
59
60 TEST(CredentialsTest, InvalidGoogleRefreshToken) {
61 std::shared_ptr<CallCredentials> bad1 = GoogleRefreshTokenCredentials("");
62 EXPECT_EQ(static_cast<CallCredentials*>(nullptr), bad1.get());
63 }
64
65 TEST(CredentialsTest, DefaultCredentials) {
66 auto creds = GoogleDefaultCredentials();
67 }
68
69 TEST(CredentialsTest, ExternalAccountCredentials) {
70 // url credentials
71 std::string url_options_string(
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_"
81 "secret\"}");
82 auto url_creds = grpc::ExternalAccountCredentials(url_options_string,
83 {"scope1", "scope2"});
84 EXPECT_TRUE(url_creds != nullptr);
85 // file credentials
86 std::string file_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\":{\"file\":\"credentials_file_path\"},"
92 "\"quota_project_id\":\"quota_"
93 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
94 "secret\"}");
95 auto file_creds = grpc::ExternalAccountCredentials(file_options_string,
96 {"scope1", "scope2"});
97 EXPECT_TRUE(file_creds != nullptr);
98 // aws credentials
99 std::string aws_options_string(
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://169.254.169.254:5555/"
106 "region_url\",\"url\":\"https://"
107 "169.254.169.254:5555/url\",\"regional_cred_verification_url\":\"https://"
108 "foo.com:5555/regional_cred_verification_url_{region}\"},"
109 "\"quota_project_id\":\"quota_"
110 "project_id\",\"client_id\":\"client_id\",\"client_secret\":\"client_"
111 "secret\"}");
112 auto aws_creds = grpc::ExternalAccountCredentials(aws_options_string,
113 {"scope1", "scope2"});
114 EXPECT_TRUE(aws_creds != nullptr);
115 }
116
117 TEST(CredentialsTest, StsCredentialsOptionsCppToCore) {
118 grpc::experimental::StsCredentialsOptions options;
119 options.token_exchange_service_uri = "https://foo.com/exchange";
120 options.resource = "resource";
121 options.audience = "audience";
122 options.scope = "scope";
123 // options.requested_token_type explicitly not set.
124 options.subject_token_path = "/foo/bar";
125 options.subject_token_type = "nice_token_type";
126 options.actor_token_path = "/foo/baz";
127 options.actor_token_type = "even_nicer_token_type";
128 grpc_sts_credentials_options core_opts =
129 grpc::experimental::StsCredentialsCppToCoreOptions(options);
130 EXPECT_EQ(options.token_exchange_service_uri,
131 core_opts.token_exchange_service_uri);
132 EXPECT_EQ(options.resource, core_opts.resource);
133 EXPECT_EQ(options.audience, core_opts.audience);
134 EXPECT_EQ(options.scope, core_opts.scope);
135 EXPECT_EQ(options.requested_token_type, core_opts.requested_token_type);
136 EXPECT_EQ(options.subject_token_path, core_opts.subject_token_path);
137 EXPECT_EQ(options.subject_token_type, core_opts.subject_token_type);
138 EXPECT_EQ(options.actor_token_path, core_opts.actor_token_path);
139 EXPECT_EQ(options.actor_token_type, core_opts.actor_token_type);
140 }
141
142 TEST(CredentialsTest, StsCredentialsOptionsJson) {
143 const char valid_json[] = R"(
144 {
145 "token_exchange_service_uri": "https://foo/exchange",
146 "resource": "resource",
147 "audience": "audience",
148 "scope": "scope",
149 "requested_token_type": "requested_token_type",
150 "subject_token_path": "subject_token_path",
151 "subject_token_type": "subject_token_type",
152 "actor_token_path": "actor_token_path",
153 "actor_token_type": "actor_token_type"
154 })";
155 grpc::experimental::StsCredentialsOptions options;
156 EXPECT_TRUE(
157 grpc::experimental::StsCredentialsOptionsFromJson(valid_json, &options)
158 .ok());
159 EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
160 EXPECT_EQ(options.resource, "resource");
161 EXPECT_EQ(options.audience, "audience");
162 EXPECT_EQ(options.scope, "scope");
163 EXPECT_EQ(options.requested_token_type, "requested_token_type");
164 EXPECT_EQ(options.subject_token_path, "subject_token_path");
165 EXPECT_EQ(options.subject_token_type, "subject_token_type");
166 EXPECT_EQ(options.actor_token_path, "actor_token_path");
167 EXPECT_EQ(options.actor_token_type, "actor_token_type");
168
169 const char minimum_valid_json[] = R"(
170 {
171 "token_exchange_service_uri": "https://foo/exchange",
172 "subject_token_path": "subject_token_path",
173 "subject_token_type": "subject_token_type"
174 })";
175 EXPECT_TRUE(grpc::experimental::StsCredentialsOptionsFromJson(
176 minimum_valid_json, &options)
177 .ok());
178 EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
179 EXPECT_EQ(options.resource, "");
180 EXPECT_EQ(options.audience, "");
181 EXPECT_EQ(options.scope, "");
182 EXPECT_EQ(options.requested_token_type, "");
183 EXPECT_EQ(options.subject_token_path, "subject_token_path");
184 EXPECT_EQ(options.subject_token_type, "subject_token_type");
185 EXPECT_EQ(options.actor_token_path, "");
186 EXPECT_EQ(options.actor_token_type, "");
187
188 const char invalid_json[] = R"(
189 I'm not a valid JSON.
190 )";
191 EXPECT_EQ(
192 grpc::StatusCode::INVALID_ARGUMENT,
193 grpc::experimental::StsCredentialsOptionsFromJson(invalid_json, &options)
194 .error_code());
195
196 const char invalid_json_missing_subject_token_type[] = R"(
197 {
198 "token_exchange_service_uri": "https://foo/exchange",
199 "subject_token_path": "subject_token_path"
200 })";
201 auto status = grpc::experimental::StsCredentialsOptionsFromJson(
202 invalid_json_missing_subject_token_type, &options);
203 EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
204 EXPECT_THAT(status.error_message(),
205 ::testing::HasSubstr("subject_token_type"));
206
207 const char invalid_json_missing_subject_token_path[] = R"(
208 {
209 "token_exchange_service_uri": "https://foo/exchange",
210 "subject_token_type": "subject_token_type"
211 })";
212 status = grpc::experimental::StsCredentialsOptionsFromJson(
213 invalid_json_missing_subject_token_path, &options);
214 EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
215 EXPECT_THAT(status.error_message(),
216 ::testing::HasSubstr("subject_token_path"));
217
218 const char invalid_json_missing_token_exchange_uri[] = R"(
219 {
220 "subject_token_path": "subject_token_path",
221 "subject_token_type": "subject_token_type"
222 })";
223 status = grpc::experimental::StsCredentialsOptionsFromJson(
224 invalid_json_missing_token_exchange_uri, &options);
225 EXPECT_EQ(grpc::StatusCode::INVALID_ARGUMENT, status.error_code());
226 EXPECT_THAT(status.error_message(),
227 ::testing::HasSubstr("token_exchange_service_uri"));
228 }
229
230 TEST(CredentialsTest, StsCredentialsOptionsFromEnv) {
231 // Unset env and check expected failure.
232 grpc_core::UnsetEnv("STS_CREDENTIALS");
233 grpc::experimental::StsCredentialsOptions options;
234 auto status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
235 EXPECT_EQ(grpc::StatusCode::NOT_FOUND, status.error_code());
236
237 // Set env and check for success.
238 const char valid_json[] = R"(
239 {
240 "token_exchange_service_uri": "https://foo/exchange",
241 "subject_token_path": "subject_token_path",
242 "subject_token_type": "subject_token_type"
243 })";
244 char* creds_file_name;
245 FILE* creds_file = gpr_tmpfile("sts_creds_options", &creds_file_name);
246 ASSERT_NE(creds_file_name, nullptr);
247 ASSERT_NE(creds_file, nullptr);
248 ASSERT_EQ(sizeof(valid_json),
249 fwrite(valid_json, 1, sizeof(valid_json), creds_file));
250 fclose(creds_file);
251 grpc_core::SetEnv("STS_CREDENTIALS", creds_file_name);
252 gpr_free(creds_file_name);
253 status = grpc::experimental::StsCredentialsOptionsFromEnv(&options);
254 EXPECT_TRUE(status.ok());
255 EXPECT_EQ(options.token_exchange_service_uri, "https://foo/exchange");
256 EXPECT_EQ(options.resource, "");
257 EXPECT_EQ(options.audience, "");
258 EXPECT_EQ(options.scope, "");
259 EXPECT_EQ(options.requested_token_type, "");
260 EXPECT_EQ(options.subject_token_path, "subject_token_path");
261 EXPECT_EQ(options.subject_token_type, "subject_token_type");
262 EXPECT_EQ(options.actor_token_path, "");
263 EXPECT_EQ(options.actor_token_type, "");
264
265 // Cleanup.
266 grpc_core::UnsetEnv("STS_CREDENTIALS");
267 }
268
269 TEST(CredentialsTest, TlsChannelCredentialsWithDefaultRootsAndDefaultVerifier) {
270 grpc::experimental::TlsChannelCredentialsOptions options;
271 options.set_verify_server_certs(true);
272 auto channel_credentials = grpc::experimental::TlsCredentials(options);
273 GPR_ASSERT(channel_credentials.get() != nullptr);
274 }
275
276 TEST(
277 CredentialsTest,
278 TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootAndIdentity) {
279 experimental::IdentityKeyCertPair key_cert_pair;
280 key_cert_pair.private_key = kIdentityCertPrivateKey;
281 key_cert_pair.certificate_chain = kIdentityCertContents;
282 std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
283 identity_key_cert_pairs.emplace_back(key_cert_pair);
284 auto certificate_provider = std::make_shared<StaticDataCertificateProvider>(
285 kRootCertContents, identity_key_cert_pairs);
286 grpc::experimental::TlsChannelCredentialsOptions options;
287 options.set_certificate_provider(certificate_provider);
288 options.watch_root_certs();
289 options.set_root_cert_name(kRootCertName);
290 options.watch_identity_key_cert_pairs();
291 options.set_identity_cert_name(kIdentityCertName);
292 auto channel_credentials = grpc::experimental::TlsCredentials(options);
293 GPR_ASSERT(channel_credentials.get() != nullptr);
294 }
295
296 TEST(CredentialsTest,
297 TlsChannelCredentialsWithStaticDataCertificateProviderLoadingRootOnly) {
298 auto certificate_provider =
299 std::make_shared<StaticDataCertificateProvider>(kRootCertContents);
300 GPR_ASSERT(certificate_provider != nullptr);
301 GPR_ASSERT(certificate_provider->c_provider() != nullptr);
302 grpc::experimental::TlsChannelCredentialsOptions options;
303 options.set_certificate_provider(certificate_provider);
304 options.watch_root_certs();
305 options.set_root_cert_name(kRootCertName);
306 auto channel_credentials = grpc::experimental::TlsCredentials(options);
307 GPR_ASSERT(channel_credentials.get() != nullptr);
308 }
309
310 TEST(
311 CredentialsTest,
312 TlsChannelCredentialsWithDefaultRootsAndStaticDataCertificateProviderLoadingIdentityOnly) {
313 experimental::IdentityKeyCertPair key_cert_pair;
314 key_cert_pair.private_key = kIdentityCertPrivateKey;
315 key_cert_pair.certificate_chain = kIdentityCertContents;
316 std::vector<experimental::IdentityKeyCertPair> identity_key_cert_pairs;
317 identity_key_cert_pairs.emplace_back(key_cert_pair);
318 auto certificate_provider =
319 std::make_shared<StaticDataCertificateProvider>(identity_key_cert_pairs);
320 grpc::experimental::TlsChannelCredentialsOptions options;
321 options.set_certificate_provider(certificate_provider);
322 options.watch_identity_key_cert_pairs();
323 options.set_identity_cert_name(kIdentityCertName);
324 auto channel_credentials = grpc::experimental::TlsCredentials(options);
325 GPR_ASSERT(channel_credentials.get() != nullptr);
326 }
327
328 TEST(
329 CredentialsTest,
330 TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootAndIdentity) {
331 auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
332 SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1);
333 grpc::experimental::TlsChannelCredentialsOptions options;
334 options.set_certificate_provider(certificate_provider);
335 options.watch_root_certs();
336 options.set_root_cert_name(kRootCertName);
337 options.watch_identity_key_cert_pairs();
338 options.set_identity_cert_name(kIdentityCertName);
339 auto channel_credentials = grpc::experimental::TlsCredentials(options);
340 GPR_ASSERT(channel_credentials.get() != nullptr);
341 }
342
343 TEST(CredentialsTest,
344 TlsChannelCredentialsWithFileWatcherCertificateProviderLoadingRootOnly) {
345 auto certificate_provider =
346 std::make_shared<FileWatcherCertificateProvider>(CA_CERT_PATH, 1);
347 grpc::experimental::TlsChannelCredentialsOptions options;
348 options.set_certificate_provider(certificate_provider);
349 options.watch_root_certs();
350 options.set_root_cert_name(kRootCertName);
351 auto channel_credentials = grpc::experimental::TlsCredentials(options);
352 GPR_ASSERT(channel_credentials.get() != nullptr);
353 }
354
355 TEST(CredentialsTest, TlsChannelCredentialsWithHostNameVerifier) {
356 auto verifier = std::make_shared<HostNameCertificateVerifier>();
357 grpc::experimental::TlsChannelCredentialsOptions options;
358 options.set_verify_server_certs(true);
359 options.set_certificate_verifier(verifier);
360 auto channel_credentials = grpc::experimental::TlsCredentials(options);
361 GPR_ASSERT(channel_credentials.get() != nullptr);
362 }
363
364 TEST(CredentialsTest, TlsChannelCredentialsWithSyncExternalVerifier) {
365 auto verifier =
366 ExternalCertificateVerifier::Create<SyncCertificateVerifier>(true);
367 grpc::experimental::TlsChannelCredentialsOptions options;
368 options.set_verify_server_certs(true);
369 options.set_certificate_verifier(verifier);
370 options.set_check_call_host(false);
371 auto channel_credentials = grpc::experimental::TlsCredentials(options);
372 GPR_ASSERT(channel_credentials.get() != nullptr);
373 }
374
375 TEST(CredentialsTest, TlsChannelCredentialsWithAsyncExternalVerifier) {
376 auto verifier =
377 ExternalCertificateVerifier::Create<AsyncCertificateVerifier>(true);
378 grpc::experimental::TlsChannelCredentialsOptions options;
379 options.set_verify_server_certs(true);
380 options.set_certificate_verifier(verifier);
381 options.set_check_call_host(false);
382 auto channel_credentials = grpc::experimental::TlsCredentials(options);
383 GPR_ASSERT(channel_credentials.get() != nullptr);
384 }
385
386 TEST(CredentialsTest, TlsChannelCredentialsWithCrlDirectory) {
387 auto certificate_provider = std::make_shared<FileWatcherCertificateProvider>(
388 SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1);
389 grpc::experimental::TlsChannelCredentialsOptions options;
390 options.set_certificate_provider(certificate_provider);
391 options.watch_root_certs();
392 options.set_root_cert_name(kRootCertName);
393 options.watch_identity_key_cert_pairs();
394 options.set_identity_cert_name(kIdentityCertName);
395 options.set_crl_directory(CRL_DIR_PATH);
396 auto channel_credentials = grpc::experimental::TlsCredentials(options);
397 GPR_ASSERT(channel_credentials.get() != nullptr);
398 }
399
400 } // namespace
401 } // namespace testing
402 } // namespace grpc
403
404 int main(int argc, char** argv) {
405 ::testing::InitGoogleTest(&argc, argv);
406 int ret = RUN_ALL_TESTS();
407 return ret;
408 }