18
18
*/
19
19
class IniFileProvider implements CredentialProvider
20
20
{
21
- private const KEY_ACCESS_KEY_ID = 'aws_access_key_id ' ;
22
- private const KEY_SECRET_ACCESS_KEY = 'aws_secret_access_key ' ;
23
- private const KEY_SESSION_TOKEN = 'aws_session_token ' ;
24
- private const KEY_ROLE_ARN = 'role_arn ' ;
25
- private const KEY_ROLE_SESSION_NAME = 'role_session_name ' ;
26
- private const KEY_SOURCE_PROFILE = 'source_profile ' ;
27
-
28
- /**
29
- * @var LoggerInterface
30
- */
21
+ private $ iniFileLoader ;
22
+
31
23
private $ logger ;
32
24
33
- public function __construct (?LoggerInterface $ logger = null )
25
+ public function __construct (?LoggerInterface $ logger = null , ? IniFileLoader $ iniFileLoader = null )
34
26
{
35
27
$ this ->logger = $ logger ?? new NullLogger ();
28
+ $ this ->iniFileLoader = $ iniFileLoader ?? new IniFileLoader ($ this ->logger );
36
29
}
37
30
38
31
public function getCredentials (Configuration $ configuration ): ?Credentials
39
32
{
40
- $ profilesData = $ this ->loadProfiles ([
33
+ $ profilesData = $ this ->iniFileLoader -> loadProfiles ([
41
34
$ configuration ->get (Configuration::OPTION_SHARED_CREDENTIALS_FILE ),
42
35
$ configuration ->get (Configuration::OPTION_SHARED_CONFIG_FILE ),
43
36
]);
@@ -67,15 +60,15 @@ private function getCredentialsFromProfile(array $profilesData, string $profile,
67
60
}
68
61
69
62
$ profileData = $ profilesData [$ profile ];
70
- if (isset ($ profileData [self ::KEY_ACCESS_KEY_ID ], $ profileData [self ::KEY_ACCESS_KEY_ID ])) {
63
+ if (isset ($ profileData [IniFileLoader ::KEY_ACCESS_KEY_ID ], $ profileData [IniFileLoader ::KEY_ACCESS_KEY_ID ])) {
71
64
return new Credentials (
72
- $ profileData [self ::KEY_ACCESS_KEY_ID ],
73
- $ profileData [self ::KEY_SECRET_ACCESS_KEY ],
74
- $ profileData [self ::KEY_SESSION_TOKEN ] ?? null
65
+ $ profileData [IniFileLoader ::KEY_ACCESS_KEY_ID ],
66
+ $ profileData [IniFileLoader ::KEY_SECRET_ACCESS_KEY ],
67
+ $ profileData [IniFileLoader ::KEY_SESSION_TOKEN ] ?? null
75
68
);
76
69
}
77
70
78
- if (isset ($ profileData [self ::KEY_ROLE_ARN ])) {
71
+ if (isset ($ profileData [IniFileLoader ::KEY_ROLE_ARN ])) {
79
72
return $ this ->getCredentialsFromRole ($ profilesData , $ profileData , $ profile , $ circularCollector );
80
73
}
81
74
@@ -86,9 +79,9 @@ private function getCredentialsFromProfile(array $profilesData, string $profile,
86
79
87
80
private function getCredentialsFromRole (array $ profilesData , array $ profileData , string $ profile , array $ circularCollector = []): ?Credentials
88
81
{
89
- $ roleArn = (string ) ($ profileData [self ::KEY_ROLE_ARN ] ?? '' );
90
- $ roleSessionName = (string ) ($ profileData [self ::KEY_ROLE_SESSION_NAME ] ?? \uniqid ('async-aws- ' , true ));
91
- if (null === $ sourceProfileName = $ profileData [self ::KEY_SOURCE_PROFILE ] ?? null ) {
82
+ $ roleArn = (string ) ($ profileData [IniFileLoader ::KEY_ROLE_ARN ] ?? '' );
83
+ $ roleSessionName = (string ) ($ profileData [IniFileLoader ::KEY_ROLE_SESSION_NAME ] ?? \uniqid ('async-aws- ' , true ));
84
+ if (null === $ sourceProfileName = $ profileData [IniFileLoader ::KEY_SOURCE_PROFILE ] ?? null ) {
92
85
$ this ->logger ->warning ('The source profile is not defined in Role "{profile}". ' , ['profile ' => $ profile ]);
93
86
94
87
return null ;
@@ -102,8 +95,7 @@ private function getCredentialsFromRole(array $profilesData, array $profileData,
102
95
return null ;
103
96
}
104
97
105
- $ stsClient = new StsClient (isset ($ profilesData [$ sourceProfileName ]['region ' ]) ? ['region ' => $ profilesData [$ sourceProfileName ]['region ' ]] : [], $ sourceCredentials );
106
-
98
+ $ stsClient = new StsClient (isset ($ profilesData [$ sourceProfileName ][IniFileLoader::KEY_REGION ]) ? ['region ' => $ profilesData [$ sourceProfileName ][IniFileLoader::KEY_REGION ]] : [], $ sourceCredentials );
107
99
$ result = $ stsClient ->assumeRole ([
108
100
'RoleArn ' => $ roleArn ,
109
101
'RoleSessionName ' => $ roleSessionName ,
@@ -126,60 +118,4 @@ private function getCredentialsFromRole(array $profilesData, array $profileData,
126
118
$ credentials ->getExpiration ()
127
119
);
128
120
}
129
-
130
- private function getHomeDir (): string
131
- {
132
- // On Linux/Unix-like systems, use the HOME environment variable
133
- if (false !== $ homeDir = \getenv ('HOME ' )) {
134
- return $ homeDir ;
135
- }
136
-
137
- // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
138
- $ homeDrive = \getenv ('HOMEDRIVE ' );
139
- $ homePath = \getenv ('HOMEPATH ' );
140
-
141
- return ($ homeDrive && $ homePath ) ? $ homeDrive . $ homePath : '/ ' ;
142
- }
143
-
144
- private function loadProfiles (array $ filepaths ): array
145
- {
146
- $ profilesData = [];
147
- $ homeDir = null ;
148
- foreach ($ filepaths as $ filepath ) {
149
- if ('' === $ filepath ) {
150
- continue ;
151
- }
152
- if ('~ ' === $ filepath [0 ]) {
153
- $ homeDir = $ homeDir ?? $ this ->getHomeDir ();
154
- $ filepath = $ homeDir . \substr ($ filepath , 1 );
155
- }
156
- if (!\is_readable ($ filepath )) {
157
- continue ;
158
- }
159
-
160
- foreach ($ this ->parseIniFile ($ filepath ) as $ name => $ profile ) {
161
- $ name = \preg_replace ('/^profile / ' , '' , $ name );
162
- if (!isset ($ profilesData [$ name ])) {
163
- $ profilesData [$ name ] = \array_map ('trim ' , $ profile );
164
- }
165
- }
166
- }
167
-
168
- return $ profilesData ;
169
- }
170
-
171
- private function parseIniFile (string $ filepath ): array
172
- {
173
- if (false === $ data = \parse_ini_string (
174
- \preg_replace ('/^#/m ' , '; ' , \file_get_contents ($ filepath )),
175
- true ,
176
- \INI_SCANNER_RAW
177
- )) {
178
- $ this ->logger ->warning ('The ini file {path} is invalid. ' , ['path ' => $ filepath ]);
179
-
180
- return [];
181
- }
182
-
183
- return $ data ;
184
- }
185
121
}
0 commit comments