Skip to content

Commit 31c7a1a

Browse files
committed
adding "noSSL" config key and env var to enable http vs https determination
1 parent 08ef9ed commit 31c7a1a

11 files changed

+195
-80
lines changed

composer.json

+2
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
],
1212
"require": {
1313
"php": ">=7.1",
14+
"ext-json": "*",
15+
"ext-curl": "*",
1416
"guzzlehttp/guzzle": "6.3.*",
1517
"psr/log": "1.*"
1618
},

files/constants.php

+1
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626

2727
// Attempt to load config from ENV if not otherwise defined
2828
const ENV_RGW_API_HTTP_ADDR = 'RGW_API_HTTP_ADDR';
29+
const ENV_RGW_API_NO_SSL = 'RGW_API_NO_SSL';
2930
const ENV_RGW_API_ADMIN_PATH = 'RGW_API_ADMIN_PATH';
3031
const ENV_RGW_API_KEY = 'RGW_API_KEY';
3132
const ENV_RGW_API_SECRET = 'RGW_API_SECRET';

files/funcs.php

+40
Original file line numberDiff line numberDiff line change
@@ -282,4 +282,44 @@ function tryGetEnvParam(string $param, string $default = ''): string
282282
} else {
283283
return $default;
284284
}
285+
}
286+
287+
/**
288+
* @param string $param
289+
* @param string $default
290+
* @return bool
291+
*/
292+
function tryGetBoolEnvParam(string $param, string $default = ''): bool
293+
{
294+
$p = tryGetEnvParam($param, $default);
295+
if ('' === $p) {
296+
return false;
297+
}
298+
299+
$p = strtolower($p);
300+
if ('true' === $p) {
301+
return true;
302+
}
303+
if ('false' === $p) {
304+
return false;
305+
}
306+
if (ctype_digit($p)) {
307+
return 0 < (int)$p;
308+
}
309+
310+
return false;
311+
}
312+
313+
/**
314+
* @param string $param
315+
* @param string $default
316+
* @return int
317+
*/
318+
function tryGetIntEnvParam(string $param, string $default = ''): int
319+
{
320+
$p = tryGetEnvParam($param, $default);
321+
if (ctype_digit($p)) {
322+
return (int)$p;
323+
}
324+
return 0;
285325
}

src/Client.php

+3-1
Original file line numberDiff line numberDiff line change
@@ -140,6 +140,7 @@ private function compilePSR7(Request $r): RequestInterface
140140
}
141141
}
142142
$uri = $uri->withQuery(implode('&', $params));
143+
$this->config->getLogger()->debug(sprintf('Compiled URL: %s', $uri));
143144
}
144145
$psrRequest = new PSR7Request($r->method(), $uri, $r->headers(), $r->body());
145146
if ($r->authenticated()) {
@@ -155,7 +156,8 @@ private function serviceAddress(): string
155156
{
156157
if (!isset($this->address)) {
157158
$this->address = sprintf(
158-
'https://%s/%s',
159+
'%s://%s/%s',
160+
$this->config->isNoSSL() ? 'http' : 'https',
159161
$this->config->getAddress(),
160162
$this->config->getAdminPath()
161163
);

src/Config.php

+71-16
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,15 @@ class Config implements LoggerAwareInterface
2222
const DEFAULT_CLIENT_TIMEOUT = 10;
2323
const DEFAULT_SKIP_SSL_VERIFY = false;
2424

25+
const FIELD_CLIENT_TIMEOUT = 'clientTimeout';
26+
const FIELD_ADDRESS = 'address';
27+
const FIELD_ADMIN_PATH = 'adminPath';
28+
const FIELD_API_KEY = 'apiKey';
29+
const FIELD_API_SECRET = 'apiSecret';
30+
const FIELD_NO_SSL = 'noSSL';
31+
const FIELD_HTTP_CLIENT = 'httpClient';
32+
const FIELD_SILENT = 'silent';
33+
2534
/** @var int */
2635
private $clientTimeout = self::DEFAULT_CLIENT_TIMEOUT;
2736
/** @var string */
@@ -32,6 +41,8 @@ class Config implements LoggerAwareInterface
3241
private $apiKey = '';
3342
/** @var string */
3443
private $apiSecret = '';
44+
/** @var bool */
45+
private $noSSL = false;
3546

3647
/** @var \GuzzleHttp\ClientInterface */
3748
private $httpClient;
@@ -52,12 +63,29 @@ public function __construct(array $config = [], ClientInterface $httpClient = nu
5263
$logger = new NullLogger();
5364
}
5465
$this->setLogger($logger);
55-
foreach ($config as $k => $v) {
56-
$k = sanitizeName($k, '_', true);
57-
if ('httpClient' === $k) {
58-
$httpClient = $v;
66+
foreach ($config + self::getConfigFromEnvironment() as $rawK => $v) {
67+
$k = sanitizeName($rawK, '_', true);
68+
if (self::FIELD_CLIENT_TIMEOUT === $k) {
69+
$this->setClientTimeout($config[self::FIELD_CLIENT_TIMEOUT]);
70+
} elseif (self::FIELD_ADDRESS === $k) {
71+
$this->setAddress($config[self::FIELD_ADDRESS]);
72+
} elseif (self::FIELD_ADMIN_PATH === $k) {
73+
$this->setAdminPath($config[self::FIELD_ADMIN_PATH]);
74+
} elseif (self::FIELD_API_KEY === $k) {
75+
$this->setApiKey($config[self::FIELD_API_KEY]);
76+
} elseif (self::FIELD_API_SECRET === $k) {
77+
$this->setApiSecret($config[self::FIELD_API_SECRET]);
78+
} elseif (self::FIELD_NO_SSL === $k) {
79+
$this->setNoSSL($config[self::FIELD_NO_SSL]);
80+
} elseif (self::FIELD_HTTP_CLIENT === $k) {
81+
$httpClient = $config[self::FIELD_HTTP_CLIENT];
82+
} elseif (self::FIELD_SILENT === $k) {
83+
$this->setSilent($config[self::FIELD_SILENT]);
5984
} else {
60-
$this->{'set' . ucfirst($k)}($v);
85+
throw new \OutOfBoundsException(sprintf(
86+
'Unknown configuration key "%s" seen.',
87+
$rawK
88+
));
6189
}
6290
}
6391
if (null === $httpClient) {
@@ -77,10 +105,11 @@ public static function getEnvironmentConfig(): array
77105
$ret = [];
78106
foreach ([
79107
ENV_RGW_API_HTTP_ADDR => tryGetEnvParam(ENV_RGW_API_HTTP_ADDR, '127.0.0.1'),
108+
ENV_RGW_API_NO_SSL => tryGetBoolEnvParam(ENV_RGW_API_NO_SSL, '0'),
80109
ENV_RGW_API_ADMIN_PATH => tryGetEnvParam(ENV_RGW_API_ADMIN_PATH, 'admin'),
81110
ENV_RGW_API_KEY => tryGetEnvParam(ENV_RGW_API_KEY),
82111
ENV_RGW_API_SECRET => tryGetEnvParam(ENV_RGW_API_SECRET),
83-
ENV_RGW_LOG_SILENT => tryGetEnvParam(ENV_RGW_LOG_SILENT, '0'),
112+
ENV_RGW_LOG_SILENT => tryGetBoolEnvParam(ENV_RGW_LOG_SILENT, '0'),
84113
] as $k => $v) {
85114
if ('' !== $v) {
86115
$ret[$k] = $v;
@@ -90,27 +119,37 @@ public static function getEnvironmentConfig(): array
90119
}
91120

92121
/**
93-
* @param \GuzzleHttp\Client|null $client
94-
* @param null|\Psr\Log\LoggerInterface $logger
95-
* @return \MyENA\RGW\Config
122+
* @return array
96123
*/
97-
public static function defaultConfig(?GuzzleClient $client = null, ?LoggerInterface $logger = null): Config
124+
public static function getConfigFromEnvironment(): array
98125
{
99126
$conf = [];
100127
foreach (self::getEnvironmentConfig() as $k => $v) {
101128
if (ENV_RGW_API_HTTP_ADDR === $k) {
102-
$conf['address'] = $v;
129+
$conf[self::FIELD_ADDRESS] = $v;
130+
} elseif (ENV_RGW_API_NO_SSL === $k) {
131+
$conf[self::FIELD_NO_SSL] = $v;
103132
} elseif (ENV_RGW_API_ADMIN_PATH === $k) {
104-
$conf['adminPath'] = $v;
133+
$conf[self::FIELD_ADMIN_PATH] = $v;
105134
} elseif (ENV_RGW_API_KEY === $k) {
106-
$conf['apiKey'] = $v;
135+
$conf[self::FIELD_API_KEY] = $v;
107136
} elseif (ENV_RGW_API_SECRET === $k) {
108-
$conf['apiSecret'] = $v;
137+
$conf[self::FIELD_API_SECRET] = $v;
109138
} elseif (ENV_RGW_LOG_SILENT === $k) {
110-
$conf['silent'] = (bool)$v;
139+
$conf[self::FIELD_SILENT] = $v;
111140
}
112141
}
113-
return new static($conf, $client, $logger);
142+
return $conf;
143+
}
144+
145+
/**
146+
* @param \GuzzleHttp\Client|null $client
147+
* @param null|\Psr\Log\LoggerInterface $logger
148+
* @return \MyENA\RGW\Config
149+
*/
150+
public static function defaultConfig(?GuzzleClient $client = null, ?LoggerInterface $logger = null): Config
151+
{
152+
return new static(self::getConfigFromEnvironment(), $client, $logger);
114153
}
115154

116155
/**
@@ -193,6 +232,22 @@ public function setApiSecret(string $apiSecret): void
193232
$this->apiSecret = $apiSecret;
194233
}
195234

235+
/**
236+
* @return bool
237+
*/
238+
public function isNoSSL(): bool
239+
{
240+
return $this->noSSL;
241+
}
242+
243+
/**
244+
* @param bool $noSSL
245+
*/
246+
public function setNoSSL(bool $noSSL): void
247+
{
248+
$this->noSSL = $noSSL;
249+
}
250+
196251
/**
197252
* @return \GuzzleHttp\ClientInterface
198253
*/

src/Models/MetadataBucketInstanceResponse.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
* @OA\Schema(
77
* schema="RGWMetadataBucketInstanceResponse",
88
* type="object",
9-
* ref="$/definitions/RGWMetadataResponse",
10-
* @OA\Property(
11-
* property="data",
12-
* type="object",
13-
* ref="#/components/schemas/RGWMetadataBucketInstanceResponseData"
14-
* )
9+
* allOf={
10+
* @OA\Schema(ref="#/components/schemas/RGWMetadataResponse"),
11+
* @OA\Schema(
12+
* @OA\Property(
13+
* property="data",
14+
* type="object",
15+
* ref="#/components/schemas/RGWMetadataBucketInstanceResponseData"
16+
* )
17+
* )
18+
* }
1519
* )
1620
*/
1721

src/Models/MetadataBucketResponse.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
* @OA\Schema(
77
* schema="RGWMetadataBucketResponse",
88
* type="object",
9-
* ref="$/definitions/RGWMetadataResponse",
10-
* @OA\Property(
11-
* property="data",
12-
* type="object",
13-
* ref="#/components/schemas/RGWMetadataBucketResponseData"
14-
* )
9+
* allOf={
10+
* @OA\Schema(ref="#/components/schemas/RGWMetadataResponse"),
11+
* @OA\Schema(
12+
* @OA\Property(
13+
* property="data",
14+
* type="object",
15+
* ref="#/components/schemas/RGWMetadataBucketResponseData"
16+
* )
17+
* )
18+
* }
1519
* )
1620
*/
1721

src/Models/MetadataUserInfo.php

+46-42
Original file line numberDiff line numberDiff line change
@@ -6,48 +6,52 @@
66
* @OA\Schema(
77
* schema="RGWMetadataUserInfo",
88
* type="object",
9-
* ref="$/definitions/RGWUserInfo",
10-
* @OA\Property(
11-
* property="auid",
12-
* type="integer"
13-
* ),
14-
* @OA\Property(
15-
* property="op_mask",
16-
* type="string"
17-
* ),
18-
* @OA\Property(
19-
* property="default_placement",
20-
* type="string"
21-
* ),
22-
* @OA\Property(
23-
* property="placement_tags",
24-
* type="array",
25-
* @OA\Items(type="string"),
26-
* ),
27-
* @OA\Property(
28-
* property="bucket_quota",
29-
* type="object",
30-
* ref="#/components/schemas/RGWQuotaMeta"
31-
* ),
32-
* @OA\Property(
33-
* property="user_quota",
34-
* type="object",
35-
* ref="#/components/schemas/RGWQuotaMeta"
36-
* ),
37-
* @OA\Property(
38-
* property="temp_url_keys",
39-
* type="array",
40-
* @OA\Items(type="string"),
41-
* ),
42-
* @OA\Property(
43-
* property="type",
44-
* type="string"
45-
* ),
46-
* @OA\Property(
47-
* property="attrs",
48-
* type="array",
49-
* @OA\Items(ref="#/components/schemas/RGWMetadataAttribute")
50-
* )
9+
* allOf={
10+
* @OA\Schema(ref="#/components/schemas/RGWUserInfo"),
11+
* @OA\Schema(
12+
* @OA\Property(
13+
* property="auid",
14+
* type="integer"
15+
* ),
16+
* @OA\Property(
17+
* property="op_mask",
18+
* type="string"
19+
* ),
20+
* @OA\Property(
21+
* property="default_placement",
22+
* type="string"
23+
* ),
24+
* @OA\Property(
25+
* property="placement_tags",
26+
* type="array",
27+
* @OA\Items(type="string"),
28+
* ),
29+
* @OA\Property(
30+
* property="bucket_quota",
31+
* type="object",
32+
* ref="#/components/schemas/RGWQuotaMeta"
33+
* ),
34+
* @OA\Property(
35+
* property="user_quota",
36+
* type="object",
37+
* ref="#/components/schemas/RGWQuotaMeta"
38+
* ),
39+
* @OA\Property(
40+
* property="temp_url_keys",
41+
* type="array",
42+
* @OA\Items(type="string"),
43+
* ),
44+
* @OA\Property(
45+
* property="type",
46+
* type="string"
47+
* ),
48+
* @OA\Property(
49+
* property="attrs",
50+
* type="array",
51+
* @OA\Items(ref="#/components/schemas/RGWMetadataAttribute")
52+
* )
53+
* )
54+
* }
5155
* )
5256
*/
5357

src/Models/MetadataUserResponse.php

+10-6
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,16 @@
66
* @OA\Schema(
77
* schema="RGWMetadataUserResponse",
88
* type="object",
9-
* ref="$/definitions/RGWMetadataResponse",
10-
* @OA\Property(
11-
* property="data",
12-
* type="object",
13-
* ref="#/components/schemas/RGWMetadataUserInfo"
14-
* )
9+
* allOf={
10+
* @OA\Schema(ref="#/components/schemas/RGWMetadataResponse"),
11+
* @OA\Schema(
12+
* @OA\Property(
13+
* property="data",
14+
* type="object",
15+
* ref="#/components/schemas/RGWMetadataUserInfo"
16+
* )
17+
* )
18+
* }
1519
* )
1620
*/
1721

src/Validator/InstanceOfValidator.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,6 @@ public function test($value): bool
4646
*/
4747
public function expectedStatement(): string
4848
{
49-
return 'object that is an instance of '.$this->class;
49+
return 'object that is an instance of ' . $this->class;
5050
}
5151
}

0 commit comments

Comments
 (0)