1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394
|
// Copyright 2021 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/ash/policy/networking/euicc_status_uploader.h"
#include "ash/constants/ash_features.h"
#include "base/json/json_string_value_serializer.h"
#include "base/metrics/histogram_functions.h"
#include "base/timer/timer.h"
#include "base/values.h"
#include "chrome/browser/ash/profiles/profile_helper.h"
#include "chrome/browser/ash/settings/device_settings_service.h"
#include "chrome/browser/profiles/profile.h"
#include "chromeos/ash/components/network/cellular_esim_profile_handler.h"
#include "chromeos/ash/components/network/managed_cellular_pref_handler.h"
#include "chromeos/ash/components/network/network_event_log.h"
#include "chromeos/ash/components/network/network_handler.h"
#include "chromeos/ash/components/network/network_state_handler.h"
#include "components/onc/onc_constants.h"
#include "components/policy/core/common/cloud/cloud_policy_client.h"
#include "components/policy/proto/device_management_backend.pb.h"
#include "components/prefs/pref_registry_simple.h"
#include "components/prefs/pref_service.h"
namespace policy {
namespace {
const char kLastUploadedEuiccStatusEuiccCountKey[] = "euicc_count";
const char kLastUploadedEuiccStatusESimProfilesKey[] = "esim_profiles";
const char kLastUploadedEuiccStatusESimProfilesIccidKey[] = "iccid";
const char kLastUploadedEuiccStatusESimProfilesNetworkNameKey[] =
"network_name";
const char kLastUploadedEuiccStatusESimProfilesSmdpActivationCodeKey[] =
"smdp_activation_code";
const char kLastUploadedEuiccStatusESimProfilesSmdsActivationCodeKey[] =
"smds_activation_code";
const net::BackoffEntry::Policy kBackOffPolicy = {
// Number of initial errors (in sequence) to ignore before applying
// exponential back-off rules.
0,
// Initial delay for exponential back-off in ms.
base::Minutes(5).InMilliseconds(),
// Factor by which the waiting time will be multiplied.
2,
// Fuzzing percentage. ex: 10% will spread requests randomly
// between 90%-100% of the calculated time.
0.5,
// Maximum amount of time we are willing to delay our request in ms.
base::Hours(6).InMilliseconds(),
// Time to keep an entry from being discarded even when it
// has no significant state, -1 to never discard.
-1,
// Starts with initial delay.
true,
};
// Returns whether the device's policy data is active and provisioned.
bool IsDeviceManaged() {
return ::ash::DeviceSettingsService::IsInitialized() &&
::ash::DeviceSettingsService::Get()->policy_data() &&
::ash::DeviceSettingsService::Get()->policy_data()->state() ==
enterprise_management::PolicyData::ACTIVE;
}
} // namespace
// static
const char EuiccStatusUploader::kLastUploadedEuiccStatusPref[] =
"esim.last_uploaded_euicc_status";
const char EuiccStatusUploader::kShouldSendClearProfilesRequestPref[] =
"esim.should_clear_profile_list";
EuiccStatusUploader::EuiccStatusUploader(CloudPolicyClient* client,
PrefService* local_state)
: EuiccStatusUploader(client,
local_state,
base::BindRepeating(&IsDeviceManaged)) {}
EuiccStatusUploader::EuiccStatusUploader(
CloudPolicyClient* client,
PrefService* local_state,
IsDeviceActiveCallback is_device_active_callback)
: client_(client),
local_state_(local_state),
is_device_managed_callback_(std::move(is_device_active_callback)),
retry_entry_(&kBackOffPolicy) {
if (!ash::NetworkHandler::IsInitialized()) {
LOG(WARNING) << "NetworkHandler is not initialized.";
return;
}
hermes_manager_observation_.Observe(ash::HermesManagerClient::Get());
hermes_euicc_observation_.Observe(ash::HermesEuiccClient::Get());
cloud_policy_client_observation_.Observe(client_.get());
auto* network_handler = ash::NetworkHandler::Get();
network_handler->managed_cellular_pref_handler()->AddObserver(this);
managed_network_configuration_handler_ =
network_handler->managed_network_configuration_handler();
managed_network_configuration_handler_->AddObserver(this);
}
EuiccStatusUploader::~EuiccStatusUploader() {
if (ash::NetworkHandler::IsInitialized()) {
ash::NetworkHandler::Get()->managed_cellular_pref_handler()->RemoveObserver(
this);
}
OnManagedNetworkConfigurationHandlerShuttingDown();
}
// static
void EuiccStatusUploader::RegisterLocalStatePrefs(
PrefRegistrySimple* registry) {
registry->RegisterDictionaryPref(kLastUploadedEuiccStatusPref,
PrefRegistry::NO_REGISTRATION_FLAGS);
registry->RegisterBooleanPref(kShouldSendClearProfilesRequestPref,
/*default_value=*/false);
}
// static
std::unique_ptr<enterprise_management::UploadEuiccInfoRequest>
EuiccStatusUploader::ConstructRequestFromStatus(const base::Value::Dict& status,
bool clear_profile_list) {
auto upload_request =
std::make_unique<enterprise_management::UploadEuiccInfoRequest>();
upload_request->set_euicc_count(
status.FindInt(kLastUploadedEuiccStatusEuiccCountKey).value());
auto* mutable_esim_profiles = upload_request->mutable_esim_profiles();
for (const auto& esim_profile :
*status.FindListByDottedPath(kLastUploadedEuiccStatusESimProfilesKey)) {
const base::Value::Dict& esim_profile_dict = esim_profile.GetDict();
enterprise_management::ESimProfileInfo esim_profile_info;
esim_profile_info.set_iccid(*esim_profile_dict.FindString(
kLastUploadedEuiccStatusESimProfilesIccidKey));
const std::string* network_name = esim_profile_dict.FindString(
kLastUploadedEuiccStatusESimProfilesNetworkNameKey);
if (network_name && !network_name->empty()) {
esim_profile_info.set_name(*network_name);
}
const std::string* smdp_activation_code = esim_profile_dict.FindString(
kLastUploadedEuiccStatusESimProfilesSmdpActivationCodeKey);
const std::string* smds_activation_code = esim_profile_dict.FindString(
kLastUploadedEuiccStatusESimProfilesSmdsActivationCodeKey);
if (smdp_activation_code && !smdp_activation_code->empty()) {
esim_profile_info.set_smdp_address(*smdp_activation_code);
} else if (smds_activation_code && !smds_activation_code->empty()) {
esim_profile_info.set_smds_address(*smds_activation_code);
} else {
NET_LOG(ERROR) << "Failed to find an activation code when constructing "
"EUICC upload request";
continue;
}
mutable_esim_profiles->Add(std::move(esim_profile_info));
}
upload_request->set_clear_profile_list(clear_profile_list);
return upload_request;
}
void EuiccStatusUploader::OnManagedNetworkConfigurationHandlerShuttingDown() {
if (managed_network_configuration_handler_ &&
managed_network_configuration_handler_->HasObserver(this)) {
managed_network_configuration_handler_->RemoveObserver(this);
}
managed_network_configuration_handler_ = nullptr;
}
void EuiccStatusUploader::OnRegistrationStateChanged(
CloudPolicyClient* client) {
MaybeUploadStatus();
}
void EuiccStatusUploader::OnPolicyFetched(CloudPolicyClient* client) {
if (is_policy_fetched_) {
return;
}
is_policy_fetched_ = true;
MaybeUploadStatus();
}
void EuiccStatusUploader::PoliciesApplied(const std::string& userhash) {
MaybeUploadStatus();
}
void EuiccStatusUploader::OnManagedCellularPrefChanged() {
MaybeUploadStatus();
}
void EuiccStatusUploader::OnAvailableEuiccListChanged() {
MaybeUploadStatus();
}
void EuiccStatusUploader::OnEuiccReset(const dbus::ObjectPath& euicc_path) {
// Remember that we should clear the profile list in the next upload. This
// ensures that profile list will be eventually cleared even if the immediate
// uploads do not succeed.
local_state_->SetBoolean(kShouldSendClearProfilesRequestPref, true);
MaybeUploadStatus();
}
base::Value::Dict EuiccStatusUploader::GetCurrentEuiccStatus() const {
auto status = base::Value::Dict().Set(
kLastUploadedEuiccStatusEuiccCountKey,
static_cast<int>(
ash::HermesManagerClient::Get()->GetAvailableEuiccs().size()));
base::Value::List esim_profiles;
for (const auto& esim_profile : ash::NetworkHandler::Get()
->cellular_esim_profile_handler()
->GetESimProfiles()) {
// Do not report non-provisioned cellular networks.
if (esim_profile.iccid().empty()) {
continue;
}
const base::Value::Dict* esim_metadata =
ash::NetworkHandler::Get()
->managed_cellular_pref_handler()
->GetESimMetadata(esim_profile.iccid());
// Report only managed profiles that we have metadata for.
if (!esim_metadata) {
continue;
}
base::Value::Dict esim_profile_to_add;
esim_profile_to_add.Set(kLastUploadedEuiccStatusESimProfilesIccidKey,
esim_profile.iccid());
const std::string* const smdp_activation_code =
esim_metadata->FindString(::onc::cellular::kSMDPAddress);
const std::string* const smds_activation_code =
esim_metadata->FindString(::onc::cellular::kSMDSAddress);
if (smdp_activation_code && !smdp_activation_code->empty()) {
esim_profile_to_add.Set(
kLastUploadedEuiccStatusESimProfilesSmdpActivationCodeKey,
*smdp_activation_code);
} else if (smds_activation_code && !smds_activation_code->empty()) {
esim_profile_to_add.Set(
kLastUploadedEuiccStatusESimProfilesSmdsActivationCodeKey,
*smds_activation_code);
} else {
// Report only managed profiles that we have an activation code for.
NET_LOG(ERROR) << "Failed to find an SM-DP+ or SM-DS activation code "
<< "in the eSIM metadata, skipping entry";
continue;
}
const std::string* network_name =
esim_metadata->FindString(::onc::network_config::kName);
if (network_name && !network_name->empty()) {
esim_profile_to_add.Set(
kLastUploadedEuiccStatusESimProfilesNetworkNameKey, *network_name);
}
esim_profiles.Append(std::move(esim_profile_to_add));
}
status.SetByDottedPath(kLastUploadedEuiccStatusESimProfilesKey,
std::move(esim_profiles));
return status;
}
void EuiccStatusUploader::MaybeUploadStatus() {
if (!client_->is_registered()) {
VLOG(1) << "Policy client is not registered.";
return;
}
if (!is_policy_fetched_) {
VLOG(1) << "Policy not fetched yet.";
return;
}
if (!is_device_managed_callback_.Run()) {
VLOG(1) << "Device is unmanaged or deprovisioned.";
return;
}
if (!managed_network_configuration_handler_) {
LOG(WARNING) << "ManageNetworkConfigurationHandler is not initialized.";
return;
}
if (ash::HermesManagerClient::Get()->GetAvailableEuiccs().empty()) {
VLOG(1) << "No EUICC available on the device.";
return;
}
const base::Value::Dict& last_uploaded_pref =
local_state_->GetDict(kLastUploadedEuiccStatusPref);
auto current_state = GetCurrentEuiccStatus();
// Force send the status if reset request was received.
if (local_state_->GetBoolean(kShouldSendClearProfilesRequestPref)) {
UploadStatus(std::move(current_state));
return;
}
if (attempted_upload_status_ == current_state) {
// We attempted to upload this status, but failed.
// Schedule retry.
if (!retry_timer_) {
retry_timer_ = std::make_unique<base::OneShotTimer>();
retry_timer_->Start(FROM_HERE, retry_entry_.GetTimeUntilRelease(),
base::BindOnce(&EuiccStatusUploader::RetryUpload,
weak_ptr_factory_.GetWeakPtr()));
}
return;
}
retry_timer_.reset();
if (last_uploaded_pref != current_state) {
UploadStatus(std::move(current_state));
}
}
void EuiccStatusUploader::UploadStatus(base::Value::Dict status) {
// Do not upload anything until the current upload finishes.
if (currently_uploading_) {
return;
}
currently_uploading_ = true;
attempted_upload_status_ = std::move(status);
const bool should_send_clear_profiles_request =
local_state_->GetBoolean(kShouldSendClearProfilesRequestPref);
auto upload_request = ConstructRequestFromStatus(
attempted_upload_status_, should_send_clear_profiles_request);
client_->UploadEuiccInfo(
std::move(upload_request),
base::BindOnce(&EuiccStatusUploader::OnStatusUploaded,
weak_ptr_factory_.GetWeakPtr(),
should_send_clear_profiles_request));
}
void EuiccStatusUploader::OnStatusUploaded(
bool should_send_clear_profiles_request,
bool success) {
currently_uploading_ = false;
retry_entry_.InformOfRequest(/*succeeded=*/success);
base::UmaHistogramBoolean(
"Network.Cellular.ESim.Policy.EuiccStatusUploadResult", success);
if (!success) {
LOG(ERROR) << "Failed to upload EUICC status.";
MaybeUploadStatus();
return;
}
VLOG(1) << "EUICC status successfully uploaded.";
// Remember the last uploaded status to not upload it again.
local_state_->SetDict(kLastUploadedEuiccStatusPref,
std::move(attempted_upload_status_));
if (should_send_clear_profiles_request) {
// Clean out the local state preference to not send `clear_profile_list` =
// true multiple times.
local_state_->ClearPref(kShouldSendClearProfilesRequestPref);
}
attempted_upload_status_.clear();
MaybeUploadStatus();
}
void EuiccStatusUploader::RetryUpload() {
attempted_upload_status_.clear();
MaybeUploadStatus();
}
void EuiccStatusUploader::FireRetryTimerIfExistsForTesting() {
if (retry_timer_) {
retry_timer_->FireNow();
}
}
} // namespace policy
|