1
|
|
|
<?php |
2
|
|
|
/** |
3
|
|
|
* This file is part of the Simple S3 package. |
4
|
|
|
* |
5
|
|
|
* (c) Mauro Cassani<https://github.com/mauretto78> |
6
|
|
|
* |
7
|
|
|
* For the full copyright and license information, please view the LICENSE |
8
|
|
|
* file that was distributed with this source code. |
9
|
|
|
* |
10
|
|
|
*/ |
11
|
|
|
|
12
|
|
|
namespace Matecat\SimpleS3; |
13
|
|
|
|
14
|
|
|
use Aws\Credentials\Credentials; |
15
|
|
|
use Aws\S3\S3Client; |
16
|
|
|
use Aws\Sts\StsClient; |
17
|
|
|
use InvalidArgumentException; |
18
|
|
|
|
19
|
|
|
/** |
20
|
|
|
* Class ClientFactory |
21
|
|
|
* |
22
|
|
|
* This class is a simple factory for S3/Client |
23
|
|
|
* |
24
|
|
|
* List of options: |
25
|
|
|
* - api_provider |
26
|
|
|
* - credentials |
27
|
|
|
* - debug |
28
|
|
|
* - stats |
29
|
|
|
* - endpoint |
30
|
|
|
* - endpoint_provider |
31
|
|
|
* - endpoint_discovery |
32
|
|
|
* - handler |
33
|
|
|
* - http |
34
|
|
|
* - http_handler |
35
|
|
|
* - profile |
36
|
|
|
* - region |
37
|
|
|
* - retries |
38
|
|
|
* - scheme |
39
|
|
|
* - service |
40
|
|
|
* - signature_provider |
41
|
|
|
* - signature_version |
42
|
|
|
* - ua_append |
43
|
|
|
* - validate |
44
|
|
|
* - version |
45
|
|
|
* |
46
|
|
|
* Please see the complete config documentation here: |
47
|
|
|
* |
48
|
|
|
* https://docs.aws.amazon.com/en_us/sdk-for-php/v3/developer-guide/guide_configuration.html |
49
|
|
|
* |
50
|
|
|
* @package SimpleS3 |
51
|
|
|
*/ |
52
|
|
|
final class ClientFactory |
53
|
|
|
{ |
54
|
|
|
/** |
55
|
|
|
* @param array $config |
56
|
|
|
* |
57
|
|
|
* @return S3Client |
58
|
|
|
*/ |
59
|
44 |
|
public static function create(array $config = []) |
60
|
|
|
{ |
61
|
44 |
|
self::validateConfig($config); |
62
|
|
|
|
63
|
43 |
|
return new S3Client(self::createConfigArray($config)); |
64
|
|
|
} |
65
|
|
|
|
66
|
|
|
/** |
67
|
|
|
* @param array $config |
68
|
|
|
* |
69
|
|
|
* @return array |
70
|
|
|
*/ |
71
|
43 |
|
private static function createConfigArray(array $config) |
72
|
|
|
{ |
73
|
43 |
|
$credentials = self::getCredentials($config); |
74
|
43 |
|
if (!empty($credentials)) { |
75
|
43 |
|
$config['credentials'] = new Credentials( |
76
|
43 |
|
$credentials['key'], |
77
|
43 |
|
$credentials['secret'], |
78
|
43 |
|
$credentials['token'] |
79
|
|
|
); |
80
|
|
|
} |
81
|
|
|
|
82
|
|
|
// Temp fix: suppressing PHP < 8.1 warnings |
83
|
43 |
|
$config['suppress_php_deprecation_warning'] = true; |
84
|
|
|
|
85
|
43 |
|
return $config; |
86
|
|
|
} |
87
|
|
|
|
88
|
|
|
/** |
89
|
|
|
* @param array $config |
90
|
|
|
*/ |
91
|
44 |
|
private static function validateConfig(array $config) |
92
|
|
|
{ |
93
|
|
|
$allowedKeys = [ |
94
|
44 |
|
'api_provider', |
95
|
|
|
'credentials', |
96
|
|
|
'debug', |
97
|
|
|
'endpoint', |
98
|
|
|
'endpoint_provider', |
99
|
|
|
'endpoint_discovery', |
100
|
|
|
'handler', |
101
|
|
|
'http', |
102
|
|
|
'http_handler', |
103
|
|
|
'iam', |
104
|
|
|
'profile', |
105
|
|
|
'region', |
106
|
|
|
'retries', |
107
|
|
|
'scheme', |
108
|
|
|
'service', |
109
|
|
|
'signature_provider', |
110
|
|
|
'signature_version', |
111
|
|
|
'stats', |
112
|
|
|
'ua_append', |
113
|
|
|
'validate', |
114
|
|
|
'version', |
115
|
|
|
]; |
116
|
|
|
|
117
|
44 |
|
foreach (array_keys($config) as $key) { |
118
|
44 |
|
if (!in_array($key, $allowedKeys)) { |
119
|
1 |
|
throw new InvalidArgumentException(sprintf('%s is not an allowed key', $key)); |
120
|
|
|
} |
121
|
|
|
} |
122
|
43 |
|
} |
123
|
|
|
|
124
|
|
|
/** |
125
|
|
|
* @param array $config |
126
|
|
|
* |
127
|
|
|
* @return array |
128
|
|
|
*/ |
129
|
43 |
|
private static function getCredentials(array $config) |
130
|
|
|
{ |
131
|
|
|
// 1. credentials |
132
|
43 |
|
if (isset($config['credentials']['key']) and isset($config['credentials']['secret'])) { |
133
|
|
|
return [ |
134
|
1 |
|
'key' => $config['credentials']['key'], |
135
|
1 |
|
'secret' => $config['credentials']['secret'], |
136
|
1 |
|
'token' => isset($config['credentials']['token']) ? $config['credentials']['token'] : null |
137
|
|
|
]; |
138
|
|
|
} |
139
|
|
|
|
140
|
|
|
// 2. IAM |
141
|
42 |
|
if (isset($config['iam'])) { |
142
|
|
|
$stsClient = new StsClient([ |
143
|
|
|
'profile' => (isset($config['profile'])) ? $config['profile'] : 'default', |
144
|
|
|
'region' => $config['region'], |
145
|
|
|
'version' => $config['version'] |
146
|
|
|
]); |
147
|
|
|
|
148
|
|
|
$result = $stsClient->assumeRole([ |
149
|
|
|
'RoleArn' => $config['iam']['arn'], |
150
|
|
|
'RoleSessionName' => $config['iam']['session'], |
151
|
|
|
]); |
152
|
|
|
|
153
|
|
|
return [ |
154
|
|
|
'key' => $result['Credentials']['AccessKeyId'], |
155
|
|
|
'secret' => $result['Credentials']['SecretAccessKey'], |
156
|
|
|
'token' => isset($result['Credentials']['SessionToken']) ? $result['Credentials']['SessionToken'] : null |
157
|
|
|
]; |
158
|
|
|
} |
159
|
|
|
|
160
|
|
|
// 3. env |
161
|
42 |
|
if (false !== getenv('AWS_ACCESS_KEY_ID') and false !== getenv('AWS_SECRET_ACCESS_KEY')) { |
162
|
|
|
return [ |
163
|
42 |
|
'key' => getenv('AWS_ACCESS_KEY_ID'), |
164
|
42 |
|
'secret' => getenv('AWS_SECRET_ACCESS_KEY'), |
165
|
42 |
|
'token' => (false !== getenv('AWS_SESSION_TOKEN')) ? getenv('AWS_SESSION_TOKEN') : null |
166
|
|
|
]; |
167
|
|
|
} |
168
|
|
|
|
169
|
|
|
return []; |
170
|
|
|
} |
171
|
|
|
} |
172
|
|
|
|