/**
  * Store a newly created resource in storage.
  * @SWG\Post(
  *     path="/projects/{project_key}/environments/{environment_key}/stacks/{stack_key}/components/{component_id}/parameters",
  *     summary="Store component parameter",
  *     @SWG\Parameter(ref="#/parameters/project_key"),
  *     @SWG\Parameter(ref="#/parameters/environment_key"),
  *     @SWG\Parameter(ref="#/parameters/stack_key"),
  *     @SWG\Parameter(ref="#/parameters/component_id"),
  *     @SWG\Parameter(ref="#/parameters/key"),
  *     @SWG\Parameter(ref="#/parameters/Parameter"),
  *     @SWG\Response(response="default", ref="#/responses/Parameters"),
  *     security={{"highcore_auth":{}}},
  * )
  * @param  int|string  $project_key
  * @param  int|string  $environment_key
  * @param  int|string  $stack_key
  * @param  string  $component_id
  * @return Response
  * @throws Exception
  */
 public function store($project_key, $environment_key, $stack_key, $component_id)
 {
     $key = $stack_key;
     $stack = Persistence::getStack(compact('project_key', 'environment_key', 'key'), Input::get('key', 'id'));
     if (!$stack->components->has($component_id)) {
         abort(404);
     }
     $parameter = Parameter::make()->fill(Input::only(['id', 'value', 'sensitive']));
     $stack->get("components.{$component_id}")->parameters->put($parameter->id, $parameter);
     Persistence::saveStack($stack);
     return $parameter;
 }
 public function testDecrypt()
 {
     $encrypted_key = 'hu8gZ19fINO3HrPUQkHgqdZ5qaLcKFg4kRgUISFOhQWiAWVYbyo53gE6JQ61gZ64tvclc0RtVghPZCGLvQSJvmQxFwdxzXF7mqvJdepFXhIdwJ6PMIE701kGmdjNoSoXFiOACb73c70DZQmzLA4sVulwcTtJcMWsRgpiC4PayrNWun9x0bWsvsDNNZ2YPZL8yf0paeyfLPsMONfqakSUrJfprYFtjbEkWcgx0hluvZpHj3Nl6AU3TFBJS6b8qFrp99qVsS7wBKAUNSUrDBzGjYNy';
     $decrypted_key = 'FOGdGgtmjWZufLyyCe91QG1njTKrylvn8wv0RaLmoCd7sEhDKAjJ6iHGXJPRrfdm2Mtw2nlpBjbukKBU';
     $credentials_param = Parameter::make(['id' => 'cloud_credentials', 'sensitive' => true, 'value' => ['access_key' => 'test-key', 'secret_key' => $encrypted_key]]);
     $params = ParametersCollection::make(['StackName' => 'test-stack', 'cloud_credentials' => $credentials_param]);
     $unencrypted_values = ['test-stack', 'cloud_credentials', true, 'test-key'];
     foreach ($unencrypted_values as $value) {
         Crypt::shouldReceive('decrypt')->once()->with($value)->andThrow(new \Illuminate\Contracts\Encryption\DecryptException('Invalid data.'));
     }
     Crypt::shouldReceive('decrypt')->once()->with($encrypted_key)->andReturn($decrypted_key);
     $decrypted_params = $params->decrypted()->toArray();
     $this->assertEquals(array_get($decrypted_params, 'cloud_credentials.value.secret_key'), $decrypted_key);
 }
 /**
  * Add a parameter to a stack component
  * @SWG\Post(
  *     path="/stacks/{stack_id}/components/{component_id}/parameters",
  *     summary="Store component parameter",
  *     @SWG\Parameter(ref="#/parameters/stack_id"),
  *     @SWG\Parameter(ref="#/parameters/component_id"),
  *     @SWG\Parameter(ref="#/parameters/Parameter"),
  *     @SWG\Response(response="default", ref="#/responses/Parameter"),
  *     security={{"highcore_auth":{}}},
  * )
  * @param int $stack_id  Stack Id
  * @param  string  $component_id
  * @return Response
  * @throws Exception
  */
 public function store($stack_id, $component_id)
 {
     $stack = Persistence::getStack($stack_id);
     if (!$stack->components->has($component_id)) {
         abort(404);
     }
     /** @var Component $component */
     $component = $stack->get("components.{$component_id}");
     $parameters = $component->parameters;
     /** @var Parameter $parameter */
     $parameter = Parameter::make()->fill(Input::only(['id', 'value', 'sensitive']));
     $parameters->put($parameter->id, $parameter);
     Persistence::saveStack($stack);
     return $parameter;
 }
 /**
  * Store a newly created resource in storage.
  * @SWG\Post(
  *     path="/projects/{project_key}/parameters",
  *     summary="Store project parameter",
  *     @SWG\Parameter(ref="#/parameters/project_key"),
  *     @SWG\Parameter(ref="#/parameters/key"),
  *     @SWG\Parameter(ref="#/parameters/source"),
  *     @SWG\Parameter(ref="#/parameters/Parameter"),
  *     @SWG\Response(response="default", ref="#/responses/Parameter"),
  *     security={{"highcore_auth":{}}},
  * )
  * @param  int|string  $project_key
  * @return Response
  * @throws Exception
  */
 public function store($project_key)
 {
     $source = Input::get('source', false);
     $input = array_filter(Input::only(['id', 'value', 'sensitive']));
     if ($source) {
         $request = Request::create($source, 'GET');
         /** @var Parameter $project */
         $parameter = Route::dispatch($request)->getOriginalContent();
     } else {
         $parameter = Parameter::make();
     }
     $parameter->fill($input);
     $project = Persistence::getProject($project_key, Input::get('key', 'id'));
     $project->parameters->put($parameter->id, $parameter);
     Persistence::saveProject($project);
     return $parameter;
 }
 /**
  * Store a newly created resource in storage.
  * @SWG\Post(
  *     path="/projects/{project_key}/environments/{environment_key}/stacks/{stack_key}/components",
  *     summary="Store stack component",
  *     @SWG\Parameter(ref="#/parameters/project_key"),
  *     @SWG\Parameter(ref="#/parameters/environment_key"),
  *     @SWG\Parameter(ref="#/parameters/stack_key"),
  *     @SWG\Parameter(ref="#/parameters/key"),
  *     @SWG\Parameter(ref="#/parameters/source"),
  *     @SWG\Parameter(ref="#/parameters/Parameter"),
  *     @SWG\Response(response="default", ref="#/responses/Components"),
  *     security={{"highcore_auth":{}}},
  * )
  * @param  int|string  $project_key
  * @param  int|string  $environment_key
  * @param  int|string  $stack_key
  * @return Response
  * @throws Exception
  */
 public function store($project_key, $environment_key, $stack_key)
 {
     $source = Input::get('source', false);
     $input = array_filter(Input::only(['id', 'value', 'sensitive']));
     if ($source) {
         $request = Request::create($source, 'GET');
         /** @var Parameter $project */
         $component = Route::dispatch($request)->getOriginalContent();
     } else {
         $component = Parameter::make();
     }
     $component->fill($input);
     $key = $stack_key;
     $stack = Persistence::getStack(compact('project_key', 'environment_key', 'key'), Input::get('key', 'id'));
     $stack->components->put($component->id, $component);
     Persistence::saveStack($stack);
     return $component;
 }
 /**
  * @param Stack $stack
  * @param Collection $remote_stacks
  * @return Collection
  */
 private function getOutputs(Stack $stack, Collection $remote_stacks = null)
 {
     $remote_stacks = $remote_stacks ? $remote_stacks : Collection::make([$stack]);
     $params = Collection::make();
     foreach ($remote_stacks as $remote_stack) {
         if (!$remote_stack->provisioned) {
             throw new CloudFormerException('Cannot use non-provisioned remote stack ' . $remote_stack->name);
         }
         $cfn_params = new ParametersCollection(['StackName' => $remote_stack->name]);
         $response = $this->cfn($stack)->describeStacks($cfn_params->all());
         foreach (array_get(head($response->get('Stacks')), 'Outputs', []) as $output) {
             $key = snake_case($output['OutputKey']);
             $value = $output['OutputValue'];
             if (starts_with($value, ['[']) && ends_with($value, [']'])) {
                 $value = explode(',', trim($value, '[]'));
             }
             $param = ['id' => $key, 'value' => $value];
             $params->put($key, Parameter::make($param));
         }
     }
     return $params;
 }