mirror of https://github.com/grpc/grpc.git
Support custom xDS channel creds (#28486)
This patch introduces a factory to allow supporting custom xDS channel creds. Three types currently supported (fake, insecure, google_default) are registered by default for backward-compatibility.pull/28560/head
parent
17859fb6b5
commit
c02fe64bea
22 changed files with 353 additions and 43 deletions
@ -0,0 +1,108 @@ |
||||
//
|
||||
// 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
|
@ -0,0 +1,50 @@ |
||||
//
|
||||
// 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,79 @@ |
||||
//
|
||||
//
|
||||
// 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