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/support/port_platform.h>
22 
23 #include <utility>
24 
25 #include <grpc/grpc.h>
26 #include <grpc/grpc_security.h>
27 
28 #include "src/core/lib/channel/channel_args.h"
29 #include "src/core/lib/gpr/useful.h"
30 #include "src/core/lib/gprpp/ref_counted_ptr.h"
31 #include "src/core/lib/gprpp/unique_type_name.h"
32 #include "src/core/lib/security/credentials/credentials.h"
33 #include "src/core/lib/security/security_connector/security_connector.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   grpc_core::UniqueTypeName type() const override;
69 
alts_creds()70   const grpc_channel_credentials* alts_creds() const {
71     return alts_creds_.get();
72   }
ssl_creds()73   const grpc_channel_credentials* ssl_creds() const { return ssl_creds_.get(); }
74 
75  private:
cmp_impl(const grpc_channel_credentials * other)76   int cmp_impl(const grpc_channel_credentials* other) const override {
77     // TODO(yashykt): Check if we can do something better here
78     return grpc_core::QsortCompare(
79         static_cast<const grpc_channel_credentials*>(this), other);
80   }
81 
82   grpc_core::RefCountedPtr<grpc_channel_credentials> alts_creds_;
83   grpc_core::RefCountedPtr<grpc_channel_credentials> ssl_creds_;
84 };
85 
86 namespace grpc_core {
87 namespace internal {
88 
89 typedef bool (*grpc_gce_tenancy_checker)(void);
90 
91 void set_gce_tenancy_checker_for_testing(grpc_gce_tenancy_checker checker);
92 
93 // TEST-ONLY. Reset the internal global state.
94 void grpc_flush_cached_google_default_credentials(void);
95 
96 }  // namespace internal
97 }  // namespace grpc_core
98 
99 #endif  // GRPC_SRC_CORE_LIB_SECURITY_CREDENTIALS_GOOGLE_DEFAULT_GOOGLE_DEFAULT_CREDENTIALS_H
100