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
|
// 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/credential_provider/extension/app_inventory_manager.h"
#include <memory>
#include "base/strings/utf_string_conversions.h"
#include "chrome/credential_provider/common/gcp_strings.h"
#include "chrome/credential_provider/gaiacp/gcp_utils.h"
#include "chrome/credential_provider/gaiacp/gcpw_strings.h"
#include "chrome/credential_provider/gaiacp/logging.h"
#include "chrome/credential_provider/gaiacp/mdm_utils.h"
#include "chrome/credential_provider/gaiacp/os_user_manager.h"
#include "chrome/credential_provider/gaiacp/reg_utils.h"
#include "chrome/credential_provider/gaiacp/win_http_url_fetcher.h"
namespace credential_provider {
const base::TimeDelta kDefaultUploadAppInventoryRequestTimeout =
base::Milliseconds(12000);
namespace {
// Constants used for contacting the gem service.
const char kGemServiceUploadAppInventoryPath[] = "/v1/uploadDeviceDetails";
const char kUploadAppInventoryRequestUserSidParameterName[] = "user_sid";
const char kUploadAppInventoryRequestDeviceResourceIdParameterName[] =
"device_resource_id";
const char kUploadAppInventoryRequestWin32AppsParameterName[] =
"windows_gpcw_app_info";
const char kDmToken[] = "dm_token";
const char kObfuscatedGaiaId[] = "obfuscated_gaia_id";
const char kAppDisplayName[] = "name";
const char kAppDisplayVersion[] = "version";
const char kAppPublisher[] = "publisher";
const char kAppType[] = "app_type";
const wchar_t kInstalledWin32AppsRegistryPath[] =
L"SOFTWARE\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
const wchar_t kInstalledWin32AppsRegistryPathWOW6432[] =
L"SOFTWARE\\WOW6432Node\\Microsoft\\Windows\\CurrentVersion\\Uninstall";
const wchar_t kDelimiter[] = L"\\";
const wchar_t kAppDisplayNameRegistryKey[] = L"DisplayName";
const wchar_t kAppDisplayVersionRegistryKey[] = L"DisplayVersion";
const wchar_t kAppPublisherRegistryKey[] = L"Publisher";
// Registry key to control whether upload app data from ESA feature is
// enabled.
const wchar_t kUploadAppInventoryFromEsaEnabledRegKey[] =
L"upload_app_inventory_from_esa";
// The period of uploading app inventory to the backend.
const base::TimeDelta kUploadAppInventoryExecutionPeriod = base::Hours(3);
// True when upload device details from ESA feature is enabled.
bool g_upload_app_inventory_from_esa_enabled = false;
// Maximum number of retries if a HTTP call to the backend fails.
constexpr unsigned int kMaxNumHttpRetries = 3;
// Defines a task that is called by the ESA to upload app data.
class UploadAppInventoryTask : public extension::Task {
public:
static std::unique_ptr<extension::Task> Create() {
std::unique_ptr<extension::Task> esa_task(new UploadAppInventoryTask());
return esa_task;
}
// ESA calls this to retrieve a configuration for the task execution. Return
// a default config for now.
extension::Config GetConfig() final {
extension::Config config;
config.execution_period = kUploadAppInventoryExecutionPeriod;
return config;
}
// ESA calls this to set all the user-device contexts for the execution of the
// task.
HRESULT SetContext(const std::vector<extension::UserDeviceContext>& c) final {
context_ = c;
return S_OK;
}
// ESA calls execute function to perform the actual task.
HRESULT Execute() final {
HRESULT task_status = S_OK;
for (const auto& c : context_) {
HRESULT hr = AppInventoryManager::Get()->UploadAppInventory(c);
if (FAILED(hr)) {
LOGFN(ERROR) << "Failed uploading device details for " << c.user_sid
<< ". hr=" << putHR(hr);
task_status = hr;
}
}
return task_status;
}
private:
std::vector<extension::UserDeviceContext> context_;
};
} // namespace
// static
AppInventoryManager* AppInventoryManager::Get() {
return *GetInstanceStorage();
}
// static
AppInventoryManager** AppInventoryManager::GetInstanceStorage() {
static AppInventoryManager instance(kDefaultUploadAppInventoryRequestTimeout);
static AppInventoryManager* instance_storage = &instance;
return &instance_storage;
}
// static
extension::TaskCreator AppInventoryManager::UploadAppInventoryTaskCreator() {
return base::BindRepeating(&UploadAppInventoryTask::Create);
}
AppInventoryManager::AppInventoryManager(
base::TimeDelta upload_app_inventory_request_timeout)
: upload_app_inventory_request_timeout_(
upload_app_inventory_request_timeout) {
g_upload_app_inventory_from_esa_enabled =
GetGlobalFlagOrDefault(kUploadAppInventoryFromEsaEnabledRegKey, 1) == 1;
}
AppInventoryManager::~AppInventoryManager() = default;
GURL AppInventoryManager::GetGemServiceUploadAppInventoryUrl() {
GURL gem_service_url = GetGcpwServiceUrl();
return gem_service_url.Resolve(kGemServiceUploadAppInventoryPath);
}
bool AppInventoryManager::UploadAppInventoryFromEsaFeatureEnabled() const {
return g_upload_app_inventory_from_esa_enabled;
}
// Uploads the app data into GEM database using |dm_token|
// for authentication and authorization. The GEM service would use
// |resource_id| for identifying the device entry in GEM database.
HRESULT AppInventoryManager::UploadAppInventory(
const extension::UserDeviceContext& context) {
std::wstring obfuscated_user_id;
HRESULT status = GetIdFromSid(context.user_sid.c_str(), &obfuscated_user_id);
if (FAILED(status)) {
LOGFN(ERROR) << "Could not get user id from sid " << context.user_sid;
return status;
}
if (obfuscated_user_id.empty()) {
LOGFN(ERROR) << "Got empty user id from sid " << context.user_sid;
return E_FAIL;
}
std::wstring dm_token_value = context.dm_token;
HRESULT hr;
if (dm_token_value.empty()) {
hr = GetGCPWDmToken(context.user_sid, &dm_token_value);
if (FAILED(hr)) {
LOGFN(WARNING) << "Failed to fetch DmToken hr=" << putHR(hr);
return hr;
}
}
request_dict_ = std::make_unique<base::Value::Dict>();
request_dict_->Set(kUploadAppInventoryRequestUserSidParameterName,
base::WideToUTF8(context.user_sid));
request_dict_->Set(kDmToken, base::WideToUTF8(dm_token_value));
request_dict_->Set(kObfuscatedGaiaId, base::WideToUTF8(obfuscated_user_id));
std::wstring known_resource_id =
context.device_resource_id.empty()
? GetUserDeviceResourceId(context.user_sid)
: context.device_resource_id;
// ResourceId cannot be empty while uploading app data. App data is updated
// only for devices with existing device record.
if (known_resource_id.empty()) {
LOGFN(ERROR) << "Could not find valid resourceId for sid:"
<< context.user_sid;
return E_FAIL;
}
request_dict_->Set(kUploadAppInventoryRequestDeviceResourceIdParameterName,
base::WideToUTF8(known_resource_id));
request_dict_->Set(kUploadAppInventoryRequestWin32AppsParameterName,
GetInstalledWin32Apps());
std::optional<base::Value::Dict> request_result;
hr = WinHttpUrlFetcher::BuildRequestAndFetchResultFromHttpService(
AppInventoryManager::Get()->GetGemServiceUploadAppInventoryUrl(),
/* access_token= */ std::string(), {}, *request_dict_,
upload_app_inventory_request_timeout_, kMaxNumHttpRetries,
&request_result);
if (FAILED(hr)) {
LOGFN(ERROR) << "BuildRequestAndFetchResultFromHttpService hr="
<< putHR(hr);
return E_FAIL;
}
return hr;
}
base::Value AppInventoryManager::GetInstalledWin32Apps() {
std::vector<std::wstring> app_name_list;
std::vector<std::wstring> app_path_list;
GetChildrenAtPath(kInstalledWin32AppsRegistryPath, app_name_list);
for (std::wstring a : app_name_list) {
app_path_list.push_back(std::wstring(kInstalledWin32AppsRegistryPath)
.append(std::wstring(kDelimiter))
.append(a));
}
app_name_list.clear();
GetChildrenAtPath(kInstalledWin32AppsRegistryPathWOW6432, app_name_list);
for (std::wstring a : app_name_list) {
app_path_list.push_back(std::wstring(kInstalledWin32AppsRegistryPathWOW6432)
.append(std::wstring(kDelimiter))
.append(a));
}
base::Value::List app_info_value_list;
for (std::wstring regPath : app_path_list) {
base::Value::Dict request_dict;
wchar_t display_name[256];
ULONG display_length = std::size(display_name);
HRESULT hr =
GetMachineRegString(regPath, std::wstring(kAppDisplayNameRegistryKey),
display_name, &display_length);
if (hr == S_OK) {
request_dict.Set(kAppDisplayName, base::WideToUTF8(display_name));
wchar_t display_version[256];
ULONG version_length = std::size(display_version);
hr = GetMachineRegString(regPath,
std::wstring(kAppDisplayVersionRegistryKey),
display_version, &version_length);
if (hr == S_OK) {
request_dict.Set(kAppDisplayVersion, base::WideToUTF8(display_version));
}
wchar_t publisher[256];
ULONG publisher_length = std::size(publisher);
hr = GetMachineRegString(regPath, std::wstring(kAppPublisherRegistryKey),
publisher, &publisher_length);
if (hr == S_OK) {
request_dict.Set(kAppPublisher, base::WideToUTF8(publisher));
}
// App_type value 1 refers to WIN_32 applications.
request_dict.Set(kAppType, 1);
app_info_value_list.Append(std::move(request_dict));
}
}
return base::Value(std::move(app_info_value_list));
}
void AppInventoryManager::SetUploadAppInventoryFromEsaFeatureEnabledForTesting(
bool value) {
g_upload_app_inventory_from_esa_enabled = value;
}
void AppInventoryManager::SetFakesForTesting(FakesForTesting* fakes) {
DCHECK(fakes);
WinHttpUrlFetcher::SetCreatorForTesting(
fakes->fake_win_http_url_fetcher_creator);
if (fakes->os_user_manager_for_testing) {
OSUserManager::SetInstanceForTesting(fakes->os_user_manager_for_testing);
}
}
} // namespace credential_provider
|