Skip to content
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

demo role_arn fixes #52

Closed
wants to merge 4 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 42 additions & 5 deletions includes/classes/Snapshots/DynamoDBConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,8 @@

use Aws\DynamoDb\DynamoDbClient;
use Aws\DynamoDb\Marshaler;
use Aws;
use TenUp\Snapshots\Exceptions\SnapshotsException;

/**
* Class for handling Amazon dynamodb calls
Expand Down Expand Up @@ -212,18 +214,34 @@ public function delete_snapshot( string $id, string $profile, string $repository
*/
private function get_client( string $profile, string $region ) : DynamoDbClient {
$client_key = $profile . '_' . $region;
$role_arn = $_ENV['role_arn'];

$args = [
'region' => $region,
'profile' => $profile,
'version' => '2012-08-10',
'csm' => false,
];

// Check if the necessary AWS env vars are set; if so, the profile arg is not needed.
// These are the same env vars the SDK checks for in CredentialProvider.php.
if ( getenv( 'AWS_ACCESS_KEY_ID' ) && getenv( 'AWS_SECRET_ACCESS_KEY' ) ) {
unset( $args['profile'] );
// if role_arn has a value use STS to assume the role
// and pass the credential info to DynamoDbClient later on
if ( $role_arn != "" ) {
$args['roleArn'] = $role_arn;

$temporaryCredentials = $this->assumeRole($args);

if ( ! is_array( $temporaryCredentials ) ) {
throw new SnapshotsException( sprintf( "Failed to assume role '%s'.", $args['roleArn'] ) );
}

$args['credentials'] = [
'key' => $temporaryCredentials['AccessKeyId'],
'secret' => $temporaryCredentials['SecretAccessKey'],
'token' => $temporaryCredentials['SessionToken']
];
}

if ( $role_arn == "" && $profile != "" ) {
$args['profile'] = $profile;
}

if ( ! isset( $this->clients[ $client_key ] ) ) {
Expand All @@ -232,4 +250,23 @@ private function get_client( string $profile, string $region ) : DynamoDbClient

return $this->clients[ $client_key ];
}

/**
* Performs STS
*
* @param string $role_arn AWS role_arn
*/
private function assumeRole( $connectionParameters ) : array {
$stsClient = new Aws\Sts\StsClient([
'region' => 'us-east-1',
'version' => '2011-06-15'
]);

$result = $stsClient->AssumeRole([
'RoleArn' => $connectionParameters['roleArn'],
'RoleSessionName' => "wpsnapshots",
]);

return $result['Credentials'];
}
}
50 changes: 43 additions & 7 deletions includes/classes/Snapshots/S3StorageConnector.php
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
use Aws\S3\S3Client;
use TenUp\Snapshots\Exceptions\SnapshotsException;
use TenUp\Snapshots\SnapshotsDirectory;
use Aws;

/**
* Class S3StorageConnector
Expand Down Expand Up @@ -232,19 +233,35 @@ public function test( string $profile, string $repository, string $region ) {
*/
private function get_client( string $profile, string $region ) : S3Client {
$client_key = $profile . '_' . $region;
$role_arn = $_ENV['role_arn'];

$args = [
'region' => $region,
'profile' => $profile,
'region' => $region,
'signature' => 'v4',
'version' => '2006-03-01',
'csm' => false,
'csm' => false,
];

// Check if the necessary AWS env vars are set; if so, the profile arg is not needed.
// These are the same env vars the SDK checks for in CredentialProvider.php.
if ( getenv( 'AWS_ACCESS_KEY_ID' ) && getenv( 'AWS_SECRET_ACCESS_KEY' ) ) {
unset( $args['profile'] );
// if role_arn has a value use STS to assume the role
// and pass the credential info to S3Client later on
if ( $role_arn != "" ) {
$args['roleArn'] = $role_arn;

$temporaryCredentials = $this->assumeRole($args);

if ( ! is_array( $temporaryCredentials ) ) {
throw new SnapshotsException( sprintf( "Failed to assume role '%s'.", $args['roleArn'] ) );
}

$args['credentials'] = [
'key' => $temporaryCredentials['AccessKeyId'],
'secret' => $temporaryCredentials['SecretAccessKey'],
'token' => $temporaryCredentials['SessionToken']
];
}

if ( $role_arn == "" && $profile != "" ) {
$args['profile'] = $profile;
}

if ( ! isset( $this->clients[ $client_key ] ) ) {
Expand All @@ -254,6 +271,25 @@ private function get_client( string $profile, string $region ) : S3Client {
return $this->clients[ $client_key ];
}

/**
* Performs STS
*
* @param string $role_arn AWS role_arn
*/
private function assumeRole( $connectionParameters ) : array {
$stsClient = new Aws\Sts\StsClient([
'region' => 'us-east-1',
'version' => '2011-06-15'
]);

$result = $stsClient->AssumeRole([
'RoleArn' => $connectionParameters['roleArn'],
'RoleSessionName' => "wpsnapshots",
]);

return $result['Credentials'];
}

/**
* Get bucket name
*
Expand Down
20 changes: 18 additions & 2 deletions includes/classes/SnapshotsConfig/SnapshotsConfigFromFileSystem.php
Original file line number Diff line number Diff line change
Expand Up @@ -130,10 +130,26 @@ public function get_repository_profile( string $repository = '' ) : string {
$settings = $this->get_repository_settings( $repository );

if ( is_array( $settings ) ) {
return $settings['profile'] ?? 'default';
return $settings['profile'] ?? '';
}

return 'default';
return '';
}

/**
* Gets the roleArn property from a repository. Defaults to ''.
*
* @param string $repository Repository name.
* @return string $profile Profile name.
*/
public function get_repository_role_arn( string $repository = '' ) : string {
$settings = $this->get_repository_settings( $repository );

if ( is_array( $settings ) ) {
return $settings['roleArn'] ?? '';
}

return '';
}

/**
Expand Down
8 changes: 8 additions & 0 deletions includes/classes/SnapshotsConfig/SnapshotsConfigInterface.php
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,14 @@ public function get_repository_region( string $repository ) : ?string;
*/
public function get_repository_profile( string $repository = '' ) : ?string;

/**
* Gets the roleArn property from a repository.
*
* @param string $repository Repository name.
* @return ?string $roleArn Role ARN string.
*/
public function get_repository_role_arn( string $repository = '' ) : ?string;

/**
* Gets repositories.
*
Expand Down
3 changes: 3 additions & 0 deletions includes/classes/WPCLI/WPCLICommand.php
Original file line number Diff line number Diff line change
Expand Up @@ -283,6 +283,9 @@ protected function get_profile_for_repository() : string {

$profile = $this->config->get_repository_profile( $repository_name );

// FIXME: hack to get the role_arn available later for demo purposes
$_ENV['role_arn'] = $this->config->get_repository_role_arn( $repository_name );

return $profile;
}

Expand Down