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 "grpcpp/grpcpp.h" 20 #include "grpcpp/security/credentials.h" 21 #include "absl/strings/string_view.h" 22 #include "tensorflow/core/lib/core/status.h" 23 24 namespace tensorflow { 25 namespace data { 26 27 // Credential factory implementations should be threadsafe since all callers 28 // to `GetCredentials` will get the same instance of `CredentialsFactory`. 29 class CredentialsFactory { 30 public: 31 virtual ~CredentialsFactory() = default; 32 33 // Returns a protocol name for the credentials factory. This is the string to 34 // look up with `GetCredentials` to find the registered credentials factory. 35 virtual std::string Protocol() = 0; 36 37 // Stores server credentials to `*out`. 38 virtual Status CreateServerCredentials( 39 std::shared_ptr<::grpc::ServerCredentials>* out) = 0; 40 41 // Stores client credentials to `*out`. 42 virtual Status CreateClientCredentials( 43 std::shared_ptr<::grpc::ChannelCredentials>* out) = 0; 44 45 // Registers a credentials factory. 46 static void Register(CredentialsFactory* factory); 47 48 // Creates server credentials using the credentials factory registered as 49 // `protocol`, and stores them to `*out`. 50 static Status CreateServerCredentials( 51 absl::string_view protocol, 52 std::shared_ptr<::grpc::ServerCredentials>* out); 53 54 // Creates client credentials using the credentials factory registered as 55 // `protocol`, and stores them to `*out`. 56 static Status CreateClientCredentials( 57 absl::string_view protocol, 58 std::shared_ptr<::grpc::ChannelCredentials>* out); 59 60 private: 61 // Gets the credentials factory registered via `Register` for the specified 62 // protocol, and stores it to `*out`. 63 static Status Get(const absl::string_view protocol, CredentialsFactory** out); 64 }; 65 66 } // namespace data 67 } // namespace tensorflow 68 69 #endif // TENSORFLOW_CORE_DATA_SERVICE_CREDENTIALS_FACTORY_H_ 70