def _from_db_object(resource, context, db_resource, only_fields=None): if db_resource is None: return None for field in resource.fields: if (only_fields is not None and field not in only_fields and field != 'id'): continue if field == 'data': resource['data'] = [ resource_data.ResourceData._from_db_object( resource_data.ResourceData(context), resd) for resd in db_resource.data ] elif field != 'attr_data': resource[field] = db_resource[field] if db_resource['rsrc_prop_data_id'] is not None: if hasattr(db_resource, '__dict__'): rpd_obj = db_resource.__dict__.get('rsrc_prop_data') else: rpd_obj = None if rpd_obj is not None: # Object is already eager loaded rpd_obj = (rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(), context, rpd_obj)) resource._properties_data = rpd_obj.data else: resource._properties_data = {} if db_resource['properties_data']: LOG.error( 'Unexpected condition where resource.rsrc_prop_data ' 'and resource.properties_data are both not null. ' 'rsrc_prop_data.id: %(rsrc_prop_data_id)s, ' 'resource id: %(res_id)s', { 'rsrc_prop_data_id': resource['rsrc_prop_data'].id, 'res_id': resource['id'] }) elif db_resource['properties_data']: # legacy field if db_resource['properties_data_encrypted']: decrypted_data = crypt.decrypted_dict( db_resource['properties_data']) resource._properties_data = decrypted_data else: resource._properties_data = db_resource['properties_data'] else: resource._properties_data = None if db_resource['attr_data'] is not None: resource._attr_data = rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(context), context, db_resource['attr_data']).data else: resource._attr_data = None resource._context = context resource.obj_reset_changes() return resource
def _from_db_object(resource, context, db_resource, only_fields=None): if db_resource is None: return None for field in resource.fields: if (only_fields is not None and field not in only_fields and field != 'id'): continue if field == 'data': resource['data'] = [resource_data.ResourceData._from_db_object( resource_data.ResourceData(context), resd ) for resd in db_resource.data] elif field != 'attr_data': resource[field] = db_resource[field] if db_resource['rsrc_prop_data_id'] is not None: if hasattr(db_resource, '__dict__'): rpd_obj = db_resource.__dict__.get('rsrc_prop_data') else: rpd_obj = None if rpd_obj is not None: # Object is already eager loaded rpd_obj = ( rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(), context, rpd_obj)) resource._properties_data = rpd_obj.data else: resource._properties_data = {} if db_resource['properties_data']: LOG.error( 'Unexpected condition where resource.rsrc_prop_data ' 'and resource.properties_data are both not null. ' 'rsrc_prop_data.id: %(rsrc_prop_data_id)s, ' 'resource id: %(res_id)s', {'rsrc_prop_data_id': resource['rsrc_prop_data'].id, 'res_id': resource['id']}) elif db_resource['properties_data']: # legacy field if db_resource['properties_data_encrypted']: decrypted_data = crypt.decrypted_dict( db_resource['properties_data']) resource._properties_data = decrypted_data else: resource._properties_data = db_resource['properties_data'] else: resource._properties_data = None if db_resource['attr_data'] is not None: resource._attr_data = rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(context), context, db_resource['attr_data']).data else: resource._attr_data = None resource._context = context resource.obj_reset_changes() return resource
def _from_db_object(rpd, context, db_rpd, data_unencrypted=None): # The data_unencrypted field allows us to avoid an extra # decrypt operation, e.g. when called from create(). for field in rpd.fields: rpd[field] = db_rpd[field] if data_unencrypted: # save a little (decryption) processing rpd['data'] = data_unencrypted elif db_rpd['encrypted'] and rpd['data'] is not None: rpd['data'] = crypt.decrypted_dict(rpd['data']) rpd.obj_reset_changes() return rpd
def _from_db_object(resource, context, db_resource): if db_resource is None: return None for field in resource.fields: if field == 'data': resource['data'] = [ resource_data.ResourceData._from_db_object( resource_data.ResourceData(context), resd) for resd in db_resource.data ] else: resource[field] = db_resource[field] if db_resource['rsrc_prop_data'] is not None: resource['rsrc_prop_data'] = \ rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(context), context, db_resource['rsrc_prop_data']) resource._properties_data = resource['rsrc_prop_data'].data if db_resource['properties_data']: LOG.error( 'Unexpected condition where resource.rsrc_prop_data ' 'and resource.properties_data are both not null. ' 'rsrc_prop_data.id: %(rsrc_prop_data_id)s, ' 'resource id: %(res_id)s', { 'rsrc_prop_data_id': resource['rsrc_prop_data'].id, 'res_id': resource['id'] }) elif db_resource['properties_data']: # legacy field if db_resource['properties_data_encrypted']: decrypted_data = crypt.decrypted_dict( db_resource['properties_data']) resource._properties_data = decrypted_data else: resource._properties_data = db_resource['properties_data'] else: resource._properties_data = {} if db_resource['attr_data'] is not None: resource['attr_data'] = \ rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(context), context, db_resource['attr_data']) resource._context = context resource.obj_reset_changes() return resource
def _from_db_object(rpd, context, db_rpd, data_unencrypted=None): # The data_unencrypted field allows us to avoid an extra # decrypt operation, e.g. when called from create(). for field in rpd.fields: rpd[field] = db_rpd[field] if data_unencrypted: # save a little (decryption) processing rpd['data'] = data_unencrypted elif db_rpd['encrypted'] and rpd['data'] is not None: rpd['data'] = crypt.decrypted_dict(rpd['data']) # TODO(cwolfe) setting the context here should go away, that # should have been done with the initialisation of the rpd # object. For now, maintaining consistency with other # _from_db_object methods. rpd._context = context rpd.obj_reset_changes() return rpd
def _test_encrypt_decrypt_dict(self, encryption_key=None): data = {'p1': u'happy', '2': [u'a', u'little', u'blue'], 'p3': {u'really': u'exited', u'ok int': 9}, '4': u'', 'p5': True, '6': 7} encrypted_data = crypt.encrypted_dict(data, encryption_key) for k in encrypted_data: self.assertEqual('cryptography_decrypt_v1', encrypted_data[k][0]) self.assertEqual(2, len(encrypted_data[k])) # the keys remain the same self.assertEqual(set(data), set(encrypted_data)) decrypted_data = crypt.decrypted_dict(encrypted_data, encryption_key) self.assertEqual(data, decrypted_data)
def _from_db_object(resource, context, db_resource): if db_resource is None: return None for field in resource.fields: if field == 'data': resource['data'] = [resource_data.ResourceData._from_db_object( resource_data.ResourceData(context), resd ) for resd in db_resource.data] else: resource[field] = db_resource[field] if resource.properties_data_encrypted and resource.properties_data: decrypted_data = crypt.decrypted_dict(resource.properties_data) resource.properties_data = decrypted_data resource._context = context resource.obj_reset_changes() return resource
def _from_db_object(resource, context, db_resource): if db_resource is None: return None for field in resource.fields: if field == 'data': resource['data'] = [resource_data.ResourceData._from_db_object( resource_data.ResourceData(context), resd ) for resd in db_resource.data] else: resource[field] = db_resource[field] if db_resource['rsrc_prop_data'] is not None: resource['rsrc_prop_data'] = \ rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(context), context, db_resource['rsrc_prop_data']) resource._properties_data = resource['rsrc_prop_data'].data if db_resource['properties_data']: LOG.error( 'Unexpected condition where resource.rsrc_prop_data ' 'and resource.properties_data are both not null. ' 'rsrc_prop_data.id: %(rsrc_prop_data_id)s, ' 'resource id: %(res_id)s', {'rsrc_prop_data_id': resource['rsrc_prop_data'].id, 'res_id': resource['id']}) elif db_resource['properties_data']: # legacy field if db_resource['properties_data_encrypted']: decrypted_data = crypt.decrypted_dict( db_resource['properties_data']) resource._properties_data = decrypted_data else: resource._properties_data = db_resource['properties_data'] else: resource._properties_data = {} if db_resource['attr_data'] is not None: resource['attr_data'] = \ rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(context), context, db_resource['attr_data']) resource._context = context resource.obj_reset_changes() return resource
def _from_db_object(resource, context, db_resource): if db_resource is None: return None for field in resource.fields: if field == 'data': resource['data'] = [ resource_data.ResourceData._from_db_object( resource_data.ResourceData(context), resd) for resd in db_resource.data ] else: resource[field] = db_resource[field] if resource.properties_data_encrypted and resource.properties_data: decrypted_data = crypt.decrypted_dict(resource.properties_data) resource.properties_data = decrypted_data resource._context = context resource.obj_reset_changes() return resource
def _from_db_object(resource, context, db_resource): if db_resource is None: return None for field in resource.fields: if field == "data": resource["data"] = [ resource_data.ResourceData._from_db_object(resource_data.ResourceData(context), resd) for resd in db_resource.data ] else: resource[field] = db_resource[field] if db_resource["rsrc_prop_data"] is not None: resource["rsrc_prop_data"] = rpd.ResourcePropertiesData._from_db_object( rpd.ResourcePropertiesData(context), context, db_resource["rsrc_prop_data"] ) resource._properties_data = resource["rsrc_prop_data"].data if db_resource["properties_data"]: LOG.error( _LE( "Unexpected condition where resource.rsrc_prop_data " "and resource.properties_data are both not null. " "rsrc_prop_data.id: %(rsrc_prop_data_id)s ," "resource id: %(res_id)s" ) % {"rsrc_prop_data_id": resource["rsrc_prop_data"].id, "res_id": resource["id"]} ) elif db_resource["properties_data"]: # legacy field if db_resource["properties_data_encrypted"]: decrypted_data = crypt.decrypted_dict(db_resource["properties_data"]) resource._properties_data = decrypted_data else: resource._properties_data = db_resource["properties_data"] else: resource._properties_data = {} resource._context = context resource.obj_reset_changes() return resource