Skip to content
Snippets Groups Projects
Commit ef8e4b85 authored by xiaohua guan's avatar xiaohua guan Committed by Yas Naoi
Browse files

Issue #3260255 by Xiaohua Guan, yas, baldwinlouie: Save keypair to entity field (AWS Cloud)

parent 7879e26e
No related branches found
No related tags found
No related merge requests found
......@@ -493,8 +493,7 @@ function aws_cloud_entity_view(array &$build, EntityInterface $entity, EntityVie
$keypair = \Drupal::entityTypeManager()->getStorage('aws_cloud_key_pair')->load($entity->id());
// If the key is on the server, prompt user to download it.
if (!empty($keypair) && !empty($keypair->getKeyFileLocation())) {
if (!empty($keypair) && !empty($keypair->getKeyMaterial())) {
$url = Url::fromRoute('entity.aws_cloud_key_pair.download', [
'cloud_context' => $keypair->getCloudContext(),
'key_pair' => $keypair->id(),
......
......@@ -5,7 +5,7 @@ namespace Drupal\aws_cloud\Controller\Ec2;
use Drupal\Core\Controller\ControllerBase;
use Drupal\Core\Messenger\Messenger;
use Symfony\Component\DependencyInjection\ContainerInterface;
use Symfony\Component\HttpFoundation\BinaryFileResponse;
use Symfony\Component\HttpFoundation\Response;
/**
* Controller responsible for AWS KeyPair.
......@@ -52,12 +52,14 @@ class AwsCloudKeyPairController extends ControllerBase {
*/
public function downloadKey($cloud_context, $key_pair, $entity_type = 'aws_cloud') {
$key_pair = $this->entityTypeManager()->getStorage("{$entity_type}_key_pair")->load($key_pair);
$file = $key_pair->getKeyFileLocation();
if ($file !== FALSE) {
$response = new BinaryFileResponse($file, 200, [], FALSE, 'attachment');
$response->setContentDisposition('attachment', $key_pair->getKeyPairName() . '.pem');
$response->deleteFileAfterSend(TRUE);
return $response;
$key_material = $key_pair->getKeyMaterial();
if (!empty($key_material)) {
$key_pair->setKeyMaterial(NULL);
$key_pair->save();
return new Response($key_material, 200, [
'Content-Type' => 'application/x-pem-file',
'Content-Disposition' => sprintf('attachment; filename="%s.pem"', $key_pair->getKeyPairName()),
]);
}
else {
// Just redirect to key pair listing page.
......
......@@ -153,19 +153,6 @@ class KeyPair extends CloudContentEntityBase implements KeyPairInterface {
return 'temporary://' . $this->getKeyPairName() . '.pem';
}
/**
* Helper function to save private key to temporary file system.
*
* @param string $key
* String of the private key.
*/
public function saveKeyFile($key): void {
if (!empty($key)) {
// See also: https://www.drupal.org/node/2913224.
\Drupal::service('file_system')->saveData($key, $this->getKeyFileName());
}
}
/**
* {@inheritdoc}
*/
......@@ -228,11 +215,6 @@ class KeyPair extends CloudContentEntityBase implements KeyPairInterface {
'default_value' => '',
'max_length' => 5120,
'text_processing' => 0,
])
->setDisplayOptions('view', [
'label' => 'inline',
'type' => 'string',
'weight' => -5,
]);
$fields['created'] = BaseFieldDefinition::create('created')
......
......@@ -82,9 +82,4 @@ interface KeyPairInterface extends ContentEntityInterface, EntityOwnerInterface,
*/
public function getKeyFileName(): ?string;
/**
* {@inheritdoc}
*/
public function saveKeyFile($key): void;
}
......@@ -97,11 +97,9 @@ class KeyPairCreateForm extends AwsCloudContentForm {
&& (!empty($entity)
&& $entity->setKeyFingerprint($result['KeyFingerprint'])
&& $entity->setKeyPairId($result['KeyPairId'])
&& $entity->setKeyMaterial($result['KeyMaterial'])
&& $entity->save())) {
// Save the file to temp.
$entity->saveKeyFile($result['KeyMaterial']);
$this->processOperationStatus($entity, 'created');
$this->clearCacheValues($entity->getCacheTags());
......
......@@ -832,8 +832,9 @@ function openstack_entity_view(array &$build, EntityInterface $entity, EntityVie
&& $entity->id() !== NULL) {
$keypair = \Drupal::entityTypeManager()->getStorage('openstack_key_pair')->load($entity->id());
// If the key is on the server, prompt user to download it.
if (!empty($keypair) && !empty($keypair->getKeyFileLocation())) {
if (!empty($keypair) && !empty($keypair->getKeyMaterial())) {
$url = Url::fromRoute('entity.openstack_key_pair.download', [
'cloud_context' => $keypair->getCloudContext(),
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment