OLD | NEW |
---|---|
1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 # Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 # Use of this source code is governed by a BSD-style license that can be | 2 # Use of this source code is governed by a BSD-style license that can be |
3 # found in the LICENSE file. | 3 # found in the LICENSE file. |
4 | 4 |
5 """A bare-bones test server for testing cloud policy support. | 5 """A bare-bones test server for testing cloud policy support. |
6 | 6 |
7 This implements a simple cloud policy test server that can be used to test | 7 This implements a simple cloud policy test server that can be used to test |
8 chrome's device management service client. The policy information is read from | 8 chrome's device management service client. The policy information is read from |
9 the file named device_management in the server's data directory. It contains | 9 the file named device_management in the server's data directory. It contains |
10 enforced and recommended policies for the device and user scope, and a list | 10 enforced and recommended policies for the device and user scope, and a list |
(...skipping 293 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
304 response = self.ProcessApiAuthorization(rmsg.service_api_access_request) | 304 response = self.ProcessApiAuthorization(rmsg.service_api_access_request) |
305 elif request_type == 'unregister': | 305 elif request_type == 'unregister': |
306 response = self.ProcessUnregister(rmsg.unregister_request) | 306 response = self.ProcessUnregister(rmsg.unregister_request) |
307 elif request_type == 'policy': | 307 elif request_type == 'policy': |
308 response = self.ProcessPolicy(rmsg, request_type) | 308 response = self.ProcessPolicy(rmsg, request_type) |
309 elif request_type == 'enterprise_check': | 309 elif request_type == 'enterprise_check': |
310 response = self.ProcessAutoEnrollment(rmsg.auto_enrollment_request) | 310 response = self.ProcessAutoEnrollment(rmsg.auto_enrollment_request) |
311 elif request_type == 'device_state_retrieval': | 311 elif request_type == 'device_state_retrieval': |
312 response = self.ProcessDeviceStateRetrievalRequest( | 312 response = self.ProcessDeviceStateRetrievalRequest( |
313 rmsg.device_state_retrieval_request) | 313 rmsg.device_state_retrieval_request) |
314 elif request_type == 'status_upload': | |
315 response = self.ProcessStatusUploadRequest( | |
316 rmsg.device_status_report_request, rmsg.session_status_report_request) | |
314 else: | 317 else: |
315 return (400, 'Invalid request parameter') | 318 return (400, 'Invalid request parameter') |
316 | 319 |
317 self.DumpMessage('Response', response[1]) | 320 if isinstance(response[1], basestring): |
318 return (response[0], response[1].SerializeToString()) | 321 body = response[1] |
322 elif isinstance(response[1], google.protobuf.message.Message): | |
323 self.DumpMessage('Response', response[1]) | |
324 body = response[1].SerializeToString() | |
325 else: | |
326 body = '' | |
327 return (response[0], body) | |
Mattias Nissler (ping if slow)
2015/01/23 13:46:35
Thanks for rescuing this snippet.
Andrew T Wilson (Slow)
2015/01/23 19:16:19
Yep :)
| |
319 | 328 |
320 def CreatePolicyForExternalPolicyData(self, policy_key): | 329 def CreatePolicyForExternalPolicyData(self, policy_key): |
321 """Returns an ExternalPolicyData protobuf for policy_key. | 330 """Returns an ExternalPolicyData protobuf for policy_key. |
322 | 331 |
323 If there is policy data for policy_key then the download url will be | 332 If there is policy data for policy_key then the download url will be |
324 set so that it points to that data, and the appropriate hash is also set. | 333 set so that it points to that data, and the appropriate hash is also set. |
325 Otherwise, the protobuf will be empty. | 334 Otherwise, the protobuf will be empty. |
326 | 335 |
327 Args: | 336 Args: |
328 policy_key: The policy type and settings entity id, joined by '/'. | 337 policy_key: The policy type and settings entity id, joined by '/'. |
(...skipping 220 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
549 ] | 558 ] |
550 for field in FIELDS: | 559 for field in FIELDS: |
551 if field in state: | 560 if field in state: |
552 setattr(device_state_retrieval_response, field, state[field]) | 561 setattr(device_state_retrieval_response, field, state[field]) |
553 | 562 |
554 response = dm.DeviceManagementResponse() | 563 response = dm.DeviceManagementResponse() |
555 response.device_state_retrieval_response.CopyFrom( | 564 response.device_state_retrieval_response.CopyFrom( |
556 device_state_retrieval_response) | 565 device_state_retrieval_response) |
557 return (200, response) | 566 return (200, response) |
558 | 567 |
568 def ProcessStatusUploadRequest(self, device_status, session_status): | |
569 """Handles a device/session status upload request. | |
570 | |
571 Returns: | |
572 A tuple of HTTP status code and response data to send to the client. | |
573 """ | |
574 # Empty responses indicate a successful upload. | |
575 device_status_report_response = dm.DeviceStatusReportResponse() | |
576 session_status_report_response = dm.SessionStatusReportResponse() | |
577 | |
578 response = dm.DeviceManagementResponse() | |
579 response.device_status_report_response.CopyFrom( | |
580 device_status_report_response) | |
581 response.session_status_report_response.CopyFrom( | |
582 session_status_report_response) | |
583 | |
584 return (200, response) | |
585 | |
559 def SetProtobufMessageField(self, group_message, field, field_value): | 586 def SetProtobufMessageField(self, group_message, field, field_value): |
560 """Sets a field in a protobuf message. | 587 """Sets a field in a protobuf message. |
561 | 588 |
562 Args: | 589 Args: |
563 group_message: The protobuf message. | 590 group_message: The protobuf message. |
564 field: The field of the message to set, it should be a member of | 591 field: The field of the message to set, it should be a member of |
565 group_message.DESCRIPTOR.fields. | 592 group_message.DESCRIPTOR.fields. |
566 field_value: The value to set. | 593 field_value: The value to set. |
567 """ | 594 """ |
568 if field.label == field.LABEL_REPEATED: | 595 if field.label == field.LABEL_REPEATED: |
(...skipping 688 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1257 if (self.options.log_to_console): | 1284 if (self.options.log_to_console): |
1258 logger.addHandler(logging.StreamHandler()) | 1285 logger.addHandler(logging.StreamHandler()) |
1259 if (self.options.log_file): | 1286 if (self.options.log_file): |
1260 logger.addHandler(logging.FileHandler(self.options.log_file)) | 1287 logger.addHandler(logging.FileHandler(self.options.log_file)) |
1261 | 1288 |
1262 testserver_base.TestServerRunner.run_server(self) | 1289 testserver_base.TestServerRunner.run_server(self) |
1263 | 1290 |
1264 | 1291 |
1265 if __name__ == '__main__': | 1292 if __name__ == '__main__': |
1266 sys.exit(PolicyServerRunner().main()) | 1293 sys.exit(PolicyServerRunner().main()) |
OLD | NEW |