Changeset View
Changeset View
Standalone View
Standalone View
src/applications/drydock/blueprint/windows/WindowsZeroConf.php
<?php | <?php | ||||
/** | /** | ||||
* Responsible for configuring and automatically installing SSH on Windows | * Responsible for configuring and automatically installing SSH or WinRM | ||||
* EC2 instances when they start. | * on Windows EC2 instances when they start. | ||||
*/ | */ | ||||
final class WindowsZeroConf extends Phobject { | final class WindowsZeroConf extends Phobject { | ||||
public function getEncodedUserData(PassphraseCredential $credential) { | public function getEncodedUserData(PassphraseCredential $credential) { | ||||
return base64_encode($this->getUserData($credential)); | return base64_encode($this->getUserData($credential)); | ||||
} | } | ||||
private function getZeroConfScript() { | private function getSSHZeroConfScript() { | ||||
$file = | $file = | ||||
dirname(phutil_get_library_root('phabricator')). | dirname(phutil_get_library_root('phabricator')). | ||||
'/resources/windows/zeroconf.ps1'; | '/resources/windows/sshzeroconf.ps1'; | ||||
return Filesystem::readFile($file); | return Filesystem::readFile($file); | ||||
} | } | ||||
private function getUserData(PassphraseCredential $credential) { | private function getWinRMZeroConfScript() { | ||||
$file = | |||||
dirname(phutil_get_library_root('phabricator')). | |||||
'/resources/windows/winrmzeroconf.ps1'; | |||||
return Filesystem::readFile($file); | |||||
} | |||||
private function getUserData(PassphraseCredential $credential, $protocol) { | |||||
$type = PassphraseCredentialType::getTypeByConstant( | $type = PassphraseCredentialType::getTypeByConstant( | ||||
$credential->getCredentialType()); | $credential->getCredentialType()); | ||||
if (!$type) { | if (!$type) { | ||||
throw new Exception(pht('Credential has invalid type "%s"!', $type)); | throw new Exception(pht('Credential has invalid type "%s"!', $type)); | ||||
} | } | ||||
if ($protocol === 'ssh') { | |||||
if (!$type->hasPublicKey()) { | if (!$type->hasPublicKey()) { | ||||
throw new Exception(pht('Credential has no public key!')); | throw new Exception(pht('Credential has no public key!')); | ||||
} | } | ||||
$username = $credential->getUsername(); | $username = $credential->getUsername(); | ||||
$publickey = $type->getPublicKey( | $publickey = $type->getPublicKey( | ||||
PhabricatorUser::getOmnipotentUser(), | PhabricatorUser::getOmnipotentUser(), | ||||
$credential); | $credential); | ||||
$publickey = trim($publickey); | $publickey = trim($publickey); | ||||
$username = str_replace('"', '`"', $username); | $username = str_replace('"', '`"', $username); | ||||
$publickey = str_replace('"', '`"', $publickey); | $publickey = str_replace('"', '`"', $publickey); | ||||
$start = <<<EOF | $start = <<<EOF | ||||
<powershell> | <powershell> | ||||
\$username = "$username"; | \$username = "$username"; | ||||
\$publickey = "$publickey"; | \$publickey = "$publickey"; | ||||
EOF; | EOF; | ||||
$script = $this->getZeroConfScript(); | $script = $this->getZeroConfScript('ssh'); | ||||
$end = <<<EOF | |||||
</powershell> | |||||
EOF; | |||||
return $start.$script.$end; | |||||
} else if ($protocol === 'winrm') { | |||||
$username = $credential->getUsername(); | |||||
$password = $credential->getSecret(); | |||||
$username = str_replace('"', '`"', $username); | |||||
$password = str_replace('"', '`"', $password); | |||||
$start = <<<EOF | |||||
<powershell> | |||||
\$username = "$username"; | |||||
\$password = "$password"; | |||||
EOF; | |||||
$script = $this->getZeroConfScript('winrm'); | |||||
$end = <<<EOF | $end = <<<EOF | ||||
</powershell> | </powershell> | ||||
EOF; | EOF; | ||||
return $start.$script.$end; | return $start.$script.$end; | ||||
} else { | |||||
throw new Exception('Unknown protocol for automatic setup'); | |||||
} | |||||
} | } | ||||
} | } |