private static function readConfiguration()
 {
     if (self::$hasReadConfiguration) {
         return true;
     }
     self::$hasReadConfiguration = true;
     foreach (PhabricatorEnv::getEnvConfig('keyring') as $spec) {
         self::addKey($spec);
     }
 }
 public function testAES256Storage()
 {
     $engine = new PhabricatorTestStorageEngine();
     $key_name = 'test.abcd';
     $key_text = 'abcdefghijklmnopABCDEFGHIJKLMNOP';
     PhabricatorKeyring::addKey(array('name' => $key_name, 'type' => 'aes-256-cbc', 'material.base64' => base64_encode($key_text)));
     $format = id(new PhabricatorFileAES256StorageFormat())->selectMasterKey($key_name);
     $data = 'The cow jumped over the full moon.';
     $params = array('name' => 'test.dat', 'storageEngines' => array($engine), 'format' => $format);
     $file = PhabricatorFile::newFromFileData($data, $params);
     // We should have a file stored as AES256.
     $format_key = $format->getStorageFormatKey();
     $this->assertEqual($format_key, $file->getStorageFormat());
     $this->assertEqual($data, $file->loadFileData());
     // The actual raw data in the storage engine should be encrypted. We
     // can't really test this, but we can make sure it's not the same as the
     // input data.
     $raw_data = $engine->readFile($file->getStorageHandle());
     $this->assertTrue($data !== $raw_data);
 }
示例#3
0
 private static function buildFromFileData($data, array $params = array())
 {
     if (isset($params['storageEngines'])) {
         $engines = $params['storageEngines'];
     } else {
         $size = strlen($data);
         $engines = PhabricatorFileStorageEngine::loadStorageEngines($size);
         if (!$engines) {
             throw new Exception(pht('No configured storage engine can store this file. See ' . '"Configuring File Storage" in the documentation for ' . 'information on configuring storage engines.'));
         }
     }
     assert_instances_of($engines, 'PhabricatorFileStorageEngine');
     if (!$engines) {
         throw new Exception(pht('No valid storage engines are available!'));
     }
     $file = self::initializeNewFile();
     $aes_type = PhabricatorFileAES256StorageFormat::FORMATKEY;
     $has_aes = PhabricatorKeyring::getDefaultKeyName($aes_type);
     if ($has_aes !== null) {
         $default_key = PhabricatorFileAES256StorageFormat::FORMATKEY;
     } else {
         $default_key = PhabricatorFileRawStorageFormat::FORMATKEY;
     }
     $key = idx($params, 'format', $default_key);
     // Callers can pass in an object explicitly instead of a key. This is
     // primarily useful for unit tests.
     if ($key instanceof PhabricatorFileStorageFormat) {
         $format = clone $key;
     } else {
         $format = clone PhabricatorFileStorageFormat::requireFormat($key);
     }
     $format->setFile($file);
     $properties = $format->newStorageProperties();
     $file->setStorageFormat($format->getStorageFormatKey());
     $file->setStorageProperties($properties);
     $data_handle = null;
     $engine_identifier = null;
     $exceptions = array();
     foreach ($engines as $engine) {
         $engine_class = get_class($engine);
         try {
             list($engine_identifier, $data_handle) = $file->writeToEngine($engine, $data, $params);
             // We stored the file somewhere so stop trying to write it to other
             // places.
             break;
         } catch (PhabricatorFileStorageConfigurationException $ex) {
             // If an engine is outright misconfigured (or misimplemented), raise
             // that immediately since it probably needs attention.
             throw $ex;
         } catch (Exception $ex) {
             phlog($ex);
             // If an engine doesn't work, keep trying all the other valid engines
             // in case something else works.
             $exceptions[$engine_class] = $ex;
         }
     }
     if (!$data_handle) {
         throw new PhutilAggregateException(pht('All storage engines failed to write file:'), $exceptions);
     }
     $file->setByteSize(strlen($data));
     $file->setContentHash(self::hashFileContent($data));
     $file->setStorageEngine($engine_identifier);
     $file->setStorageHandle($data_handle);
     $file->readPropertiesFromParameters($params);
     if (!$file->getMimeType()) {
         $tmp = new TempFile();
         Filesystem::writeFile($tmp, $data);
         $file->setMimeType(Filesystem::getMimeType($tmp));
     }
     try {
         $file->updateDimensions(false);
     } catch (Exception $ex) {
         // Do nothing
     }
     $file->save();
     return $file;
 }
 private function getMasterKeyMaterial($key_name)
 {
     return PhabricatorKeyring::getKey($key_name, self::FORMATKEY);
 }