AwsHelper::getJSON()   A
last analyzed

Complexity

Conditions 1
Paths 1

Size

Total Lines 4
Code Lines 2

Duplication

Lines 0
Ratio 0 %

Importance

Changes 2
Bugs 0 Features 0
Metric Value
c 2
b 0
f 0
dl 0
loc 4
rs 10
cc 1
eloc 2
nc 1
nop 0
1
<?php namespace AwsHelper;
2
3
/* this class is designed to work on EC2 servers and in the office only.
4
If you need to access the security settings outside the office then please
5
add your own aws.json file to the storage folder with the needed security
6
access key and password */
7
8
class AwsHelper
9
{
10
    private $iam_role;
11
    private $iam_url;
12
13
    public function __construct($iam_role, $iam_url='http://169.254.169.254/latest/meta-data/iam/security-credentials/')
14
    {
15
16
        if (empty($iam_role))
17
            throw new \Exception('iam_role is empty');
18
19
        if (empty($iam_url))
20
            throw new \Exception('iam_url is empty');
21
22
        $this->iam_role = $iam_role;
23
        $this->iam_url  = $iam_url;
24
25
        $json = $this->getAccessData($iam_role, $iam_url);
26
27
        if ($json->Code !== 'Success')
28
            throw new \Exception('Unsuccessful in returning iam amazon credentials');
29
30
        if (empty($json->AccessKeyId))
31
            throw new \Exception('AccessKeyId does not exist.');
32
33
        if (empty($json->SecretAccessKey))
34
            throw new \Exception('SecretAccessKey does not exist.');
35
36
        $this->json = $json;
0 ignored issues
show
Bug introduced by
The property json does not exist. Did you maybe forget to declare it?

In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:

class MyClass { }

$x = new MyClass();
$x->foo = true;

Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion:

class MyClass {
    public $foo;
}

$x = new MyClass();
$x->foo = true;
Loading history...
37
    }
38
39
    public function getJSON()
40
    {
41
        return $this->json;
42
    }
43
44
    public function getDefaultOptions($aws_key=null, $aws_secret=null)
45
    {
46
        $opts = [
47
            'key'    => $aws_key,
48
            'secret' => $aws_secret,
49
        ];
50
51
        if (empty($aws_key) || empty($aws_secret))
52
        {
53
            $json = $this->getJSON();
54
55
            if ($json->Code !== 'Success')
56
                throw new \Exception('Unsuccessful in returning iam amazon credentials');
57
58
            if (empty($json->AccessKeyId))
59
                throw new \Exception('AccessKeyId does not exist. This is possibly a problem with you aws setup.');
60
61
            if (empty($json->SecretAccessKey))
62
                throw new \Exception('SecretAccessKey does not exist. This is possibly a problem with you aws setup.');
63
64
            if (!empty($json->Token))
65
            {
66
                // check token expiration
67
                if ($this->hasAccessExpired($json->Expiration))
68
                {
69
                    $this->json = $this->getAccessData($this->iam_role, $this->iam_url);
70
                    return $this->getDefaultOptions();
71
                }
72
            }
73
74
            $opts = [
75
                'key'    => $json->AccessKeyId,
76
                'secret' => $json->SecretAccessKey,
77
                'token'  => $json->Token,
78
            ];
79
        }
80
81
        return $opts;
82
    }
83
84
    private function getAccessData($iam_role, $iam_url)
85
    {
86
        /* grab the access and secret key from the iam role "aws-opsworks-ec2-role" */
87
        if (file_exists(storage_path().'/aws.json'))
88
        {
89
            $json = json_decode(file_get_contents(storage_path().'/aws.json'));
90
            if (property_exists($json, 'Expiration') and !$this->hasAccessExpired($json->Expiration))
0 ignored issues
show
Comprehensibility Best Practice introduced by
Using logical operators such as and instead of && is generally not recommended.

PHP has two types of connecting operators (logical operators, and boolean operators):

  Logical Operators Boolean Operator
AND - meaning and &&
OR - meaning or ||

The difference between these is the order in which they are executed. In most cases, you would want to use a boolean operator like &&, or ||.

Let’s take a look at a few examples:

// Logical operators have lower precedence:
$f = false or true;

// is executed like this:
($f = false) or true;


// Boolean operators have higher precedence:
$f = false || true;

// is executed like this:
$f = (false || true);

Logical Operators are used for Control-Flow

One case where you explicitly want to use logical operators is for control-flow such as this:

$x === 5
    or die('$x must be 5.');

// Instead of
if ($x !== 5) {
    die('$x must be 5.');
}

Since die introduces problems of its own, f.e. it makes our code hardly testable, and prevents any kind of more sophisticated error handling; you probably do not want to use this in real-world code. Unfortunately, logical operators cannot be combined with throw at this point:

// The following is currently a parse error.
$x === 5
    or throw new RuntimeException('$x must be 5.');

These limitations lead to logical operators rarely being of use in current PHP code.

Loading history...
91
            {
92
                return $json;
93
            }
94
        }
95
96
        // get json from the amazon iam service.
97
        $json = file_get_contents($iam_url.$iam_role);
98
        file_put_contents(storage_path().'/aws.json', $json);
99
100
        return json_decode($json);
101
    }
102
103
    public function hasAccessExpired($date)
104
    {
105
        $date = new \DateTime($date);
106
        if ($date->format('U')-date('U') > 0)
107
            return false;
108
109
        return true;
110
    }
111
}
112