Skip to content

Commit eef6347

Browse files
committed
merge
2 parents 53dac01 + 10ce05a commit eef6347

File tree

6 files changed

+115
-22
lines changed

6 files changed

+115
-22
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,4 @@
11
.idea
2+
tmp/
3+
vendor/
4+
composer.lock

CHANGELOG.md

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,15 @@
11
# CHANGELOG
22

33

4-
## 1.4.2 (2022-01-13)
4+
## 1.4.3 (2023-02-15)
5+
6+
### Features
7+
8+
- anonymous authentication method
9+
- insecure grpc connection
10+
11+
12+
## 1.4.2 (2023-01-13)
513

614
### Bugs
715

README.md

Lines changed: 29 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@ YDB supports the following authentication methods:
3232
- JWT + private key
3333
- JWT + JSON file
3434
- Metadata URL
35+
- Anonymous
3536

3637
## OAuth token
3738

@@ -160,6 +161,34 @@ $ydb = new Ydb($config);
160161

161162
```
162163

164+
## Anonymous
165+
166+
```php
167+
<?php
168+
169+
use YdbPlatform\Ydb\Ydb;
170+
171+
$config = [
172+
173+
// Database path
174+
'database' => '/local',
175+
176+
// Database endpoint
177+
'endpoint' => 'localhost:2136',
178+
179+
// Auto discovery (dedicated server only)
180+
'discovery' => false,
181+
182+
// IAM config
183+
'iam_config' => [
184+
'anonymous' => true,
185+
'insecure' => true,
186+
],
187+
];
188+
189+
$ydb = new Ydb($config);
190+
191+
```
163192

164193
# Usage
165194

composer.json

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
"grpc/grpc": "^1.35",
1919
"lcobucci/jwt": "~4.1.5",
2020
"phpseclib/phpseclib": "^2.0|^3.0",
21-
"psr/log": "~1.0"
21+
"psr/log": "^1|^2|^3"
2222
},
2323
"autoload": {
2424
"psr-4": {

src/Iam.php

Lines changed: 64 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use YdbPlatform\Ydb\Jwt\Signer\Sha256;
1111
use YdbPlatform\Ydb\Contracts\IamTokenContract;
1212

13+
use function filter_var;
14+
1315
class Iam implements IamTokenContract
1416
{
1517
use Traits\LoggerTrait;
@@ -48,7 +50,7 @@ public function __construct(array $config = [], LoggerInterface $logger = null)
4850
{
4951
if ($config)
5052
{
51-
$this->config = $config;
53+
$this->config = $this->parseConfig($config);
5254
}
5355

5456
$this->logger = $logger;
@@ -95,7 +97,6 @@ public function newToken()
9597
else if ($this->config('private_key'))
9698
{
9799
$token = $this->getJwtToken();
98-
99100
$request_data = [
100101
'jwt' => $token->toString(),
101102
];
@@ -159,6 +160,11 @@ public function newToken()
159160
*/
160161
public function getCredentials()
161162
{
163+
if ($this->config('insecure'))
164+
{
165+
return ChannelCredentials::createInsecure();
166+
}
167+
162168
$root_pem_file = $this->config('root_cert_file');
163169

164170
if ($root_pem_file && is_file($root_pem_file))
@@ -169,28 +175,72 @@ public function getCredentials()
169175
return ChannelCredentials::createSsl($pem ?? null);
170176
}
171177

178+
/**
179+
* @param array $config
180+
* @return array
181+
*/
182+
protected function parseConfig(array $config)
183+
{
184+
$parsedConfig = [];
185+
186+
$stringParams = [
187+
'temp_dir',
188+
'root_cert_file',
189+
'oauth_token',
190+
'key_id',
191+
'service_account_id',
192+
'private_key_file',
193+
'service_file',
194+
];
195+
196+
foreach ($stringParams as $param)
197+
{
198+
$parsedConfig[$param] = (string)($config[$param] ?? '');
199+
}
200+
201+
$boolParams = [
202+
'use_metadata',
203+
'anonymous',
204+
'insecure',
205+
];
206+
207+
foreach ($boolParams as $param)
208+
{
209+
$parsedConfig[$param] = (
210+
isset($config[$param])
211+
&& filter_var($config[$param], \FILTER_VALIDATE_BOOLEAN)
212+
);
213+
}
214+
215+
return $parsedConfig;
216+
}
217+
172218
/**
173219
* @return void
174220
* @throws Exception
175221
*/
176222
protected function initConfig()
177223
{
178-
if (empty($this->config['temp_dir']))
224+
if (!$this->config('temp_dir'))
179225
{
180226
$this->config['temp_dir'] = sys_get_temp_dir();
181227
}
182228

183-
if (!empty($this->config['use_metadata']))
229+
if ($this->config('anonymous'))
230+
{
231+
$this->logger()->info('YDB: Authentication method: Anonymous');
232+
}
233+
else if ($this->config('use_metadata'))
184234
{
185235
$this->logger()->info('YDB: Authentication method: Metadata URL');
186236
}
187-
else if (!empty($this->config['service_file']))
237+
else if ($serviceFile = $this->config('service_file'))
188238
{
189-
if (is_file($this->config['service_file']))
239+
if (is_file($serviceFile))
190240
{
191241
$this->logger()->info('YDB: Authentication method: SA JSON file');
192242

193-
$service = json_decode(file_get_contents($this->config['service_file']));
243+
$service = json_decode(file_get_contents($serviceFile));
194244

195245
if (is_object($service)
196246
&& isset($service->id)
@@ -203,28 +253,28 @@ protected function initConfig()
203253
}
204254
else
205255
{
206-
throw new Exception('Service file [' . $this->config['service_file'] . '] is broken.');
256+
throw new Exception('Service file [' . $serviceFile . '] is broken.');
207257
}
208258
}
209259
else
210260
{
211-
throw new Exception('Service file [' . $this->config['service_file'] . '] is missing.');
261+
throw new Exception('Service file [' . $serviceFile . '] is missing.');
212262
}
213263
}
214-
else if (!empty($this->config['private_key_file']))
264+
else if ($privateKeyFile = $this->config('private_key_file'))
215265
{
216266
$this->logger()->info('YDB: Authentication method: Private key');
217267

218-
if (is_file($this->config['private_key_file']))
268+
if (is_file($privateKeyFile))
219269
{
220-
$this->config['private_key'] = file_get_contents($this->config['private_key_file']);
270+
$this->config['private_key'] = file_get_contents($privateKeyFile);
221271
}
222272
else
223273
{
224-
throw new Exception('Private key [' . $this->config['private_key_file'] . '] is missing.');
274+
throw new Exception('Private key [' . $privateKeyFile . '] is missing.');
225275
}
226276
}
227-
else if (!empty($this->config['oauth_token']))
277+
else if ($this->config('oauth_token'))
228278
{
229279
$this->logger()->info('YDB: Authentication method: OAuth token');
230280
}

src/Ydb.php

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -102,16 +102,19 @@ public function database()
102102
return $this->database;
103103
}
104104

105-
/**
106-
* @return array
107-
*/
108-
public function meta()
105+
public function meta(): array
109106
{
110-
return [
111-
'x-ydb-auth-ticket' => [$this->iam()->token()],
107+
$meta = [
112108
'x-ydb-database' => [$this->database],
113109
'x-ydb-sdk-build-info' => ['ydb-php-sdk/' . static::VERSION],
114110
];
111+
112+
if (!$this->iam()->config('anonymous'))
113+
{
114+
$meta['x-ydb-auth-ticket'] = [$this->iam()->token()];
115+
}
116+
117+
return $meta;
115118
}
116119

117120
/**

0 commit comments

Comments
 (0)