summaryrefslogtreecommitdiff
path: root/vendor/aws/aws-sdk-php/src/Configuration/ConfigurationResolver.php
blob: a08595a75d3e96c5624f67caa6b406686126e1b5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
<?php

namespace Aws\Configuration;

class ConfigurationResolver
{
    const ENV_PROFILE = 'AWS_PROFILE';
    const ENV_CONFIG_FILE = 'AWS_CONFIG_FILE';

    public static $envPrefix = 'AWS_';

    /**
     * Generic configuration resolver that first checks for environment
     * variables, then checks for a specified profile in the environment-defined
     * config file location (env variable is 'AWS_CONFIG_FILE', file location
     * defaults to ~/.aws/config), then checks for the "default" profile in the
     * environment-defined config file location, and failing those uses a default
     * fallback value.
     *
     * @param string $key      Configuration key to be used when attempting
     *                         to retrieve value from the environment or ini file.
     * @param mixed $defaultValue
     * @param string $expectedType  The expected type of the retrieved value.
     * @param array $config additional configuration options.
     *
     * @return mixed
     */
    public static function resolve(
        $key,
        $defaultValue,
        $expectedType,
        $config = []
    )
    {
        $iniOptions = isset($config['ini_resolver_options'])
            ? $config['ini_resolver_options']
            : [];

        $envValue = self::env($key, $expectedType);
        if (!is_null($envValue)) {
            return $envValue;
        }

        if (!isset($config['use_aws_shared_config_files'])
            || $config['use_aws_shared_config_files'] != false
        ) {
            $iniValue = self::ini(
                $key,
                $expectedType,
                null,
                null,
                $iniOptions
            );
            if(!is_null($iniValue)) {
                return $iniValue;
            }
        }

        return $defaultValue;
    }

    /**
     * Resolves config values from environment variables.
     *
     * @param string $key      Configuration key to be used when attempting
     *                         to retrieve value from the environment.
     * @param string $expectedType  The expected type of the retrieved value.
     *
     * @return null | mixed
     */
    public static function env($key, $expectedType)
    {
        // Use config from environment variables, if available
        $envValue = getenv(self::$envPrefix . strtoupper($key));
        if (!empty($envValue)) {
            if ($expectedType) {
                $envValue = self::convertType($envValue, $expectedType);
            }
            return $envValue;
        }

        return null;
    }

    /**
     * Gets config values from a config file whose location
     * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
     * ~/.aws/config if not specified
     *
     *
     * @param string $key      Configuration key to be used when attempting
     *                         to retrieve value from ini file.
     * @param string $expectedType  The expected type of the retrieved value.
     * @param string|null $profile  Profile to use. If not specified will use
     *                              the "default" profile.
     * @param string|null $filename If provided, uses a custom filename rather
     *                              than looking in the default directory.
     *
     * @return null | mixed
     */
    public static function ini(
        $key,
        $expectedType,
        $profile = null,
        $filename = null,
        $options = []
    ){
        $filename = $filename ?: (self::getDefaultConfigFilename());
        $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');

        if (!@is_readable($filename)) {
            return null;
        }
        // Use INI_SCANNER_NORMAL instead of INI_SCANNER_TYPED for PHP 5.5 compatibility
        //TODO change after deprecation
        $data = @\Aws\parse_ini_file($filename, true, INI_SCANNER_NORMAL);

        if (isset($options['section'])
            && isset($options['subsection'])
            && isset($options['key']))
        {
            return self::retrieveValueFromIniSubsection(
                $data,
                $profile,
                $filename,
                $expectedType,
                $options
            );
        }

        if ($data === false
            || !isset($data[$profile])
            || !isset($data[$profile][$key])
        ) {
            return null;
        }

        // INI_SCANNER_NORMAL parses false-y values as an empty string
        if ($data[$profile][$key] === "") {
            if ($expectedType === 'bool') {
                $data[$profile][$key] = false;
            } elseif ($expectedType === 'int') {
                $data[$profile][$key] = 0;
            }
        }

        return self::convertType($data[$profile][$key], $expectedType);
    }

    /**
     * Gets the environment's HOME directory if available.
     *
     * @return null | string
     */
    private static function getHomeDir()
    {
        // On Linux/Unix-like systems, use the HOME environment variable
        if ($homeDir = getenv('HOME')) {
            return $homeDir;
        }

        // Get the HOMEDRIVE and HOMEPATH values for Windows hosts
        $homeDrive = getenv('HOMEDRIVE');
        $homePath = getenv('HOMEPATH');

        return ($homeDrive && $homePath) ? $homeDrive . $homePath : null;
    }

    /**
     * Gets default config file location from environment, falling back to aws
     * default location
     *
     * @return string
     */
    private static function getDefaultConfigFilename()
    {
        if ($filename = getenv(self::ENV_CONFIG_FILE)) {
            return $filename;
        }
        return self::getHomeDir() . '/.aws/config';
    }

    /**
     * Normalizes string values pulled out of ini files and
     * environment variables.
     *
     * @param string $value The value retrieved from the environment or
     *                      ini file.
     * @param $type $string The type that the value needs to be converted to.
     *
     * @return mixed
     */
    private static function convertType($value, $type)
    {
        if ($type === 'bool'
            && !is_null($convertedValue = \Aws\boolean_value($value))
        ) {
            return $convertedValue;
        }

        if ($type === 'int'
            && filter_var($value, FILTER_VALIDATE_INT)
        ) {
            $value = intVal($value);
        }
        return $value;
    }

    /**
     * Normalizes string values pulled out of ini files and
     * environment variables.
     *
     * @param array $data The data retrieved the ini file
     * @param string $profile The specified ini profile
     * @param string $filename The full path to the ini file
     * @param array $options Additional arguments passed to the configuration resolver
     *
     * @return mixed
     */
    private static function retrieveValueFromIniSubsection(
        $data,
        $profile,
        $filename,
        $expectedType,
        $options
    ){
        $section = $options['section'];
        if ($data === false
            || !isset($data[$profile][$section])
            || !isset($data["{$section} {$data[$profile][$section]}"])
        ) {
            return null;
        }

        $services_section = \Aws\parse_ini_section_with_subsections(
            $filename,
            "services {$data[$profile]['services']}"
        );

        if (!isset($services_section[$options['subsection']][$options['key']])
        ) {
            return null;
        }

        return self::convertType(
            $services_section[$options['subsection']][$options['key']],
            $expectedType
        );
    }
}