mirror of https://github.com/grpc/grpc.git
Move XdsChannelCreds to CoreConfiguration (#28746)
* Move XdsChannelCreds to CoreConfiguration * move xDS channel creds files to src/core/lib/security/credentials/xds * Change back to returning a RefCountedPtr. * make remove "xds_" from xds_channel_* files. * Renamed to address comments. * clang fix * Fix another clang errorreviewable/pr25586/r27^2
parent
4169f24dcc
commit
172120f6b4
28 changed files with 476 additions and 499 deletions
@ -1,108 +0,0 @@ |
||||
//
|
||||
// Copyright 2019 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/ext/xds/xds_channel_creds.h" |
||||
|
||||
#include "src/core/lib/security/credentials/fake/fake_credentials.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
namespace { |
||||
|
||||
using ChannelCredsMap = |
||||
std::map<absl::string_view, std::unique_ptr<XdsChannelCredsImpl>>; |
||||
ChannelCredsMap* g_creds = nullptr; |
||||
|
||||
} // namespace
|
||||
|
||||
//
|
||||
// XdsChannelCredsImpl implementations for default-supported cred types.
|
||||
//
|
||||
|
||||
class GoogleDefaultXdsChannelCredsImpl : public XdsChannelCredsImpl { |
||||
public: |
||||
absl::string_view creds_type() const override { return "google_default"; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateXdsChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_google_default_credentials_create(nullptr)); |
||||
} |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
}; |
||||
|
||||
class InsecureXdsChannelCredsImpl : public XdsChannelCredsImpl { |
||||
public: |
||||
absl::string_view creds_type() const override { return "insecure"; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateXdsChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_insecure_credentials_create()); |
||||
} |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
}; |
||||
|
||||
class FakeXdsChannelCredsImpl : public XdsChannelCredsImpl { |
||||
public: |
||||
absl::string_view creds_type() const override { return "fake"; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateXdsChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_fake_transport_security_credentials_create()); |
||||
} |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
}; |
||||
|
||||
//
|
||||
// XdsChannelCredsRegistry
|
||||
//
|
||||
|
||||
bool XdsChannelCredsRegistry::IsSupported(const std::string& creds_type) { |
||||
return g_creds->find(creds_type) != g_creds->end(); |
||||
} |
||||
|
||||
bool XdsChannelCredsRegistry::IsValidConfig(const std::string& creds_type, |
||||
const Json& config) { |
||||
const auto iter = g_creds->find(creds_type); |
||||
if (iter == g_creds->cend()) return false; |
||||
return iter->second->IsValidConfig(config); |
||||
} |
||||
|
||||
RefCountedPtr<grpc_channel_credentials> |
||||
XdsChannelCredsRegistry::CreateXdsChannelCreds(const std::string& creds_type, |
||||
const Json& config) { |
||||
const auto iter = g_creds->find(creds_type); |
||||
if (iter == g_creds->cend()) return nullptr; |
||||
return iter->second->CreateXdsChannelCreds(config); |
||||
} |
||||
|
||||
void XdsChannelCredsRegistry::Init() { |
||||
g_creds = new ChannelCredsMap(); |
||||
RegisterXdsChannelCreds( |
||||
absl::make_unique<GoogleDefaultXdsChannelCredsImpl>()); |
||||
RegisterXdsChannelCreds(absl::make_unique<InsecureXdsChannelCredsImpl>()); |
||||
RegisterXdsChannelCreds(absl::make_unique<FakeXdsChannelCredsImpl>()); |
||||
} |
||||
|
||||
void XdsChannelCredsRegistry::Shutdown() { delete g_creds; } |
||||
|
||||
void XdsChannelCredsRegistry::RegisterXdsChannelCreds( |
||||
std::unique_ptr<XdsChannelCredsImpl> creds) { |
||||
(*g_creds)[creds->creds_type()] = std::move(creds); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
@ -1,50 +0,0 @@ |
||||
//
|
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#ifndef GRPC_CORE_EXT_XDS_XDS_CHANNEL_CREDS_H |
||||
#define GRPC_CORE_EXT_XDS_XDS_CHANNEL_CREDS_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/json/json.h" |
||||
#include "src/core/lib/security/credentials/credentials.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
class XdsChannelCredsImpl { |
||||
public: |
||||
virtual ~XdsChannelCredsImpl() {} |
||||
virtual absl::string_view creds_type() const = 0; |
||||
virtual bool IsValidConfig(const Json& config) const = 0; |
||||
virtual RefCountedPtr<grpc_channel_credentials> CreateXdsChannelCreds( |
||||
const Json& config) const = 0; |
||||
}; |
||||
|
||||
class XdsChannelCredsRegistry { |
||||
public: |
||||
static bool IsSupported(const std::string& creds_type); |
||||
static bool IsValidConfig(const std::string& creds_type, const Json& config); |
||||
static RefCountedPtr<grpc_channel_credentials> CreateXdsChannelCreds( |
||||
const std::string& creds_type, const Json& config); |
||||
static void Init(); |
||||
static void Shutdown(); |
||||
static void RegisterXdsChannelCreds( |
||||
std::unique_ptr<XdsChannelCredsImpl> creds); |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_EXT_XDS_XDS_CHANNEL_CREDS_H
|
@ -0,0 +1,97 @@ |
||||
//
|
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
|
||||
#ifndef GRPC_CORE_LIB_SECURITY_CREDENTIALS_CHANNEL_CREDS_REGISTRY_H |
||||
#define GRPC_CORE_LIB_SECURITY_CREDENTIALS_CHANNEL_CREDS_REGISTRY_H |
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include <grpc/impl/codegen/grpc_types.h> |
||||
|
||||
#include "src/core/lib/json/json.h" |
||||
|
||||
struct grpc_channel_credentials; |
||||
|
||||
namespace grpc_core { |
||||
|
||||
template <typename T = grpc_channel_credentials> |
||||
class ChannelCredsFactory final { |
||||
public: |
||||
virtual ~ChannelCredsFactory() {} |
||||
virtual absl::string_view creds_type() const = delete; |
||||
virtual bool IsValidConfig(const Json& config) const = delete; |
||||
virtual RefCountedPtr<T> CreateChannelCreds(const Json& config) const = |
||||
delete; |
||||
}; |
||||
|
||||
template <> |
||||
class ChannelCredsFactory<grpc_channel_credentials> { |
||||
public: |
||||
virtual ~ChannelCredsFactory() {} |
||||
virtual absl::string_view creds_type() const = 0; |
||||
virtual bool IsValidConfig(const Json& config) const = 0; |
||||
virtual RefCountedPtr<grpc_channel_credentials> CreateChannelCreds( |
||||
const Json& config) const = 0; |
||||
}; |
||||
|
||||
template <typename T = grpc_channel_credentials> |
||||
class ChannelCredsRegistry { |
||||
public: |
||||
static_assert(std::is_base_of<grpc_channel_credentials, T>::value, |
||||
"ChannelCredsRegistry must be instantiated with " |
||||
"grpc_channel_credentials."); |
||||
class Builder { |
||||
public: |
||||
void RegisterChannelCredsFactory( |
||||
std::unique_ptr<ChannelCredsFactory<T>> factory) { |
||||
factories_[factory->creds_type()] = std::move(factory); |
||||
} |
||||
ChannelCredsRegistry Build() { |
||||
ChannelCredsRegistry<T> registry; |
||||
registry.factories_.swap(factories_); |
||||
return registry; |
||||
} |
||||
|
||||
private: |
||||
std::map<absl::string_view, std::unique_ptr<ChannelCredsFactory<T>>> |
||||
factories_; |
||||
}; |
||||
|
||||
bool IsSupported(const std::string& creds_type) const { |
||||
return factories_.find(creds_type) != factories_.end(); |
||||
} |
||||
|
||||
bool IsValidConfig(const std::string& creds_type, const Json& config) const { |
||||
const auto iter = factories_.find(creds_type); |
||||
return iter != factories_.cend() && iter->second->IsValidConfig(config); |
||||
} |
||||
|
||||
RefCountedPtr<T> CreateChannelCreds(const std::string& creds_type, |
||||
const Json& config) const { |
||||
const auto iter = factories_.find(creds_type); |
||||
if (iter == factories_.cend()) return nullptr; |
||||
return iter->second->CreateChannelCreds(config); |
||||
} |
||||
|
||||
private: |
||||
ChannelCredsRegistry() = default; |
||||
std::map<absl::string_view, std::unique_ptr<ChannelCredsFactory<T>>> |
||||
factories_; |
||||
}; |
||||
|
||||
} // namespace grpc_core
|
||||
|
||||
#endif // GRPC_CORE_LIB_SECURITY_CREDENTIALS_CHANNEL_CREDS_REGISTRY_H
|
@ -0,0 +1,70 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include <grpc/support/port_platform.h> |
||||
|
||||
#include "src/core/lib/config/core_configuration.h" |
||||
#include "src/core/lib/json/json.h" |
||||
#include "src/core/lib/security/credentials/credentials.h" |
||||
#include "src/core/lib/security/credentials/fake/fake_credentials.h" |
||||
|
||||
namespace grpc_core { |
||||
|
||||
class GoogleDefaultChannelCredsFactory : public ChannelCredsFactory<> { |
||||
public: |
||||
absl::string_view creds_type() const override { return "google_default"; } |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_google_default_credentials_create(nullptr)); |
||||
} |
||||
}; |
||||
|
||||
class InsecureChannelCredsFactory : public ChannelCredsFactory<> { |
||||
public: |
||||
absl::string_view creds_type() const override { return "insecure"; } |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_insecure_credentials_create()); |
||||
} |
||||
}; |
||||
|
||||
class FakeChannelCredsFactory : public ChannelCredsFactory<> { |
||||
public: |
||||
absl::string_view creds_type() const override { return "fake"; } |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_fake_transport_security_credentials_create()); |
||||
} |
||||
}; |
||||
|
||||
void RegisterChannelDefaultCreds(CoreConfiguration::Builder* builder) { |
||||
builder->channel_creds_registry()->RegisterChannelCredsFactory( |
||||
absl::make_unique<GoogleDefaultChannelCredsFactory>()); |
||||
builder->channel_creds_registry()->RegisterChannelCredsFactory( |
||||
absl::make_unique<InsecureChannelCredsFactory>()); |
||||
builder->channel_creds_registry()->RegisterChannelCredsFactory( |
||||
absl::make_unique<FakeChannelCredsFactory>()); |
||||
} |
||||
|
||||
} // namespace grpc_core
|
@ -0,0 +1,104 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include "src/core/lib/security/credentials/channel_creds_registry.h" |
||||
|
||||
#include <gmock/gmock.h> |
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/grpc.h> |
||||
|
||||
#include "src/core/lib/config/core_configuration.h" |
||||
#include "src/core/lib/security/credentials/channel_creds_registry.h" |
||||
#include "src/core/lib/security/credentials/fake/fake_credentials.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace testing { |
||||
namespace { |
||||
|
||||
class TestChannelCredsFactory : public ChannelCredsFactory<> { |
||||
public: |
||||
absl::string_view creds_type() const override { return "test"; } |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_fake_transport_security_credentials_create()); |
||||
} |
||||
}; |
||||
|
||||
TEST(ChannelCredsRegistry2Test, DefaultCreds) { |
||||
// Default creds.
|
||||
EXPECT_TRUE(CoreConfiguration::Get().channel_creds_registry().IsSupported( |
||||
"google_default")); |
||||
EXPECT_TRUE(CoreConfiguration::Get().channel_creds_registry().IsSupported( |
||||
"insecure")); |
||||
EXPECT_TRUE( |
||||
CoreConfiguration::Get().channel_creds_registry().IsSupported("fake")); |
||||
|
||||
// Non-default creds.
|
||||
EXPECT_EQ( |
||||
CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds( |
||||
"test", Json()), |
||||
nullptr); |
||||
EXPECT_EQ( |
||||
CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds( |
||||
"", Json()), |
||||
nullptr); |
||||
} |
||||
|
||||
TEST(ChannelCredsRegistry2Test, Register) { |
||||
CoreConfiguration::Reset(); |
||||
grpc_init(); |
||||
|
||||
// Before registration.
|
||||
EXPECT_FALSE( |
||||
CoreConfiguration::Get().channel_creds_registry().IsSupported("test")); |
||||
EXPECT_EQ( |
||||
CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds( |
||||
"test", Json()), |
||||
nullptr); |
||||
|
||||
// Registration.
|
||||
CoreConfiguration::BuildSpecialConfiguration( |
||||
[](CoreConfiguration::Builder* builder) { |
||||
BuildCoreConfiguration(builder); |
||||
builder->channel_creds_registry()->RegisterChannelCredsFactory( |
||||
absl::make_unique<TestChannelCredsFactory>()); |
||||
}); |
||||
|
||||
RefCountedPtr<grpc_channel_credentials> test_cred( |
||||
CoreConfiguration::Get().channel_creds_registry().CreateChannelCreds( |
||||
"test", Json())); |
||||
EXPECT_TRUE( |
||||
CoreConfiguration::Get().channel_creds_registry().IsSupported("test")); |
||||
EXPECT_NE(test_cred.get(), nullptr); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace testing
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
grpc::testing::TestEnvironment env(argc, argv); |
||||
grpc_init(); |
||||
auto result = RUN_ALL_TESTS(); |
||||
return result; |
||||
} |
@ -1,79 +0,0 @@ |
||||
//
|
||||
//
|
||||
// Copyright 2022 gRPC authors.
|
||||
//
|
||||
// Licensed under the Apache License, Version 2.0 (the "License");
|
||||
// you may not use this file except in compliance with the License.
|
||||
// You may obtain a copy of the License at
|
||||
//
|
||||
// http://www.apache.org/licenses/LICENSE-2.0
|
||||
//
|
||||
// Unless required by applicable law or agreed to in writing, software
|
||||
// distributed under the License is distributed on an "AS IS" BASIS,
|
||||
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
// See the License for the specific language governing permissions and
|
||||
// limitations under the License.
|
||||
//
|
||||
//
|
||||
|
||||
#include <gmock/gmock.h> |
||||
#include <gtest/gtest.h> |
||||
|
||||
#include <grpc/grpc.h> |
||||
|
||||
#include "src/core/ext/xds/xds_bootstrap.h" |
||||
#include "src/core/ext/xds/xds_channel_creds.h" |
||||
#include "src/core/lib/security/credentials/fake/fake_credentials.h" |
||||
#include "test/core/util/test_config.h" |
||||
|
||||
namespace grpc_core { |
||||
namespace testing { |
||||
namespace { |
||||
|
||||
class TestXdsChannelCredsImpl : public XdsChannelCredsImpl { |
||||
public: |
||||
absl::string_view creds_type() const override { return "test"; } |
||||
bool IsValidConfig(const Json& /*config*/) const override { return true; } |
||||
RefCountedPtr<grpc_channel_credentials> CreateXdsChannelCreds( |
||||
const Json& /*config*/) const override { |
||||
return RefCountedPtr<grpc_channel_credentials>( |
||||
grpc_fake_transport_security_credentials_create()); |
||||
} |
||||
}; |
||||
|
||||
TEST(XdsChannelCredsRegistryTest, DefaultCreds) { // Default creds.
|
||||
EXPECT_TRUE(XdsChannelCredsRegistry::IsSupported("google_default")); |
||||
EXPECT_TRUE(XdsChannelCredsRegistry::IsSupported("insecure")); |
||||
EXPECT_TRUE(XdsChannelCredsRegistry::IsSupported("fake")); |
||||
|
||||
// Non-default creds.
|
||||
EXPECT_EQ(XdsChannelCredsRegistry::CreateXdsChannelCreds("test", Json()), |
||||
nullptr); |
||||
EXPECT_EQ(XdsChannelCredsRegistry::CreateXdsChannelCreds("", Json()), |
||||
nullptr); |
||||
} |
||||
|
||||
TEST(XdsChannelCredsRegistryTest, Register) { |
||||
// Before registration.
|
||||
EXPECT_FALSE(XdsChannelCredsRegistry::IsSupported("test")); |
||||
EXPECT_EQ(XdsChannelCredsRegistry::CreateXdsChannelCreds("test", Json()), |
||||
nullptr); |
||||
|
||||
// Registration.
|
||||
XdsChannelCredsRegistry::RegisterXdsChannelCreds( |
||||
absl::make_unique<TestXdsChannelCredsImpl>()); |
||||
EXPECT_NE(XdsChannelCredsRegistry::CreateXdsChannelCreds("test", Json()), |
||||
nullptr); |
||||
} |
||||
|
||||
} // namespace
|
||||
} // namespace testing
|
||||
} // namespace grpc_core
|
||||
|
||||
int main(int argc, char** argv) { |
||||
::testing::InitGoogleTest(&argc, argv); |
||||
grpc::testing::TestEnvironment env(argc, argv); |
||||
grpc_init(); |
||||
auto result = RUN_ALL_TESTS(); |
||||
return result; |
||||
} |
Loading…
Reference in new issue