/Classes/Command/EncryptCommandController.php

https://github.com/liwo/Deploy-Package · PHP · 147 lines · 68 code · 11 blank · 68 comment · 2 complexity · 11a7547ea2fd1106e4495a7108957f17 MD5 · raw file

  1. <?php
  2. declare(ENCODING = 'utf-8');
  3. namespace TYPO3\Deploy\Command;
  4. /* *
  5. * This script belongs to the FLOW3 package "TYPO3.Deploy". *
  6. * *
  7. * */
  8. use Doctrine\ORM\Mapping as ORM;
  9. use TYPO3\FLOW3\Annotations as FLOW3;
  10. /**
  11. * Encryption command controller
  12. */
  13. class EncryptCommandController extends \TYPO3\FLOW3\MVC\Controller\CommandController {
  14. /**
  15. * @FLOW3\Inject
  16. * @var \TYPO3\Deploy\Encryption\EncryptionServiceInterface
  17. */
  18. protected $encryptionService;
  19. /**
  20. * Setup encryption with a local key for the deployment system
  21. *
  22. * The local key should be kept secretly and could be encrypted with
  23. * an optional passphrase. The name defaults to "Local.key".
  24. *
  25. * @param string $passphrase Passphrase for the generated key (optional)
  26. * @return void
  27. * @author Christopher Hlubek <hlubek@networkteam.com>
  28. */
  29. public function setupCommand($passphrase = NULL) {
  30. if (file_exists($this->getDeploymentConfigurationPath() . '/Keys/Local.key')) {
  31. $this->outputLine('Local key already exists');
  32. $this->quit(1);
  33. }
  34. $keyPair = $this->encryptionService->generateKeyPair($passphrase);
  35. $this->writeKeyPair($keyPair, $this->getDeploymentConfigurationPath() . '/Keys/Local.key');
  36. $this->outputLine('Local key generated');
  37. }
  38. /**
  39. * Encrypt configuration with the local key
  40. *
  41. * This command scans the subdirectory of "Build/Deploy/Configuration" for configuration
  42. * files that should be encrypted. An optional deployment name restricts this operation to configuration
  43. * files of a specific deployment (e.g. "Build/Deploy/Configuration/Staging").
  44. *
  45. * Only .yaml files with a header of "#!ENCRYPT" are encrypted.
  46. *
  47. * @param string $deploymentName Optional deployment name to selectively encrypt the configuration
  48. * @return void
  49. * @see typo3.deploy:encrypt:open
  50. * @author Christopher Hlubek <hlubek@networkteam.com>
  51. */
  52. public function sealCommand($deploymentName = '') {
  53. $keyPair = $this->readKeyPair($this->getDeploymentConfigurationPath() . '/Keys/Local.key');
  54. $configurations = \TYPO3\FLOW3\Utility\Files::readDirectoryRecursively($this->getDeploymentConfigurationPath() . '/Configuration/' . $deploymentName, 'yaml');
  55. foreach ($configurations as $configuration) {
  56. $data = file_get_contents($configuration);
  57. if (strpos($data, '#!ENCRYPT') !== 0) {
  58. continue;
  59. }
  60. $crypted = $this->encryptionService->encryptData($data, $keyPair->getPublicKey());
  61. $targetFilename = $configuration . '.encrypted';
  62. file_put_contents($targetFilename, $crypted);
  63. unlink($configuration);
  64. $this->outputLine('Sealed ' . $targetFilename);
  65. }
  66. }
  67. /**
  68. * Open encrypted configuration with the local key
  69. *
  70. * Like the seal command, this can be restricted to a specific deployment. If a passphrase
  71. * was used to encrypt the local private key, it must be specified as the passphrase
  72. * argument to open the configuration files.
  73. *
  74. * @param string $passphrase Passphrase to decrypt the local key (if encrypted)
  75. * @param string $deploymentName Optional deployment name to selectively decrypt the configuration
  76. * @return void
  77. * @see typo3.deploy:encrypt:seal
  78. * @author Christopher Hlubek <hlubek@networkteam.com>
  79. */
  80. public function openCommand($passphrase = NULL, $deploymentName = '') {
  81. $keyPair = $this->readKeyPair($this->getDeploymentConfigurationPath() . '/Keys/Local.key');
  82. try {
  83. $keyPair = $this->encryptionService->openKeyPair($keyPair, $passphrase);
  84. } catch(\TYPO3\Deploy\Encryption\InvalidPassphraseException $exception) {
  85. $this->outputLine('Local key is encrypted with passphrase. Wrong or no passphrase given.');
  86. $this->quit(1);
  87. }
  88. $configurations = \TYPO3\FLOW3\Utility\Files::readDirectoryRecursively($this->getDeploymentConfigurationPath() . '/Configuration/' . $deploymentName, 'yaml.encrypted');
  89. foreach ($configurations as $configuration) {
  90. $crypted = file_get_contents($configuration);
  91. $data = $this->encryptionService->decryptData($crypted, $keyPair->getPrivateKey());
  92. $targetFilename = substr($configuration, 0, -strlen('.encrypted'));
  93. file_put_contents($targetFilename, $data);
  94. unlink($configuration);
  95. $this->outputLine('Opened ' . $targetFilename);
  96. }
  97. }
  98. /**
  99. * Writes a key pair to a file
  100. *
  101. * @param \TYPO3\Deploy\Encryption\KeyPair $keyPair
  102. * @param string $filename
  103. * @return void
  104. * @author Christopher Hlubek <hlubek@networkteam.com>
  105. */
  106. protected function writeKeyPair(\TYPO3\Deploy\Encryption\KeyPair $keyPair, $filename) {
  107. $data = json_encode(array(
  108. 'encrypted' => $keyPair->isEncrypted(),
  109. 'privateKey' => $keyPair->getPrivateKey(),
  110. 'publicKey' => $keyPair->getPublicKey()
  111. ));
  112. file_put_contents($filename, $data);
  113. }
  114. /**
  115. * Reads a key pair from a file
  116. *
  117. * @param string $filename
  118. * @return \TYPO3\Deploy\Encryption\KeyPair
  119. * @author Christopher Hlubek <hlubek@networkteam.com>
  120. */
  121. protected function readKeyPair($filename) {
  122. $data = file_get_contents($filename);
  123. $data = json_decode($data, TRUE);
  124. $keyPair = new \TYPO3\Deploy\Encryption\KeyPair($data['privateKey'], $data['publicKey'], $data['encrypted']);
  125. return $keyPair;
  126. }
  127. /**
  128. * Get the deployment configuration base path
  129. *
  130. * @return string
  131. */
  132. protected function getDeploymentConfigurationPath() {
  133. return FLOW3_PATH_ROOT . 'Build/Deploy';
  134. }
  135. }
  136. ?>