def _on_deregister(self, ch, method, properties, message):
"""
Event method that is called when a de-registration request is received.
Removes the given plugin from the internal data model.
:param properties: request properties
:param message: request body (contains UUID to identify plugin)
:return: response message
"""
message = json.loads(str(message))
try:
p = model.Plugin.objects.get(uuid=message.get("uuid"))
p.delete()
except DoesNotExist:
LOG.debug("Couldn't find plugin with UUID %r in DB" % pid)
LOG.info("DE-REGISTERED: %r" % message.get("uuid"))
# broadcast a plugin status update to the other plugin
self.send_plugin_status_update()
# return result
response = {
"status": "OK"
}
return json.dumps(response)
python类DoesNotExist()的实例源码
def put(self, plugin_uuid=None):
LOG.debug("PUT plugin lifecycle: %r" % plugin_uuid)
try:
p = model.Plugin.objects.get(uuid=plugin_uuid)
# get target state from request body
ts = json.loads(request.json).get("target_state")
if ts is None:
LOG.error("Malformed request: %r" % request.json)
return {"message": "malformed request"}, 500
if ts == "start":
PM.send_start_notification(p)
elif ts == "pause":
PM.send_pause_notification(p)
elif ts == "stop":
PM.send_stop_notification(p)
else:
return {"message": "Malformed request"}, 500
return {}, 200
except DoesNotExist as e:
LOG.error("Lookup error: %r" % plugin_uuid)
return {}, 404
# reference to plugin manager
def get_process(self, process):
"""
Get current process. If not exists, create it.
:param process: Process data.
:type process: dict.
:return: Process
:rtype: :class:`audit_tools.audit.Process`
"""
from audit_tools.audit.models import Process
p = getattr(self.namespace, "audit_current_process", None)
if p is None:
try:
p = Process.objects.get(pid=process['pid'], machine=process['machine'],
creation_time=process['creation_time'])
except DoesNotExist:
p = Process(**process)
p.save()
self.set_process(p)
return p
def fetch(retry=0):
proxy = 'http://{}'.format(Proxy.get_random()['address'])
headers = {'user-agent': get_user_agent()}
conn = aiohttp.ProxyConnector(proxy=proxy)
url = 'http://httpbin.org/ip'
try:
with aiohttp.ClientSession(connector=conn) as session:
with aiohttp.Timeout(TIMEOUT):
async with session.get(url, headers=headers) as resp:
return await resp.json()
except (ProxyConnectionError, TimeoutError):
try:
p = Proxy.objects.get(address=proxy)
if p:
p.delete()
except DoesNotExist:
pass
retry += 1
if retry > 5:
raise TimeoutError()
await asyncio.sleep(1)
return await fetch(retry=retry)
def save_document(cls, zip_code, address, neighborhood, city, state):
"""
Create or update a document
returns True if created and False if only updated
Update condition is based on zipcode. If zipcode already exists in db
the the document is only updated. Otherwise the document is created
"""
try:
object_ = cls.objects.get(zip_code=zip_code)
object_.update(
address=address,
neighborhood=neighborhood,
city=city,
state=state
)
return False
except DoesNotExist:
cls(
zip_code=zip_code,
address=address,
neighborhood=neighborhood,
city=city,
state=state).save()
return True
def removeAssociation(self, server_url, handle):
"""
This method removes the matching association if it's found,
and returns whether the association was removed or not.
"""
try:
mist_associations = MistAssociation.objects(server_url=server_url,
handle=handle.encode('hex'))
except me.DoesNotExist:
return False
for assoc in mist_associations:
assoc.delete()
return len(mist_associations) > 0
def useNonce(self, server_url, timestamp, salt):
"""Called when using a nonce.
This method should return C{True} if the nonce has not been
used before, and store it for a while to make sure nobody
tries to use the same value again. If the nonce has already
been used or the timestamp is not current, return C{False}.
You may use L{openid.store.nonce.SKEW} for your timestamp window.
"""
if is_nonce_old(timestamp):
return False
try:
mist_nonces = MistNonce.objects(server_url=server_url, salt=salt,
timestamp=timestamp)
except me.DoesNotExist:
mist_nonces = []
if len(mist_nonces) == 0:
print "Timestamp = %s" % timestamp
MistNonce(server_url=server_url, salt=salt, timestamp=timestamp).save()
return True
return False
def cleanupNonces(self):
"""Remove expired nonces from the store.
Discards any nonce from storage that is old enough that its
timestamp would not pass L{useNonce}.
This method is not called in the normal operation of the
library. It provides a way for store admins to keep
their storage from filling up with expired data.
@return: the number of nonces expired.
@returntype: int
"""
try:
mist_nonces = MistNonce.objects()
except me.DoesNotExist:
mist_nonces = []
counter = 0
for nonce in mist_nonces:
if nonce.is_old():
nonce.delete()
counter += 1
return counter
def cleanupAssociations(self):
"""Remove expired associations from the store.
This method is not called in the normal operation of the
library. It provides a way for store admins to keep
their storage from filling up with expired data.
@return: the number of associations expired.
@returntype: int
"""
try:
mist_associations = MistAssociation.objects()
except me.DoesNotExist:
mist_associations = []
counter = 0
for assoc in mist_associations:
if assoc.is_expired():
assoc.delete()
counter += 1
return counter
def list_dns_records(request):
"""
List all DNS zone records for a particular zone.
---
"""
auth_context = auth_context_from_request(request)
cloud_id = request.matchdict['cloud']
zone_id = request.matchdict['zone']
try:
cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id)
except me.DoesNotExist:
raise CloudNotFoundError
try:
zone = Zone.objects.get(owner=auth_context.owner, cloud=cloud,
id=zone_id)
except Zone.DoesNotExist:
raise NotFoundError('Zone does not exist')
return filter_list_records(auth_context, zone)
def delete_dns_zone(request):
"""
Delete a specific DNS zone under a cloud.
---
"""
auth_context = auth_context_from_request(request)
cloud_id = request.matchdict['cloud']
zone_id = request.matchdict['zone']
# Do we need the cloud here, now that the models have been created?
try:
cloud = Cloud.objects.get(owner=auth_context.owner, id=cloud_id)
except me.DoesNotExist:
raise CloudNotFoundError
try:
zone = Zone.objects.get(owner=auth_context.owner, id=zone_id)
except Zone.DoesNotExist:
raise NotFoundError('Zone does not exist')
auth_context.check_perm("zone", "remove", zone_id)
zone.ctl.delete_zone()
# Schedule a UI update
trigger_session_update(auth_context.owner, ['zones'])
return OK
def add(cls, machine, interval=None, ttl=300):
try:
schedule = cls.objects.get(machine_id=machine.id)
except cls.DoesNotExist:
schedule = cls(machine_id=machine.id)
try:
schedule.save()
except me.NotUniqueError:
# Work around race condition where schedule was created since
# last time we checked.
schedule = cls.objects.get(machine_id=machine.id)
schedule.set_default_interval(60 * 60 * 2)
if interval is not None:
schedule.add_interval(interval, ttl)
schedule.run_immediately = True
schedule.cleanup_expired_intervals()
schedule.save()
return schedule
def get_object_with_id(owner, rid, rtype, *args, **kwargs):
query = {}
if rtype in ['machine', 'network', 'image', 'location']:
if 'cloud_id' not in kwargs:
raise RequiredParameterMissingError('No cloud id provided')
else:
query.update({'cloud': kwargs['cloud_id']})
if rtype == 'machine':
query.update({'machine_id': rid})
else:
query.update({'id': rid, 'deleted': None})
if rtype not in ['machine', 'image']:
query.update({'owner': owner})
try:
resource_obj = get_resource_model(rtype).objects.get(**query)
except DoesNotExist:
raise NotFoundError('Resource with this id could not be located')
return resource_obj
def get_singleton(cls):
"""Return (and create if missing) the single Portal document"""
try:
portal = cls.objects.get()
log.debug("Loaded portal info from db.")
except me.DoesNotExist:
log.info("No portal info found in db, will try to initialize.")
try:
portal = cls()
portal.save()
log.info("Initialized portal info.")
except me.NotUniqueError:
log.warning("Probable race condition while initializing "
"portal info, will try to reload.")
portal = cls.objects.get()
log.debug("Loaded portal info from db.")
except me.MultipleObjectsReturned:
log.error("Multiple Portal info found in database.")
portal = cls.objects.first()
return portal
def get_avatar(request):
"""
Returns the requested avatar
---
avatar:
description: 'Avatar Id'
in: path
required: true
type: string
"""
avatar_id = request.matchdict['avatar']
try:
avatar = Avatar.objects.get(id=avatar_id)
except me.DoesNotExist:
raise NotFoundError()
return Response(content_type=str(avatar.content_type), body=str(avatar.body))
def delete_dev_user(request):
if not config.ENABLE_DEV_USERS:
raise NotFoundError()
params = params_from_request(request)
email = params.get('email', '')
if not email:
raise RequiredParameterMissingError('email')
try:
user = User.objects.get(email=email)
user.delete()
log.warning("[DEV ENDPOINT]: Delete user with email: %s", email)
except User.DoesNotExist:
# If user does not exist we are okay
log.warning("[DEV ENDPOINT]: User with email: %s is already absent",
email)
return OK
def add(self, fail_on_error=True, fail_on_invalid_params=True, **kwargs):
"""This is a hack to associate a key with the VM hosting this cloud"""
super(LibvirtMainController, self).add(
fail_on_error=fail_on_error,
fail_on_invalid_params=fail_on_invalid_params,
add=True, **kwargs
)
# FIXME: Don't use self.cloud.host as machine_id, this prevents us from
# changing the cloud's host.
# FIXME: Add type field to differentiate between actual vm's and the
# host.
try:
machine = Machine.objects.get(cloud=self.cloud,
machine_id=self.cloud.host)
except me.DoesNotExist:
machine = Machine.objects(cloud=self.cloud,
machine_id=self.cloud.host).save()
if self.cloud.key:
machine.ctl.associate_key(self.cloud.key,
username=self.cloud.username,
port=self.cloud.port)
def set_default_key(request):
"""
Set default key
Sets a new default key
EDIT permission required on key.
---
key:
description: The key id
in: path
required: true
type: string
"""
key_id = request.matchdict['key']
auth_context = auth_context_from_request(request)
try:
key = Key.objects.get(owner=auth_context.owner,
id=key_id, deleted=None)
except me.DoesNotExist:
raise NotFoundError('Key id does not exist')
auth_context.check_perm('key', 'edit', key.id)
key.ctl.set_default()
return OK
def get_public_key(request):
"""
Get public key
Gets public key from key name.
READ permission required on key.
---
key:
description: The key id
in: path
required: true
type: string
"""
key_id = request.matchdict['key']
if not key_id:
raise RequiredParameterMissingError("key_id")
auth_context = auth_context_from_request(request)
try:
key = SSHKey.objects.get(owner=auth_context.owner,
id=key_id, deleted=None)
except me.DoesNotExist:
raise NotFoundError('Key id does not exist')
auth_context.check_perm('key', 'read', key.id)
return key.public
def show_schedule_entry(request):
"""
Show a schedule details of a user
READ permission required on schedule
---
schedule_id:
type: string
"""
schedule_id = request.matchdict['schedule_id']
auth_context = auth_context_from_request(request)
if not schedule_id:
raise RequiredParameterMissingError('No schedule id provided')
try:
schedule = Schedule.objects.get(id=schedule_id, deleted=None,
owner=auth_context.owner)
except me.DoesNotExist:
raise ScheduleTaskNotFound()
# SEC require READ permission on schedule
auth_context.check_perm('schedule', 'read', schedule_id)
return schedule.as_dict()
def user_toggle(user_id=None, action=None):
try:
user = umodels.User.get_user(user_id)
if action == "remove":
user.delete()
flash.success("User \"{}\" was successfully deleted!"
"".format(str(user)))
elif action == "is_admin":
user.is_admin = not user.is_admin
user.save()
flash.success("User \"{}\" field \"{}\" was successfully "
"updated to \"{}\"!".format(str(user), action,
user.is_admin))
except (me.DoesNotExist, me.ValidationError) as e:
flash.warning("User with id \"{}\" does not exist."
"".format(user_id))
return redirect(url_for("admin.home"))
def index_stock(stockid):
try:
page = int(request.args.get('page')) if request.args.get('page') else 1
stock = models.Stock.objects.filter(id=stockid, blacklisted=False).first()
if not stock:
return redirect(url_for('index'))
leaders = models.get_leaders()
return render_template('index.html',
view="market",
base_url=settings.SERVER_NAME,
leaders=leaders,
stock=stock,
stocks=api_views.get_paged_stocks(page),
page=page)
except DoesNotExist as e:
return redirect(url_for('index'))
except ValidationError as e:
return redirect(url_for('index'))
def get_journal_object(journal_number):
'''
If the journal exists in database, return the object.
Else construct the object in database, and return it.
:param journal_number:
:return:
'''
try:
journal = Journal.objects.get(entry_number=str(journal_number))
except DoesNotExist:
journal = Journal()
journal.entry_number = str(journal_number)
url = 'http://ieeexplore.ieee.org/xpl/RecentIssue.jsp'
payload = {
'punumber': journal_number
}
r = requests.get(url, params=payload)
query = PyQuery(r.text)
journal.name = query('#journal-page-hdr h1').text().strip()
journal.save()
return journal
def _on_heartbeat(self, ch, method, properties, message):
message = json.loads(str(message))
pid = message.get("uuid")
try:
p = model.Plugin.objects.get(uuid=pid)
# update heartbeat timestamp
p.last_heartbeat_at = datetime.datetime.now()
change = False
# TODO ugly: state management of plugins should be hidden with plugin class
if message.get("state") == "READY" and p.state != "READY":
# a plugin just announced that it is ready, lets start it
self.send_start_notification(p)
change = True
elif message.get("state") != p.state:
# lets keep track of the reported state update
p.state = message.get("state")
change = True
p.save()
if change:
# there was a state change lets schedule an plugin status update notification
self.send_plugin_status_update()
except DoesNotExist:
LOG.debug("Couldn't find plugin with UUID %r in DB" % pid)
def get(self, plugin_uuid=None):
LOG.debug("GET plugin info for: %r" % plugin_uuid)
try:
p = model.Plugin.objects.get(uuid=plugin_uuid)
return p.to_dict(), 200
except DoesNotExist as e:
LOG.error("Lookup error: %r" % plugin_uuid)
return {}, 404
def delete(self, plugin_uuid=None):
LOG.debug("DELETE plugin: %r" % plugin_uuid)
try:
p = model.Plugin.objects.get(uuid=plugin_uuid)
# send lifecycle stop event to plugin
PM.send_stop_notification(p)
# TODO ensure that record is deleted even if plugin does not deregister itself (use a timeout?)
return {}, 200
except DoesNotExist as e:
LOG.error("Lookup error: %r" % plugin_uuid)
return {}, 404
def save_search_result(p, queue, retry=0):
proxy = Proxy.get_random()['address']
url = SEARCH_URL.format(SEARCH_TEXT, p)
try:
r = fetch(url, proxy=proxy)
except (Timeout, ConnectionError):
sleep(0.1)
retry += 1
if retry > 5:
queue.put(url)
raise GreenletExit()
try:
p = Proxy.objects.get(address=proxy)
if p:
p.delete()
except DoesNotExist:
pass
return save_search_result(url, queue, retry)
soup = BeautifulSoup(r.text, 'lxml')
results = soup.find(class_='results')
if results is None:
# ???????, ??????
sleep(0.1)
retry += 1
if retry > 5:
queue.put(url)
raise GreenletExit()
return save_search_result(url, queue, retry)
articles = results.find_all(
'div', lambda x: 'wx-rb' in x)
for article in articles:
save_article(article)
def get_or_404(cls, *args, **kwargs):
message = kwargs.pop('message', None)
try:
return cls.objects.get(*args, **kwargs)
except (MultipleObjectsReturned, DoesNotExist, ValidationError):
if message is not None:
abort(404, message=message)
abort(404)
def getAssociation(self, server_url, handle=None):
"""
Gets a server url and the handle and finds a matching association that
has not expired. Expired associations are deleted. The association
returned is the one with the most recent issuing timestamp.
"""
query = {'server_url': server_url}
if handle:
query.update({'handle': handle.encode('hex')})
try:
mist_associations = MistAssociation.objects(**query)
except me.DoesNotExist:
mist_associations = []
filtered_mist_assocs = []
for assoc in mist_associations:
if assoc.is_expired():
assoc.delete()
else:
filtered_mist_assocs.append(assoc)
filtered_mist_assocs = sorted(filtered_mist_assocs,
key=lambda assoc: assoc.issued,
reverse=True)
if len(filtered_mist_assocs) > 0:
mist_assoc = filtered_mist_assocs[0]
association = Association(handle=mist_assoc.handle.decode('hex'),
secret=mist_assoc.secret.decode('hex'),
issued=mist_assoc.issued,
lifetime=mist_assoc.lifetime,
assoc_type=mist_assoc.assoc_type)
return association
return None
def su(request):
"""
Impersonate another user.
This allows an admin to take the identity of any other user. It is meant to
be used strictly for debugging. You can return to your regular user simply
by logging out. This won't affect the last login time of the actual user.
An email should be immediately sent out to the team, notifying of the 'su'
action for security reasons.
"""
# SEC raise exception if user not admin
user = user_from_request(request, admin=True)
session = request.environ['session']
if isinstance(session, ApiToken):
raise ForbiddenError('Cannot do su when authenticated with api token')
real_email = user.email
params = params_from_request(request)
email = params.get('email')
if not email:
raise RequiredParameterMissingError('email')
try:
user = User.objects.get(email=email)
except (UserNotFoundError, User.DoesNotExist):
raise UserUnauthorizedError()
reissue_cookie_session(request, real_email, su=user.id)
# alert admins
subject = "Some admin used su"
body = "Admin: %s\nUser: %s\nServer: %s" % (real_email, user.email,
config.CORE_URI)
send_email(subject, body, config.NOTIFICATION_EMAIL['ops'])
return HTTPFound('/')