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 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
64 import os | 64 import os |
65 import random | 65 import random |
66 import re | 66 import re |
67 import sys | 67 import sys |
68 import time | 68 import time |
69 import tlslite | 69 import tlslite |
70 import tlslite.api | 70 import tlslite.api |
71 import tlslite.utils | 71 import tlslite.utils |
72 import tlslite.utils.cryptomath | 72 import tlslite.utils.cryptomath |
73 import urlparse | 73 import urlparse |
74 | 74 import json |
Mattias Nissler (ping if slow)
2015/06/15 07:47:18
alphabetize
| |
75 # The name and availability of the json module varies in python versions. | |
76 try: | |
77 import simplejson as json | |
78 except ImportError: | |
79 try: | |
80 import json | |
81 except ImportError: | |
82 json = None | |
83 | |
84 import asn1der | 75 import asn1der |
85 import testserver_base | 76 import testserver_base |
86 | 77 |
87 import device_management_backend_pb2 as dm | 78 import device_management_backend_pb2 as dm |
88 import cloud_policy_pb2 as cp | 79 import cloud_policy_pb2 as cp |
89 | 80 |
90 # Policy for extensions is not supported on Android nor iOS. | 81 # Policy for extensions is not supported on Android nor iOS. |
91 try: | 82 try: |
92 import chrome_extension_policy_pb2 as ep | 83 import chrome_extension_policy_pb2 as ep |
93 except ImportError: | 84 except ImportError: |
(...skipping 658 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
752 payload = self.server.ReadPolicyFromDataDir(policy_key, settings) | 743 payload = self.server.ReadPolicyFromDataDir(policy_key, settings) |
753 if payload is None: | 744 if payload is None: |
754 self.GatherDevicePolicySettings(settings, policy.get(policy_key, {})) | 745 self.GatherDevicePolicySettings(settings, policy.get(policy_key, {})) |
755 payload = settings.SerializeToString() | 746 payload = settings.SerializeToString() |
756 elif ep is not None and msg.policy_type == 'google/chrome/extension': | 747 elif ep is not None and msg.policy_type == 'google/chrome/extension': |
757 settings = ep.ExternalPolicyData() | 748 settings = ep.ExternalPolicyData() |
758 payload = self.server.ReadPolicyFromDataDir(policy_key, settings) | 749 payload = self.server.ReadPolicyFromDataDir(policy_key, settings) |
759 if payload is None: | 750 if payload is None: |
760 payload = self.CreatePolicyForExternalPolicyData(policy_key) | 751 payload = self.CreatePolicyForExternalPolicyData(policy_key) |
761 else: | 752 else: |
753 if ep is None: | |
754 logging.error('chrome_extension_policy_pb2 does not exist') | |
755 if dp is None: | |
756 logging.error('chrome_device_policy_pb2 does not exist') | |
Mattias Nissler (ping if slow)
2015/06/15 07:47:18
Why not just drop the ep is None and dp is None ch
| |
762 response.error_code = 400 | 757 response.error_code = 400 |
763 response.error_message = 'Invalid policy type' | 758 response.error_message = 'Invalid policy type' |
764 return | 759 return |
765 else: | 760 else: |
766 response.error_code = 400 | 761 response.error_code = 400 |
767 response.error_message = 'Request not allowed for the token used' | 762 response.error_message = 'Request not allowed for the token used' |
768 return | 763 return |
769 | 764 |
770 # Sign with 'current_key_index', defaulting to key 0. | 765 # Sign with 'current_key_index', defaulting to key 0. |
771 signing_key = None | 766 signing_key = None |
(...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
979 self._registered_tokens = json.loads(file_contents, strict=False) | 974 self._registered_tokens = json.loads(file_contents, strict=False) |
980 except IOError: | 975 except IOError: |
981 pass | 976 pass |
982 | 977 |
983 def GetPolicies(self): | 978 def GetPolicies(self): |
984 """Returns the policies to be used, reloaded form the backend file every | 979 """Returns the policies to be used, reloaded form the backend file every |
985 time this is called. | 980 time this is called. |
986 """ | 981 """ |
987 policy = {} | 982 policy = {} |
988 if json is None: | 983 if json is None: |
989 print 'No JSON module, cannot parse policy information' | 984 logging.error('No JSON module, cannot parse policy information') |
990 else : | 985 else : |
991 try: | 986 try: |
992 policy = json.loads(open(self.policy_path).read(), strict=False) | 987 policy = json.loads(open(self.policy_path).read(), strict=False) |
993 except IOError: | 988 except IOError: |
994 print 'Failed to load policy from %s' % self.policy_path | 989 logging.error('Failed to load policies from %s' % self.policy_path) |
995 return policy | 990 return policy |
996 | 991 |
997 def RegisterDevice(self, device_id, machine_id, type): | 992 def RegisterDevice(self, device_id, machine_id, type): |
998 """Registers a device or user and generates a DM token for it. | 993 """Registers a device or user and generates a DM token for it. |
999 | 994 |
1000 Args: | 995 Args: |
1001 device_id: The device identifier provided by the client. | 996 device_id: The device identifier provided by the client. |
1002 | 997 |
1003 Returns: | 998 Returns: |
1004 The newly generated device token for the device. | 999 The newly generated device token for the device. |
(...skipping 279 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
1284 if (self.options.log_to_console): | 1279 if (self.options.log_to_console): |
1285 logger.addHandler(logging.StreamHandler()) | 1280 logger.addHandler(logging.StreamHandler()) |
1286 if (self.options.log_file): | 1281 if (self.options.log_file): |
1287 logger.addHandler(logging.FileHandler(self.options.log_file)) | 1282 logger.addHandler(logging.FileHandler(self.options.log_file)) |
1288 | 1283 |
1289 testserver_base.TestServerRunner.run_server(self) | 1284 testserver_base.TestServerRunner.run_server(self) |
1290 | 1285 |
1291 | 1286 |
1292 if __name__ == '__main__': | 1287 if __name__ == '__main__': |
1293 sys.exit(PolicyServerRunner().main()) | 1288 sys.exit(PolicyServerRunner().main()) |
OLD | NEW |