Beispiel #1
0
def Expire(exm_key):
    """Transitions an Exemption to the EXPIRED state.

  Args:
    exm_key: The NDB Key of the Exemption entity.

  Raises:
    InvalidStateChangeError: If the desired state cannot be transitioned to from
        the current state.
  """
    host_id = exemption_models.Exemption.GetHostId(exm_key)
    logging.info('Expiring Exemption for Host %s', host_id)

    # Verify that the desired state change is still valid.
    exm = exm_key.get()
    if not exm.CanChangeToState(_STATE.EXPIRED):
        raise InvalidStateChangeError('%s to %s' % (exm.state, _STATE.EXPIRED))

    _EnableLockdown(exm_key)
    exemption_models.Exemption.ChangeState(exm_key, _STATE.EXPIRED)
    notify.DeferUpdateEmail(exm_key, _STATE.EXPIRED, transactional=True)
Beispiel #2
0
def Request(host_id, reason, other_text, duration):
    """Creates a new Exemption, or reuses an existing one.

  If no corresponding Exemption exists, creates a new one in the REQUESTED
  state. Otherwise, if one exists in a terminal state
  (CANCELLED/REVOKED/EXPIRED), sets it back to REQUESTED with the new
  deactivation date.

  Args:
    host_id: (str) Host ID
    reason: (str) The reason for requesting an Exemption. Must be one of
        constants.EXEMPTION_REASON.
    other_text: (str) Additional text if the reason is OTHER
    duration: (str) The requested duration of the Exemption. Must be one of
        constants.EXEMPTION_DURATION.

  Raises:
    InvalidReasonError: if the provided reason is invalid.
    InvalidDurationError: if the provided duration is invalid.
    InvalidRenewalError: if the Exemption cannot currently be renewed.
  """
    logging.info('Requesting Exemption for host %s', host_id)

    # Validate the reason.
    if reason not in constants.EXEMPTION_REASON.SET_ALL:
        message = 'Invalid reason provided: %s' % reason
        logging.error(message)
        raise InvalidReasonError(message)

    # Validate the duration.
    if duration not in constants.EXEMPTION_DURATION.SET_ALL:
        message = 'Invalid exemption duration: %s' % duration
        logging.error(message)
        raise InvalidDurationError(message)

    duration_delta = datetime.timedelta(
        days=constants.EXEMPTION_DURATION.MAP_TO_DAYS[duration])
    deactivation_dt = datetime.datetime.utcnow() + duration_delta

    exm = exemption_models.Exemption.Get(host_id)

    # If an Exemption has never existed for this host_id, just create one.
    if exm is None:
        exm_key = exemption_models.Exemption.Insert(host_id,
                                                    deactivation_dt,
                                                    reason,
                                                    other_text=other_text)
        notify.DeferUpdateEmail(exm_key, _STATE.REQUESTED, transactional=True)
        return

    # If we're dealing with an existing Exemption which can state change back to
    # REQUESTED, then make the change.
    if exm.CanChangeToState(_STATE.REQUESTED):
        exm_key = exemption_models.Exemption.CreateKey(host_id)
        details = [reason, other_text] if other_text else [reason]
        exemption_models.Exemption.ChangeState(exm_key,
                                               _STATE.REQUESTED,
                                               details=details)
        exm.deactivation_dt = deactivation_dt
        exm.put()
        notify.DeferUpdateEmail(exm_key, _STATE.REQUESTED, transactional=True)

    # Otherwise, we've received a request for an invalid renewal.
    else:
        message = 'Host %s already has a(n) %s Exemption' % (host_id,
                                                             exm.state)
        logging.error(message)
        raise InvalidRenewalError(message)