Chris Masone | d976e0e | 2013-05-06 13:10:07 -0700 | [diff] [blame^] | 1 | # Copyright (c) 2012 The Chromium OS Authors. All rights reserved. |
| 2 | # Use of this source code is governed by a BSD-style license that can be |
| 3 | # found in the LICENSE file. |
| 4 | |
| 5 | import dbus, sys |
| 6 | |
| 7 | import common |
| 8 | from autotest_lib.client.common_lib import error |
| 9 | from autotest_lib.client.cros import login, ownership |
| 10 | |
| 11 | |
| 12 | """Utility class for tests that generate, push and fetch policies. |
| 13 | |
| 14 | As the python bindings for the protobufs used in policies are built as a part |
| 15 | of tests that use them, callers must pass in their location at call time.""" |
| 16 | |
| 17 | |
| 18 | def compare_policy_response(proto_binding_location, policy_response, |
| 19 | owner=None, guests=None, new_users=None, |
| 20 | roaming=None, whitelist=None, proxies=None): |
| 21 | """Check the contents of |policy_response| against given args. |
| 22 | |
| 23 | Deserializes |policy_response| into a PolicyFetchResponse protobuf, |
| 24 | with an embedded (serialized) PolicyData protobuf that embeds a |
| 25 | (serialized) ChromeDeviceSettingsProto, and checks to see if this |
| 26 | protobuf turducken contains the information passed in. |
| 27 | |
| 28 | @param proto_binding_location: the location of generated python bindings |
| 29 | for policy protobufs. |
| 30 | @param policy_response: string serialization of a PolicyData protobuf. |
| 31 | @param owner: string representing the owner's name/account. |
| 32 | @param guests: boolean indicating whether guests should be allowed. |
| 33 | @param new_users: boolean indicating if user pods are on login screen. |
| 34 | @param roaming: boolean indicating whether data roaming is enabled. |
| 35 | @param whitelist: list of accounts that are allowed to log in. |
| 36 | @param proxies: dictionary - { 'proxy_mode': <string> } |
| 37 | |
| 38 | @return True if |policy_response| has all the provided data, else False. |
| 39 | """ |
| 40 | # Pull in protobuf bindings. |
| 41 | sys.path.append(proto_binding_location) |
| 42 | from device_management_backend_pb2 import PolicyFetchResponse |
| 43 | from device_management_backend_pb2 import PolicyData |
| 44 | from chrome_device_policy_pb2 import ChromeDeviceSettingsProto |
| 45 | from chrome_device_policy_pb2 import AllowNewUsersProto |
| 46 | from chrome_device_policy_pb2 import GuestModeEnabledProto |
| 47 | from chrome_device_policy_pb2 import ShowUserNamesOnSigninProto |
| 48 | from chrome_device_policy_pb2 import DataRoamingEnabledProto |
| 49 | from chrome_device_policy_pb2 import DeviceProxySettingsProto |
| 50 | |
| 51 | response_proto = PolicyFetchResponse() |
| 52 | response_proto.ParseFromString(policy_response) |
| 53 | ownership.assert_has_policy_data(response_proto) |
| 54 | |
| 55 | data_proto = PolicyData() |
| 56 | data_proto.ParseFromString(response_proto.policy_data) |
| 57 | ownership.assert_has_device_settings(data_proto) |
| 58 | if owner: ownership.assert_username(data_proto, owner) |
| 59 | |
| 60 | settings = ChromeDeviceSettingsProto() |
| 61 | settings.ParseFromString(data_proto.policy_value) |
| 62 | if guests: ownership.assert_guest_setting(settings, guests) |
| 63 | if new_users: ownership.assert_show_users(settings, new_users) |
| 64 | if roaming: ownership.assert_roaming(settings, roaming) |
| 65 | if whitelist: |
| 66 | ownership.assert_new_users(settings, False) |
| 67 | ownership.assert_users_on_whitelist(settings, whitelist) |
| 68 | if proxies: ownership.assert_proxy_settings(settings, proxies) |
| 69 | |
| 70 | |
| 71 | def build_policy_data(proto_binding_location, owner=None, guests=None, |
| 72 | new_users=None, roaming=None, whitelist=None, |
| 73 | proxies=None): |
| 74 | """Generate and serialize a populated device policy protobuffer. |
| 75 | |
| 76 | Creates a PolicyData protobuf, with an embedded |
| 77 | ChromeDeviceSettingsProto, containing the information passed in. |
| 78 | |
| 79 | @param proto_binding_location: the location of generated python bindings |
| 80 | for policy protobufs. |
| 81 | @param owner: string representing the owner's name/account. |
| 82 | @param guests: boolean indicating whether guests should be allowed. |
| 83 | @param new_users: boolean indicating if user pods are on login screen. |
| 84 | @param roaming: boolean indicating whether data roaming is enabled. |
| 85 | @param whitelist: list of accounts that are allowed to log in. |
| 86 | @param proxies: dictionary - { 'proxy_mode': <string> } |
| 87 | |
| 88 | @return serialization of the PolicyData proto that we build. |
| 89 | """ |
| 90 | # Pull in protobuf bindings. |
| 91 | sys.path.append(proto_binding_location) |
| 92 | from device_management_backend_pb2 import PolicyData |
| 93 | from chrome_device_policy_pb2 import ChromeDeviceSettingsProto |
| 94 | from chrome_device_policy_pb2 import AllowNewUsersProto |
| 95 | from chrome_device_policy_pb2 import GuestModeEnabledProto |
| 96 | from chrome_device_policy_pb2 import ShowUserNamesOnSigninProto |
| 97 | from chrome_device_policy_pb2 import DataRoamingEnabledProto |
| 98 | from chrome_device_policy_pb2 import DeviceProxySettingsProto |
| 99 | |
| 100 | data_proto = PolicyData() |
| 101 | data_proto.policy_type = ownership.POLICY_TYPE |
| 102 | if owner: data_proto.username = owner |
| 103 | |
| 104 | settings = ChromeDeviceSettingsProto() |
| 105 | if guests: |
| 106 | settings.guest_mode_enabled.guest_mode_enabled = guests |
| 107 | if new_users: |
| 108 | settings.show_user_names.show_user_names = new_users |
| 109 | if roaming: |
| 110 | settings.data_roaming_enabled.data_roaming_enabled = roaming |
| 111 | if whitelist: |
| 112 | settings.allow_new_users.allow_new_users = False |
| 113 | for user in whitelist: |
| 114 | settings.user_whitelist.user_whitelist.append(user) |
| 115 | if proxies: |
| 116 | settings.device_proxy_settings.proxy_mode = proxies['proxy_mode'] |
| 117 | |
| 118 | data_proto.policy_value = settings.SerializeToString() |
| 119 | return data_proto.SerializeToString() |
| 120 | |
| 121 | |
| 122 | def generate_policy(proto_binding_location, key, pubkey, policy, old_key=None): |
| 123 | """Generate and serialize a populated, signed device policy protobuffer. |
| 124 | |
| 125 | Creates a protobuf containing the device policy |policy|, signed with |
| 126 | |key|. Also includes the public key |pubkey|, signed with |old_key| |
| 127 | if provided. If not, |pubkey| is signed with |key|. The protobuf |
| 128 | is serialized to a string and returned. |
| 129 | |
| 130 | @param proto_binding_location: the location of generated python bindings |
| 131 | for policy protobufs. |
| 132 | @param key: new policy signing key. |
| 133 | @param pubkey: new public key to be signed and embedded in generated |
| 134 | PolicyFetchResponse. |
| 135 | @param policy: policy data to be embedded in generated PolicyFetchResponse. |
| 136 | @param old_key: if provided, this implies the generated PolicyFetchRespone |
| 137 | is intended to represent a key rotation. pubkey will be |
| 138 | signed with this key before embedding. |
| 139 | |
| 140 | @return serialization of the PolicyFetchResponse proto that we build. |
| 141 | """ |
| 142 | # Pull in protobuf bindings. |
| 143 | sys.path.append(proto_binding_location) |
| 144 | from device_management_backend_pb2 import PolicyFetchResponse |
| 145 | |
| 146 | if old_key == None: |
| 147 | old_key = key |
| 148 | policy_proto = PolicyFetchResponse() |
| 149 | policy_proto.policy_data = policy |
| 150 | policy_proto.policy_data_signature = ownership.sign(key, policy) |
| 151 | policy_proto.new_public_key = pubkey |
| 152 | policy_proto.new_public_key_signature = ownership.sign(old_key, pubkey) |
| 153 | return policy_proto.SerializeToString() |
| 154 | |
| 155 | |
| 156 | def push_policy_and_verify(policy_string, sm): |
| 157 | """Push a device policy to the session manager over DBus. |
| 158 | |
| 159 | The serialized device policy |policy_string| is sent to the session |
| 160 | manager with the StorePolicy DBus call. Success of the store is |
| 161 | validated by fetching the policy again and comparing. |
| 162 | |
| 163 | @param policy_string: serialized policy to push to the session manager. |
| 164 | @param sm: a connected SessionManagerInterface. |
| 165 | |
| 166 | @raises error.TestFail if policy push failed. |
| 167 | """ |
| 168 | sm.StorePolicy(dbus.ByteArray(policy_string), byte_arrays=True) |
| 169 | login.wait_for_ownership() |
| 170 | |
| 171 | retrieved_policy = sm.RetrievePolicy(byte_arrays=True) |
| 172 | if retrieved_policy != policy_string: |
| 173 | raise error.TestFail('Policy should not be %s' % retrieved_policy) |
| 174 | |
| 175 | |
| 176 | def get_policy(sm): |
| 177 | """Get a device policy from the session manager over DBus. |
| 178 | |
| 179 | Provided mainly for symmetry with push_policy_and_verify(). |
| 180 | |
| 181 | @param sm: a connected SessionManagerInterface. |
| 182 | |
| 183 | @return Serialized PolicyFetchResponse. |
| 184 | """ |
| 185 | return sm.RetrievePolicy(byte_arrays=True) |