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
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/policy/device_token_fetcher.h"
#include <algorithm>
#include "base/bind.h"
#include "base/metrics/histogram.h"
#include "base/time.h"
#include "chrome/browser/policy/cloud_policy_cache_base.h"
#include "chrome/browser/policy/cloud_policy_constants.h"
#include "chrome/browser/policy/cloud_policy_data_store.h"
#include "chrome/browser/policy/delayed_work_scheduler.h"
#include "chrome/browser/policy/device_management_service.h"
#include "chrome/browser/policy/enterprise_metrics.h"
#include "chrome/browser/policy/policy_notifier.h"
#include "chrome/browser/policy/proto/device_management_local.pb.h"
namespace em = enterprise_management;
namespace policy {
namespace {
// Retry after 5 minutes (with exponential backoff) after token fetch errors.
const int64 kTokenFetchErrorDelayMilliseconds = 5 * 60 * 1000;
// Retry after max 3 hours after token fetch errors.
const int64 kTokenFetchErrorMaxDelayMilliseconds = 3 * 60 * 60 * 1000;
// For unmanaged devices, check once per day whether they're still unmanaged.
const int64 kUnmanagedDeviceRefreshRateMilliseconds = 24 * 60 * 60 * 1000;
// Records the UMA metric corresponding to |status|, if it represents an error.
// Also records that a fetch response was received.
void SampleErrorStatus(DeviceManagementStatus status) {
UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchResponseReceived,
kMetricTokenSize);
int sample = -1;
switch (status) {
case DM_STATUS_SUCCESS:
return;
case DM_STATUS_REQUEST_FAILED:
case DM_STATUS_REQUEST_INVALID:
case DM_STATUS_SERVICE_MANAGEMENT_TOKEN_INVALID:
sample = kMetricTokenFetchRequestFailed;
break;
case DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED:
sample = kMetricTokenFetchManagementNotSupported;
break;
case DM_STATUS_SERVICE_DEVICE_NOT_FOUND:
sample = kMetricTokenFetchDeviceNotFound;
break;
case DM_STATUS_SERVICE_DEVICE_ID_CONFLICT:
sample = kMetricTokenFetchDeviceIdConflict;
break;
case DM_STATUS_SERVICE_INVALID_SERIAL_NUMBER:
sample = kMetricTokenFetchInvalidSerialNumber;
break;
case DM_STATUS_RESPONSE_DECODING_ERROR:
sample = kMetricTokenFetchBadResponse;
break;
case DM_STATUS_TEMPORARY_UNAVAILABLE:
case DM_STATUS_SERVICE_ACTIVATION_PENDING:
case DM_STATUS_SERVICE_POLICY_NOT_FOUND:
case DM_STATUS_HTTP_STATUS_ERROR:
sample = kMetricTokenFetchServerFailed;
break;
}
if (sample != -1)
UMA_HISTOGRAM_ENUMERATION(kMetricToken, sample, kMetricTokenSize);
else
NOTREACHED();
}
// Translates the DeviceRegisterResponse::DeviceMode |mode| to the enum used
// internally to represent different device modes.
DeviceMode TranslateProtobufDeviceMode(
em::DeviceRegisterResponse::DeviceMode mode) {
switch (mode) {
case em::DeviceRegisterResponse::ENTERPRISE:
return DEVICE_MODE_ENTERPRISE;
case em::DeviceRegisterResponse::KIOSK:
return DEVICE_MODE_KIOSK;
}
LOG(ERROR) << "Unknown enrollment mode in registration response: " << mode;
return DEVICE_MODE_UNKNOWN;
}
} // namespace
DeviceTokenFetcher::DeviceTokenFetcher(
DeviceManagementService* service,
CloudPolicyCacheBase* cache,
CloudPolicyDataStore* data_store,
PolicyNotifier* notifier)
: effective_token_fetch_error_delay_ms_(
kTokenFetchErrorDelayMilliseconds) {
Initialize(service,
cache,
data_store,
notifier,
new DelayedWorkScheduler);
}
DeviceTokenFetcher::DeviceTokenFetcher(
DeviceManagementService* service,
CloudPolicyCacheBase* cache,
CloudPolicyDataStore* data_store,
PolicyNotifier* notifier,
DelayedWorkScheduler* scheduler)
: effective_token_fetch_error_delay_ms_(
kTokenFetchErrorDelayMilliseconds) {
Initialize(service, cache, data_store, notifier, scheduler);
}
DeviceTokenFetcher::~DeviceTokenFetcher() {
scheduler_->CancelDelayedWork();
}
void DeviceTokenFetcher::FetchToken() {
SetState(STATE_INACTIVE);
FetchTokenInternal();
}
void DeviceTokenFetcher::SetUnmanagedState() {
// The call to |cache_->SetUnmanaged()| has to happen first because it sets
// the timestamp that |SetState()| needs to determine the correct refresh
// time.
cache_->SetUnmanaged();
SetState(STATE_UNMANAGED);
}
void DeviceTokenFetcher::SetSerialNumberInvalidState() {
SetState(STATE_BAD_SERIAL);
}
void DeviceTokenFetcher::Reset() {
SetState(STATE_INACTIVE);
}
void DeviceTokenFetcher::Initialize(DeviceManagementService* service,
CloudPolicyCacheBase* cache,
CloudPolicyDataStore* data_store,
PolicyNotifier* notifier,
DelayedWorkScheduler* scheduler) {
service_ = service;
cache_ = cache;
notifier_ = notifier;
data_store_ = data_store;
effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
state_ = STATE_INACTIVE;
scheduler_.reset(scheduler);
if (cache_->is_unmanaged())
SetState(STATE_UNMANAGED);
}
void DeviceTokenFetcher::FetchTokenInternal() {
DCHECK(state_ != STATE_TOKEN_AVAILABLE);
if (!data_store_->has_auth_token() || data_store_->device_id().empty()) {
// Maybe this device is unmanaged, just exit. The CloudPolicyController
// will call FetchToken() again if something changes.
return;
}
// Reinitialize |request_job_|, discarding any previous requests.
request_job_.reset(
service_->CreateJob(DeviceManagementRequestJob::TYPE_REGISTRATION));
request_job_->SetGaiaToken(data_store_->gaia_token());
request_job_->SetOAuthToken(data_store_->oauth_token());
request_job_->SetClientID(data_store_->device_id());
em::DeviceRegisterRequest* request =
request_job_->GetRequest()->mutable_register_request();
request->set_type(data_store_->policy_register_type());
if (!data_store_->machine_id().empty())
request->set_machine_id(data_store_->machine_id());
if (!data_store_->machine_model().empty())
request->set_machine_model(data_store_->machine_model());
if (data_store_->known_machine_id())
request->set_known_machine_id(true);
request_job_->Start(base::Bind(&DeviceTokenFetcher::OnTokenFetchCompleted,
base::Unretained(this)));
UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchRequested,
kMetricTokenSize);
}
void DeviceTokenFetcher::OnTokenFetchCompleted(
DeviceManagementStatus status,
const em::DeviceManagementResponse& response) {
if (status == DM_STATUS_SUCCESS && !response.has_register_response()) {
// Handled below.
status = DM_STATUS_RESPONSE_DECODING_ERROR;
}
SampleErrorStatus(status);
switch (status) {
case DM_STATUS_SUCCESS: {
const em::DeviceRegisterResponse& register_response =
response.register_response();
if (register_response.has_device_management_token()) {
UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchOK,
kMetricTokenSize);
if (data_store_->policy_register_type() ==
em::DeviceRegisterRequest::DEVICE) {
// TODO(pastarmovj): Default to DEVICE_MODE_UNKNOWN once DM server has
// been updated. http://crosbug.com/26624
DeviceMode mode = DEVICE_MODE_ENTERPRISE;
if (register_response.has_enrollment_type()) {
mode = TranslateProtobufDeviceMode(
register_response.enrollment_type());
}
if (mode == DEVICE_MODE_UNKNOWN) {
LOG(ERROR) << "Enrollment mode missing or unknown!";
SetState(STATE_BAD_ENROLLMENT_MODE);
return;
}
data_store_->set_device_mode(mode);
}
data_store_->SetDeviceToken(register_response.device_management_token(),
false);
SetState(STATE_TOKEN_AVAILABLE);
} else {
NOTREACHED();
UMA_HISTOGRAM_ENUMERATION(kMetricToken, kMetricTokenFetchBadResponse,
kMetricTokenSize);
SetState(STATE_ERROR);
}
return;
}
case DM_STATUS_SERVICE_MANAGEMENT_NOT_SUPPORTED:
SetUnmanagedState();
return;
case DM_STATUS_REQUEST_FAILED:
case DM_STATUS_TEMPORARY_UNAVAILABLE:
case DM_STATUS_SERVICE_DEVICE_NOT_FOUND:
case DM_STATUS_SERVICE_DEVICE_ID_CONFLICT:
SetState(STATE_TEMPORARY_ERROR);
return;
case DM_STATUS_SERVICE_MANAGEMENT_TOKEN_INVALID:
// Most probably the GAIA auth cookie has expired. We can not do anything
// until the user logs-in again.
SetState(STATE_BAD_AUTH);
return;
case DM_STATUS_SERVICE_INVALID_SERIAL_NUMBER:
SetSerialNumberInvalidState();
return;
case DM_STATUS_REQUEST_INVALID:
case DM_STATUS_HTTP_STATUS_ERROR:
case DM_STATUS_RESPONSE_DECODING_ERROR:
case DM_STATUS_SERVICE_ACTIVATION_PENDING:
case DM_STATUS_SERVICE_POLICY_NOT_FOUND:
SetState(STATE_ERROR);
return;
}
NOTREACHED();
SetState(STATE_ERROR);
}
void DeviceTokenFetcher::SetState(FetcherState state) {
state_ = state;
if (state_ != STATE_TEMPORARY_ERROR)
effective_token_fetch_error_delay_ms_ = kTokenFetchErrorDelayMilliseconds;
request_job_.reset(); // Stop any pending requests.
base::Time delayed_work_at;
switch (state_) {
case STATE_INACTIVE:
notifier_->Inform(CloudPolicySubsystem::UNENROLLED,
CloudPolicySubsystem::NO_DETAILS,
PolicyNotifier::TOKEN_FETCHER);
break;
case STATE_TOKEN_AVAILABLE:
notifier_->Inform(CloudPolicySubsystem::SUCCESS,
CloudPolicySubsystem::NO_DETAILS,
PolicyNotifier::TOKEN_FETCHER);
break;
case STATE_BAD_SERIAL:
notifier_->Inform(CloudPolicySubsystem::UNENROLLED,
CloudPolicySubsystem::BAD_SERIAL_NUMBER,
PolicyNotifier::TOKEN_FETCHER);
break;
case STATE_BAD_ENROLLMENT_MODE:
notifier_->Inform(CloudPolicySubsystem::UNENROLLED,
CloudPolicySubsystem::BAD_ENROLLMENT_MODE,
PolicyNotifier::TOKEN_FETCHER);
break;
case STATE_UNMANAGED:
delayed_work_at = cache_->last_policy_refresh_time() +
base::TimeDelta::FromMilliseconds(
kUnmanagedDeviceRefreshRateMilliseconds);
notifier_->Inform(CloudPolicySubsystem::UNMANAGED,
CloudPolicySubsystem::NO_DETAILS,
PolicyNotifier::TOKEN_FETCHER);
break;
case STATE_TEMPORARY_ERROR:
delayed_work_at = base::Time::Now() +
base::TimeDelta::FromMilliseconds(
effective_token_fetch_error_delay_ms_);
effective_token_fetch_error_delay_ms_ =
std::min(effective_token_fetch_error_delay_ms_ * 2,
kTokenFetchErrorMaxDelayMilliseconds);
notifier_->Inform(CloudPolicySubsystem::NETWORK_ERROR,
CloudPolicySubsystem::DMTOKEN_NETWORK_ERROR,
PolicyNotifier::TOKEN_FETCHER);
break;
case STATE_ERROR:
effective_token_fetch_error_delay_ms_ =
kTokenFetchErrorMaxDelayMilliseconds;
delayed_work_at = base::Time::Now() +
base::TimeDelta::FromMilliseconds(
effective_token_fetch_error_delay_ms_);
notifier_->Inform(CloudPolicySubsystem::NETWORK_ERROR,
CloudPolicySubsystem::DMTOKEN_NETWORK_ERROR,
PolicyNotifier::TOKEN_FETCHER);
break;
case STATE_BAD_AUTH:
// Can't do anything, need to wait for new credentials.
notifier_->Inform(CloudPolicySubsystem::BAD_GAIA_TOKEN,
CloudPolicySubsystem::NO_DETAILS,
PolicyNotifier::TOKEN_FETCHER);
break;
}
scheduler_->CancelDelayedWork();
if (!delayed_work_at.is_null()) {
base::Time now(base::Time::Now());
int64 delay = std::max<int64>((delayed_work_at - now).InMilliseconds(), 0);
scheduler_->PostDelayedWork(
base::Bind(&DeviceTokenFetcher::DoWork, base::Unretained(this)), delay);
}
// Inform the cache if a token fetch attempt has failed.
if (state_ != STATE_INACTIVE && state_ != STATE_TOKEN_AVAILABLE)
cache_->SetFetchingDone();
}
void DeviceTokenFetcher::DoWork() {
switch (state_) {
case STATE_INACTIVE:
case STATE_TOKEN_AVAILABLE:
case STATE_BAD_SERIAL:
case STATE_BAD_ENROLLMENT_MODE:
break;
case STATE_UNMANAGED:
case STATE_ERROR:
case STATE_TEMPORARY_ERROR:
case STATE_BAD_AUTH:
FetchTokenInternal();
break;
}
}
} // namespace policy
|