Proyectos de Subversion Moodle

Rev

| Ultima modificación | Ver Log |

Rev Autor Línea Nro. Línea
1 efrain 1
<?php
2
namespace Aws\Sts\RegionalEndpoints;
3
 
4
use Aws\AbstractConfigurationProvider;
5
use Aws\CacheInterface;
6
use Aws\ConfigurationProviderInterface;
7
use Aws\Sts\RegionalEndpoints\Exception\ConfigurationException;
8
use GuzzleHttp\Promise;
9
use GuzzleHttp\Promise\PromiseInterface;
10
 
11
/**
12
 * A configuration provider is a function that returns a promise that is
13
 * fulfilled with a {@see \Aws\Sts\RegionalEndpoints\ConfigurationInterface}
14
 * or rejected with an {@see \Aws\Sts\RegionalEndpoints\Exception\ConfigurationException}.
15
 *
16
 * <code>
17
 * use Aws\Sts\RegionalEndpoints\ConfigurationProvider;
18
 * $provider = ConfigurationProvider::defaultProvider();
19
 * // Returns a ConfigurationInterface or throws.
20
 * $config = $provider()->wait();
21
 * </code>
22
 *
23
 * Configuration providers can be composed to create configuration using
24
 * conditional logic that can create different configurations in different
25
 * environments. You can compose multiple providers into a single provider using
26
 * {@see \Aws\Sts\RegionalEndpoints\ConfigurationProvider::chain}. This function
27
 * accepts providers as variadic arguments and returns a new function that will
28
 * invoke each provider until a successful configuration is returned.
29
 *
30
 * <code>
31
 * // First try an INI file at this location.
32
 * $a = ConfigurationProvider::ini(null, '/path/to/file.ini');
33
 * // Then try an INI file at this location.
34
 * $b = ConfigurationProvider::ini(null, '/path/to/other-file.ini');
35
 * // Then try loading from environment variables.
36
 * $c = ConfigurationProvider::env();
37
 * // Combine the three providers together.
38
 * $composed = ConfigurationProvider::chain($a, $b, $c);
39
 * // Returns a promise that is fulfilled with a configuration or throws.
40
 * $promise = $composed();
41
 * // Wait on the configuration to resolve.
42
 * $config = $promise->wait();
43
 * </code>
44
 */
45
class ConfigurationProvider extends AbstractConfigurationProvider
46
    implements ConfigurationProviderInterface
47
{
48
    const DEFAULT_ENDPOINTS_TYPE = 'legacy';
49
    const ENV_ENDPOINTS_TYPE = 'AWS_STS_REGIONAL_ENDPOINTS';
50
    const ENV_PROFILE = 'AWS_PROFILE';
51
    const INI_ENDPOINTS_TYPE = 'sts_regional_endpoints';
52
 
53
    public static $cacheKey = 'aws_sts_regional_endpoints_config';
54
 
55
    protected static $interfaceClass = ConfigurationInterface::class;
56
    protected static $exceptionClass = ConfigurationException::class;
57
 
58
    /**
59
     * Create a default config provider that first checks for environment
60
     * variables, then checks for a specified profile in the environment-defined
61
     * config file location (env variable is 'AWS_CONFIG_FILE', file location
62
     * defaults to ~/.aws/config), then checks for the "default" profile in the
63
     * environment-defined config file location, and failing those uses a default
64
     * fallback set of configuration options.
65
     *
66
     * This provider is automatically wrapped in a memoize function that caches
67
     * previously provided config options.
68
     *
69
     * @param array $config
70
     *
71
     * @return callable
72
     */
73
    public static function defaultProvider(array $config = [])
74
    {
75
        $configProviders = [self::env()];
76
        if (
77
            !isset($config['use_aws_shared_config_files'])
78
            || $config['use_aws_shared_config_files'] != false
79
        ) {
80
            $configProviders[] = self::ini();
81
        }
82
        $configProviders[] = self::fallback();
83
 
84
        $memo = self::memoize(
85
            call_user_func_array([ConfigurationProvider::class, 'chain'], $configProviders)
86
        );
87
 
88
        if (isset($config['sts_regional_endpoints'])
89
            && $config['sts_regional_endpoints'] instanceof CacheInterface
90
        ) {
91
            return self::cache($memo, $config['sts_regional_endpoints'], self::$cacheKey);
92
        }
93
 
94
        return $memo;
95
    }
96
 
97
    /**
98
     * Provider that creates config from environment variables.
99
     *
100
     * @return callable
101
     */
102
    public static function env()
103
    {
104
        return function () {
105
            // Use config from environment variables, if available
106
            $endpointsType = getenv(self::ENV_ENDPOINTS_TYPE);
107
            if (!empty($endpointsType)) {
108
                return Promise\Create::promiseFor(
109
                    new Configuration($endpointsType)
110
                );
111
            }
112
 
113
            return self::reject('Could not find environment variable config'
114
                . ' in ' . self::ENV_ENDPOINTS_TYPE);
115
        };
116
    }
117
 
118
    /**
119
     * Fallback config options when other sources are not set.
120
     *
121
     * @return callable
122
     */
123
    public static function fallback()
124
    {
125
        return function () {
126
            return Promise\Create::promiseFor(
127
                new Configuration(self::DEFAULT_ENDPOINTS_TYPE, true)
128
            );
129
        };
130
    }
131
 
132
    /**
133
     * Config provider that creates config using a config file whose location
134
     * is specified by an environment variable 'AWS_CONFIG_FILE', defaulting to
135
     * ~/.aws/config if not specified
136
     *
137
     * @param string|null $profile  Profile to use. If not specified will use
138
     *                              the "default" profile.
139
     * @param string|null $filename If provided, uses a custom filename rather
140
     *                              than looking in the default directory.
141
     *
142
     * @return callable
143
     */
144
    public static function ini(
145
        $profile = null,
146
        $filename = null
147
    ) {
148
        $filename = $filename ?: (self::getDefaultConfigFilename());
149
        $profile = $profile ?: (getenv(self::ENV_PROFILE) ?: 'default');
150
 
151
        return function () use ($profile, $filename) {
152
            if (!@is_readable($filename)) {
153
                return self::reject("Cannot read configuration from $filename");
154
            }
155
            $data = \Aws\parse_ini_file($filename, true);
156
            if ($data === false) {
157
                return self::reject("Invalid config file: $filename");
158
            }
159
            if (!isset($data[$profile])) {
160
                return self::reject("'$profile' not found in config file");
161
            }
162
            if (!isset($data[$profile][self::INI_ENDPOINTS_TYPE])) {
163
                return self::reject("Required STS regional endpoints config values
164
                    not present in INI profile '{$profile}' ({$filename})");
165
            }
166
 
167
            return Promise\Create::promiseFor(
168
                new Configuration($data[$profile][self::INI_ENDPOINTS_TYPE])
169
            );
170
        };
171
    }
172
 
173
    /**
174
     * Unwraps a configuration object in whatever valid form it is in,
175
     * always returning a ConfigurationInterface object.
176
     *
177
     * @param  mixed $config
178
     * @return ConfigurationInterface
179
     * @throws \InvalidArgumentException
180
     */
181
    public static function unwrap($config)
182
    {
183
        if (is_callable($config)) {
184
            $config = $config();
185
        }
186
        if ($config instanceof PromiseInterface) {
187
            $config = $config->wait();
188
        }
189
        if ($config instanceof ConfigurationInterface) {
190
            return $config;
191
        }
192
        if (is_string($config)) {
193
            return new Configuration($config);
194
        }
195
        if (is_array($config) && isset($config['endpoints_type'])) {
196
            return new Configuration($config['endpoints_type']);
197
        }
198
 
199
        throw new \InvalidArgumentException('Not a valid STS regional endpoints '
200
            . 'configuration argument.');
201
    }
202
}