• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_SRC_CORE_LIB_SECURITY_CREDENTIALS_GOOGLE_DEFAULT_GOOGLE_DEFAULT_CREDENTIALS_H
20 #define GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_GOOGLE_DEFAULT_GOOGLE_DEFAULT_CREDENTIALS_H
21 #include <grpc/credentials.h>
22 #include <grpc/grpc.h>
23 #include <grpc/grpc_security.h>
24 #include <grpc/support/port_platform.h>
25 
26 #include <utility>
27 
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/security/credentials/credentials.h"
30 #include "src/core/lib/security/security_connector/security_connector.h"
31 #include "src/core/util/ref_counted_ptr.h"
32 #include "src/core/util/unique_type_name.h"
33 #include "src/core/util/useful.h"
34 
35 #define GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY "gcloud"
36 #define GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE \
37   "application_default_credentials.json"
38 
39 #ifdef GPR_WINDOWS
40 #define GRPC_GOOGLE_CREDENTIALS_PATH_ENV_VAR "APPDATA"
41 #define GRPC_GOOGLE_CREDENTIALS_PATH_SUFFIX \
42   GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY    \
43   "/" GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE
44 #else
45 #define GRPC_GOOGLE_CREDENTIALS_PATH_ENV_VAR "HOME"
46 #define GRPC_GOOGLE_CREDENTIALS_PATH_SUFFIX         \
47   ".config/" GRPC_GOOGLE_CLOUD_SDK_CONFIG_DIRECTORY \
48   "/" GRPC_GOOGLE_WELL_KNOWN_CREDENTIALS_FILE
49 #endif
50 
51 class grpc_google_default_channel_credentials
52     : public grpc_channel_credentials {
53  public:
grpc_google_default_channel_credentials(grpc_core::RefCountedPtr<grpc_channel_credentials> alts_creds,grpc_core::RefCountedPtr<grpc_channel_credentials> ssl_creds)54   grpc_google_default_channel_credentials(
55       grpc_core::RefCountedPtr<grpc_channel_credentials> alts_creds,
56       grpc_core::RefCountedPtr<grpc_channel_credentials> ssl_creds)
57       : alts_creds_(std::move(alts_creds)), ssl_creds_(std::move(ssl_creds)) {}
58 
59   ~grpc_google_default_channel_credentials() override = default;
60 
61   grpc_core::RefCountedPtr<grpc_channel_security_connector>
62   create_security_connector(
63       grpc_core::RefCountedPtr<grpc_call_credentials> call_creds,
64       const char* target, grpc_core::ChannelArgs* args) override;
65 
66   grpc_core::ChannelArgs update_arguments(grpc_core::ChannelArgs args) override;
67 
68   static grpc_core::UniqueTypeName Type();
69 
type()70   grpc_core::UniqueTypeName type() const override { return Type(); }
71 
alts_creds()72   const grpc_channel_credentials* alts_creds() const {
73     return alts_creds_.get();
74   }
ssl_creds()75   const grpc_channel_credentials* ssl_creds() const { return ssl_creds_.get(); }
76 
77  private:
cmp_impl(const grpc_channel_credentials * other)78   int cmp_impl(const grpc_channel_credentials* other) const override {
79     // TODO(yashykt): Check if we can do something better here
80     return grpc_core::QsortCompare(
81         static_cast<const grpc_channel_credentials*>(this), other);
82   }
83 
84   grpc_core::RefCountedPtr<grpc_channel_credentials> alts_creds_;
85   grpc_core::RefCountedPtr<grpc_channel_credentials> ssl_creds_;
86 };
87 
88 namespace grpc_core {
89 namespace internal {
90 
91 typedef bool (*grpc_gce_tenancy_checker)(void);
92 
93 void set_gce_tenancy_checker_for_testing(grpc_gce_tenancy_checker checker);
94 
95 // TEST-ONLY. Reset the internal global state.
96 void grpc_flush_cached_google_default_credentials(void);
97 
98 }  // namespace internal
99 }  // namespace grpc_core
100 
101 #endif  // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_GOOGLE_DEFAULT_GOOGLE_DEFAULT_CREDENTIALS_H
102