Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

IAM Role Name considred as Class in Yii 1

I have attached the policy to EC2 IAM role to access AWS services. After that i have have used below code in Yii 1 controller file:

ExampleController.php

class ExampleController extends Controller
{
    public function init()
    {
        require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
        $config = array(
                'version' => 'latest',
                'region' => 'us-west-2',
        );
        $s3_instance = new \Aws\Ssm\SsmClient($config);
        $result = $s3_instance->getParameters([
            'Names' => array('host_name'),
            'WithDecryption' => true
        ]);
        //converting S3 private data to array to read
        $keys = $result->toArray();
        var_dump($keys);
        exit("Exit");
    }
}

Output

PHP warning
include(TestRole.php): failed to open stream: No such file or directory

Note: TestRole is IAM Role Name.

I have used same code in Single PHP file (Not ties with Yii1)

test.php

require_once 'protected/extensions/awsv3/vendor/autoload.php';
$config = array(
        'version' => 'latest',
        'region' => 'us-west-2',
);
$s3_instance = new \Aws\Ssm\SsmClient($config);
$result = $s3_instance->getParameters([
    'Names' => array('host_name'),
    'WithDecryption' => true
]);
//converting S3 private data to array to read
$keys = $result->toArray();
var_dump($keys);
exit("Exit");

array(3) { ["Parameters"]=> array(1) { [0]=> array(3) { ["Name"]=> string(12) "host_name" ["Type"]=> string(6) "String" ["Value"]=> string(9) "localhost" } } ["InvalidParameters"]=> array(0) { } ["@metadata"]=> array(4) { ["statusCode"]=> int(200) ["effectiveUri"]=> string(35) "https://ssm.us-west-2.amazonaws.com" ["headers"]=> array(4) { ["x-amzn-requestid"]=> string(36) "d3fb85bc-da4e-494b-be4f-b31fe3814100" ["content-type"]=> string(26) "application/x-amz-json-1.1" ["content-length"]=> string(3) "182" ["date"]=> string(29) "Tue, 19 Jun 2018 12:28:50 GMT" } ["transferStats"]=> array(1) { ["http"]=> array(1) { [0]=> array(0) { } } } } } Exit

its working with single php file.

So question is how to fix it in Yii 1 and why its considering IAM Role Name as Class file?

Stack Trace

enter image description here

like image 236
DS9 Avatar asked Jun 19 '18 13:06

DS9


1 Answers

I was able to fix this out, Thanks to @javierfdezg.

So basically, Yii's auto loader and AWS's auto loader was got conflicted and may be due to Yii's assumption that class names must match file names.

So first i have unregistered the Yii's auto load then after the api call is finished registered it again.

class ExampleController extends Controller
{
    public function init()
    {
        /* Unregister  YiiBase */
        spl_autoload_unregister(array('YiiBase', 'autoload'));
        require_once dirname(dirname(__FILE__)) . '/extensions/awsv3/vendor/autoload.php';
        $config = array(
                'version' => 'latest',
                'region' => 'us-west-2',
        );
        $s3_instance = new \Aws\Ssm\SsmClient($config);
        $result = $s3_instance->getParameters([
            'Names' => array('host_name'),
            'WithDecryption' => true
        ]);

        /* Register  YiiBase */
        spl_autoload_register(array('YiiBase', 'autoload'));


        $keys = $result->toArray();
        var_dump($keys);
        exit("Exit");
    }
}
like image 115
DS9 Avatar answered Oct 01 '22 02:10

DS9