Example #1
0
 /**
  * Get latest version of script
  *
  * @return ScriptVersion
  *
  * @throws \Exception
  */
 public function getLatestVersion()
 {
     /* @var $version ScriptVersion */
     $version = ScriptVersion::findOne([['scriptId' => $this->id]], null, ['version' => false]);
     if (!$version) {
         throw new Exception(sprintf('No version found for script %d', $this->id));
     }
     return $version;
 }
Example #2
0
 /**
  * Gets ScriptVersion entity
  *
  * @param string $criteria search criteria
  * @param array $params query params
  * @return ScriptVersion
  */
 protected function getScriptVersion($criteria, $params)
 {
     /* @var  $sv ScriptVersion */
     $sv = ScriptVersion::findOne([['scriptId' => $params['scriptId']], ['version' => $criteria]]);
     static::toDelete(ScriptVersion::class, [$sv->scriptId, $sv->version]);
     return $sv;
 }
Example #3
0
 /**
  * Get latest version of script
  *
  * @return ScriptVersion
  * @throws \Exception
  */
 public function getLatestVersion()
 {
     /* @var ScriptVersion $version */
     $version = ScriptVersion::findOne(array(array('scriptId' => $this->id)), array('version' => ''));
     if (!$version) {
         throw new \Exception(sprintf('No version found for script %d', $this->id));
     }
     return $version;
 }
Example #4
0
 /**
  * {@inheritdoc}
  * @see \Scalr\Api\DataType\ApiEntityAdapter::validateEntity()
  */
 public function validateEntity($entity)
 {
     if (!$entity instanceof RoleScript) {
         throw new InvalidArgumentException(sprintf("First argument must be instance of Scalr\\Model\\Entity\\RoleScript class"));
     }
     if ($entity->id !== null) {
         if (!RoleScript::findPk($entity->id)) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the rule with ID: %d", $entity->id));
         }
     }
     //Getting the role initiates check permissions
     $role = $this->controller->getRole($entity->roleId);
     if (!empty($entity->scriptId)) {
         if ($entity->version == ScriptVersion::LATEST_SCRIPT_VERSION) {
             $found = ScriptVersion::findOne([['scriptId' => $entity->scriptId]], null, ['version' => false]);
         } else {
             $found = ScriptVersion::findPk($entity->scriptId, $entity->version);
         }
         if (empty($found)) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find version %d of the script with ID: %d", $entity->version, $entity->scriptId));
         }
         if (Script::findPk($entity->scriptId)->os == 'windows' && $role->getOs()->family != 'windows') {
             throw new ApiErrorException(409, ErrorMessage::ERR_OS_MISMATCH, "Script OS family does not match role OS family");
         }
     }
     if (empty($entity->eventName)) {
         $entity->eventName = '*';
     } else {
         if ($entity->eventName !== '*') {
             if (array_key_exists($entity->eventName, array_merge(EVENT_TYPE::getScriptingEventsWithScope(), EventDefinition::getList($this->controller->getUser()->id, $this->controller->getEnvironment()->id))) === false) {
                 throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Could not find out the event '{$entity->eventName}'");
             }
             if ($entity->scriptType == OrchestrationRule::ORCHESTRATION_RULE_TYPE_CHEF && in_array($entity->eventName, EVENT_TYPE::getChefRestrictedEvents())) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Chef can't be used with {$entity->eventName}");
             }
         }
     }
     if (!$this->controller->hasPermissions($entity, true)) {
         //Checks entity level write access permissions
         throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
     }
 }
Example #5
0
 /**
  * {@inheritdoc}
  * @see ApiEntityAdapter::validateEntity()
  */
 public function validateEntity($entity)
 {
     /* @var $entity OrchestrationRule */
     $entityClass = $this->entityClass;
     if (!$entity instanceof $entityClass) {
         throw new InvalidArgumentException(sprintf("First argument must be instance of {$entityClass} class"));
     }
     if ($entity->id !== null) {
         if (!$entityClass::findPk($entity->id)) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find out the rule with ID: %d", $entity->id));
         }
     }
     if (!empty($entity->scriptId)) {
         if ($entity->version == ScriptVersion::LATEST_SCRIPT_VERSION) {
             $found = ScriptVersion::findOne([['scriptId' => $entity->scriptId]], null, ['version' => false]);
         } else {
             $found = ScriptVersion::findPk($entity->scriptId, $entity->version);
         }
         /* @var $found ScriptVersion */
         if (empty($found) || !$found->hasAccessPermissions($this->controller->getUser(), $this->controller->getEnvironment())) {
             throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, sprintf("Could not find version %d of the script with ID: %d", $entity->version, $entity->scriptId));
         }
     }
     if (empty($entity->eventName)) {
         $entity->eventName = '*';
     } else {
         if ($entity->eventName !== '*') {
             if (array_key_exists($entity->eventName, array_merge(EVENT_TYPE::getScriptingEventsWithScope(), EventDefinition::getList($this->controller->getUser()->id, $this->controller->getScope() === ScopeInterface::SCOPE_ENVIRONMENT ? $this->controller->getEnvironment()->id : null))) === false) {
                 throw new ApiErrorException(404, ErrorMessage::ERR_OBJECT_NOT_FOUND, "Could not find out the event '{$entity->eventName}'");
             }
             if ($entity->scriptType == OrchestrationRule::ORCHESTRATION_RULE_TYPE_CHEF && in_array($entity->eventName, EVENT_TYPE::getChefRestrictedEvents())) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Chef can't be used with {$entity->eventName}");
             }
             if ($entity->eventName == EVENT_TYPE::BEFORE_INSTANCE_LAUNCH && $entity->target == Script::TARGET_INSTANCE) {
                 throw new ApiErrorException(400, ErrorMessage::ERR_INVALID_VALUE, "Event '{$entity->eventName}' will never be handled by the triggering server");
             }
         }
     }
     if (!$this->controller->hasPermissions($entity, true)) {
         //Checks entity level write access permissions
         throw new ApiErrorException(403, ErrorMessage::ERR_PERMISSION_VIOLATION, "Insufficient permissions");
     }
 }