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

Support IAM Assume Role as access method #48

Merged
merged 1 commit into from
Jul 13, 2020
Merged
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
28 changes: 28 additions & 0 deletions library/Aws/AssumeRole.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
<?php

namespace Icinga\Module\Aws;

class AssumeRole
{
protected $arn;

protected $session;

public static function create($arn, $session)
{
$assumeRole = new static();

$assumeRole->arn = $arn;
$assumeRole->session = $session;

return $assumeRole;
}

public function getParams()
{
return [
'RoleArn' => $this->arn,
'RoleSessionName' => $this->session
];
}
}
14 changes: 13 additions & 1 deletion library/Aws/AwsClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,11 @@
namespace Icinga\Module\Aws;

use Aws\Api\DateTimeResult;
use Aws\Credentials\AssumeRoleCredentialProvider;
use Aws\Credentials\CredentialProvider;
use Aws\Credentials\InstanceProfileProvider;
use Aws\Sdk;
use Aws\Sts\StsClient;
use Icinga\Application\Config;

class AwsClient
Expand All @@ -17,7 +21,7 @@ class AwsClient
*/
protected $sdk;

public function __construct(AwsKey $key = null, $region)
public function __construct($key = null, $region)
{
$this->region = $region;
$this->key = $key;
Expand Down Expand Up @@ -265,6 +269,14 @@ protected function initializeSdk()

if ($this->key instanceof AwsKey) {
$params['credentials'] = $this->key->getCredentials();
} else if ($this->key instanceof AssumeRole) {
$assumeRoleCredentials = new AssumeRoleCredentialProvider([
'client' => new StsClient($params + [
'credentials' => new InstanceProfileProvider()
]),
'assume_role_params' => $this->key->getParams()
]);
$params['credentials'] = CredentialProvider::memoize($assumeRoleCredentials);
}

$config = Config::module('aws');
Expand Down
24 changes: 21 additions & 3 deletions library/Aws/ProvidedHook/Director/ImportSource.php
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

namespace Icinga\Module\Aws\ProvidedHook\Director;

use Icinga\Module\Aws\AssumeRole;
use Icinga\Module\Director\Forms\ImportSourceForm;
use Icinga\Module\Director\Hook\ImportSourceHook;
use Icinga\Module\Director\Web\Form\QuickForm;
use Icinga\Module\Aws\AwsClient;
Expand All @@ -25,7 +27,11 @@ public function fetchData()
$key = null;

if ($keyName) {
$key = AwsKey::loadByName($keyName);
if ($keyName === 'IAM assume role') {
$key = AssumeRole::create($this->getSetting('iam_assume_role'), 'director');
} else {
$key = AwsKey::loadByName($keyName);
}
}

$client = new AwsClient($key, $this->getSetting('aws_region'));
Expand Down Expand Up @@ -158,15 +164,27 @@ public static function addSettingsFormFields(QuickForm $form)
'label' => 'AWS access method',
'required' => false,
'description' => $form->translate(
'Use IAM role credential or select your AWS key. This shows all keys from your keys.ini.'
'Use IAM role credential, assume role or select your AWS key.'
. ' This shows all keys from your keys.ini.'
. ' Please check the documentation if you miss the keys in the list.'
),
'multiOptions' => $form->optionalEnum(AwsKey::enumKeyNames(), $form->translate(
'multiOptions' => $form->optionalEnum(
AwsKey::enumKeyNames()
+ ['IAM assume role' => $form->translate('IAM assume role')],
$form->translate(
'IAM role credentials'
)),
'class' => 'autosubmit',
));

/** @var ImportSourceForm $form */
if ($form->getSentOrObjectSetting('aws_access_key') === 'IAM assume role') {
$form->addElement('text', 'iam_assume_role', [
'label' => 'Assume role',
'required' => true
]);
}

$form->addElement('select', 'object_type', array(
'label' => 'Object type',
'required' => true,
Expand Down