xref: /aosp_15_r20/external/tensorflow/tensorflow/core/data/service/credentials_factory.h (revision b6fb3261f9314811a0f4371741dbb8839866f948)
1 /* Copyright 2020 The TensorFlow Authors. All Rights Reserved.
2 
3 Licensed under the Apache License, Version 2.0 (the "License");
4 you may not use this file except in compliance with the License.
5 You may obtain a copy of the License at
6 
7     http://www.apache.org/licenses/LICENSE-2.0
8 
9 Unless required by applicable law or agreed to in writing, software
10 distributed under the License is distributed on an "AS IS" BASIS,
11 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 See the License for the specific language governing permissions and
13 limitations under the License.
14 ==============================================================================*/
15 
16 #ifndef TENSORFLOW_CORE_DATA_SERVICE_CREDENTIALS_FACTORY_H_
17 #define TENSORFLOW_CORE_DATA_SERVICE_CREDENTIALS_FACTORY_H_
18 
19 #include <memory>
20 #include <string>
21 
22 #include "grpcpp/grpcpp.h"
23 #include "grpcpp/security/credentials.h"
24 #include "absl/strings/string_view.h"
25 #include "tensorflow/core/lib/core/status.h"
26 
27 namespace tensorflow {
28 namespace data {
29 
30 // Credential factory implementations should be threadsafe since all callers
31 // to `GetCredentials` will get the same instance of `CredentialsFactory`.
32 class CredentialsFactory {
33  public:
34   virtual ~CredentialsFactory() = default;
35 
36   // Returns a protocol name for the credentials factory. This is the string to
37   // look up with `GetCredentials` to find the registered credentials factory.
38   virtual std::string Protocol() = 0;
39 
40   // Stores server credentials to `*out`.
41   virtual Status CreateServerCredentials(
42       std::shared_ptr<::grpc::ServerCredentials>* out) = 0;
43 
44   // Stores client credentials to `*out`.
45   virtual Status CreateClientCredentials(
46       std::shared_ptr<::grpc::ChannelCredentials>* out) = 0;
47 
48   // Registers a credentials factory.
49   static void Register(CredentialsFactory* factory);
50 
51   // Creates server credentials using the credentials factory registered as
52   // `protocol`, and stores them to `*out`.
53   static Status CreateServerCredentials(
54       absl::string_view protocol,
55       std::shared_ptr<::grpc::ServerCredentials>* out);
56 
57   // Creates client credentials using the credentials factory registered as
58   // `protocol`, and stores them to `*out`.
59   static Status CreateClientCredentials(
60       absl::string_view protocol,
61       std::shared_ptr<::grpc::ChannelCredentials>* out);
62 
63   // Returns whether a factory has been registered under the given protocol
64   // name.
65   static bool Exists(absl::string_view protocol);
66 
67  private:
68   // Gets the credentials factory registered via `Register` for the specified
69   // protocol, and stores it to `*out`.
70   static Status Get(const absl::string_view protocol, CredentialsFactory** out);
71 };
72 
73 }  // namespace data
74 }  // namespace tensorflow
75 
76 #endif  // TENSORFLOW_CORE_DATA_SERVICE_CREDENTIALS_FACTORY_H_
77