Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 1 | /* |
| 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 Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 34 | #include <grpc/support/log.h> |
| 35 | |
| 36 | #include <grpc++/channel_arguments.h> |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 37 | #include "src/cpp/client/channel.h" |
Julien Boeuf | 1d2240c | 2015-04-09 21:07:56 -0700 | [diff] [blame] | 38 | #include "src/cpp/client/secure_credentials.h" |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 39 | |
| 40 | namespace grpc { |
| 41 | |
Julien Boeuf | 1d2240c | 2015-04-09 21:07:56 -0700 | [diff] [blame] | 42 | std::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 Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 51 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 52 | bool SecureCredentials::ApplyToCall(grpc_call* call) { |
| 53 | return grpc_call_set_credentials(call, c_creds_) == GRPC_CALL_OK; |
| 54 | } |
| 55 | |
Craig Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 56 | namespace { |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 57 | std::shared_ptr<Credentials> WrapCredentials(grpc_credentials* creds) { |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 58 | return creds == nullptr |
| 59 | ? nullptr |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 60 | : std::shared_ptr<Credentials>(new SecureCredentials(creds)); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 61 | } |
| 62 | } // namespace |
| 63 | |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 64 | std::shared_ptr<Credentials> GoogleDefaultCredentials() { |
Craig Tiller | e8eb8a4 | 2015-02-23 14:56:42 -0800 | [diff] [blame] | 65 | return WrapCredentials(grpc_google_default_credentials_create()); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | // Builds SSL Credentials given SSL specific options |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 69 | std::shared_ptr<Credentials> SslCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 70 | 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 Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 81 | std::shared_ptr<Credentials> ComputeEngineCredentials() { |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 82 | return WrapCredentials(grpc_compute_engine_credentials_create()); |
| 83 | } |
| 84 | |
| 85 | // Builds service account credentials. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 86 | std::shared_ptr<Credentials> ServiceAccountCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 87 | const grpc::string& json_key, const grpc::string& scope, |
Nicolas "Pixel" Noble | e24dde5 | 2015-04-24 00:43:09 +0200 | [diff] [blame] | 88 | long token_lifetime_seconds) { |
| 89 | if (token_lifetime_seconds <= 0) { |
Yang Gao | 94e296c | 2015-03-13 16:06:56 -0700 | [diff] [blame] | 90 | gpr_log(GPR_ERROR, |
Yang Gao | 2fbbb9b | 2015-03-13 16:10:14 -0700 | [diff] [blame] | 91 | "Trying to create ServiceAccountCredentials " |
| 92 | "with non-positive lifetime"); |
Yang Gao | 05e3eef | 2015-03-12 23:41:46 -0700 | [diff] [blame] | 93 | return WrapCredentials(nullptr); |
| 94 | } |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 95 | gpr_timespec lifetime = |
| 96 | gpr_time_from_seconds(token_lifetime_seconds, GPR_TIMESPAN); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 97 | return WrapCredentials(grpc_service_account_credentials_create( |
| 98 | json_key.c_str(), scope.c_str(), lifetime)); |
| 99 | } |
| 100 | |
Yang Gao | 0535da3 | 2015-03-11 14:51:03 -0700 | [diff] [blame] | 101 | // Builds JWT credentials. |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 102 | std::shared_ptr<Credentials> ServiceAccountJWTAccessCredentials( |
| 103 | const grpc::string& json_key, long token_lifetime_seconds) { |
Nicolas "Pixel" Noble | e24dde5 | 2015-04-24 00:43:09 +0200 | [diff] [blame] | 104 | if (token_lifetime_seconds <= 0) { |
Yang Gao | 94e296c | 2015-03-13 16:06:56 -0700 | [diff] [blame] | 105 | gpr_log(GPR_ERROR, |
| 106 | "Trying to create JWTCredentials with non-positive lifetime"); |
Yang Gao | 05e3eef | 2015-03-12 23:41:46 -0700 | [diff] [blame] | 107 | return WrapCredentials(nullptr); |
| 108 | } |
Craig Tiller | 677c50c | 2015-07-13 10:49:06 -0700 | [diff] [blame] | 109 | gpr_timespec lifetime = |
| 110 | gpr_time_from_seconds(token_lifetime_seconds, GPR_TIMESPAN); |
Julien Boeuf | fe4c3f4 | 2015-07-22 16:20:13 -0700 | [diff] [blame] | 111 | return WrapCredentials(grpc_service_account_jwt_access_credentials_create( |
| 112 | json_key.c_str(), lifetime)); |
Yang Gao | 0535da3 | 2015-03-11 14:51:03 -0700 | [diff] [blame] | 113 | } |
| 114 | |
Yang Gao | 5ebd6c7 | 2015-03-17 16:22:32 -0700 | [diff] [blame] | 115 | // Builds refresh token credentials. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 116 | std::shared_ptr<Credentials> RefreshTokenCredentials( |
Yang Gao | 5ebd6c7 | 2015-03-17 16:22:32 -0700 | [diff] [blame] | 117 | const grpc::string& json_refresh_token) { |
| 118 | return WrapCredentials( |
| 119 | grpc_refresh_token_credentials_create(json_refresh_token.c_str())); |
| 120 | } |
| 121 | |
Julien Boeuf | 2805be1 | 2015-07-01 02:47:18 -0700 | [diff] [blame] | 122 | // Builds access token credentials. |
| 123 | std::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 Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 129 | // Builds IAM credentials. |
Yang Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 130 | std::shared_ptr<Credentials> IAMCredentials( |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 131 | 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 Gao | a893892 | 2015-05-14 11:51:07 -0700 | [diff] [blame] | 138 | std::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 Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 142 | // 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 Tiller | ad9d0c4 | 2015-02-23 10:53:01 -0800 | [diff] [blame] | 145 | SecureCredentials* s1 = creds1->AsSecureCredentials(); |
| 146 | SecureCredentials* s2 = creds2->AsSecureCredentials(); |
Craig Tiller | 47c83fd | 2015-02-21 22:45:35 -0800 | [diff] [blame] | 147 | if (s1 && s2) { |
| 148 | return WrapCredentials(grpc_composite_credentials_create( |
| 149 | s1->GetRawCreds(), s2->GetRawCreds())); |
| 150 | } |
| 151 | return nullptr; |
| 152 | } |
| 153 | |
| 154 | } // namespace grpc |