Skip to content

Commit 10ce05a

Browse files
committed
Authentication method: Anonymous, Insecure grpc connection
1 parent d36974c commit 10ce05a

File tree

6 files changed

+62
-10
lines changed

6 files changed

+62
-10
lines changed

.gitignore

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

CHANGELOG.md

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,11 @@
11
# CHANGELOG
22

3+
## 1.4.0 (???)
4+
5+
### Features
6+
7+
- Authentication method: Anonymous
8+
- Insecure grpc connection
39

410
## 1.3.1 (2022-02-25)
511

README.md

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ Yandex Database supports the following authentication methods:
3131
- JWT + private key
3232
- JWT + JSON file
3333
- Metadata URL
34+
- Anonymous
3435

3536
## OAuth token
3637

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

161162
```
162163

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

164194
# Usage
165195

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: 14 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,6 +10,8 @@
1010
use YandexCloud\Ydb\Jwt\Signer\Sha256;
1111
use YandexCloud\Ydb\Contracts\IamTokenContract;
1212

13+
use function filter_var;
14+
1315
class Iam implements IamTokenContract
1416
{
1517
use Traits\LoggerTrait;
@@ -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,10 @@ public function newToken()
159160
*/
160161
public function getCredentials()
161162
{
163+
if ($this->config('insecure')) {
164+
return ChannelCredentials::createInsecure();
165+
}
166+
162167
$root_pem_file = $this->config('root_cert_file');
163168

164169
if ($root_pem_file && is_file($root_pem_file))
@@ -180,7 +185,14 @@ protected function initConfig()
180185
$this->config['temp_dir'] = sys_get_temp_dir();
181186
}
182187

183-
if (!empty($this->config['use_metadata']))
188+
$this->config['insecure'] = (isset($this->config['insecure']) && filter_var($this->config['insecure'], \FILTER_VALIDATE_BOOLEAN));
189+
$this->config['anonymous'] = (isset($this->config['anonymous']) && filter_var($this->config['anonymous'], \FILTER_VALIDATE_BOOLEAN));
190+
191+
if ($this->config['anonymous'])
192+
{
193+
$this->logger()->info('YDB: Authentication method: Anonymous');
194+
}
195+
else if (!empty($this->config['use_metadata']))
184196
{
185197
$this->logger()->info('YDB: Authentication method: Metadata URL');
186198
}

src/Ydb.php

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ class Ydb
88
{
99
use Traits\LoggerTrait;
1010

11-
const VERSION = '1.3.1';
11+
const VERSION = '1.4.0';
1212

1313
/**
1414
* @var string
@@ -102,16 +102,18 @@ 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', false)) {
113+
$meta['x-ydb-auth-ticket'] = [$this->iam()->token()];
114+
}
115+
116+
return $meta;
115117
}
116118

117119
/**

0 commit comments

Comments
 (0)