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; |
|
|
|
|
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)) |
|
|
|
|
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
|
|
|
|
In PHP it is possible to write to properties without declaring them. For example, the following is perfectly valid PHP code:
Generally, it is a good practice to explictly declare properties to avoid accidental typos and provide IDE auto-completion: