credentials.h 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341
  1. //
  2. //
  3. // Copyright 2015 gRPC authors.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. //
  17. //
  18. #ifndef GRPCPP_SECURITY_CREDENTIALS_H
  19. #define GRPCPP_SECURITY_CREDENTIALS_H
  20. #if defined(__GNUC__)
  21. #pragma GCC system_header
  22. #endif
  23. #include <map>
  24. #include <memory>
  25. #include <vector>
  26. #include <grpc/grpc_security_constants.h>
  27. #include <grpcpp/channel.h>
  28. #include <grpcpp/impl/grpc_library.h>
  29. #include <grpcpp/security/auth_context.h>
  30. #include <grpcpp/security/tls_credentials_options.h>
  31. #include <grpcpp/support/channel_arguments.h>
  32. #include <grpcpp/support/client_interceptor.h>
  33. #include <grpcpp/support/status.h>
  34. #include <grpcpp/support/string_ref.h>
  35. struct grpc_call;
  36. namespace grpc {
  37. class CallCredentials;
  38. class SecureCallCredentials;
  39. class SecureChannelCredentials;
  40. class ChannelCredentials;
  41. std::shared_ptr<Channel> CreateCustomChannel(
  42. const grpc::string& target,
  43. const std::shared_ptr<grpc::ChannelCredentials>& creds,
  44. const grpc::ChannelArguments& args);
  45. namespace experimental {
  46. std::shared_ptr<grpc::Channel> CreateCustomChannelWithInterceptors(
  47. const grpc::string& target,
  48. const std::shared_ptr<grpc::ChannelCredentials>& creds,
  49. const grpc::ChannelArguments& args,
  50. std::vector<
  51. std::unique_ptr<grpc::experimental::ClientInterceptorFactoryInterface>>
  52. interceptor_creators);
  53. } // namespace experimental
  54. /// Builds XDS Credentials.
  55. std::shared_ptr<ChannelCredentials> XdsCredentials(
  56. const std::shared_ptr<ChannelCredentials>& fallback_creds);
  57. /// A channel credentials object encapsulates all the state needed by a client
  58. /// to authenticate with a server for a given channel.
  59. /// It can make various assertions, e.g., about the client’s identity, role
  60. /// for all the calls on that channel.
  61. ///
  62. /// \see https://grpc.io/docs/guides/auth.html
  63. class ChannelCredentials : private grpc::internal::GrpcLibrary {
  64. public:
  65. protected:
  66. friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
  67. const std::shared_ptr<ChannelCredentials>& channel_creds,
  68. const std::shared_ptr<CallCredentials>& call_creds);
  69. // TODO(yashykt): We need this friend declaration mainly for access to
  70. // AsSecureCredentials(). Once we are able to remove insecure builds from gRPC
  71. // (and also internal dependencies on the indirect method of creating a
  72. // channel through credentials), we would be able to remove this.
  73. friend std::shared_ptr<ChannelCredentials> grpc::XdsCredentials(
  74. const std::shared_ptr<ChannelCredentials>& fallback_creds);
  75. virtual SecureChannelCredentials* AsSecureCredentials() = 0;
  76. private:
  77. friend std::shared_ptr<grpc::Channel> CreateCustomChannel(
  78. const grpc::string& target,
  79. const std::shared_ptr<grpc::ChannelCredentials>& creds,
  80. const grpc::ChannelArguments& args);
  81. friend std::shared_ptr<grpc::Channel>
  82. grpc::experimental::CreateCustomChannelWithInterceptors(
  83. const grpc::string& target,
  84. const std::shared_ptr<grpc::ChannelCredentials>& creds,
  85. const grpc::ChannelArguments& args,
  86. std::vector<std::unique_ptr<
  87. grpc::experimental::ClientInterceptorFactoryInterface>>
  88. interceptor_creators);
  89. virtual std::shared_ptr<Channel> CreateChannelImpl(
  90. const grpc::string& target, const ChannelArguments& args) = 0;
  91. // This function should have been a pure virtual function, but it is
  92. // implemented as a virtual function so that it does not break API.
  93. virtual std::shared_ptr<Channel> CreateChannelWithInterceptors(
  94. const grpc::string& /*target*/, const ChannelArguments& /*args*/,
  95. std::vector<std::unique_ptr<
  96. grpc::experimental::ClientInterceptorFactoryInterface>>
  97. /*interceptor_creators*/) {
  98. return nullptr;
  99. }
  100. // TODO(yashkt): This is a hack that is needed since InsecureCredentials can
  101. // not use grpc_channel_credentials internally and should be removed after
  102. // insecure builds are removed from gRPC.
  103. virtual bool IsInsecure() const { return false; }
  104. };
  105. /// A call credentials object encapsulates the state needed by a client to
  106. /// authenticate with a server for a given call on a channel.
  107. ///
  108. /// \see https://grpc.io/docs/guides/auth.html
  109. class CallCredentials : private grpc::internal::GrpcLibrary {
  110. public:
  111. /// Apply this instance's credentials to \a call.
  112. virtual bool ApplyToCall(grpc_call* call) = 0;
  113. virtual grpc::string DebugString() {
  114. return "CallCredentials did not provide a debug string";
  115. }
  116. protected:
  117. friend std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
  118. const std::shared_ptr<ChannelCredentials>& channel_creds,
  119. const std::shared_ptr<CallCredentials>& call_creds);
  120. friend std::shared_ptr<CallCredentials> CompositeCallCredentials(
  121. const std::shared_ptr<CallCredentials>& creds1,
  122. const std::shared_ptr<CallCredentials>& creds2);
  123. virtual SecureCallCredentials* AsSecureCredentials() = 0;
  124. };
  125. /// Options used to build SslCredentials.
  126. struct SslCredentialsOptions {
  127. /// The buffer containing the PEM encoding of the server root certificates. If
  128. /// this parameter is empty, the default roots will be used. The default
  129. /// roots can be overridden using the \a GRPC_DEFAULT_SSL_ROOTS_FILE_PATH
  130. /// environment variable pointing to a file on the file system containing the
  131. /// roots.
  132. grpc::string pem_root_certs;
  133. /// The buffer containing the PEM encoding of the client's private key. This
  134. /// parameter can be empty if the client does not have a private key.
  135. grpc::string pem_private_key;
  136. /// The buffer containing the PEM encoding of the client's certificate chain.
  137. /// This parameter can be empty if the client does not have a certificate
  138. /// chain.
  139. grpc::string pem_cert_chain;
  140. };
  141. // Factories for building different types of Credentials The functions may
  142. // return empty shared_ptr when credentials cannot be created. If a
  143. // Credentials pointer is returned, it can still be invalid when used to create
  144. // a channel. A lame channel will be created then and all rpcs will fail on it.
  145. /// Builds credentials with reasonable defaults.
  146. ///
  147. /// \warning Only use these credentials when connecting to a Google endpoint.
  148. /// Using these credentials to connect to any other service may result in this
  149. /// service being able to impersonate your client for requests to Google
  150. /// services.
  151. std::shared_ptr<ChannelCredentials> GoogleDefaultCredentials();
  152. /// Builds SSL Credentials given SSL specific options
  153. std::shared_ptr<ChannelCredentials> SslCredentials(
  154. const SslCredentialsOptions& options);
  155. /// Builds credentials for use when running in GCE
  156. ///
  157. /// \warning Only use these credentials when connecting to a Google endpoint.
  158. /// Using these credentials to connect to any other service may result in this
  159. /// service being able to impersonate your client for requests to Google
  160. /// services.
  161. std::shared_ptr<CallCredentials> GoogleComputeEngineCredentials();
  162. constexpr long kMaxAuthTokenLifetimeSecs = 3600;
  163. /// Builds Service Account JWT Access credentials.
  164. /// json_key is the JSON key string containing the client's private key.
  165. /// token_lifetime_seconds is the lifetime in seconds of each Json Web Token
  166. /// (JWT) created with this credentials. It should not exceed
  167. /// \a kMaxAuthTokenLifetimeSecs or will be cropped to this value.
  168. std::shared_ptr<CallCredentials> ServiceAccountJWTAccessCredentials(
  169. const grpc::string& json_key,
  170. long token_lifetime_seconds = kMaxAuthTokenLifetimeSecs);
  171. /// Builds refresh token credentials.
  172. /// json_refresh_token is the JSON string containing the refresh token along
  173. /// with a client_id and client_secret.
  174. ///
  175. /// \warning Only use these credentials when connecting to a Google endpoint.
  176. /// Using these credentials to connect to any other service may result in this
  177. /// service being able to impersonate your client for requests to Google
  178. /// services.
  179. std::shared_ptr<CallCredentials> GoogleRefreshTokenCredentials(
  180. const grpc::string& json_refresh_token);
  181. /// Builds access token credentials.
  182. /// access_token is an oauth2 access token that was fetched using an out of band
  183. /// mechanism.
  184. ///
  185. /// \warning Only use these credentials when connecting to a Google endpoint.
  186. /// Using these credentials to connect to any other service may result in this
  187. /// service being able to impersonate your client for requests to Google
  188. /// services.
  189. std::shared_ptr<CallCredentials> AccessTokenCredentials(
  190. const grpc::string& access_token);
  191. /// Builds IAM credentials.
  192. ///
  193. /// \warning Only use these credentials when connecting to a Google endpoint.
  194. /// Using these credentials to connect to any other service may result in this
  195. /// service being able to impersonate your client for requests to Google
  196. /// services.
  197. std::shared_ptr<CallCredentials> GoogleIAMCredentials(
  198. const grpc::string& authorization_token,
  199. const grpc::string& authority_selector);
  200. /// Combines a channel credentials and a call credentials into a composite
  201. /// channel credentials.
  202. std::shared_ptr<ChannelCredentials> CompositeChannelCredentials(
  203. const std::shared_ptr<ChannelCredentials>& channel_creds,
  204. const std::shared_ptr<CallCredentials>& call_creds);
  205. /// Combines two call credentials objects into a composite call credentials.
  206. std::shared_ptr<CallCredentials> CompositeCallCredentials(
  207. const std::shared_ptr<CallCredentials>& creds1,
  208. const std::shared_ptr<CallCredentials>& creds2);
  209. /// Credentials for an unencrypted, unauthenticated channel
  210. std::shared_ptr<ChannelCredentials> InsecureChannelCredentials();
  211. /// User defined metadata credentials.
  212. class MetadataCredentialsPlugin {
  213. public:
  214. virtual ~MetadataCredentialsPlugin() {}
  215. /// If this method returns true, the Process function will be scheduled in
  216. /// a different thread from the one processing the call.
  217. virtual bool IsBlocking() const { return true; }
  218. /// Type of credentials this plugin is implementing.
  219. virtual const char* GetType() const { return ""; }
  220. /// Gets the auth metatada produced by this plugin.
  221. /// The fully qualified method name is:
  222. /// service_url + "/" + method_name.
  223. /// The channel_auth_context contains (among other things), the identity of
  224. /// the server.
  225. virtual grpc::Status GetMetadata(
  226. grpc::string_ref service_url, grpc::string_ref method_name,
  227. const grpc::AuthContext& channel_auth_context,
  228. std::multimap<grpc::string, grpc::string>* metadata) = 0;
  229. virtual grpc::string DebugString() {
  230. return "MetadataCredentialsPlugin did not provide a debug string";
  231. }
  232. };
  233. std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
  234. std::unique_ptr<MetadataCredentialsPlugin> plugin);
  235. /// Builds External Account credentials.
  236. /// json_string is the JSON string containing the credentials options.
  237. /// scopes contains the scopes to be binded with the credentials.
  238. std::shared_ptr<CallCredentials> ExternalAccountCredentials(
  239. const grpc::string& json_string, const std::vector<grpc::string>& scopes);
  240. namespace experimental {
  241. /// Options for creating STS Oauth Token Exchange credentials following the IETF
  242. /// draft https://tools.ietf.org/html/draft-ietf-oauth-token-exchange-16.
  243. /// Optional fields may be set to empty string. It is the responsibility of the
  244. /// caller to ensure that the subject and actor tokens are refreshed on disk at
  245. /// the specified paths.
  246. struct StsCredentialsOptions {
  247. grpc::string token_exchange_service_uri; // Required.
  248. grpc::string resource; // Optional.
  249. grpc::string audience; // Optional.
  250. grpc::string scope; // Optional.
  251. grpc::string requested_token_type; // Optional.
  252. grpc::string subject_token_path; // Required.
  253. grpc::string subject_token_type; // Required.
  254. grpc::string actor_token_path; // Optional.
  255. grpc::string actor_token_type; // Optional.
  256. };
  257. grpc::Status StsCredentialsOptionsFromJson(const TString& json_string,
  258. StsCredentialsOptions* options);
  259. /// Creates STS credentials options from the $STS_CREDENTIALS environment
  260. /// variable. This environment variable points to the path of a JSON file
  261. /// comforming to the schema described above.
  262. grpc::Status StsCredentialsOptionsFromEnv(StsCredentialsOptions* options);
  263. std::shared_ptr<CallCredentials> StsCredentials(
  264. const StsCredentialsOptions& options);
  265. std::shared_ptr<CallCredentials> MetadataCredentialsFromPlugin(
  266. std::unique_ptr<MetadataCredentialsPlugin> plugin,
  267. grpc_security_level min_security_level);
  268. /// Options used to build AltsCredentials.
  269. struct AltsCredentialsOptions {
  270. /// service accounts of target endpoint that will be acceptable
  271. /// by the client. If service accounts are provided and none of them matches
  272. /// that of the server, authentication will fail.
  273. std::vector<grpc::string> target_service_accounts;
  274. };
  275. /// Builds ALTS Credentials given ALTS specific options
  276. std::shared_ptr<ChannelCredentials> AltsCredentials(
  277. const AltsCredentialsOptions& options);
  278. /// Builds Local Credentials.
  279. std::shared_ptr<ChannelCredentials> LocalCredentials(
  280. grpc_local_connect_type type);
  281. /// Builds TLS Credentials given TLS options.
  282. std::shared_ptr<ChannelCredentials> TlsCredentials(
  283. const TlsChannelCredentialsOptions& options);
  284. } // namespace experimental
  285. } // namespace grpc
  286. #endif // GRPCPP_SECURITY_CREDENTIALS_H