blob: c8dec58c29da11a9c354507a0204e1edc636ce8f [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"
19
20namespace brillo {
21namespace dbus_utils {
22class DBusObject;
23}
24
25} // namespace brillo
26
27namespace system_proxy {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010028
Andreea Costinas922fbaf2020-05-28 11:55:22 +020029class KerberosClient;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010030class SandboxedWorker;
31
Andreea Costinas942284d2020-01-28 16:28:40 +010032// Implementation of the SystemProxy D-Bus interface.
33class 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 Costinasc7d5ad02020-03-09 09:41:51 +010040 virtual ~SystemProxyAdaptor();
Andreea Costinas942284d2020-01-28 16:28:40 +010041
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 Costinas77b180e2020-05-12 15:17:32 +020048 std::vector<uint8_t> SetAuthenticationDetails(
49 const std::vector<uint8_t>& request_blob) override;
Andreea Costinas942284d2020-01-28 16:28:40 +010050 std::vector<uint8_t> SetSystemTrafficCredentials(
51 const std::vector<uint8_t>& request_blob) override;
52 std::vector<uint8_t> ShutDown() override;
53
Andreea Costinas5862b102020-03-19 14:45:36 +010054 void GetChromeProxyServersAsync(
55 const std::string& target_url,
56 const brillo::http::GetChromeProxyServersCallback& callback);
57
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020058 void RequestAuthenticationCredentials(
59 const worker::ProtectionSpace& protection_space);
60
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010061 protected:
62 virtual std::unique_ptr<SandboxedWorker> CreateWorker();
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020063 virtual bool ConnectNamespace(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +020064 // Triggers the |WorkerActive| signal.
65 void OnNamespaceConnected(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010066
Andreea Costinas942284d2020-01-28 16:28:40 +010067 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010068 friend class SystemProxyAdaptorTest;
Andreea Costinas77b180e2020-05-12 15:17:32 +020069 FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails);
Andreea Costinas41e06442020-03-09 09:41:51 +010070 FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials);
Andreea Costinas922fbaf2020-05-28 11:55:22 +020071 FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled);
Andreea Costinas41e06442020-03-09 09:41:51 +010072 FRIEND_TEST(SystemProxyAdaptorTest, ShutDown);
Andreea Costinasa89309d2020-05-08 15:51:12 +020073 FRIEND_TEST(SystemProxyAdaptorTest, ConnectNamespace);
74 FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter);
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020075 FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceAuthenticationRequired);
76 FRIEND_TEST(SystemProxyAdaptorTest, ProtectionSpaceNoCredentials);
Andreea Costinas41e06442020-03-09 09:41:51 +010077
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010078 void SetCredentialsTask(SandboxedWorker* worker,
Andreea Costinasdb2cbee2020-06-15 11:43:44 +020079 const worker::Credentials& credentials);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010080
Andreea Costinas922fbaf2020-05-28 11:55:22 +020081 void SetKerberosEnabledTask(SandboxedWorker* worker,
82 bool kerberos_enabled,
83 const std::string& principal_name);
84
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010085 void ShutDownTask();
86
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020087 bool StartWorker(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010088
Andreea Costinas77b180e2020-05-12 15:17:32 +020089 // Checks if a worker process exists and if not creates one and sends a
90 // request to patchpanel to setup the network namespace for it. Returns true
91 // if the worker exists or was created successfully, false otherwise.
92 bool CreateWorkerIfNeeded(bool user_traffic);
93
Andreea Costinasa89309d2020-05-08 15:51:12 +020094 // Called when the patchpanel D-Bus service becomes available.
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020095 void OnPatchpanelServiceAvailable(bool is_available);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010096
Andreea Costinas5862b102020-03-19 14:45:36 +010097 // The callback of |GetChromeProxyServersAsync|.
98 void OnGetProxyServers(bool success, const std::vector<std::string>& servers);
99
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100100 // Worker that authenticates and forwards to a remote web proxy traffic
101 // coming form Chrome OS system services.
102 std::unique_ptr<SandboxedWorker> system_services_worker_;
103 // Worker that authenticates and forwards to a remote web proxy traffic
104 // coming form ARC++ apps.
105 std::unique_ptr<SandboxedWorker> arc_worker_;
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200106 std::unique_ptr<KerberosClient> kerberos_client_;
107
Andreea Costinas942284d2020-01-28 16:28:40 +0100108 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100109 base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_;
Andreea Costinas942284d2020-01-28 16:28:40 +0100110};
111
112} // namespace system_proxy
113#endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_