| 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/lib/security/credentials/tls/grpc_tls_credentials_options.h" |
| 20 |
|
| 21 |
#include <gmock/gmock.h> |
| 22 |
#include <gtest/gtest.h> |
| 23 |
|
| 24 |
#include <grpc/support/alloc.h> |
| 25 |
#include <grpc/support/log.h> |
| 26 |
#include <grpc/support/string_util.h> |
| 27 |
|
| 28 |
#include "src/core/lib/gpr/tmpfile.h" |
| 29 |
#include "src/core/lib/iomgr/load_file.h" |
| 30 |
#include "src/core/lib/security/credentials/tls/tls_credentials.h" |
| 31 |
#include "src/core/lib/security/security_connector/ssl_utils_config.h" |
| 32 |
#include "src/core/lib/security/security_connector/tls/tls_security_connector.h" |
| 33 |
#include "test/core/util/test_config.h" |
| 34 |
#include "test/core/util/tls_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 CA_CERT_PATH_2 "src/core/tsi/test_creds/multi-domain.pem" |
| 40 |
#define SERVER_CERT_PATH_2 "src/core/tsi/test_creds/server0.pem" |
| 41 |
#define SERVER_KEY_PATH_2 "src/core/tsi/test_creds/server0.key" |
| 42 |
#define INVALID_PATH "invalid/path" |
| 43 |
|
| 44 |
namespace grpc_core { |
| 45 |
|
| 46 |
namespace testing { |
| 47 |
|
| 48 |
class GrpcTlsCredentialsOptionsTest : public ::testing::Test { |
| 49 |
protected: |
| 50 |
void SetUp() override { |
| 51 |
root_cert_ = GetFileContents(CA_CERT_PATH); |
| 52 |
cert_chain_ = GetFileContents(SERVER_CERT_PATH); |
| 53 |
private_key_ = GetFileContents(SERVER_KEY_PATH); |
| 54 |
root_cert_2_ = GetFileContents(CA_CERT_PATH_2); |
| 55 |
cert_chain_2_ = GetFileContents(SERVER_CERT_PATH_2); |
| 56 |
private_key_2_ = GetFileContents(SERVER_KEY_PATH_2); |
| 57 |
} |
| 58 |
|
| 59 |
std::string root_cert_; |
| 60 |
std::string private_key_; |
| 61 |
std::string cert_chain_; |
| 62 |
std::string root_cert_2_; |
| 63 |
std::string private_key_2_; |
| 64 |
std::string cert_chain_2_; |
| 65 |
HostNameCertificateVerifier hostname_certificate_verifier_; |
| 66 |
}; |
| 67 |
|
| 68 |
// |
| 69 |
// Tests for Default Root Certs. |
| 70 |
// |
| 71 |
|
| 72 |
TEST_F(GrpcTlsCredentialsOptionsTest, ClientOptionsOnDefaultRootCerts) { |
| 73 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 74 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 75 |
ASSERT_NE(credentials, nullptr); |
| 76 |
ChannelArgs new_args; |
| 77 |
auto connector = credentials->create_security_connector( |
| 78 |
nullptr, "random targets", &new_args); |
| 79 |
ASSERT_NE(connector, nullptr); |
| 80 |
TlsChannelSecurityConnector* tls_connector = |
| 81 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 82 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 83 |
} |
| 84 |
|
| 85 |
// |
| 86 |
// Tests for StaticDataCertificateProvider. |
| 87 |
// |
| 88 |
|
| 89 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 90 |
ClientOptionsWithStaticDataProviderOnBothCerts) { |
| 91 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 92 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 93 |
root_cert_, MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 94 |
options->set_certificate_provider(std::move(provider)); |
| 95 |
options->set_watch_root_cert(true); |
| 96 |
options->set_watch_identity_pair(true); |
| 97 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 98 |
ASSERT_NE(credentials, nullptr); |
| 99 |
ChannelArgs new_args; |
| 100 |
auto connector = credentials->create_security_connector( |
| 101 |
nullptr, "random targets", &new_args); |
| 102 |
ASSERT_NE(connector, nullptr); |
| 103 |
TlsChannelSecurityConnector* tls_connector = |
| 104 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 105 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 106 |
EXPECT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 107 |
EXPECT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 108 |
} |
| 109 |
|
| 110 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 111 |
ClientOptionsWithStaticDataProviderOnRootCerts) { |
| 112 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 113 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 114 |
root_cert_, PemKeyCertPairList()); |
| 115 |
options->set_certificate_provider(std::move(provider)); |
| 116 |
options->set_watch_root_cert(true); |
| 117 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 118 |
ASSERT_NE(credentials, nullptr); |
| 119 |
ChannelArgs new_args; |
| 120 |
auto connector = credentials->create_security_connector( |
| 121 |
nullptr, "random targets", &new_args); |
| 122 |
ASSERT_NE(connector, nullptr); |
| 123 |
TlsChannelSecurityConnector* tls_connector = |
| 124 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 125 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 126 |
EXPECT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 127 |
EXPECT_FALSE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 128 |
} |
| 129 |
|
| 130 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 131 |
ClientOptionsWithStaticDataProviderOnNotProvidedCerts) { |
| 132 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 133 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 134 |
"", MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 135 |
options->set_certificate_provider(std::move(provider)); |
| 136 |
options->set_watch_root_cert(true); |
| 137 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 138 |
ASSERT_NE(credentials, nullptr); |
| 139 |
ChannelArgs new_args; |
| 140 |
auto connector = credentials->create_security_connector( |
| 141 |
nullptr, "random targets", &new_args); |
| 142 |
ASSERT_NE(connector, nullptr); |
| 143 |
TlsChannelSecurityConnector* tls_connector = |
| 144 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 145 |
EXPECT_EQ(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 146 |
} |
| 147 |
|
| 148 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 149 |
ClientOptionsWithDefaultRootAndStaticDataProviderOnIdentityCerts) { |
| 150 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 151 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 152 |
"", MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 153 |
options->set_certificate_provider(std::move(provider)); |
| 154 |
options->set_watch_identity_pair(true); |
| 155 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 156 |
ASSERT_NE(credentials, nullptr); |
| 157 |
ChannelArgs new_args; |
| 158 |
auto connector = credentials->create_security_connector( |
| 159 |
nullptr, "random targets", &new_args); |
| 160 |
ASSERT_NE(connector, nullptr); |
| 161 |
TlsChannelSecurityConnector* tls_connector = |
| 162 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 163 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 164 |
} |
| 165 |
|
| 166 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 167 |
ServerOptionsWithStaticDataProviderOnBothCerts) { |
| 168 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 169 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 170 |
root_cert_, MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 171 |
options->set_certificate_provider(std::move(provider)); |
| 172 |
options->set_watch_root_cert(true); |
| 173 |
options->set_watch_identity_pair(true); |
| 174 |
options->set_cert_request_type( |
| 175 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 176 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 177 |
ASSERT_NE(credentials, nullptr); |
| 178 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 179 |
ASSERT_NE(connector, nullptr); |
| 180 |
TlsServerSecurityConnector* tls_connector = |
| 181 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 182 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 183 |
EXPECT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 184 |
EXPECT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 185 |
} |
| 186 |
|
| 187 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 188 |
ServerOptionsWithStaticDataProviderOnIdentityCerts) { |
| 189 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 190 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 191 |
"", MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 192 |
options->set_certificate_provider(std::move(provider)); |
| 193 |
options->set_watch_identity_pair(true); |
| 194 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 195 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 196 |
ASSERT_NE(credentials, nullptr); |
| 197 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 198 |
ASSERT_NE(connector, nullptr); |
| 199 |
TlsServerSecurityConnector* tls_connector = |
| 200 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 201 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 202 |
EXPECT_FALSE(tls_connector->RootCertsForTesting().has_value()); |
| 203 |
EXPECT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 204 |
} |
| 205 |
|
| 206 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 207 |
ServerOptionsWithStaticDataProviderOnNotProvidedCerts) { |
| 208 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 209 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 210 |
root_cert_, PemKeyCertPairList()); |
| 211 |
options->set_certificate_provider(std::move(provider)); |
| 212 |
options->set_watch_identity_pair(true); |
| 213 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 214 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 215 |
ASSERT_NE(credentials, nullptr); |
| 216 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 217 |
ASSERT_NE(connector, nullptr); |
| 218 |
TlsServerSecurityConnector* tls_connector = |
| 219 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 220 |
EXPECT_EQ(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 221 |
} |
| 222 |
|
| 223 |
// |
| 224 |
// Tests for FileWatcherCertificateProvider. |
| 225 |
// |
| 226 |
|
| 227 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 228 |
ClientOptionsWithCertWatcherProviderOnBothCerts) { |
| 229 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 230 |
auto provider = MakeRefCounted<FileWatcherCertificateProvider>( |
| 231 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 232 |
options->set_certificate_provider(std::move(provider)); |
| 233 |
options->set_watch_root_cert(true); |
| 234 |
options->set_watch_identity_pair(true); |
| 235 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 236 |
ASSERT_NE(credentials, nullptr); |
| 237 |
ChannelArgs new_args; |
| 238 |
auto connector = credentials->create_security_connector( |
| 239 |
nullptr, "random targets", &new_args); |
| 240 |
ASSERT_NE(connector, nullptr); |
| 241 |
TlsChannelSecurityConnector* tls_connector = |
| 242 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 243 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 244 |
EXPECT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 245 |
EXPECT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 246 |
} |
| 247 |
|
| 248 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 249 |
ClientOptionsWithCertWatcherProviderOnRootCerts) { |
| 250 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 251 |
auto provider = |
| 252 |
MakeRefCounted<FileWatcherCertificateProvider>("", "", CA_CERT_PATH, 1); |
| 253 |
options->set_certificate_provider(std::move(provider)); |
| 254 |
options->set_watch_root_cert(true); |
| 255 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 256 |
ASSERT_NE(credentials, nullptr); |
| 257 |
ChannelArgs new_args; |
| 258 |
auto connector = credentials->create_security_connector( |
| 259 |
nullptr, "random targets", &new_args); |
| 260 |
ASSERT_NE(connector, nullptr); |
| 261 |
TlsChannelSecurityConnector* tls_connector = |
| 262 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 263 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 264 |
EXPECT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 265 |
EXPECT_FALSE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 266 |
} |
| 267 |
|
| 268 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 269 |
ClientOptionsWithCertWatcherProviderOnNotProvidedCerts) { |
| 270 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 271 |
auto provider = MakeRefCounted<FileWatcherCertificateProvider>( |
| 272 |
SERVER_KEY_PATH, SERVER_CERT_PATH, "", 1); |
| 273 |
options->set_certificate_provider(std::move(provider)); |
| 274 |
options->set_watch_root_cert(true); |
| 275 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 276 |
ASSERT_NE(credentials, nullptr); |
| 277 |
ChannelArgs new_args; |
| 278 |
auto connector = credentials->create_security_connector( |
| 279 |
nullptr, "random targets", &new_args); |
| 280 |
ASSERT_NE(connector, nullptr); |
| 281 |
TlsChannelSecurityConnector* tls_connector = |
| 282 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 283 |
EXPECT_EQ(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 284 |
} |
| 285 |
|
| 286 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 287 |
ClientOptionsWithCertWatcherProviderOnBadTrustCerts) { |
| 288 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 289 |
auto provider = |
| 290 |
MakeRefCounted<FileWatcherCertificateProvider>("", "", INVALID_PATH, 1); |
| 291 |
options->set_certificate_provider(std::move(provider)); |
| 292 |
options->set_watch_root_cert(true); |
| 293 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 294 |
ASSERT_NE(credentials, nullptr); |
| 295 |
ChannelArgs new_args; |
| 296 |
auto connector = credentials->create_security_connector( |
| 297 |
nullptr, "random targets", &new_args); |
| 298 |
ASSERT_NE(connector, nullptr); |
| 299 |
TlsChannelSecurityConnector* tls_connector = |
| 300 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 301 |
EXPECT_EQ(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 302 |
} |
| 303 |
|
| 304 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 305 |
ServerOptionsWithCertWatcherProviderOnBothCerts) { |
| 306 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 307 |
auto provider = MakeRefCounted<FileWatcherCertificateProvider>( |
| 308 |
SERVER_KEY_PATH, SERVER_CERT_PATH, CA_CERT_PATH, 1); |
| 309 |
options->set_certificate_provider(std::move(provider)); |
| 310 |
options->set_watch_root_cert(true); |
| 311 |
options->set_watch_identity_pair(true); |
| 312 |
options->set_cert_request_type( |
| 313 |
GRPC_SSL_REQUEST_AND_REQUIRE_CLIENT_CERTIFICATE_AND_VERIFY); |
| 314 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 315 |
ASSERT_NE(credentials, nullptr); |
| 316 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 317 |
ASSERT_NE(connector, nullptr); |
| 318 |
TlsServerSecurityConnector* tls_connector = |
| 319 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 320 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 321 |
EXPECT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 322 |
EXPECT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 323 |
} |
| 324 |
|
| 325 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 326 |
ServerOptionsWithCertWatcherProviderOnIdentityCerts) { |
| 327 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 328 |
auto provider = MakeRefCounted<FileWatcherCertificateProvider>( |
| 329 |
SERVER_KEY_PATH, SERVER_CERT_PATH, "", 1); |
| 330 |
options->set_certificate_provider(std::move(provider)); |
| 331 |
options->set_watch_identity_pair(true); |
| 332 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 333 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 334 |
ASSERT_NE(credentials, nullptr); |
| 335 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 336 |
ASSERT_NE(connector, nullptr); |
| 337 |
TlsServerSecurityConnector* tls_connector = |
| 338 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 339 |
EXPECT_NE(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 340 |
EXPECT_FALSE(tls_connector->RootCertsForTesting().has_value()); |
| 341 |
EXPECT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 342 |
} |
| 343 |
|
| 344 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 345 |
ServerOptionsWithCertWatcherProviderOnNotProvidedCerts) { |
| 346 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 347 |
auto provider = |
| 348 |
MakeRefCounted<FileWatcherCertificateProvider>("", "", CA_CERT_PATH, 1); |
| 349 |
options->set_certificate_provider(std::move(provider)); |
| 350 |
options->set_watch_identity_pair(true); |
| 351 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 352 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 353 |
ASSERT_NE(credentials, nullptr); |
| 354 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 355 |
ASSERT_NE(connector, nullptr); |
| 356 |
TlsServerSecurityConnector* tls_connector = |
| 357 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 358 |
EXPECT_EQ(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 359 |
} |
| 360 |
|
| 361 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 362 |
ServerOptionsWithCertWatcherProviderOnBadIdentityCerts) { |
| 363 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 364 |
auto provider = MakeRefCounted<FileWatcherCertificateProvider>( |
| 365 |
INVALID_PATH, INVALID_PATH, "", 1); |
| 366 |
options->set_certificate_provider(std::move(provider)); |
| 367 |
options->set_watch_identity_pair(true); |
| 368 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 369 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 370 |
ASSERT_NE(credentials, nullptr); |
| 371 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 372 |
ASSERT_NE(connector, nullptr); |
| 373 |
TlsServerSecurityConnector* tls_connector = |
| 374 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 375 |
EXPECT_EQ(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 376 |
} |
| 377 |
|
| 378 |
// |
| 379 |
// Tests writing credential data to temporary files to test the |
| 380 |
// transition behavior of the provider. |
| 381 |
// |
| 382 |
|
| 383 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 384 |
ClientOptionsWithCertWatcherProviderOnCertificateRefreshed) { |
| 385 |
// Create temporary files and copy cert data into them. |
| 386 |
TmpFile tmp_root_cert(root_cert_); |
| 387 |
TmpFile tmp_identity_key(private_key_); |
| 388 |
TmpFile tmp_identity_cert(cert_chain_); |
| 389 |
// Create ClientOptions using FileWatcherCertificateProvider. |
| 390 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 391 |
auto provider = MakeRefCounted<FileWatcherCertificateProvider>( |
| 392 |
tmp_identity_key.name(), tmp_identity_cert.name(), tmp_root_cert.name(), |
| 393 |
1); |
| 394 |
options->set_certificate_provider(std::move(provider)); |
| 395 |
options->set_watch_root_cert(true); |
| 396 |
options->set_watch_identity_pair(true); |
| 397 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 398 |
ASSERT_NE(credentials, nullptr); |
| 399 |
ChannelArgs new_args; |
| 400 |
auto connector = credentials->create_security_connector( |
| 401 |
nullptr, "random targets", &new_args); |
| 402 |
ASSERT_NE(connector, nullptr); |
| 403 |
TlsChannelSecurityConnector* tls_connector = |
| 404 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 405 |
// Expect to see the credential data. |
| 406 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 407 |
ASSERT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 408 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_); |
| 409 |
ASSERT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 410 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), |
| 411 |
MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 412 |
// Copy new data to files. |
| 413 |
// TODO(ZhenLian): right now it is not completely atomic. Use the real atomic |
| 414 |
// update when the directory renaming is added in gpr. |
| 415 |
tmp_root_cert.RewriteFile(root_cert_2_); |
| 416 |
tmp_identity_key.RewriteFile(private_key_2_); |
| 417 |
tmp_identity_cert.RewriteFile(cert_chain_2_); |
| 418 |
// Wait 10 seconds for the provider's refresh thread to read the updated |
| 419 |
// files. |
| 420 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
| 421 |
gpr_time_from_seconds(10, GPR_TIMESPAN))); |
| 422 |
// Expect to see new credential data loaded by the security connector. |
| 423 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 424 |
ASSERT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 425 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_2_); |
| 426 |
ASSERT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 427 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), |
| 428 |
MakeCertKeyPairs(private_key_2_.c_str(), cert_chain_2_.c_str())); |
| 429 |
} |
| 430 |
|
| 431 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 432 |
ClientOptionsWithCertWatcherProviderOnDeletedFiles) { |
| 433 |
// Create temporary files and copy cert data into it. |
| 434 |
auto tmp_root_cert = std::make_unique<TmpFile>(root_cert_); |
| 435 |
auto tmp_identity_key = std::make_unique<TmpFile>(private_key_); |
| 436 |
auto tmp_identity_cert = std::make_unique<TmpFile>(cert_chain_); |
| 437 |
// Create ClientOptions using FileWatcherCertificateProvider. |
| 438 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 439 |
auto provider = MakeRefCounted<FileWatcherCertificateProvider>( |
| 440 |
tmp_identity_key->name(), tmp_identity_cert->name(), |
| 441 |
tmp_root_cert->name(), 1); |
| 442 |
options->set_certificate_provider(std::move(provider)); |
| 443 |
options->set_watch_root_cert(true); |
| 444 |
options->set_watch_identity_pair(true); |
| 445 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 446 |
ASSERT_NE(credentials, nullptr); |
| 447 |
ChannelArgs new_args; |
| 448 |
auto connector = credentials->create_security_connector( |
| 449 |
nullptr, "random targets", &new_args); |
| 450 |
ASSERT_NE(connector, nullptr); |
| 451 |
TlsChannelSecurityConnector* tls_connector = |
| 452 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 453 |
// The initial data is all good, so we expect to have successful credential |
| 454 |
// updates. |
| 455 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 456 |
ASSERT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 457 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_); |
| 458 |
ASSERT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 459 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), |
| 460 |
MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 461 |
// Delete TmpFile objects, which will remove the corresponding files. |
| 462 |
tmp_root_cert.reset(); |
| 463 |
tmp_identity_key.reset(); |
| 464 |
tmp_identity_cert.reset(); |
| 465 |
// Wait 10 seconds for the provider's refresh thread to read the deleted |
| 466 |
// files. |
| 467 |
gpr_sleep_until(gpr_time_add(gpr_now(GPR_CLOCK_MONOTONIC), |
| 468 |
gpr_time_from_seconds(10, GPR_TIMESPAN))); |
| 469 |
// It's a bit hard to test if errors are sent to the security connector, |
| 470 |
// because the security connector simply logs the error. We will see the err |
| 471 |
// messages if we open the log. |
| 472 |
// The old certs should still being used. |
| 473 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 474 |
ASSERT_TRUE(tls_connector->RootCertsForTesting().has_value()); |
| 475 |
EXPECT_EQ(tls_connector->RootCertsForTesting(), root_cert_); |
| 476 |
ASSERT_TRUE(tls_connector->KeyCertPairListForTesting().has_value()); |
| 477 |
EXPECT_EQ(tls_connector->KeyCertPairListForTesting(), |
| 478 |
MakeCertKeyPairs(private_key_.c_str(), cert_chain_.c_str())); |
| 479 |
} |
| 480 |
|
| 481 |
// |
| 482 |
// Tests for ExternalCertificateVerifier. |
| 483 |
// It will only test the creation of security connector, so the actual verify |
| 484 |
// logic is not invoked. |
| 485 |
// |
| 486 |
|
| 487 |
TEST_F(GrpcTlsCredentialsOptionsTest, ClientOptionsWithExternalVerifier) { |
| 488 |
auto* sync_verifier_ = new SyncExternalVerifier(true); |
| 489 |
ExternalCertificateVerifier core_external_verifier(sync_verifier_->base()); |
| 490 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 491 |
options->set_verify_server_cert(true); |
| 492 |
options->set_certificate_verifier(core_external_verifier.Ref()); |
| 493 |
options->set_check_call_host(false); |
| 494 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 495 |
ASSERT_NE(credentials, nullptr); |
| 496 |
ChannelArgs new_args; |
| 497 |
auto connector = credentials->create_security_connector( |
| 498 |
nullptr, "random targets", &new_args); |
| 499 |
ASSERT_NE(connector, nullptr); |
| 500 |
TlsChannelSecurityConnector* tls_connector = |
| 501 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 502 |
EXPECT_NE(tls_connector, nullptr); |
| 503 |
} |
| 504 |
|
| 505 |
TEST_F(GrpcTlsCredentialsOptionsTest, ServerOptionsWithExternalVerifier) { |
| 506 |
auto* sync_verifier_ = new SyncExternalVerifier(true); |
| 507 |
ExternalCertificateVerifier core_external_verifier(sync_verifier_->base()); |
| 508 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 509 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 510 |
options->set_certificate_verifier(core_external_verifier.Ref()); |
| 511 |
// On server side we have to set the provider providing identity certs. |
| 512 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 513 |
root_cert_, PemKeyCertPairList()); |
| 514 |
options->set_certificate_provider(std::move(provider)); |
| 515 |
options->set_watch_identity_pair(true); |
| 516 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 517 |
ASSERT_NE(credentials, nullptr); |
| 518 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 519 |
ASSERT_NE(connector, nullptr); |
| 520 |
TlsServerSecurityConnector* tls_connector = |
| 521 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 522 |
EXPECT_NE(tls_connector, nullptr); |
| 523 |
} |
| 524 |
|
| 525 |
// |
| 526 |
// Tests for HostnameCertificateVerifier. |
| 527 |
// |
| 528 |
|
| 529 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 530 |
ClientOptionsWithHostnameCertificateVerifier) { |
| 531 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 532 |
options->set_verify_server_cert(true); |
| 533 |
options->set_certificate_verifier(hostname_certificate_verifier_.Ref()); |
| 534 |
auto credentials = MakeRefCounted<TlsCredentials>(options); |
| 535 |
ASSERT_NE(credentials, nullptr); |
| 536 |
ChannelArgs new_args; |
| 537 |
auto connector = credentials->create_security_connector( |
| 538 |
nullptr, "random targets", &new_args); |
| 539 |
ASSERT_NE(connector, nullptr); |
| 540 |
TlsChannelSecurityConnector* tls_connector = |
| 541 |
static_cast<TlsChannelSecurityConnector*>(connector.get()); |
| 542 |
EXPECT_NE(tls_connector->ClientHandshakerFactoryForTesting(), nullptr); |
| 543 |
} |
| 544 |
|
| 545 |
TEST_F(GrpcTlsCredentialsOptionsTest, |
| 546 |
ServerOptionsWithHostnameCertificateVerifier) { |
| 547 |
auto options = MakeRefCounted<grpc_tls_credentials_options>(); |
| 548 |
options->set_cert_request_type(GRPC_SSL_DONT_REQUEST_CLIENT_CERTIFICATE); |
| 549 |
options->set_certificate_verifier(hostname_certificate_verifier_.Ref()); |
| 550 |
// On server side we have to set the provider providing identity certs. |
| 551 |
auto provider = MakeRefCounted<StaticDataCertificateProvider>( |
| 552 |
root_cert_, PemKeyCertPairList()); |
| 553 |
options->set_certificate_provider(std::move(provider)); |
| 554 |
options->set_watch_identity_pair(true); |
| 555 |
auto credentials = MakeRefCounted<TlsServerCredentials>(options); |
| 556 |
ASSERT_NE(credentials, nullptr); |
| 557 |
auto connector = credentials->create_security_connector(ChannelArgs()); |
| 558 |
ASSERT_NE(connector, nullptr); |
| 559 |
TlsServerSecurityConnector* tls_connector = |
| 560 |
static_cast<TlsServerSecurityConnector*>(connector.get()); |
| 561 |
EXPECT_EQ(tls_connector->ServerHandshakerFactoryForTesting(), nullptr); |
| 562 |
} |
| 563 |
|
| 564 |
} // namespace testing |
| 565 |
|
| 566 |
} // namespace grpc_core |
| 567 |
|
| 568 |
int main(int argc, char** argv) { |
| 569 |
grpc::testing::TestEnvironment env(&argc, argv); |
| 570 |
GPR_GLOBAL_CONFIG_SET(grpc_default_ssl_roots_file_path, CA_CERT_PATH); |
| 571 |
::testing::InitGoogleTest(&argc, argv); |
| 572 |
grpc_init(); |
| 573 |
int ret = RUN_ALL_TESTS(); |
| 574 |
grpc_shutdown(); |
| 575 |
return ret; |
| 576 |
} |