Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add group policy for sync service URL #25498

Merged
merged 2 commits into from
Sep 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions browser/policy/brave_simple_policy_map.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@

#if BUILDFLAG(IS_WIN) || BUILDFLAG(IS_MAC) || BUILDFLAG(IS_LINUX)
#include "brave/components/brave_rewards/common/pref_names.h"
#include "brave/components/brave_sync/brave_sync_prefs.h"
#include "brave/components/brave_wallet/common/pref_names.h"
#endif

Expand Down Expand Up @@ -43,7 +44,10 @@ inline constexpr PolicyToPreferenceMapEntry kBraveSimplePolicyMap[] = {
kManagedBraveShieldsDisabledForUrls, base::Value::Type::LIST},
{policy::key::kBraveShieldsEnabledForUrls,
kManagedBraveShieldsEnabledForUrls, base::Value::Type::LIST},
{policy::key::kBraveSyncUrl, brave_sync::kCustomSyncServiceUrl,
base::Value::Type::STRING},
#endif

#if BUILDFLAG(ENABLE_TOR)
{policy::key::kTorDisabled, tor::prefs::kTorDisabled,
base::Value::Type::BOOLEAN},
Expand Down
20 changes: 20 additions & 0 deletions chromium_src/components/policy/tools/generate_policy_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,6 +166,26 @@ def AddBravePolicies(template_file_contents):
'desc': ('''This policy allows an admin to specify that Brave '''
'''AI Chat feature will be enabled.'''),
},
{
'name': 'BraveSyncUrl',
'type': 'main',
'schema': {
'type': 'string'
},
'supported_on': ['chrome.*:129-'],
'features': {
'dynamic_refresh': False,
'per_profile': True,
'can_be_recommended': False,
'can_be_mandatory': True
},
'example_value': ['https://sync-v2.brave.com/v2'],
'id': 8,
'caption': '''Custom sync server URL.''',
'tags': [],
'desc': ('''This policy allows an admin to specify a custom '''
'''sync server URL for Brave.'''),
},
]

# Our new polices are added with highest id
Expand Down
1 change: 1 addition & 0 deletions chromium_src/components/sync/service/DEPS
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
include_rules = [
"+brave/components/brave_sync",
"+brave/components/sync/service",
]
38 changes: 38 additions & 0 deletions chromium_src/components/sync/service/sync_service_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,51 @@
* License, v. 2.0. If a copy of the MPL was not distributed with this file,
* You can obtain one at https://mozilla.org/MPL/2.0/. */

#include "brave/components/brave_sync/brave_sync_prefs.h"
#include "brave/components/sync/service/brave_sync_auth_manager.h"
#include "brave/components/sync/service/brave_sync_stopped_reporter.h"
#include "components/prefs/pref_service.h"
#include "components/sync/base/sync_util.h"

namespace syncer {

GURL BraveGetSyncServiceURL(const base::CommandLine& command_line,
version_info::Channel channel,
PrefService* prefs) {
// Allow group policy to override sync service URL.
// This has a higher priority than the --sync-url command-line param.
// https://github.com/brave/brave-browser/issues/20431
if (prefs && prefs->IsManagedPreference(brave_sync::kCustomSyncServiceUrl)) {
std::string value(prefs->GetString(brave_sync::kCustomSyncServiceUrl));
if (!value.empty()) {
GURL custom_sync_url(value);
// Provided URL must be HTTPS.
if (custom_sync_url.is_valid() &&
custom_sync_url.SchemeIs(url::kHttpsScheme)) {
DVLOG(2) << "Sync URL specified via GPO: "
<< prefs->GetString(brave_sync::kCustomSyncServiceUrl);
return custom_sync_url;
} else {
LOG(WARNING) << "The following sync URL specified via GPO "
<< "is invalid: " << value;
}
}
}

// Default logic.
// See `GetSyncServiceURL` in `components/sync/base/sync_util.cc`
return GetSyncServiceURL(command_line, channel);
}

} // namespace syncer

#define SyncAuthManager BraveSyncAuthManager
#define SyncStoppedReporter BraveSyncStoppedReporter
#define GetSyncServiceURL(...) \
BraveGetSyncServiceURL(__VA_ARGS__, sync_client_->GetPrefService())
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

👍

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks again for your help here! 😄


#include "src/components/sync/service/sync_service_impl.cc"

#undef SyncAuthManager
#undef SyncStoppedReporter
#undef GetSyncServiceURL
1 change: 1 addition & 0 deletions components/brave_sync/brave_sync_prefs.cc
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ void Prefs::RegisterProfilePrefs(PrefRegistrySimple* registry) {
registry->RegisterBooleanPref(kSyncFailedDecryptSeedNoticeDismissed, false);
registry->RegisterBooleanPref(kSyncAccountDeletedNoticePending, false);
registry->RegisterStringPref(kSyncLeaveChainDetails, std::string());
registry->RegisterStringPref(kCustomSyncServiceUrl, std::string());
}

// static
Expand Down
2 changes: 2 additions & 0 deletions components/brave_sync/brave_sync_prefs.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Time;

namespace brave_sync {

inline constexpr char kCustomSyncServiceUrl[] = "brave_sync.sync_service_url";

class Prefs {
public:
explicit Prefs(PrefService* pref_service);
Expand Down
45 changes: 44 additions & 1 deletion components/sync/service/brave_sync_service_impl_unittest.cc
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@
#include "components/sync/test/fake_sync_manager.h"
#include "components/sync/test/sync_service_impl_bundle.h"
#include "components/sync/test/test_data_type_store_service.h"
#include "components/sync_preferences/testing_pref_service_syncable.h"
#include "content/public/test/browser_task_environment.h"
#include "testing/gtest/include/gtest/gtest.h"

Expand Down Expand Up @@ -126,7 +127,7 @@ class BraveSyncServiceImplTest : public testing::Test {

SyncPrefs* sync_prefs() { return &sync_prefs_; }

PrefService* pref_service() {
sync_preferences::TestingPrefServiceSyncable* pref_service() {
return sync_service_impl_bundle_.pref_service();
}

Expand Down Expand Up @@ -155,6 +156,48 @@ class BraveSyncServiceImplTest : public testing::Test {
std::unique_ptr<BraveSyncServiceImpl> sync_service_impl_;
};

TEST_F(BraveSyncServiceImplTest, GroupPolicyOverride) {
pref_service()->SetManagedPref(brave_sync::kCustomSyncServiceUrl,
base::Value("https://sync.example.com/v2"));

OSCryptMocker::SetUp();

CreateSyncService();

EXPECT_FALSE(engine());

GURL expected_service_url = GURL("https://sync.example.com/v2");
GURL actual_service_url =
brave_sync_service_impl()->GetSyncServiceUrlForDebugging();
EXPECT_EQ(expected_service_url, actual_service_url);

OSCryptMocker::TearDown();

pref_service()->SetManagedPref(brave_sync::kCustomSyncServiceUrl,
base::Value(""));
}

TEST_F(BraveSyncServiceImplTest, GroupPolicyNonHttpsOverride) {
pref_service()->SetManagedPref(brave_sync::kCustomSyncServiceUrl,
base::Value("http://sync.example.com/v2"));

OSCryptMocker::SetUp();

CreateSyncService();

EXPECT_FALSE(engine());

GURL expected_service_url = GURL("http://sync.example.com/v2");
GURL actual_service_url =
brave_sync_service_impl()->GetSyncServiceUrlForDebugging();
EXPECT_NE(expected_service_url, actual_service_url);

OSCryptMocker::TearDown();

pref_service()->SetManagedPref(brave_sync::kCustomSyncServiceUrl,
base::Value(""));
}

TEST_F(BraveSyncServiceImplTest, ValidPassphrase) {
OSCryptMocker::SetUp();

Expand Down
Loading