forked from envoyproxy/envoy
-
Notifications
You must be signed in to change notification settings - Fork 63
Sds api #1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Sds api #1
Changes from 11 commits
Commits
Show all changes
20 commits
Select commit
Hold shift + click to select a range
a9ec642
clusters and listeners read static secrets from Bootstrap.static_reso…
mangchiandjjoe 6a270c4
Added secretManager() to ClusterManagerFactory interface
mangchiandjjoe 3d330c5
Removed unnecessary changes
mangchiandjjoe 79defaf
Changed the location of secret_manager argument
mangchiandjjoe df3e59c
Add SDS api.
JimmyCYJ b32cb31
fix format.
JimmyCYJ 8d0a41f
fix test files.
JimmyCYJ 0dd3cc8
fix format.
JimmyCYJ 7927531
fix format and mock interfaces.
JimmyCYJ 2f56357
fix tests.
JimmyCYJ 051d7b3
Minor changes SecretManager.
JimmyCYJ 0554e71
Use SdsApiPtr
JimmyCYJ fa631a1
minor fix
JimmyCYJ d6f4d28
updated readConfig()
JimmyCYJ 2fe5a73
Add period to comments.
JimmyCYJ 7c44cc3
Update BUILD file.
JimmyCYJ de31e41
Merge remote-tracking branch 'upstream/master' into sds_api
lizan 8398425
fix test
lizan 6db4351
Fix conflicts in test.
JimmyCYJ cc22768
Update test and use StreamSecrets as gRPC method in SdsApi
JimmyCYJ File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,65 @@ | ||
| #include "common/secret/sds_api.h" | ||
|
|
||
| #include <unordered_map> | ||
|
|
||
| #include "common/config/resources.h" | ||
| #include "common/config/subscription_factory.h" | ||
| #include "common/secret/secret_manager_util.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Secret { | ||
|
|
||
| SdsApi::SdsApi(Server::Instance& server, const envoy::api::v2::core::ConfigSource& sds_config, | ||
| std::string sds_config_hash, std::string sds_config_name) | ||
| : server_(server), sds_config_(sds_config), sds_config_source_hash_(sds_config_hash), | ||
| sds_config_name_(sds_config_name) { | ||
| server_.initManager().registerTarget(*this); | ||
| } | ||
|
|
||
| void SdsApi::initialize(std::function<void()> callback) { | ||
| initialize_callback_ = callback; | ||
| subscription_ = Envoy::Config::SubscriptionFactory::subscriptionFromConfigSource< | ||
| envoy::api::v2::auth::Secret>( | ||
| sds_config_, server_.localInfo().node(), server_.dispatcher(), server_.clusterManager(), | ||
| server_.random(), server_.stats(), /* rest_legacy_constructor */ nullptr, | ||
| "envoy.service.discovery.v2.SecretDiscoveryService.FetchSecrets", | ||
| // TODO(jaebong): replace next line with | ||
| // "envoy.service.discovery.v2.SecretDiscoveryService.StreamSecrets" to support streaming | ||
| // service | ||
| "envoy.service.discovery.v2.SecretDiscoveryService.FetchSecrets"); | ||
|
|
||
| Config::Utility::checkLocalInfo("sds", server_.localInfo()); | ||
|
|
||
| subscription_->start({sds_config_name_}, *this); | ||
| } | ||
|
|
||
| void SdsApi::onConfigUpdate(const ResourceVector& resources, const std::string&) { | ||
| for (const auto& resource : resources) { | ||
| switch (resource.type_case()) { | ||
| case envoy::api::v2::auth::Secret::kTlsCertificate: | ||
| server_.secretManager().addOrUpdateSecret(sds_config_source_hash_, resource); | ||
| break; | ||
| case envoy::api::v2::auth::Secret::kSessionTicketKeys: | ||
| NOT_IMPLEMENTED; | ||
| default: | ||
| throw EnvoyException("sds: invalid configuration"); | ||
| } | ||
| } | ||
|
|
||
| runInitializeCallbackIfAny(); | ||
| } | ||
|
|
||
| void SdsApi::onConfigUpdateFailed(const EnvoyException*) { | ||
| // We need to allow server startup to continue, even if we have a bad config. | ||
| runInitializeCallbackIfAny(); | ||
| } | ||
|
|
||
| void SdsApi::runInitializeCallbackIfAny() { | ||
| if (initialize_callback_) { | ||
| initialize_callback_(); | ||
| initialize_callback_ = nullptr; | ||
| } | ||
| } | ||
|
|
||
| } // namespace Secret | ||
| } // namespace Envoy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| #pragma once | ||
|
|
||
| #include <functional> | ||
|
|
||
| #include "envoy/api/v2/auth/cert.pb.h" | ||
| #include "envoy/api/v2/core/config_source.pb.h" | ||
| #include "envoy/config/subscription.h" | ||
| #include "envoy/server/instance.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Secret { | ||
|
|
||
| /** | ||
| * SDS API implementation that fetches secrets from SDS server via Subscription. | ||
| */ | ||
| class SdsApi : public Init::Target, Config::SubscriptionCallbacks<envoy::api::v2::auth::Secret> { | ||
| public: | ||
| SdsApi(Server::Instance& server, const envoy::api::v2::core::ConfigSource& sds_config, | ||
| std::string sds_config_hash, std::string sds_config_name); | ||
|
|
||
| // Init::Target | ||
| void initialize(std::function<void()> callback) override; | ||
|
|
||
| // Config::SubscriptionCallbacks | ||
| void onConfigUpdate(const ResourceVector& resources, const std::string& version_info) override; | ||
| void onConfigUpdateFailed(const EnvoyException* e) override; | ||
| std::string resourceName(const ProtobufWkt::Any& resource) override { | ||
| return MessageUtil::anyConvert<envoy::api::v2::auth::Secret>(resource).name(); | ||
| } | ||
|
|
||
| private: | ||
| void runInitializeCallbackIfAny(); | ||
|
|
||
| Server::Instance& server_; | ||
| const envoy::api::v2::core::ConfigSource sds_config_; | ||
| const std::string sds_config_source_hash_; | ||
| std::unique_ptr<Config::Subscription<envoy::api::v2::auth::Secret>> subscription_; | ||
| std::function<void()> initialize_callback_; | ||
| std::string sds_config_name_; | ||
| }; | ||
|
|
||
| } // namespace Secret | ||
| } // namespace Envoy |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,22 +1,40 @@ | ||
| #pragma once | ||
|
|
||
| #include <shared_mutex> | ||
| #include <unordered_map> | ||
|
|
||
| #include "envoy/secret/secret_manager.h" | ||
| #include "envoy/server/instance.h" | ||
| #include "envoy/ssl/tls_certificate_config.h" | ||
|
|
||
| #include "common/common/logger.h" | ||
| #include "common/secret/sds_api.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Secret { | ||
|
|
||
| class SecretManagerImpl : public SecretManager, Logger::Loggable<Logger::Id::upstream> { | ||
| public: | ||
| void addOrUpdateSecret(const envoy::api::v2::auth::Secret& secret) override; | ||
| const Ssl::TlsCertificateConfig* findTlsCertificate(const std::string& name) const override; | ||
| SecretManagerImpl(Server::Instance& server) : server_(server) {} | ||
|
|
||
| void addOrUpdateSecret(const std::string& config_source_hash, | ||
| const envoy::api::v2::auth::Secret& secret) override; | ||
| const Ssl::TlsCertificateConfig* findTlsCertificate(const std::string& config_source_hash, | ||
| const std::string& name) const override; | ||
| std::string addOrUpdateSdsService(const envoy::api::v2::core::ConfigSource& config_source, | ||
| std::string config_name) override; | ||
|
|
||
| private: | ||
| std::unordered_map<std::string, Ssl::TlsCertificateConfigPtr> tls_certificate_secrets_; | ||
| Server::Instance& server_; | ||
| // map hash code of SDS config source and SdsApi object | ||
| std::unordered_map<std::string, std::unique_ptr<SdsApi>> sds_apis_; | ||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. typedef |
||
| mutable std::shared_timed_mutex sds_api_mutex_; | ||
|
|
||
| // Manages pairs of name and Ssl::TlsCertificateConfig grouped by SDS config source hash. | ||
| // If SDS config source hash is empty, it is a static secret. | ||
| std::unordered_map<std::string, std::unordered_map<std::string, Ssl::TlsCertificateConfigPtr>> | ||
| tls_certificate_secrets_; | ||
| mutable std::shared_timed_mutex tls_certificate_secrets_mutex_; | ||
| }; | ||
|
|
||
| } // namespace Secret | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,37 @@ | ||
| #pragma once | ||
|
|
||
| #include "envoy/api/v2/core/config_source.pb.h" | ||
|
|
||
| #include "common/common/fmt.h" | ||
| #include "common/json/json_loader.h" | ||
| #include "common/protobuf/protobuf.h" | ||
|
|
||
| namespace Envoy { | ||
| namespace Secret { | ||
|
|
||
| class SecretManagerUtil { | ||
| public: | ||
| virtual ~SecretManagerUtil() {} | ||
|
|
||
| /** | ||
| * Calculate hash code of ConfigSource. To identify the same ConfigSource, calculate the hash | ||
| * code from the ConfigSource | ||
| * | ||
| * @param config_source envoy::api::v2::core::ConfigSource | ||
| * @return hash code | ||
| */ | ||
| static std::string configSourceHash(const envoy::api::v2::core::ConfigSource& config_source) { | ||
| std::string jsonstr; | ||
| if (Protobuf::util::MessageToJsonString(config_source, &jsonstr).ok()) { | ||
| auto obj = Json::Factory::loadFromString(jsonstr); | ||
| if (obj.get() != nullptr) { | ||
| return std::to_string(obj->hash()); | ||
| } | ||
| } | ||
| throw EnvoyException( | ||
| fmt::format("Invalid ConfigSource message: {}", config_source.DebugString())); | ||
| } | ||
| }; | ||
|
|
||
| } // namespace Secret | ||
| } // namespace Envoy |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
no need of std::move
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Done.