Chromium Code Reviews| Index: net/tools/testserver/device_management.py |
| diff --git a/net/tools/testserver/device_management.py b/net/tools/testserver/device_management.py |
| index d71522741a950dcc2cf2eab2ca6373a7bf10fd52..a4b704ca557316f8703756a0e70aa62e3a203016 100644 |
| --- a/net/tools/testserver/device_management.py |
| +++ b/net/tools/testserver/device_management.py |
| @@ -17,11 +17,13 @@ identical to what the Linux implementation reads from /etc. Here is an example: |
| """ |
| +import calendar |
| import cgi |
| import logging |
| import random |
| import re |
| import sys |
| +import time |
| # The name and availability of the json module varies in python versions. |
| try: |
| @@ -33,6 +35,10 @@ except ImportError: |
| json = None |
| import device_management_backend_pb2 as dm |
| +import cloud_policy_pb2 as cp |
| + |
| +LOG_FILENAME = '/dev/stdout' |
| +logging.basicConfig(filename=LOG_FILENAME,level=logging.DEBUG) |
|
Mattias Nissler (ping if slow)
2011/01/25 11:05:57
I guess these two lines are not meant for committi
|
| class RequestHandler(object): |
| """Decodes and handles device management requests from clients. |
| @@ -95,6 +101,8 @@ class RequestHandler(object): |
| return self.ProcessUnregister(rmsg.unregister_request) |
| elif request_type == 'policy': |
| return self.ProcessPolicy(rmsg.policy_request) |
| + elif request_type == 'cloud_policy': |
| + return self.ProcessCloudPolicyRequest(rmsg.cloud_policy_request) |
| else: |
| return (400, 'Invalid request parameter') |
| @@ -214,6 +222,78 @@ class RequestHandler(object): |
| return (200, response.SerializeToString()) |
| + def _SetProtobufMessageField(self, group_message, field, field_value): |
| + '''Sets a field in a protobuf message. |
| + |
| + Args: |
| + group_message: The protobuf message. |
| + field: The field of the message to set, it shuold be a member of |
| + group_message.DESCRIPTOR.fields. |
| + field_value: The value to set. |
| + ''' |
| + if field.type == field.TYPE_BYTES: |
| + assert type(field_value) == list |
| + string_list = cp.StringList() |
| + for list_item in field_value: |
| + string_list.entry.append(list_item) |
| + serialized_string_list = string_list.SerializeToString() |
| + group_message.__setattr__(field.name, serialized_string_list) |
| + else: |
| + # Simple cases: |
| + if field.type == field.TYPE_BOOL: |
| + assert type(field_value) == bool |
| + elif field.type == field.TYPE_STRING: |
| + assert type(field_value) == str |
| + elif field.type == field.TYPE_INT64: |
| + assert type(field_value) == int |
| + else: |
| + raise Exception('Unknown field type %s' % field.type_name) |
| + group_message.__setattr__(field.name, field_value) |
| + |
| + def ProcessCloudPolicyRequest(self, msg): |
| + token, response = self.CheckToken() |
| + if not token: |
| + return response |
| + |
| + settings = cp.CloudPolicySettings() |
| + |
| + if msg.policy_scope == 'chromeos/device': |
| + pass |
| + |
| + for group in settings.DESCRIPTOR.fields: |
| + # Create protobuf message for group. |
| + group_message = eval('cp.' + group.message_type.name + '()') |
| + # Indiactes if at least one field was set in |group_message|. |
| + got_fields = False |
| + # Iterate over fields of the message and feed them from the |
| + # policy config file. |
| + for field in group_message.DESCRIPTOR.fields: |
| + if field.name in self._server.policy: |
| + got_fields = True |
| + field_value = self._server.policy[field.name] |
| + self._SetProtobufMessageField(group_message, field, field_value) |
| + if got_fields: |
| + settings.__getattribute__(group.name).CopyFrom(group_message) |
| + |
| + # Construct response |
| + signed_response = dm.SignedCloudPolicyResponse() |
| + signed_response.settings.CopyFrom(settings) |
| + signed_response.timestamp = calendar.timegm(time.gmtime()) |
| + signed_response.device_token = token; |
| + signed_response.device_name = 'TODO'; |
| + |
| + cloud_response = dm.CloudPolicyResponse() |
| + cloud_response.signed_response = signed_response.SerializeToString() |
| + cloud_response.signature = 'TODO' |
|
Mattias Nissler (ping if slow)
2011/01/25 11:05:57
should have some actual signing here :) Maybe pass
|
| + |
| + response = dm.DeviceManagementResponse() |
| + response.error = dm.DeviceManagementResponse.SUCCESS |
| + response.cloud_policy_response.CopyFrom(cloud_response) |
| + |
| + self.DumpMessage('Response', response) |
| + |
| + return (200, response.SerializeToString()) |
| + |
| def CheckToken(self): |
| """Helper for checking whether the client supplied a valid DM token. |