blob: 7e17de3ec60fb7e0e218074a8ffc9a5f27694bf9 [file] [log] [blame]
Andreea Costinas942284d2020-01-28 16:28:40 +01001// 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 Costinasc7d5ad02020-03-09 09:41:51 +01008#include <string>
Andreea Costinas942284d2020-01-28 16:28:40 +01009#include <vector>
10
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010011#include <base/memory/weak_ptr.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010012#include <brillo/dbus/async_event_sequencer.h>
Andreea Costinas5862b102020-03-19 14:45:36 +010013#include <brillo/http/http_proxy.h>
Andreea Costinas41e06442020-03-09 09:41:51 +010014#include <gtest/gtest_prod.h> // for FRIEND_TEST
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010015#include <patchpanel/proto_bindings/patchpanel_service.pb.h>
Andreea Costinas942284d2020-01-28 16:28:40 +010016
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020017#include "bindings/worker_common.pb.h"
Andreea Costinas942284d2020-01-28 16:28:40 +010018#include "system_proxy/org.chromium.SystemProxy.h"
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +020019#include "system_proxy/proto_bindings/system_proxy_service.pb.h"
Andreea Costinas942284d2020-01-28 16:28:40 +010020
21namespace brillo {
22namespace dbus_utils {
23class DBusObject;
24}
25
26} // namespace brillo
27
28namespace system_proxy {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010029
Andreea Costinas922fbaf2020-05-28 11:55:22 +020030class KerberosClient;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010031class SandboxedWorker;
32
Andreea Costinas942284d2020-01-28 16:28:40 +010033// Implementation of the SystemProxy D-Bus interface.
34class 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 Costinasc7d5ad02020-03-09 09:41:51 +010041 virtual ~SystemProxyAdaptor();
Andreea Costinas942284d2020-01-28 16:28:40 +010042
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 Costinas77b180e2020-05-12 15:17:32 +020049 std::vector<uint8_t> SetAuthenticationDetails(
50 const std::vector<uint8_t>& request_blob) override;
Andreea Costinase9c73592020-07-17 15:27:54 +020051 std::vector<uint8_t> ClearUserCredentials(
52 const std::vector<uint8_t>& request_blob) override;
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +020053 std::vector<uint8_t> ShutDownProcess(
54 const std::vector<uint8_t>& request_blob) override;
Andreea Costinas942284d2020-01-28 16:28:40 +010055
Andreea Costinas5862b102020-03-19 14:45:36 +010056 void GetChromeProxyServersAsync(
57 const std::string& target_url,
58 const brillo::http::GetChromeProxyServersCallback& callback);
59
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020060 void RequestAuthenticationCredentials(
Andreea Costinased9e6122020-08-12 12:06:19 +020061 const worker::ProtectionSpace& protection_space,
62 bool bad_cached_credentials);
Andreea Costinas350e4aa2020-07-20 20:29:46 +020063 // Returns true if |proxy| points to one of the local proxy workers. The
64 // method only matches against host and port, but not scheme.
65 // TODO(acostinas, crbug.com/1109207) Add an option to the proxy resolution
66 // service in Chrome that allows fetching only the addresses of "real"
67 // proxies.
68 bool IsLocalProxy(const std::string& proxy);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020069
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010070 protected:
71 virtual std::unique_ptr<SandboxedWorker> CreateWorker();
Andreea Costinas350e4aa2020-07-20 20:29:46 +020072 virtual void ConnectNamespace(bool user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +020073 // Triggers the |WorkerActive| signal.
74 void OnNamespaceConnected(SandboxedWorker* worker, bool user_traffic);
Andreea Costinas350e4aa2020-07-20 20:29:46 +020075 // Returns a pointer to the worker process associated with |user_traffic|. Can
76 // return nullptr.
77 SandboxedWorker* GetWorker(bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010078
Andreea Costinas942284d2020-01-28 16:28:40 +010079 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010080 friend class SystemProxyAdaptorTest;
Andreea Costinas77b180e2020-05-12 15:17:32 +020081 FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails);
Andreea Costinas350e4aa2020-07-20 20:29:46 +020082 FRIEND_TEST(SystemProxyAdaptorTest,
83 SetAuthenticationDetailsOnlySystemTraffic);
Andreea Costinas922fbaf2020-05-28 11:55:22 +020084 FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled);
Irem Uguz8eed46f2021-03-12 13:34:12 +000085 FRIEND_TEST(SystemProxyAdaptorTest, ShutDownProcess);
Andreea Costinas350e4aa2020-07-20 20:29:46 +020086 FRIEND_TEST(SystemProxyAdaptorTest, ShutDownArc);
Andreea Costinasa89309d2020-05-08 15:51:12 +020087 FRIEND_TEST(SystemProxyAdaptorTest, ConnectNamespace);
88 FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020089 FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceAuthenticationRequired);
90 FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceNoCredentials);
Andreea Costinase9c73592020-07-17 15:27:54 +020091 FRIEND_TEST(SystemProxyAdaptorTest, ClearUserCredentials);
92 FRIEND_TEST(SystemProxyAdaptorTest, ClearUserCredentialsRestartService);
Andreea Costinas41e06442020-03-09 09:41:51 +010093
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010094 void SetCredentialsTask(SandboxedWorker* worker,
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020095 const worker::Credentials& credentials);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010096
Andreea Costinas922fbaf2020-05-28 11:55:22 +020097 void SetKerberosEnabledTask(SandboxedWorker* worker,
98 bool kerberos_enabled,
99 const std::string& principal_name);
100
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100101 void ShutDownTask();
102
Andreea Costinas91f75352020-07-08 14:47:47 +0200103 void ConnectNamespaceTask(SandboxedWorker* worker, bool user_traffic);
104
Andreea Costinase9c73592020-07-17 15:27:54 +0200105 // Terminates the worker process for traffic indicated by |user_traffic| and
106 // frees the SandboxedWorker associated with it.
107 bool ResetWorker(bool user_traffic);
108
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200109 void SetWorker(bool user_traffic, std::unique_ptr<SandboxedWorker> worker);
Andreea Costinase9c73592020-07-17 15:27:54 +0200110
Andreea Costinasfc3dc7d2020-07-20 18:54:38 +0200111 // Return true if |traffic_origin| represents the traffic originating from
112 // system services or if it includes all traffic.
113 bool IncludesSystemTraffic(TrafficOrigin traffic_origin);
114 // Return true if |traffic_origin| represents the traffic originating from ARC
115 // or if it includes all traffic.
116 bool IncludesUserTraffic(TrafficOrigin traffic_origin);
117
Andreea Costinas77b180e2020-05-12 15:17:32 +0200118 // Checks if a worker process exists and if not creates one and sends a
119 // request to patchpanel to setup the network namespace for it. Returns true
120 // if the worker exists or was created successfully, false otherwise.
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200121 SandboxedWorker* CreateWorkerIfNeeded(bool user_traffic);
122
123 // If setting the authentication details to |worker| fails, it updates
124 // |error_message| with an appropriate error message.
125 void SetAuthenticationDetails(SetAuthenticationDetailsRequest auth_details,
126 bool user_traffic,
127 std::string* error_message);
Andreea Costinas77b180e2020-05-12 15:17:32 +0200128
Andreea Costinase9c73592020-07-17 15:27:54 +0200129 // Sends a request to the worker process associated with |user_traffic| to
130 // clear the cached user credentials. If sending the request fails, the worker
131 // will be restarted.
132 void ClearUserCredentials(bool user_traffic, std::string* error_message);
133
Andreea Costinasa89309d2020-05-08 15:51:12 +0200134 // Called when the patchpanel D-Bus service becomes available.
Andreea Costinas350e4aa2020-07-20 20:29:46 +0200135 void OnPatchpanelServiceAvailable(bool user_traffic, bool is_available);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100136
Andreea Costinas5862b102020-03-19 14:45:36 +0100137 // The callback of |GetChromeProxyServersAsync|.
138 void OnGetProxyServers(bool success, const std::vector<std::string>& servers);
139
Andreea Costinas91f75352020-07-08 14:47:47 +0200140 // The number of tries left for setting up the network namespace of the
141 // System-proxy worker for system traffic. TODO(acostinas, b/160736881) Remove
142 // when patchpaneld creates the veth pair directly across the host and worker
143 // network namespaces.
144 int netns_reconnect_attempts_available_;
145
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100146 // Worker that authenticates and forwards to a remote web proxy traffic
147 // coming form Chrome OS system services.
148 std::unique_ptr<SandboxedWorker> system_services_worker_;
149 // Worker that authenticates and forwards to a remote web proxy traffic
150 // coming form ARC++ apps.
151 std::unique_ptr<SandboxedWorker> arc_worker_;
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200152 std::unique_ptr<KerberosClient> kerberos_client_;
153
Andreea Costinas942284d2020-01-28 16:28:40 +0100154 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100155 base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_;
Andreea Costinas942284d2020-01-28 16:28:40 +0100156};
157
158} // namespace system_proxy
159#endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_