Home | History | Annotate | Download | only in util
      1 /*
      2  *
      3  * Copyright 2016 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 
     19 #ifndef GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H
     20 #define GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H
     21 
     22 #include <memory>
     23 
     24 #include <grpcpp/security/credentials.h>
     25 #include <grpcpp/security/server_credentials.h>
     26 #include <grpcpp/support/channel_arguments.h>
     27 
     28 namespace grpc {
     29 namespace testing {
     30 
     31 const char kInsecureCredentialsType[] = "INSECURE_CREDENTIALS";
     32 // For real credentials, like tls/ssl, this name should match the AuthContext
     33 // property "transport_security_type".
     34 const char kTlsCredentialsType[] = "ssl";
     35 const char kAltsCredentialsType[] = "alts";
     36 const char kGoogleDefaultCredentialsType[] = "google_default_credentials";
     37 
     38 // Provide test credentials of a particular type.
     39 class CredentialTypeProvider {
     40  public:
     41   virtual ~CredentialTypeProvider() {}
     42 
     43   virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
     44       ChannelArguments* args) = 0;
     45   virtual std::shared_ptr<ServerCredentials> GetServerCredentials() = 0;
     46 };
     47 
     48 // Provide test credentials. Thread-safe.
     49 class CredentialsProvider {
     50  public:
     51   virtual ~CredentialsProvider() {}
     52 
     53   // Add a secure type in addition to the defaults. The default provider has
     54   // (kInsecureCredentialsType, kTlsCredentialsType).
     55   virtual void AddSecureType(
     56       const grpc::string& type,
     57       std::unique_ptr<CredentialTypeProvider> type_provider) = 0;
     58 
     59   // Provide channel credentials according to the given type. Alter the channel
     60   // arguments if needed. Return nullptr if type is not registered.
     61   virtual std::shared_ptr<ChannelCredentials> GetChannelCredentials(
     62       const grpc::string& type, ChannelArguments* args) = 0;
     63 
     64   // Provide server credentials according to the given type.
     65   // Return nullptr if type is not registered.
     66   virtual std::shared_ptr<ServerCredentials> GetServerCredentials(
     67       const grpc::string& type) = 0;
     68 
     69   // Provide a list of secure credentials type.
     70   virtual std::vector<grpc::string> GetSecureCredentialsTypeList() = 0;
     71 };
     72 
     73 // Get the current provider. Create a default one if not set.
     74 // Not thread-safe.
     75 CredentialsProvider* GetCredentialsProvider();
     76 
     77 // Set the global provider. Takes ownership. The previous set provider will be
     78 // destroyed.
     79 // Not thread-safe.
     80 void SetCredentialsProvider(CredentialsProvider* provider);
     81 
     82 }  // namespace testing
     83 }  // namespace grpc
     84 
     85 #endif  // GRPC_TEST_CPP_UTIL_TEST_CREDENTIALS_PROVIDER_H
     86