diff options
author | 2015-01-09 14:19:44 -0800 | |
---|---|---|
committer | 2015-01-12 11:22:01 -0800 | |
commit | 4105e2b86c91ecc3687def3abcbb602bee894b0a (patch) | |
tree | dbeed01da6a51b69f0a7651cac16541b99c293c5 /src/cpp/client/credentials.cc | |
parent | 5d61ac074c26a1631cfdbd34d590c730139a9de6 (diff) |
Add ServiceAccount Credentials wrapping and handle credentials creation
failure.
Change on 2015/01/09 by yangg <yangg@google.com>
-------------
Created by MOE: http://code.google.com/p/moe-java
MOE_MIGRATED_REVID=83634736
Diffstat (limited to 'src/cpp/client/credentials.cc')
-rw-r--r-- | src/cpp/client/credentials.cc | 37 |
1 files changed, 34 insertions, 3 deletions
diff --git a/src/cpp/client/credentials.cc b/src/cpp/client/credentials.cc index 986008f7bb..cac1d3d106 100644 --- a/src/cpp/client/credentials.cc +++ b/src/cpp/client/credentials.cc @@ -35,6 +35,7 @@ #include <string> #include <grpc/grpc_security.h> +#include <grpc/support/log.h> #include <grpc++/credentials.h> @@ -58,6 +59,9 @@ std::unique_ptr<Credentials> CredentialsFactory::SslCredentials( options.pem_root_certs.empty() ? nullptr : reinterpret_cast<const unsigned char*>( options.pem_root_certs.c_str()); + if (pem_root_certs == nullptr) { + return std::unique_ptr<Credentials>(); + } const unsigned char* pem_private_key = options.pem_private_key.empty() ? nullptr : reinterpret_cast<const unsigned char*>( @@ -71,14 +75,40 @@ std::unique_ptr<Credentials> CredentialsFactory::SslCredentials( pem_root_certs, options.pem_root_certs.size(), pem_private_key, options.pem_private_key.size(), pem_cert_chain, options.pem_cert_chain.size()); - std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds)); + std::unique_ptr<Credentials> cpp_creds( + c_creds == nullptr ? nullptr : new Credentials(c_creds)); return cpp_creds; } // Builds credentials for use when running in GCE std::unique_ptr<Credentials> CredentialsFactory::ComputeEngineCredentials() { grpc_credentials* c_creds = grpc_compute_engine_credentials_create(); - std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds)); + std::unique_ptr<Credentials> cpp_creds( + c_creds == nullptr ? nullptr : new Credentials(c_creds)); + return cpp_creds; +} + +// Builds service account credentials. +std::unique_ptr<Credentials> CredentialsFactory::ServiceAccountCredentials( + const grpc::string& json_key, const grpc::string& scope, + std::chrono::seconds token_lifetime) { + gpr_timespec lifetime = gpr_time_from_seconds( + token_lifetime.count() > 0 ? token_lifetime.count() : 0); + grpc_credentials* c_creds = grpc_service_account_credentials_create( + json_key.c_str(), scope.c_str(), lifetime); + std::unique_ptr<Credentials> cpp_creds( + c_creds == nullptr ? nullptr : new Credentials(c_creds)); + return cpp_creds; +} + +// Builds IAM credentials. +std::unique_ptr<Credentials> CredentialsFactory::IAMCredentials( + const grpc::string& authorization_token, + const grpc::string& authority_selector) { + grpc_credentials* c_creds = grpc_iam_credentials_create( + authorization_token.c_str(), authority_selector.c_str()); + std::unique_ptr<Credentials> cpp_creds( + c_creds == nullptr ? nullptr : new Credentials(c_creds)); return cpp_creds; } @@ -93,7 +123,8 @@ std::unique_ptr<Credentials> CredentialsFactory::ComposeCredentials( // refcounts incremented. grpc_credentials* c_creds = grpc_composite_credentials_create( creds1->GetRawCreds(), creds2->GetRawCreds()); - std::unique_ptr<Credentials> cpp_creds(new Credentials(c_creds)); + std::unique_ptr<Credentials> cpp_creds( + c_creds == nullptr ? nullptr : new Credentials(c_creds)); return cpp_creds; } |