Test credentials provider update

pull/8838/head
yang-g 8 years ago
parent 01eda53c81
commit 8e9fc9bd0e
  1. 19
      test/cpp/end2end/async_end2end_test.cc
  2. 20
      test/cpp/end2end/end2end_test.cc
  3. 1
      test/cpp/interop/client_helper.cc
  4. 12
      test/cpp/interop/server_helper.cc
  5. 50
      test/cpp/util/create_test_channel.cc
  6. 52
      test/cpp/util/test_credentials_provider.cc
  7. 50
      test/cpp/util/test_credentials_provider.h

@ -254,7 +254,8 @@ class AsyncEnd2endTest : public ::testing::TestWithParam<TestScenario> {
// Setup server
ServerBuilder builder;
auto server_creds = GetServerCredentials(GetParam().credentials_type);
auto server_creds = GetCredentialsProvider()->GetServerCredentials(
GetParam().credentials_type);
builder.AddListeningPort(server_address_.str(), server_creds);
builder.RegisterService(&service_);
cq_ = builder.AddCompletionQueue();
@ -283,8 +284,8 @@ class AsyncEnd2endTest : public ::testing::TestWithParam<TestScenario> {
void ResetStub() {
ChannelArguments args;
auto channel_creds =
GetChannelCredentials(GetParam().credentials_type, &args);
auto channel_creds = GetCredentialsProvider()->GetChannelCredentials(
GetParam().credentials_type, &args);
std::shared_ptr<Channel> channel =
CreateCustomChannel(server_address_.str(), channel_creds, args);
stub_ = grpc::testing::EchoTestService::NewStub(channel);
@ -892,8 +893,8 @@ TEST_P(AsyncEnd2endTest, ServerCheckDone) {
TEST_P(AsyncEnd2endTest, UnimplementedRpc) {
ChannelArguments args;
auto channel_creds =
GetChannelCredentials(GetParam().credentials_type, &args);
auto channel_creds = GetCredentialsProvider()->GetChannelCredentials(
GetParam().credentials_type, &args);
std::shared_ptr<Channel> channel =
CreateCustomChannel(server_address_.str(), channel_creds, args);
std::unique_ptr<grpc::testing::UnimplementedEchoService::Stub> stub;
@ -1404,11 +1405,15 @@ std::vector<TestScenario> CreateTestScenarios(bool test_disable_blocking,
std::vector<grpc::string> credentials_types;
std::vector<grpc::string> messages;
credentials_types.push_back(kInsecureCredentialsType);
auto sec_list = GetSecureCredentialsTypeList();
if (GetCredentialsProvider()->GetChannelCredentials(kInsecureCredentialsType,
nullptr) != nullptr) {
credentials_types.push_back(kInsecureCredentialsType);
}
auto sec_list = GetCredentialsProvider()->GetSecureCredentialsTypeList();
for (auto sec = sec_list.begin(); sec != sec_list.end(); sec++) {
credentials_types.push_back(*sec);
}
GPR_ASSERT(!credentials_types.empty());
messages.push_back("Hello");
for (int sz = 1; sz < test_big_limit; sz *= 2) {

@ -242,7 +242,8 @@ class End2endTest : public ::testing::TestWithParam<TestScenario> {
// Setup server
ServerBuilder builder;
ConfigureServerBuilder(&builder);
auto server_creds = GetServerCredentials(GetParam().credentials_type);
auto server_creds = GetCredentialsProvider()->GetServerCredentials(
GetParam().credentials_type);
if (GetParam().credentials_type != kInsecureCredentialsType) {
server_creds->SetAuthMetadataProcessor(processor);
}
@ -270,8 +271,8 @@ class End2endTest : public ::testing::TestWithParam<TestScenario> {
}
EXPECT_TRUE(is_server_started_);
ChannelArguments args;
auto channel_creds =
GetChannelCredentials(GetParam().credentials_type, &args);
auto channel_creds = GetCredentialsProvider()->GetChannelCredentials(
GetParam().credentials_type, &args);
if (!user_agent_prefix_.empty()) {
args.SetUserAgentPrefix(user_agent_prefix_);
}
@ -1520,11 +1521,18 @@ std::vector<TestScenario> CreateTestScenarios(bool use_proxy,
std::vector<TestScenario> scenarios;
std::vector<grpc::string> credentials_types;
if (test_secure) {
credentials_types = GetSecureCredentialsTypeList();
credentials_types =
GetCredentialsProvider()->GetSecureCredentialsTypeList();
}
if (test_insecure) {
credentials_types.push_back(kInsecureCredentialsType);
// Only add insecure credentials type when it is registered with the
// provider. User may create providers that do not have insecure.
if (GetCredentialsProvider()->GetChannelCredentials(
kInsecureCredentialsType, nullptr) != nullptr) {
credentials_types.push_back(kInsecureCredentialsType);
}
}
GPR_ASSERT(!credentials_types.empty());
for (auto it = credentials_types.begin(); it != credentials_types.end();
++it) {
scenarios.emplace_back(false, *it);
@ -1541,7 +1549,7 @@ INSTANTIATE_TEST_CASE_P(End2end, End2endTest,
INSTANTIATE_TEST_CASE_P(End2endServerTryCancel, End2endServerTryCancelTest,
::testing::ValuesIn(CreateTestScenarios(false, true,
false)));
true)));
INSTANTIATE_TEST_CASE_P(ProxyEnd2end, ProxyEnd2endTest,
::testing::ValuesIn(CreateTestScenarios(true, true,

@ -50,6 +50,7 @@
#include "src/cpp/client/secure_credentials.h"
#include "test/core/security/oauth2_utils.h"
#include "test/cpp/util/create_test_channel.h"
#include "test/cpp/util/test_credentials_provider.h"
DECLARE_bool(use_tls);
DECLARE_bool(use_test_ca);

@ -39,7 +39,7 @@
#include <grpc++/security/server_credentials.h>
#include "src/core/lib/surface/call_test_only.h"
#include "test/core/end2end/data/ssl_test_data.h"
#include "test/cpp/util/test_credentials_provider.h"
DECLARE_bool(use_tls);
@ -48,14 +48,10 @@ namespace testing {
std::shared_ptr<ServerCredentials> CreateInteropServerCredentials() {
if (FLAGS_use_tls) {
SslServerCredentialsOptions::PemKeyCertPair pkcp = {test_server1_key,
test_server1_cert};
SslServerCredentialsOptions ssl_opts;
ssl_opts.pem_root_certs = "";
ssl_opts.pem_key_cert_pairs.push_back(pkcp);
return SslServerCredentials(ssl_opts);
return GetCredentialsProvider()->GetServerCredentials(kTlsCredentialsType);
} else {
return InsecureServerCredentials();
return GetCredentialsProvider()->GetServerCredentials(
kInsecureCredentialsType);
}
}

@ -35,11 +35,37 @@
#include <grpc++/create_channel.h>
#include <grpc++/security/credentials.h>
#include <grpc/support/log.h>
#include "test/core/end2end/data/ssl_test_data.h"
#include "test/cpp/util/test_credentials_provider.h"
namespace grpc {
namespace {
const char kProdTlsCredentialsType[] = "prod_ssl";
class SslCredentialProvider : public testing::CredentialTypeProvider {
public:
std::shared_ptr<ChannelCredentials> GetChannelCredentials(
grpc::ChannelArguments* args) override {
return SslCredentials(SslCredentialsOptions());
}
std::shared_ptr<ServerCredentials> GetServerCredentials() override {
return nullptr;
}
};
gpr_once g_once_init_add_prod_ssl_provider = GPR_ONCE_INIT;
// Register ssl with non-test roots type to the credentials provider.
void AddProdSslType() {
testing::GetCredentialsProvider()->AddSecureType(
kProdTlsCredentialsType, std::unique_ptr<testing::CredentialTypeProvider>(
new SslCredentialProvider));
}
} // namespace
// When ssl is enabled, if server is empty, override_hostname is used to
// create channel. Otherwise, connect to server and override hostname if
// override_hostname is provided.
@ -61,16 +87,22 @@ std::shared_ptr<Channel> CreateTestChannel(
const std::shared_ptr<CallCredentials>& creds,
const ChannelArguments& args) {
ChannelArguments channel_args(args);
std::shared_ptr<ChannelCredentials> channel_creds;
if (enable_ssl) {
const char* roots_certs = use_prod_roots ? "" : test_root_cert;
SslCredentialsOptions ssl_opts = {roots_certs, "", ""};
std::shared_ptr<ChannelCredentials> channel_creds =
SslCredentials(ssl_opts);
if (!server.empty() && !override_hostname.empty()) {
channel_args.SetSslTargetNameOverride(override_hostname);
if (use_prod_roots) {
gpr_once_init(&g_once_init_add_prod_ssl_provider, &AddProdSslType);
channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
kProdTlsCredentialsType, &channel_args);
if (!server.empty() && !override_hostname.empty()) {
channel_args.SetSslTargetNameOverride(override_hostname);
}
} else {
// override_hostname is discarded as the provider handles it.
channel_creds = testing::GetCredentialsProvider()->GetChannelCredentials(
testing::kTlsCredentialsType, &channel_args);
}
GPR_ASSERT(channel_creds != nullptr);
const grpc::string& connect_to =
server.empty() ? override_hostname : server;
if (creds.get()) {

@ -43,25 +43,9 @@
#include "test/core/end2end/data/ssl_test_data.h"
namespace grpc {
namespace testing {
namespace {
using grpc::testing::CredentialTypeProvider;
// Provide test credentials. Thread-safe.
class CredentialsProvider {
public:
virtual ~CredentialsProvider() {}
virtual void AddSecureType(
const grpc::string& type,
std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
const grpc::string& type, ChannelArguments* args) = 0;
virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
const grpc::string& type) = 0;
virtual std::vector<grpc::string> GetSecureCredentialsTypeList() = 0;
};
class DefaultCredentialsProvider : public CredentialsProvider {
public:
~DefaultCredentialsProvider() override {}
@ -145,37 +129,21 @@ class DefaultCredentialsProvider : public CredentialsProvider {
added_secure_type_providers_;
};
gpr_once g_once_init_provider = GPR_ONCE_INIT;
CredentialsProvider* g_provider = nullptr;
void CreateDefaultProvider() { g_provider = new DefaultCredentialsProvider; }
CredentialsProvider* GetProvider() {
gpr_once_init(&g_once_init_provider, &CreateDefaultProvider);
return g_provider;
}
} // namespace
namespace testing {
void AddSecureType(const grpc::string& type,
std::unique_ptr<CredentialTypeProvider> type_provider) {
GetProvider()->AddSecureType(type, std::move(type_provider));
}
std::shared_ptr<ChannelCredentials> GetChannelCredentials(
const grpc::string& type, ChannelArguments* args) {
return GetProvider()->GetChannelCredentials(type, args);
}
std::shared_ptr<ServerCredentials> GetServerCredentials(
const grpc::string& type) {
return GetProvider()->GetServerCredentials(type);
CredentialsProvider* GetCredentialsProvider() {
if (g_provider == nullptr) {
g_provider = new DefaultCredentialsProvider;
}
return g_provider;
}
std::vector<grpc::string> GetSecureCredentialsTypeList() {
return GetProvider()->GetSecureCredentialsTypeList();
void SetCredentialsProvider(CredentialsProvider* provider) {
// For now, forbids overriding provider.
GPR_ASSERT(g_provider == nullptr);
g_provider = provider;
}
} // namespace testing

@ -59,23 +59,39 @@ class CredentialTypeProvider {
virtual std::shared_ptr<ServerCredentials> GetServerCredentials() = 0;
};
// Add a secure type in addition to the defaults above
// (kInsecureCredentialsType, kTlsCredentialsType) that can be returned from the
// functions below.
void AddSecureType(const grpc::string& type,
std::unique_ptr<CredentialTypeProvider> type_provider);
// Provide channel credentials according to the given type. Alter the channel
// arguments if needed.
std::shared_ptr<ChannelCredentials> GetChannelCredentials(
const grpc::string& type, ChannelArguments* args);
// Provide server credentials according to the given type.
std::shared_ptr<ServerCredentials> GetServerCredentials(
const grpc::string& type);
// Provide a list of secure credentials type.
std::vector<grpc::string> GetSecureCredentialsTypeList();
// Provide test credentials. Thread-safe.
class CredentialsProvider {
public:
virtual ~CredentialsProvider() {}
// Add a secure type in addition to the defaults. The default provider has
// (kInsecureCredentialsType, kTlsCredentialsType).
virtual void AddSecureType(
const grpc::string& type,
std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
// Provide channel credentials according to the given type. Alter the channel
// arguments if needed. Return nullptr if type is not registered.
virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
const grpc::string& type, ChannelArguments* args) = 0;
// Provide server credentials according to the given type.
// Return nullptr if type is not registered.
virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
const grpc::string& type) = 0;
// Provide a list of secure credentials type.
virtual std::vector<grpc::string> GetSecureCredentialsTypeList() = 0;
};
// Get the current provider. Create a default one if not set.
// Not thread-safe.
CredentialsProvider* GetCredentialsProvider();
// Set the global provider. Takes ownership. The previous set provider will be
// destroyed.
// Not thread-safe.
void SetCredentialsProvider(CredentialsProvider* provider);
} // namespace testing
} // namespace grpc

Loading…
Cancel
Save