blob: abf0cb387e317b363a7ba3b5a43ab2f286c7b8fd [file] [log] [blame]
Craig Tiller47c83fd2015-02-21 22:45:35 -08001/*
2 *
3 * Copyright 2015, Google Inc.
4 * All rights reserved.
5 *
6 * Redistribution and use in source and binary forms, with or without
7 * modification, are permitted provided that the following conditions are
8 * met:
9 *
10 * * Redistributions of source code must retain the above copyright
11 * notice, this list of conditions and the following disclaimer.
12 * * Redistributions in binary form must reproduce the above
13 * copyright notice, this list of conditions and the following disclaimer
14 * in the documentation and/or other materials provided with the
15 * distribution.
16 * * Neither the name of Google Inc. nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
21 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
23 * A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
24 * OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
25 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
26 * LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
27 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
28 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
29 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
30 * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
31 *
32 */
33
Craig Tiller47c83fd2015-02-21 22:45:35 -080034#include <grpc/support/log.h>
35
36#include <grpc++/channel_arguments.h>
Craig Tiller47c83fd2015-02-21 22:45:35 -080037#include "src/cpp/client/channel.h"
Julien Boeuf1d2240c2015-04-09 21:07:56 -070038#include "src/cpp/client/secure_credentials.h"
Craig Tiller47c83fd2015-02-21 22:45:35 -080039
40namespace grpc {
41
Julien Boeuf1d2240c2015-04-09 21:07:56 -070042std::shared_ptr<grpc::ChannelInterface> SecureCredentials::CreateChannel(
43 const string& target, const grpc::ChannelArguments& args) {
44 grpc_channel_args channel_args;
45 args.SetChannelArgs(&channel_args);
46 return std::shared_ptr<ChannelInterface>(new Channel(
47 args.GetSslTargetNameOverride().empty() ? target
48 : args.GetSslTargetNameOverride(),
49 grpc_secure_channel_create(c_creds_, target.c_str(), &channel_args)));
50}
Craig Tiller47c83fd2015-02-21 22:45:35 -080051
Yang Gaoa8938922015-05-14 11:51:07 -070052bool SecureCredentials::ApplyToCall(grpc_call* call) {
53 return grpc_call_set_credentials(call, c_creds_) == GRPC_CALL_OK;
54}
55
Craig Tillerad9d0c42015-02-23 10:53:01 -080056namespace {
Yang Gaoa8938922015-05-14 11:51:07 -070057std::shared_ptr<Credentials> WrapCredentials(grpc_credentials* creds) {
Craig Tiller47c83fd2015-02-21 22:45:35 -080058 return creds == nullptr
59 ? nullptr
Yang Gaoa8938922015-05-14 11:51:07 -070060 : std::shared_ptr<Credentials>(new SecureCredentials(creds));
Craig Tiller47c83fd2015-02-21 22:45:35 -080061}
62} // namespace
63
Yang Gaoa8938922015-05-14 11:51:07 -070064std::shared_ptr<Credentials> GoogleDefaultCredentials() {
Craig Tillere8eb8a42015-02-23 14:56:42 -080065 return WrapCredentials(grpc_google_default_credentials_create());
Craig Tiller47c83fd2015-02-21 22:45:35 -080066}
67
68// Builds SSL Credentials given SSL specific options
Yang Gaoa8938922015-05-14 11:51:07 -070069std::shared_ptr<Credentials> SslCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -080070 const SslCredentialsOptions& options) {
71 grpc_ssl_pem_key_cert_pair pem_key_cert_pair = {
72 options.pem_private_key.c_str(), options.pem_cert_chain.c_str()};
73
74 grpc_credentials* c_creds = grpc_ssl_credentials_create(
75 options.pem_root_certs.empty() ? nullptr : options.pem_root_certs.c_str(),
76 options.pem_private_key.empty() ? nullptr : &pem_key_cert_pair);
77 return WrapCredentials(c_creds);
78}
79
80// Builds credentials for use when running in GCE
Yang Gaoa8938922015-05-14 11:51:07 -070081std::shared_ptr<Credentials> ComputeEngineCredentials() {
Craig Tiller47c83fd2015-02-21 22:45:35 -080082 return WrapCredentials(grpc_compute_engine_credentials_create());
83}
84
85// Builds service account credentials.
Yang Gaoa8938922015-05-14 11:51:07 -070086std::shared_ptr<Credentials> ServiceAccountCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -080087 const grpc::string& json_key, const grpc::string& scope,
Nicolas "Pixel" Noblee24dde52015-04-24 00:43:09 +020088 long token_lifetime_seconds) {
89 if (token_lifetime_seconds <= 0) {
Yang Gao94e296c2015-03-13 16:06:56 -070090 gpr_log(GPR_ERROR,
Yang Gao2fbbb9b2015-03-13 16:10:14 -070091 "Trying to create ServiceAccountCredentials "
92 "with non-positive lifetime");
Yang Gao05e3eef2015-03-12 23:41:46 -070093 return WrapCredentials(nullptr);
94 }
Craig Tiller677c50c2015-07-13 10:49:06 -070095 gpr_timespec lifetime =
96 gpr_time_from_seconds(token_lifetime_seconds, GPR_TIMESPAN);
Craig Tiller47c83fd2015-02-21 22:45:35 -080097 return WrapCredentials(grpc_service_account_credentials_create(
98 json_key.c_str(), scope.c_str(), lifetime));
99}
100
Yang Gao0535da32015-03-11 14:51:03 -0700101// Builds JWT credentials.
Julien Boeuffe4c3f42015-07-22 16:20:13 -0700102std::shared_ptr<Credentials> ServiceAccountJWTAccessCredentials(
103 const grpc::string& json_key, long token_lifetime_seconds) {
Nicolas "Pixel" Noblee24dde52015-04-24 00:43:09 +0200104 if (token_lifetime_seconds <= 0) {
Yang Gao94e296c2015-03-13 16:06:56 -0700105 gpr_log(GPR_ERROR,
106 "Trying to create JWTCredentials with non-positive lifetime");
Yang Gao05e3eef2015-03-12 23:41:46 -0700107 return WrapCredentials(nullptr);
108 }
Craig Tiller677c50c2015-07-13 10:49:06 -0700109 gpr_timespec lifetime =
110 gpr_time_from_seconds(token_lifetime_seconds, GPR_TIMESPAN);
Julien Boeuffe4c3f42015-07-22 16:20:13 -0700111 return WrapCredentials(grpc_service_account_jwt_access_credentials_create(
112 json_key.c_str(), lifetime));
Yang Gao0535da32015-03-11 14:51:03 -0700113}
114
Yang Gao5ebd6c72015-03-17 16:22:32 -0700115// Builds refresh token credentials.
Yang Gaoa8938922015-05-14 11:51:07 -0700116std::shared_ptr<Credentials> RefreshTokenCredentials(
Yang Gao5ebd6c72015-03-17 16:22:32 -0700117 const grpc::string& json_refresh_token) {
118 return WrapCredentials(
119 grpc_refresh_token_credentials_create(json_refresh_token.c_str()));
120}
121
Julien Boeuf2805be12015-07-01 02:47:18 -0700122// Builds access token credentials.
123std::shared_ptr<Credentials> AccessTokenCredentials(
124 const grpc::string& access_token) {
125 return WrapCredentials(
126 grpc_access_token_credentials_create(access_token.c_str()));
127}
128
Craig Tiller47c83fd2015-02-21 22:45:35 -0800129// Builds IAM credentials.
Yang Gaoa8938922015-05-14 11:51:07 -0700130std::shared_ptr<Credentials> IAMCredentials(
Craig Tiller47c83fd2015-02-21 22:45:35 -0800131 const grpc::string& authorization_token,
132 const grpc::string& authority_selector) {
133 return WrapCredentials(grpc_iam_credentials_create(
134 authorization_token.c_str(), authority_selector.c_str()));
135}
136
137// Combines two credentials objects into a composite credentials.
Yang Gaoa8938922015-05-14 11:51:07 -0700138std::shared_ptr<Credentials> CompositeCredentials(
139 const std::shared_ptr<Credentials>& creds1,
140 const std::shared_ptr<Credentials>& creds2) {
141 // Note that we are not saving shared_ptrs to the two credentials
Craig Tiller47c83fd2015-02-21 22:45:35 -0800142 // passed in here. This is OK because the underlying C objects (i.e.,
143 // creds1 and creds2) into grpc_composite_credentials_create will see their
144 // refcounts incremented.
Craig Tillerad9d0c42015-02-23 10:53:01 -0800145 SecureCredentials* s1 = creds1->AsSecureCredentials();
146 SecureCredentials* s2 = creds2->AsSecureCredentials();
Craig Tiller47c83fd2015-02-21 22:45:35 -0800147 if (s1 && s2) {
148 return WrapCredentials(grpc_composite_credentials_create(
149 s1->GetRawCreds(), s2->GetRawCreds()));
150 }
151 return nullptr;
152}
153
154} // namespace grpc