diff options
Diffstat (limited to 'src/cpp')
-rw-r--r-- | src/cpp/client/client_context.cc | 12 | ||||
-rw-r--r-- | src/cpp/client/create_channel.cc | 2 | ||||
-rw-r--r-- | src/cpp/client/insecure_credentials.cc | 7 | ||||
-rw-r--r-- | src/cpp/client/secure_credentials.cc | 32 | ||||
-rw-r--r-- | src/cpp/client/secure_credentials.h | 1 |
5 files changed, 37 insertions, 17 deletions
diff --git a/src/cpp/client/client_context.cc b/src/cpp/client/client_context.cc index f38a694734..72cdd49d19 100644 --- a/src/cpp/client/client_context.cc +++ b/src/cpp/client/client_context.cc @@ -34,6 +34,7 @@ #include <grpc++/client_context.h> #include <grpc/grpc.h> +#include <grpc++/credentials.h> #include <grpc++/time.h> namespace grpc { @@ -63,6 +64,17 @@ void ClientContext::AddMetadata(const grpc::string& meta_key, send_initial_metadata_.insert(std::make_pair(meta_key, meta_value)); } +void ClientContext::set_call(grpc_call* call, + const std::shared_ptr<ChannelInterface>& channel) { + GPR_ASSERT(call_ == nullptr); + call_ = call; + channel_ = channel; + if (creds_ && !creds_->ApplyToCall(call_)) { + grpc_call_cancel_with_status(call, GRPC_STATUS_CANCELLED, + "Failed to set credentials to rpc."); + } +} + void ClientContext::TryCancel() { if (call_) { grpc_call_cancel(call_); diff --git a/src/cpp/client/create_channel.cc b/src/cpp/client/create_channel.cc index 301430572a..510af2bb00 100644 --- a/src/cpp/client/create_channel.cc +++ b/src/cpp/client/create_channel.cc @@ -41,7 +41,7 @@ namespace grpc { class ChannelArguments; std::shared_ptr<ChannelInterface> CreateChannel( - const grpc::string& target, const std::unique_ptr<Credentials>& creds, + const grpc::string& target, const std::shared_ptr<Credentials>& creds, const ChannelArguments& args) { return creds ? creds->CreateChannel(target, args) : std::shared_ptr<ChannelInterface>( diff --git a/src/cpp/client/insecure_credentials.cc b/src/cpp/client/insecure_credentials.cc index 8945b038de..5ad8784567 100644 --- a/src/cpp/client/insecure_credentials.cc +++ b/src/cpp/client/insecure_credentials.cc @@ -52,12 +52,15 @@ class InsecureCredentialsImpl GRPC_FINAL : public Credentials { target, grpc_channel_create(target.c_str(), &channel_args))); } + // InsecureCredentials should not be applied to a call. + bool ApplyToCall(grpc_call* call) GRPC_OVERRIDE { return false; } + SecureCredentials* AsSecureCredentials() GRPC_OVERRIDE { return nullptr; } }; } // namespace -std::unique_ptr<Credentials> InsecureCredentials() { - return std::unique_ptr<Credentials>(new InsecureCredentialsImpl()); +std::shared_ptr<Credentials> InsecureCredentials() { + return std::shared_ptr<Credentials>(new InsecureCredentialsImpl()); } } // namespace grpc diff --git a/src/cpp/client/secure_credentials.cc b/src/cpp/client/secure_credentials.cc index 48bf7430b2..b5134b3140 100644 --- a/src/cpp/client/secure_credentials.cc +++ b/src/cpp/client/secure_credentials.cc @@ -49,20 +49,24 @@ std::shared_ptr<grpc::ChannelInterface> SecureCredentials::CreateChannel( grpc_secure_channel_create(c_creds_, target.c_str(), &channel_args))); } +bool SecureCredentials::ApplyToCall(grpc_call* call) { + return grpc_call_set_credentials(call, c_creds_) == GRPC_CALL_OK; +} + namespace { -std::unique_ptr<Credentials> WrapCredentials(grpc_credentials* creds) { +std::shared_ptr<Credentials> WrapCredentials(grpc_credentials* creds) { return creds == nullptr ? nullptr - : std::unique_ptr<Credentials>(new SecureCredentials(creds)); + : std::shared_ptr<Credentials>(new SecureCredentials(creds)); } } // namespace -std::unique_ptr<Credentials> GoogleDefaultCredentials() { +std::shared_ptr<Credentials> GoogleDefaultCredentials() { return WrapCredentials(grpc_google_default_credentials_create()); } // Builds SSL Credentials given SSL specific options -std::unique_ptr<Credentials> SslCredentials( +std::shared_ptr<Credentials> SslCredentials( const SslCredentialsOptions& options) { grpc_ssl_pem_key_cert_pair pem_key_cert_pair = { options.pem_private_key.c_str(), options.pem_cert_chain.c_str()}; @@ -74,12 +78,12 @@ std::unique_ptr<Credentials> SslCredentials( } // Builds credentials for use when running in GCE -std::unique_ptr<Credentials> ComputeEngineCredentials() { +std::shared_ptr<Credentials> ComputeEngineCredentials() { return WrapCredentials(grpc_compute_engine_credentials_create()); } // Builds service account credentials. -std::unique_ptr<Credentials> ServiceAccountCredentials( +std::shared_ptr<Credentials> ServiceAccountCredentials( const grpc::string& json_key, const grpc::string& scope, long token_lifetime_seconds) { if (token_lifetime_seconds <= 0) { @@ -94,8 +98,8 @@ std::unique_ptr<Credentials> ServiceAccountCredentials( } // Builds JWT credentials. -std::unique_ptr<Credentials> JWTCredentials( - const grpc::string& json_key, long token_lifetime_seconds) { +std::shared_ptr<Credentials> JWTCredentials(const grpc::string& json_key, + long token_lifetime_seconds) { if (token_lifetime_seconds <= 0) { gpr_log(GPR_ERROR, "Trying to create JWTCredentials with non-positive lifetime"); @@ -107,14 +111,14 @@ std::unique_ptr<Credentials> JWTCredentials( } // Builds refresh token credentials. -std::unique_ptr<Credentials> RefreshTokenCredentials( +std::shared_ptr<Credentials> RefreshTokenCredentials( const grpc::string& json_refresh_token) { return WrapCredentials( grpc_refresh_token_credentials_create(json_refresh_token.c_str())); } // Builds IAM credentials. -std::unique_ptr<Credentials> IAMCredentials( +std::shared_ptr<Credentials> IAMCredentials( const grpc::string& authorization_token, const grpc::string& authority_selector) { return WrapCredentials(grpc_iam_credentials_create( @@ -122,10 +126,10 @@ std::unique_ptr<Credentials> IAMCredentials( } // Combines two credentials objects into a composite credentials. -std::unique_ptr<Credentials> CompositeCredentials( - const std::unique_ptr<Credentials>& creds1, - const std::unique_ptr<Credentials>& creds2) { - // Note that we are not saving unique_ptrs to the two credentials +std::shared_ptr<Credentials> CompositeCredentials( + const std::shared_ptr<Credentials>& creds1, + const std::shared_ptr<Credentials>& creds2) { + // Note that we are not saving shared_ptrs to the two credentials // passed in here. This is OK because the underlying C objects (i.e., // creds1 and creds2) into grpc_composite_credentials_create will see their // refcounts incremented. diff --git a/src/cpp/client/secure_credentials.h b/src/cpp/client/secure_credentials.h index 77d575813e..ddf69911b5 100644 --- a/src/cpp/client/secure_credentials.h +++ b/src/cpp/client/secure_credentials.h @@ -46,6 +46,7 @@ class SecureCredentials GRPC_FINAL : public Credentials { explicit SecureCredentials(grpc_credentials* c_creds) : c_creds_(c_creds) {} ~SecureCredentials() GRPC_OVERRIDE { grpc_credentials_release(c_creds_); } grpc_credentials* GetRawCreds() { return c_creds_; } + bool ApplyToCall(grpc_call* call) GRPC_OVERRIDE; std::shared_ptr<grpc::ChannelInterface> CreateChannel( const string& target, const grpc::ChannelArguments& args) GRPC_OVERRIDE; |