-
Notifications
You must be signed in to change notification settings - Fork 0
Adds ssm:copy command to allow for scp over ssm #13
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
spamoom
wants to merge
1
commit into
master
Choose a base branch
from
feature/ssm-scp
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,161 @@ | ||
<?php | ||
|
||
namespace App\Commands; | ||
|
||
use App\Exceptions\ProcessFailed; | ||
use Symfony\Component\Process\Process; | ||
use Symfony\Component\Console\Input\InputOption; | ||
use Symfony\Component\Console\Output\OutputInterface; | ||
|
||
class AwsSsmCopy extends AwsSsmConnect | ||
{ | ||
/** | ||
* The signature of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $signature = 'aws:ssm:copy'; | ||
|
||
/** | ||
* The description of the command. | ||
* | ||
* @var string | ||
*/ | ||
protected $description = 'Copy files from/to a server over SSM'; | ||
|
||
public function configure() | ||
{ | ||
$this->setDefinition(array_merge([ | ||
new InputOption('instance-id', null, InputOption::VALUE_OPTIONAL, 'The instance ID to connect to'), | ||
new InputOption('username', null, InputOption::VALUE_OPTIONAL, 'The username connect with'), | ||
new InputOption('direction', null, InputOption::VALUE_OPTIONAL, 'Direction (Up/Down)'), | ||
new InputOption('local-path', null, InputOption::VALUE_OPTIONAL, 'The path of the other server (local or remote). Can be a file or folder'), | ||
new InputOption('remote-path', null, InputOption::VALUE_OPTIONAL, 'The path of the server. Can be a file or folder'), | ||
new InputOption('other-server', null, InputOption::VALUE_OPTIONAL, 'Left blank, the other server is the local machine. Otherwise, specify user@host'), | ||
], $this->helpers->aws()->commonConsoleOptions())); | ||
} | ||
|
||
/** | ||
* Execute the console command. | ||
* | ||
* @return mixed | ||
*/ | ||
public function handle() | ||
{ | ||
$requiredBinaries = ['aws', 'ssh']; | ||
|
||
if ($this->helpers->checks()->checkAndReportMissingBinaries($requiredBinaries)) { | ||
return 1; | ||
} | ||
|
||
$rebuildOptions = []; | ||
|
||
$instanceId = $this->option('instance-id') ?: $this->askForInstanceId(); | ||
$username = $this->option('username') ?: $this->askForUsername(); | ||
|
||
if (!$instanceId) { | ||
$this->error('No instance ID provided.'); | ||
return 1; | ||
} | ||
|
||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'username', $username); | ||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'instance-id', $instanceId); | ||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'aws-profile'); | ||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'aws-region'); | ||
|
||
$key = $this->generateTempSshKey(); | ||
$command = $this->generateRemoteCommand($username, $key); | ||
|
||
$this->info("Sending a temporary SSH key to the server...", OutputInterface::VERBOSITY_VERBOSE); | ||
if (!$this->helpers->aws()->ssm()->sendRemoteCommand($instanceId, $command)) { | ||
$this->error('Failed to send SSH key to server'); | ||
return 1; | ||
} | ||
|
||
$sessionCommand = $this->helpers->aws()->ssm()->startSessionProcess($instanceId); | ||
$sessionCommandString = implode(' ', $sessionCommand->getArguments()); | ||
|
||
$options = [ | ||
'-o', 'IdentityFile ~/.ssh/netsells-cli-ssm-ssh-tmp', | ||
'-o', 'IdentitiesOnly yes', | ||
'-o', 'GSSAPIAuthentication no', | ||
'-o', 'PasswordAuthentication no', | ||
'-o', "ProxyCommand {$sessionCommandString}", | ||
]; | ||
|
||
$direction = trim($this->option('direction')) ?: $this->askForDirection(); | ||
|
||
if (!$this->validateDirection($direction)) { | ||
$this->error('Invalid direction'); | ||
} | ||
|
||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'direction', $direction); | ||
|
||
$otherServer = trim($this->option('other-server')) ?: $this->askForOtherServer(); | ||
|
||
$localPath = trim($this->option('local-path')) ?: $this->askForLocalPath($otherServer); | ||
$remotePath = trim($this->option('remote-path')) ?: $this->askForRemotePath(); | ||
|
||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'local-path', $localPath); | ||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'remote-path', $remotePath); | ||
$rebuildOptions = $this->appendResolvedArgument($rebuildOptions, 'other-server', $otherServer); | ||
|
||
$this->sendReRunHelper($rebuildOptions); | ||
|
||
$localOption = ($otherServer !== '__localhost__' ? "{$otherServer}:" : null) . $localPath; | ||
$remoteOption = sprintf("%s@%s", $username, $instanceId) . ":{$remotePath}"; | ||
|
||
$directionOptions = ($direction == 'up') ? [$localOption, $remoteOption] : [$remoteOption, $localOption]; | ||
|
||
try { | ||
$this->helpers->process()->withCommand(array_merge( | ||
[ | ||
'scp', | ||
], | ||
$options, | ||
$directionOptions, | ||
)) | ||
->withTimeout(null) | ||
->withProcessModifications(function ($process) { | ||
$process->setTty(Process::isTtySupported()); | ||
$process->setIdleTimeout(null); | ||
}) | ||
->run(); | ||
} catch (ProcessFailed $e) { | ||
$this->info(' '); | ||
$this->error("SCP command exited with an exit code of " . $e->getCode()); | ||
} | ||
} | ||
|
||
protected function askForDirection() | ||
{ | ||
return $this->menu("Which direction are you sending the files?", [ | ||
'up' => 'Upstream', | ||
'down' => 'Downstream', | ||
])->open(); | ||
} | ||
|
||
protected function validateDirection($direction): bool | ||
{ | ||
return in_array($direction, ['up', 'down']); | ||
} | ||
|
||
protected function askForLocalPath($otherServer) | ||
{ | ||
if ($otherServer == '__localhost__') { | ||
return $this->ask("What is the path of the file/folder on your computer?"); | ||
} | ||
|
||
return $this->ask("What is the path of the file/folder on the other server? ({$otherServer})"); | ||
} | ||
|
||
protected function askForRemotePath() | ||
{ | ||
return $this->ask("What is the path of the file/folder on the remote server?"); | ||
} | ||
|
||
protected function askForOtherServer() | ||
{ | ||
return $this->ask("What is the path of the other server? Should be in the format username@hostname. Leave blank for this computer", '__localhost__'); | ||
} | ||
} |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I can see few improvements that could be made.
To reduce the copy-pasta, I think the main AwsSssConnect command could use a template method pattern. We can have a protected method "composeProcessComand" which returns the "command array" (passed to the withCommand method) that is called from within handle method. This way AwsSsmCopy could just override the configure + composeProcessCommand with the common code staying in one place. However since we plan to rewrite in Go anyway, I think it's less of a concern and we can focus on making the command user friendly, which brings me to the next point.
I think this command should be as close to actual scp as possible to make it as intuitive (and familiar) as can be. Instead of asking for direction, the direction would be from left to right, source to destination, just like scp and cp. Given that it uses scp underneath, I think we could just proxy arguments and options down to the scp command. In my mind, we can use it just like:
If we proxy the arguments and options, the above should "just work". It will also support options such as '-r' for recursive copying - so bigs wins straightaway.
The only problem remaining, is that we need to know the username and host/instance. The username and instance should be optional, allowing the user to pick them from a list (as is currently). In order to do that, I think we can parse the arguments using parse_url and determine the source/destination + user/host. Few examples:
Algorithm:
The above will allow for doing this, when we don't know the server:
And of course it will allow for the first version, where the username + host is explicitly passed in.
Perhaps we could do the parsing in a different way, that's just an idea I wanted to put forward. I think starting the path with just colon makes sense to denote remote server, and still is very similar to usual scp.
Please let me know what you think @spamoom