blob: 1863f5103c89f8003e570c65b6a6e23735c8f671 [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
17#include "system_proxy/org.chromium.SystemProxy.h"
18
19namespace brillo {
20namespace dbus_utils {
21class DBusObject;
22}
23
24} // namespace brillo
25
26namespace system_proxy {
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010027
Andreea Costinas922fbaf2020-05-28 11:55:22 +020028class KerberosClient;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010029class SandboxedWorker;
30
Andreea Costinas942284d2020-01-28 16:28:40 +010031// Implementation of the SystemProxy D-Bus interface.
32class SystemProxyAdaptor : public org::chromium::SystemProxyAdaptor,
33 public org::chromium::SystemProxyInterface {
34 public:
35 explicit SystemProxyAdaptor(
36 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object);
37 SystemProxyAdaptor(const SystemProxyAdaptor&) = delete;
38 SystemProxyAdaptor& operator=(const SystemProxyAdaptor&) = delete;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010039 virtual ~SystemProxyAdaptor();
Andreea Costinas942284d2020-01-28 16:28:40 +010040
41 // Registers the D-Bus object and interfaces.
42 void RegisterAsync(
43 const brillo::dbus_utils::AsyncEventSequencer::CompletionAction&
44 completion_callback);
45
46 // org::chromium::SystemProxyInterface: (see org.chromium.SystemProxy.xml).
Andreea Costinas77b180e2020-05-12 15:17:32 +020047 std::vector<uint8_t> SetAuthenticationDetails(
48 const std::vector<uint8_t>& request_blob) override;
Andreea Costinas942284d2020-01-28 16:28:40 +010049 std::vector<uint8_t> SetSystemTrafficCredentials(
50 const std::vector<uint8_t>& request_blob) override;
51 std::vector<uint8_t> ShutDown() override;
52
Andreea Costinas5862b102020-03-19 14:45:36 +010053 void GetChromeProxyServersAsync(
54 const std::string& target_url,
55 const brillo::http::GetChromeProxyServersCallback& callback);
56
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010057 protected:
58 virtual std::unique_ptr<SandboxedWorker> CreateWorker();
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020059 virtual bool ConnectNamespace(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasa89309d2020-05-08 15:51:12 +020060 // Triggers the |WorkerActive| signal.
61 void OnNamespaceConnected(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010062
Andreea Costinas942284d2020-01-28 16:28:40 +010063 private:
Andreea Costinas41e06442020-03-09 09:41:51 +010064 friend class SystemProxyAdaptorTest;
Andreea Costinas77b180e2020-05-12 15:17:32 +020065 FRIEND_TEST(SystemProxyAdaptorTest, SetAuthenticationDetails);
Andreea Costinas41e06442020-03-09 09:41:51 +010066 FRIEND_TEST(SystemProxyAdaptorTest, SetSystemTrafficCredentials);
Andreea Costinas922fbaf2020-05-28 11:55:22 +020067 FRIEND_TEST(SystemProxyAdaptorTest, KerberosEnabled);
Andreea Costinas41e06442020-03-09 09:41:51 +010068 FRIEND_TEST(SystemProxyAdaptorTest, ShutDown);
Andreea Costinasa89309d2020-05-08 15:51:12 +020069 FRIEND_TEST(SystemProxyAdaptorTest, ConnectNamespace);
70 FRIEND_TEST(SystemProxyAdaptorTest, ProxyResolutionFilter);
Andreea Costinas41e06442020-03-09 09:41:51 +010071
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010072 void SetCredentialsTask(SandboxedWorker* worker,
73 const std::string& username,
74 const std::string& password);
75
Andreea Costinas922fbaf2020-05-28 11:55:22 +020076 void SetKerberosEnabledTask(SandboxedWorker* worker,
77 bool kerberos_enabled,
78 const std::string& principal_name);
79
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010080 void ShutDownTask();
81
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020082 bool StartWorker(SandboxedWorker* worker, bool user_traffic);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010083
Andreea Costinas77b180e2020-05-12 15:17:32 +020084 // Checks if a worker process exists and if not creates one and sends a
85 // request to patchpanel to setup the network namespace for it. Returns true
86 // if the worker exists or was created successfully, false otherwise.
87 bool CreateWorkerIfNeeded(bool user_traffic);
88
Andreea Costinasa89309d2020-05-08 15:51:12 +020089 // Called when the patchpanel D-Bus service becomes available.
Andreea Costinasedb7c8e2020-04-22 10:58:04 +020090 void OnPatchpanelServiceAvailable(bool is_available);
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010091
Andreea Costinas5862b102020-03-19 14:45:36 +010092 // The callback of |GetChromeProxyServersAsync|.
93 void OnGetProxyServers(bool success, const std::vector<std::string>& servers);
94
Andreea Costinasc7d5ad02020-03-09 09:41:51 +010095 // Worker that authenticates and forwards to a remote web proxy traffic
96 // coming form Chrome OS system services.
97 std::unique_ptr<SandboxedWorker> system_services_worker_;
98 // Worker that authenticates and forwards to a remote web proxy traffic
99 // coming form ARC++ apps.
100 std::unique_ptr<SandboxedWorker> arc_worker_;
Andreea Costinas922fbaf2020-05-28 11:55:22 +0200101 std::unique_ptr<KerberosClient> kerberos_client_;
102
Andreea Costinas942284d2020-01-28 16:28:40 +0100103 std::unique_ptr<brillo::dbus_utils::DBusObject> dbus_object_;
Andreea Costinasc7d5ad02020-03-09 09:41:51 +0100104 base::WeakPtrFactory<SystemProxyAdaptor> weak_ptr_factory_;
Andreea Costinas942284d2020-01-28 16:28:40 +0100105};
106
107} // namespace system_proxy
108#endif // SYSTEM_PROXY_SYSTEM_PROXY_ADAPTOR_H_