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" |
| 19 | |
| 20 | namespace brillo { |
| 21 | namespace dbus_utils { |
| 22 | class DBusObject; |
| 23 | } |
| 24 | |
| 25 | } // namespace brillo |
| 26 | |
| 27 | namespace system_proxy { |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 28 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 29 | class KerberosClient; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 30 | class SandboxedWorker; |
| 31 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 32 | // Implementation of the SystemProxy D-Bus interface. |
| 33 | class SystemProxyAdaptor : public org::chromium::SystemProxyAdaptor, |
| 34 | public org::chromium::SystemProxyInterface { |
| 35 | public: |
| 36 | explicit SystemProxyAdaptor( |
| 37 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object); |
| 38 | SystemProxyAdaptor(const SystemProxyAdaptor&) = delete; |
| 39 | SystemProxyAdaptor& operator=(const SystemProxyAdaptor&) = delete; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 40 | virtual ~SystemProxyAdaptor(); |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 41 | |
| 42 | // Registers the D-Bus object and interfaces. |
| 43 | void RegisterAsync( |
| 44 | const brillo::dbus_utils::AsyncEventSequencer::CompletionAction& |
| 45 | completion_callback); |
| 46 | |
| 47 | // org::chromium::SystemProxyInterface: (see org.chromium.SystemProxy.xml). |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 48 | std::vector<uint8_t> SetAuthenticationDetails( |
| 49 | const std::vector<uint8_t>& request_blob) override; |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 50 | std::vector<uint8_t> ShutDown() override; |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame^] | 51 | std::vector<uint8_t> ClearUserCredentials( |
| 52 | const std::vector<uint8_t>& request_blob) override; |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 53 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 54 | void GetChromeProxyServersAsync( |
| 55 | const std::string& target_url, |
| 56 | const brillo::http::GetChromeProxyServersCallback& callback); |
| 57 | |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 58 | void RequestAuthenticationCredentials( |
| 59 | const worker::ProtectionSpace& protection_space); |
| 60 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 61 | protected: |
| 62 | virtual std::unique_ptr<SandboxedWorker> CreateWorker(); |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 63 | virtual void ConnectNamespace(SandboxedWorker* worker, bool user_traffic); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 64 | // Triggers the |WorkerActive| signal. |
| 65 | void OnNamespaceConnected(SandboxedWorker* worker, bool user_traffic); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 66 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 67 | private: |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 68 | friend class SystemProxyAdaptorTest; |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 69 | FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails); |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 70 | FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 71 | FRIEND_TEST(SystemProxyAdaptorTest, ShutDown); |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 72 | FRIEND_TEST(SystemProxyAdaptorTest, ConnectNamespace); |
| 73 | FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter); |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 74 | FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceAuthenticationRequired); |
| 75 | FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceNoCredentials); |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame^] | 76 | FRIEND_TEST(SystemProxyAdaptorTest, ClearUserCredentials); |
| 77 | FRIEND_TEST(SystemProxyAdaptorTest, ClearUserCredentialsRestartService); |
Andreea Costinas | 41e0644 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 78 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 79 | void SetCredentialsTask(SandboxedWorker* worker, |
Andreea Costinas | db2cbee | 2020-06-15 11:43:44 +0200 | [diff] [blame] | 80 | const worker::Credentials& credentials); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 81 | |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 82 | void SetKerberosEnabledTask(SandboxedWorker* worker, |
| 83 | bool kerberos_enabled, |
| 84 | const std::string& principal_name); |
| 85 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 86 | void ShutDownTask(); |
| 87 | |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 88 | void ConnectNamespaceTask(SandboxedWorker* worker, bool user_traffic); |
| 89 | |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 90 | bool StartWorker(SandboxedWorker* worker, bool user_traffic); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 91 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame^] | 92 | // Terminates the worker process for traffic indicated by |user_traffic| and |
| 93 | // frees the SandboxedWorker associated with it. |
| 94 | bool ResetWorker(bool user_traffic); |
| 95 | |
| 96 | // Returns a pointer to the worker process associated with |user_traffic|. Can |
| 97 | // return nullptr. |
| 98 | SandboxedWorker* GetWorker(bool user_traffic); |
| 99 | |
Andreea Costinas | 77b180e | 2020-05-12 15:17:32 +0200 | [diff] [blame] | 100 | // Checks if a worker process exists and if not creates one and sends a |
| 101 | // request to patchpanel to setup the network namespace for it. Returns true |
| 102 | // if the worker exists or was created successfully, false otherwise. |
| 103 | bool CreateWorkerIfNeeded(bool user_traffic); |
| 104 | |
Andreea Costinas | e9c7359 | 2020-07-17 15:27:54 +0200 | [diff] [blame^] | 105 | // Sends a request to the worker process associated with |user_traffic| to |
| 106 | // clear the cached user credentials. If sending the request fails, the worker |
| 107 | // will be restarted. |
| 108 | void ClearUserCredentials(bool user_traffic, std::string* error_message); |
| 109 | |
Andreea Costinas | a89309d | 2020-05-08 15:51:12 +0200 | [diff] [blame] | 110 | // Called when the patchpanel D-Bus service becomes available. |
Andreea Costinas | edb7c8e | 2020-04-22 10:58:04 +0200 | [diff] [blame] | 111 | void OnPatchpanelServiceAvailable(bool is_available); |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 112 | |
Andreea Costinas | 5862b10 | 2020-03-19 14:45:36 +0100 | [diff] [blame] | 113 | // The callback of |GetChromeProxyServersAsync|. |
| 114 | void OnGetProxyServers(bool success, const std::vector<std::string>& servers); |
| 115 | |
Andreea Costinas | 91f7535 | 2020-07-08 14:47:47 +0200 | [diff] [blame] | 116 | // The number of tries left for setting up the network namespace of the |
| 117 | // System-proxy worker for system traffic. TODO(acostinas, b/160736881) Remove |
| 118 | // when patchpaneld creates the veth pair directly across the host and worker |
| 119 | // network namespaces. |
| 120 | int netns_reconnect_attempts_available_; |
| 121 | |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 122 | // Worker that authenticates and forwards to a remote web proxy traffic |
| 123 | // coming form Chrome OS system services. |
| 124 | std::unique_ptr<SandboxedWorker> system_services_worker_; |
| 125 | // Worker that authenticates and forwards to a remote web proxy traffic |
| 126 | // coming form ARC++ apps. |
| 127 | std::unique_ptr<SandboxedWorker> arc_worker_; |
Andreea Costinas | 922fbaf | 2020-05-28 11:55:22 +0200 | [diff] [blame] | 128 | std::unique_ptr<KerberosClient> kerberos_client_; |
| 129 | |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 130 | std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_; |
Andreea Costinas | c7d5ad0 | 2020-03-09 09:41:51 +0100 | [diff] [blame] | 131 | base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_; |
Andreea Costinas | 942284d | 2020-01-28 16:28:40 +0100 | [diff] [blame] | 132 | }; |
| 133 | |
| 134 | } // namespace system_proxy |
| 135 | #endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_ |