Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 1 | // Copyright 2020 The Chromium OS Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | #ifndef SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_ |
| 5 | #define SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_ |
| 6 | |
| 7 | #include <memory> |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 8 | #include <string> |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 11 | #include <base/memory/weak_ptr.h> |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 12 | #include <brillo/dbus/async_event_sequencer.h> |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 13 | #include <brillo/http/http_proxy.h> |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 14 | #include <gtest/gtest_prod.h> // for FRIEND_TEST |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 15 | #include <patchpanel/proto_bindings/patchpanel_service.pb.h> |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 16 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 17 | #include "bindings/worker_common.pb.h" |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 18 | #include "system_proxy/org.chromium.SystemProxy.h" |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 19 | #include "system_proxy/proto_bindings/system_proxy_service.pb.h" |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 20 | |
| 21 | namespace brillo { |
| 22 | namespace dbus_utils { |
| 23 | class DBusObject; |
| 24 | } |
| 25 | |
| 26 | } // namespace brillo |
| 27 | |
| 28 | namespace system_proxy { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 29 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 30 | class KerberosClient; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 31 | class SandboxedWorker; |
| 32 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 33 | // Implementation of the SystemProxy D-Bus interface. |
| 34 | class SystemProxyAdaptor : public org::chromium::SystemProxyAdaptor, |
| 35 | public org::chromium::SystemProxyInterface { |
| 36 | public: |
| 37 | explicit SystemProxyAdaptor( |
| 38 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object); |
| 39 | SystemProxyAdaptor(const SystemProxyAdaptor&) = delete; |
| 40 | SystemProxyAdaptor& operator=(const SystemProxyAdaptor&) = delete; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 41 | virtual ~SystemProxyAdaptor(); |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 42 | |
| 43 | // Registers the D-Bus object and interfaces. |
| 44 | void RegisterAsync( |
| 45 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& |
| 46 | completion_callback); |
| 47 | |
| 48 | // org::chromium::SystemProxyInterface: (see org.chromium.SystemProxy.xml). |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 49 | std::vector<uint8_t> SetAuthenticationDetails( |
| 50 | const std::vector<uint8_t>& request_blob) override; |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 51 | std::vector<uint8_t> ShutDown() override; |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 52 | std::vector<uint8_t> ClearUserCredentials( |
| 53 | const std::vector<uint8_t>& request_blob) override; |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 54 | std::vector<uint8_t> ShutDownProcess( |
| 55 | const std::vector<uint8_t>& request_blob) override; |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 56 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 57 | void GetChromeProxyServersAsync( |
| 58 | const std::string& target_url, |
| 59 | const brillo::http::GetChromeProxyServersCallback& callback); |
| 60 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 61 | void RequestAuthenticationCredentials( |
Andreea Costinas | ed9e612 | 2020-08-12 12:06:19 +0200 | [diff] [blame] | 62 | const worker::ProtectionSpace& protection_space, |
| 63 | bool bad_cached_credentials); |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 64 | // Returns true if |proxy| points to one of the local proxy workers. The |
| 65 | // method only matches against host and port, but not scheme. |
| 66 | // TODO(acostinas, crbug.com/1109207) Add an option to the proxy resolution |
| 67 | // service in Chrome that allows fetching only the addresses of "real" |
| 68 | // proxies. |
| 69 | bool IsLocalProxy(const std::string& proxy); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 70 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 71 | protected: |
| 72 | virtual std::unique_ptr<SandboxedWorker> CreateWorker(); |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 73 | virtual void ConnectNamespace(bool user_traffic); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 74 | // Triggers the |WorkerActive| signal. |
| 75 | void OnNamespaceConnected(SandboxedWorker* worker, bool user_traffic); |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 76 | // Returns a pointer to the worker process associated with |user_traffic|. Can |
| 77 | // return nullptr. |
| 78 | SandboxedWorker* GetWorker(bool user_traffic); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 79 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 80 | private: |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 81 | friend class SystemProxyAdaptorTest; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 82 | FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails); |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 83 | FRIEND_TEST(SystemProxyAdaptorTest, |
| 84 | SetAuthenticationDetailsOnlySystemTraffic); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 85 | FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 86 | FRIEND_TEST(SystemProxyAdaptorTest, ShutDown); |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 87 | FRIEND_TEST(SystemProxyAdaptorTest, ShutDownArc); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 88 | FRIEND_TEST(SystemProxyAdaptorTest, ConnectNamespace); |
| 89 | FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 90 | FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceAuthenticationRequired); |
| 91 | FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceNoCredentials); |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 92 | FRIEND_TEST(SystemProxyAdaptorTest, ClearUserCredentials); |
| 93 | FRIEND_TEST(SystemProxyAdaptorTest, ClearUserCredentialsRestartService); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 94 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 95 | void SetCredentialsTask(SandboxedWorker* worker, |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 96 | const worker::Credentials& credentials); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 97 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 98 | void SetKerberosEnabledTask(SandboxedWorker* worker, |
| 99 | bool kerberos_enabled, |
| 100 | const std::string& principal_name); |
| 101 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 102 | void ShutDownTask(); |
| 103 | |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 104 | void ConnectNamespaceTask(SandboxedWorker* worker, bool user_traffic); |
| 105 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 106 | // Terminates the worker process for traffic indicated by |user_traffic| and |
| 107 | // frees the SandboxedWorker associated with it. |
| 108 | bool ResetWorker(bool user_traffic); |
| 109 | |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 110 | void SetWorker(bool user_traffic, std::unique_ptr<SandboxedWorker> worker); |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 111 | |
Andreea Costinas | fc3dc7d | 2020-07-20 18:54:38 +0200 | [diff] [blame] | 112 | // Return true if |traffic_origin| represents the traffic originating from |
| 113 | // system services or if it includes all traffic. |
| 114 | bool IncludesSystemTraffic(TrafficOrigin traffic_origin); |
| 115 | // Return true if |traffic_origin| represents the traffic originating from ARC |
| 116 | // or if it includes all traffic. |
| 117 | bool IncludesUserTraffic(TrafficOrigin traffic_origin); |
| 118 | |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 119 | // Checks if a worker process exists and if not creates one and sends a |
| 120 | // request to patchpanel to setup the network namespace for it. Returns true |
| 121 | // if the worker exists or was created successfully, false otherwise. |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 122 | SandboxedWorker* CreateWorkerIfNeeded(bool user_traffic); |
| 123 | |
| 124 | // If setting the authentication details to |worker| fails, it updates |
| 125 | // |error_message| with an appropriate error message. |
| 126 | void SetAuthenticationDetails(SetAuthenticationDetailsRequest auth_details, |
| 127 | bool user_traffic, |
| 128 | std::string* error_message); |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 129 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame] | 130 | // Sends a request to the worker process associated with |user_traffic| to |
| 131 | // clear the cached user credentials. If sending the request fails, the worker |
| 132 | // will be restarted. |
| 133 | void ClearUserCredentials(bool user_traffic, std::string* error_message); |
| 134 | |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 135 | // Called when the patchpanel D-Bus service becomes available. |
Andreea Costinas | 350e4aa | 2020-07-20 20:29:46 +0200 | [diff] [blame] | 136 | void OnPatchpanelServiceAvailable(bool user_traffic, bool is_available); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 137 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 138 | // The callback of |GetChromeProxyServersAsync|. |
| 139 | void OnGetProxyServers(bool success, const std::vector<std::string>& servers); |
| 140 | |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 141 | // The number of tries left for setting up the network namespace of the |
| 142 | // System-proxy worker for system traffic. TODO(acostinas, b/160736881) Remove |
| 143 | // when patchpaneld creates the veth pair directly across the host and worker |
| 144 | // network namespaces. |
| 145 | int netns_reconnect_attempts_available_; |
| 146 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 147 | // Worker that authenticates and forwards to a remote web proxy traffic |
| 148 | // coming form Chrome OS system services. |
| 149 | std::unique_ptr<SandboxedWorker> system_services_worker_; |
| 150 | // Worker that authenticates and forwards to a remote web proxy traffic |
| 151 | // coming form ARC++ apps. |
| 152 | std::unique_ptr<SandboxedWorker> arc_worker_; |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 153 | std::unique_ptr<KerberosClient> kerberos_client_; |
| 154 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 155 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 156 | base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_; |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 157 | }; |
| 158 | |
| 159 | } // namespace system_proxy |
| 160 | #endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_ |