|
| 1 | +# Copyright (c) 2016-2017 Adobe Inc. All rights reserved. |
| 2 | +# |
| 3 | +# Permission is hereby granted, free of charge, to any person obtaining a copy |
| 4 | +# of this software and associated documentation files (the "Software"), to deal |
| 5 | +# in the Software without restriction, including without limitation the rights |
| 6 | +# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell |
| 7 | +# copies of the Software, and to permit persons to whom the Software is |
| 8 | +# furnished to do so, subject to the following conditions: |
| 9 | +# |
| 10 | +# The above copyright notice and this permission notice shall be included in all |
| 11 | +# copies or substantial portions of the Software. |
| 12 | +# |
| 13 | +# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 14 | +# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 15 | +# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 16 | +# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 17 | +# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 18 | +# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE |
| 19 | +# SOFTWARE. |
| 20 | + |
| 21 | +import six |
| 22 | +import umapi_client |
| 23 | +import user_sync.config |
| 24 | +import user_sync.connector.helper |
| 25 | +import user_sync.helper |
| 26 | +import user_sync.identity_type |
| 27 | +from user_sync.error import AssertionException |
| 28 | +from user_sync.version import __version__ as app_version |
| 29 | +from user_sync.connector.umapi_util import make_auth_dict |
| 30 | +from user_sync.helper import normalize_string |
| 31 | +from user_sync.identity_type import parse_identity_type |
| 32 | + |
| 33 | + |
| 34 | +def connector_metadata(): |
| 35 | + metadata = { |
| 36 | + 'name': AdobeConsoleConnector.name |
| 37 | + } |
| 38 | + return metadata |
| 39 | + |
| 40 | + |
| 41 | +def connector_initialize(options): |
| 42 | + """ |
| 43 | + :type options: dict |
| 44 | + """ |
| 45 | + state = AdobeConsoleConnector(options) |
| 46 | + return state |
| 47 | + |
| 48 | + |
| 49 | +def connector_load_users_and_groups(state, groups=None, extended_attributes=None, all_users=True): |
| 50 | + """ |
| 51 | + :type state: OktaDirectoryConnector |
| 52 | + :type groups: list(str) |
| 53 | + :type extended_attributes: list(str) |
| 54 | + :type all_users: bool |
| 55 | + :rtype (bool, iterable(dict)) |
| 56 | + """ |
| 57 | + |
| 58 | + return state.load_users_and_groups(groups or [], extended_attributes or [], all_users) |
| 59 | + |
| 60 | + |
| 61 | +class AdobeConsoleConnector(object): |
| 62 | + name = 'adobe_console' |
| 63 | + |
| 64 | + def __init__(self, caller_options): |
| 65 | + |
| 66 | + caller_config = user_sync.config.DictConfig('<%s configuration>' % self.name, caller_options) |
| 67 | + builder = user_sync.config.OptionsBuilder(caller_config) |
| 68 | + # Let just ignore this |
| 69 | + builder.set_string_value('user_identity_type', None) |
| 70 | + builder.set_string_value('identity_type_filter', 'all') |
| 71 | + options = builder.get_options() |
| 72 | + |
| 73 | + if not options['identity_type_filter'] == 'all': |
| 74 | + try: |
| 75 | + options['identity_type_filter'] = parse_identity_type(options['identity_type_filter']) |
| 76 | + except Exception as e: |
| 77 | + raise AssertionException("Error parsing identity_type_filter option: %s" % e) |
| 78 | + self.filter_by_identity_type = options['identity_type_filter'] |
| 79 | + |
| 80 | + server_config = caller_config.get_dict_config('server', True) |
| 81 | + server_builder = user_sync.config.OptionsBuilder(server_config) |
| 82 | + server_builder.set_string_value('host', 'usermanagement.adobe.io') |
| 83 | + server_builder.set_string_value('endpoint', '/v2/usermanagement') |
| 84 | + server_builder.set_string_value('ims_host', 'ims-na1.adobelogin.com') |
| 85 | + server_builder.set_string_value('ims_endpoint_jwt', '/ims/exchange/jwt') |
| 86 | + server_builder.set_int_value('timeout', 120) |
| 87 | + server_builder.set_int_value('retries', 3) |
| 88 | + options['server'] = server_options = server_builder.get_options() |
| 89 | + |
| 90 | + enterprise_config = caller_config.get_dict_config('integration') |
| 91 | + integration_builder = user_sync.config.OptionsBuilder(enterprise_config) |
| 92 | + integration_builder.require_string_value('org_id') |
| 93 | + integration_builder.require_string_value('tech_acct') |
| 94 | + options['integration'] = integration_options = integration_builder.get_options() |
| 95 | + |
| 96 | + self.logger = logger = user_sync.connector.helper.create_logger(options) |
| 97 | + logger.debug('%s initialized with options: %s', self.name, options) |
| 98 | + |
| 99 | + self.options = options |
| 100 | + |
| 101 | + ims_host = server_options['ims_host'] |
| 102 | + self.org_id = org_id = integration_options['org_id'] |
| 103 | + auth_dict = make_auth_dict(self.name, enterprise_config, org_id, integration_options['tech_acct'], logger) |
| 104 | + |
| 105 | + # this check must come after we fetch all the settings |
| 106 | + caller_config.report_unused_values(logger) |
| 107 | + # open the connection |
| 108 | + um_endpoint = "https://" + server_options['host'] + server_options['endpoint'] |
| 109 | + logger.debug('%s: creating connection for org %s at endpoint %s', self.name, org_id, um_endpoint) |
| 110 | + |
| 111 | + try: |
| 112 | + self.connection = umapi_client.Connection( |
| 113 | + org_id=org_id, |
| 114 | + auth_dict=auth_dict, |
| 115 | + ims_host=ims_host, |
| 116 | + ims_endpoint_jwt=server_options['ims_endpoint_jwt'], |
| 117 | + user_management_endpoint=um_endpoint, |
| 118 | + test_mode=False, |
| 119 | + user_agent="user-sync/" + app_version, |
| 120 | + logger=self.logger, |
| 121 | + timeout_seconds=float(server_options['timeout']), |
| 122 | + retry_max_attempts=server_options['retries'] + 1, |
| 123 | + ) |
| 124 | + except Exception as e: |
| 125 | + raise AssertionException("Connection to org %s at endpoint %s failed: %s" % (org_id, um_endpoint, e)) |
| 126 | + logger.debug('%s: connection established', self.name) |
| 127 | + self.umapi_users = [] |
| 128 | + self.user_by_usr_key = {} |
| 129 | + |
| 130 | + def load_users_and_groups(self, groups, extended_attributes, all_users): |
| 131 | + """ |
| 132 | + :type groups: list(str) |
| 133 | + :type extended_attributes: list(str) |
| 134 | + :type all_users: bool |
| 135 | + :rtype (bool, iterable(dict)) |
| 136 | + """ |
| 137 | + |
| 138 | + if extended_attributes: |
| 139 | + self.logger.warning("Extended Attributes is not supported") |
| 140 | + |
| 141 | + # Loading all the groups because UMAPI doesn't support group query. DOH! |
| 142 | + self.logger.info('Loading groups...') |
| 143 | + umapi_groups = list(self.iter_umapi_groups()) |
| 144 | + self.logger.info('Loading users...') |
| 145 | + |
| 146 | + # Loading all umapi users based on ID Type first before doing group filtering |
| 147 | + filter_by_identity_type = self.filter_by_identity_type |
| 148 | + self.load_umapi_users(identity_type=filter_by_identity_type) |
| 149 | + |
| 150 | + grouped_user_records = {} |
| 151 | + for group in groups: |
| 152 | + group_users_count = 0 |
| 153 | + if group in umapi_groups: |
| 154 | + grouped_users = self.iter_group_members(group) |
| 155 | + for user_key in grouped_users: |
| 156 | + if user_key in self.user_by_usr_key: |
| 157 | + user = self.user_by_usr_key[user_key] |
| 158 | + user['groups'].append(group) |
| 159 | + self.user_by_usr_key[user_key] = grouped_user_records[user_key] = user |
| 160 | + group_users_count = group_users_count + 1 |
| 161 | + self.logger.debug('Count of users in group "%s": %d', group, group_users_count) |
| 162 | + else: |
| 163 | + self.logger.warning("No group found for: %s", group) |
| 164 | + if all_users: |
| 165 | + self.logger.debug('Count of users in any groups: %d', len(grouped_user_records)) |
| 166 | + self.logger.debug('Count of users not in any groups: %d', |
| 167 | + len(self.user_by_usr_key) - len(grouped_user_records)) |
| 168 | + return six.itervalues(self.user_by_usr_key) |
| 169 | + else: |
| 170 | + return six.itervalues(grouped_user_records) |
| 171 | + |
| 172 | + def convert_user(self, record): |
| 173 | + |
| 174 | + source_attributes = {} |
| 175 | + user = user_sync.connector.helper.create_blank_user() |
| 176 | + user['uid'] = record['username'] |
| 177 | + source_attributes['email'] = user['email'] = email = record['email'] |
| 178 | + user_identity_type = record['type'] |
| 179 | + try: |
| 180 | + source_attributes['type'] = user['identity_type'] = user_sync.identity_type.parse_identity_type( |
| 181 | + user_identity_type) |
| 182 | + except AssertionException as e: |
| 183 | + self.logger.warning('Skipping user %s: %s', email, e) |
| 184 | + return None |
| 185 | + |
| 186 | + source_attributes['username'] = user['username'] = record['username'] |
| 187 | + source_attributes['domain'] = user['domain'] = record['domain'] |
| 188 | + |
| 189 | + if 'firstname' in record: |
| 190 | + firstname = record['firstname'] |
| 191 | + else: |
| 192 | + firstname = None |
| 193 | + source_attributes['firstname'] = user['firstname'] = firstname |
| 194 | + |
| 195 | + if 'lastname' in record: |
| 196 | + lastname = record['lastname'] |
| 197 | + else: |
| 198 | + lastname = None |
| 199 | + source_attributes['lastname'] = user['lastname'] = lastname |
| 200 | + |
| 201 | + source_attributes['country'] = user['country'] = record['country'] |
| 202 | + |
| 203 | + user['source_attributes'] = source_attributes.copy() |
| 204 | + return user |
| 205 | + |
| 206 | + def iter_umapi_groups(self): |
| 207 | + try: |
| 208 | + groups = umapi_client.GroupsQuery(self.connection) |
| 209 | + for group in groups: |
| 210 | + yield group['groupName'] |
| 211 | + except umapi_client.UnavailableError as e: |
| 212 | + raise AssertionException("Error to query groups from Adobe Console: %s" % e) |
| 213 | + |
| 214 | + def iter_group_members(self, group): |
| 215 | + umapi_users = self.umapi_users |
| 216 | + members = filter(lambda u: ('groups' in u and group in u['groups']), umapi_users) |
| 217 | + for member in members: |
| 218 | + user_key = self.generate_user_key(member['type'], member['username'], member['domain']) |
| 219 | + yield (user_key) |
| 220 | + |
| 221 | + def load_umapi_users(self, identity_type): |
| 222 | + try: |
| 223 | + u_query = umapi_client.UsersQuery(self.connection) |
| 224 | + umapi_users = u_query.all_results() |
| 225 | + |
| 226 | + if not identity_type == 'all': |
| 227 | + umapi_users = list(filter(lambda usr: usr['type'] == identity_type, umapi_users)) |
| 228 | + |
| 229 | + self.umapi_users = umapi_users |
| 230 | + for user in umapi_users: |
| 231 | + # Generate unique user key because Username/Email is a bad unique identifier |
| 232 | + user_key = self.generate_user_key(user['type'], user['username'], user['domain']) |
| 233 | + self.user_by_usr_key[user_key] = self.convert_user(user) |
| 234 | + except umapi_client.UnavailableError as e: |
| 235 | + raise AssertionException("Error contacting UMAPI server: %s" % e) |
| 236 | + |
| 237 | + def generate_user_key(self, identity_type, username, domain): |
| 238 | + return '%s,%s,%s' % (normalize_string(identity_type), normalize_string(username), normalize_string(domain)) |
0 commit comments