Skip to content

Commit 8d54286

Browse files
authored
Merge pull request #85 from ydb-platform/added-evn-cred
Added environment credentials
2 parents 28401e1 + 7cae1f5 commit 8d54286

File tree

5 files changed

+161
-6
lines changed

5 files changed

+161
-6
lines changed

CHANGELOG.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,5 @@
1+
* added environment credentials
2+
13
## 1.6.0
24

35
* added retry function

README.md

Lines changed: 44 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ or:
7171
<?php
7272

7373
use YdbPlatform\Ydb\Ydb;
74-
use YdbPlatform\Ydb\Auth\AccessTokenAuthentication;
74+
use YdbPlatform\Ydb\Auth\Implement\AccessTokenAuthentication;
7575

7676
$config = [
7777

@@ -134,7 +134,7 @@ or
134134
<?php
135135

136136
use YdbPlatform\Ydb\Ydb;
137-
use YdbPlatform\Ydb\Auth\OAuthTokenAuthentication;
137+
use YdbPlatform\Ydb\Auth\Implement\OAuthTokenAuthentication;
138138

139139
$config = [
140140

@@ -192,7 +192,7 @@ or
192192
<?php
193193

194194
use YdbPlatform\Ydb\Ydb;
195-
use YdbPlatform\Ydb\Auth\JwtWithPrivateKeyAuthentication;
195+
use YdbPlatform\Ydb\Auth\Implement\JwtWithPrivateKeyAuthentication;
196196

197197
$config = [
198198
'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx',
@@ -249,7 +249,7 @@ or:
249249
<?php
250250

251251
use YdbPlatform\Ydb\Ydb;
252-
use YdbPlatform\Ydb\Auth\JwtWithJsonAuthentication;
252+
use YdbPlatform\Ydb\Auth\Implement\JwtWithJsonAuthentication;
253253

254254
$config = [
255255
'database' => '/ru-central1/b1glxxxxxxxxxxxxxxxx/etn0xxxxxxxxxxxxxxxx',
@@ -299,7 +299,7 @@ or
299299
<?php
300300

301301
use YdbPlatform\Ydb\Ydb;
302-
use YdbPlatform\Ydb\Auth\MetadataAuthentication;
302+
use YdbPlatform\Ydb\Auth\Implement\MetadataAuthentication;
303303

304304
$config = [
305305

@@ -355,7 +355,7 @@ or:
355355
<?php
356356

357357
use YdbPlatform\Ydb\Ydb;
358-
use YdbPlatform\Ydb\Auth\AnonymousAuthentication;
358+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
359359

360360
$config = [
361361

@@ -379,6 +379,44 @@ $config = [
379379
$ydb = new Ydb($config);
380380
```
381381

382+
## Determined by environment variables
383+
384+
```php
385+
<?php
386+
387+
use YdbPlatform\Ydb\Ydb;
388+
use YdbPlatform\Ydb\Auth\Implement\EnvironCredentials;
389+
390+
$config = [
391+
392+
// Database path
393+
'database' => '/local',
394+
395+
// Database endpoint
396+
'endpoint' => 'localhost:2136',
397+
398+
// Auto discovery (dedicated server only)
399+
'discovery' => false,
400+
401+
// IAM config
402+
'iam_config' => [
403+
'insecure' => true,
404+
],
405+
406+
'credentials' => new EnvironCredentials()
407+
];
408+
409+
$ydb = new Ydb($config);
410+
```
411+
412+
The following algorithm that is the same for YDB-PHP-SDK applies:
413+
414+
1. If the value of the `YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS` environment variable is set, the **System Account Key** authentication mode is used and the key is taken from the file whose name is specified in this variable.
415+
2. Otherwise, if the value of the `YDB_ANONYMOUS_CREDENTIALS` environment variable is set to 1, the anonymous authentication mode is used.
416+
3. Otherwise, if the value of the `YDB_METADATA_CREDENTIALS` environment variable is set to 1, the **Metadata** authentication mode is used.
417+
4. Otherwise, if the value of the `YDB_ACCESS_TOKEN_CREDENTIALS` environment variable is set, the **Access token** authentication mode is used, where the this variable value is passed.
418+
5. Otherwise, the **Metadata** authentication mode is used.
419+
382420
# Usage
383421

384422
You should initialize a session from the Table service to start querying.

src/Auth/EnvironCredentials.php

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
<?php
2+
3+
namespace YdbPlatform\Ydb\Auth;
4+
5+
use YdbPlatform\Ydb\Auth\Implement\AccessTokenAuthentication;
6+
use YdbPlatform\Ydb\Auth\Implement\AnonymousAuthentication;
7+
use YdbPlatform\Ydb\Auth\Implement\JwtWithJsonAuthentication;
8+
use YdbPlatform\Ydb\Auth\Implement\MetadataAuthentication;
9+
10+
class EnvironCredentials extends \YdbPlatform\Ydb\Auth\Auth
11+
{
12+
/**
13+
* @var Auth
14+
*/
15+
protected $auth;
16+
public function __construct()
17+
{
18+
if ($jsonfile = getenv("YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS")){
19+
$this->auth = new JwtWithJsonAuthentication($jsonfile);
20+
} elseif (getenv("YDB_ANONYMOUS_CREDENTIALS") == 1){
21+
$this->auth = new AnonymousAuthentication();
22+
} elseif (getenv("YDB_METADATA_CREDENTIALS") == 1){
23+
$this->auth = new MetadataAuthentication();
24+
} elseif ($token = getenv("YDB_ACCESS_TOKEN_CREDENTIALS")){
25+
$this->auth = new AccessTokenAuthentication($token);
26+
} else {
27+
$this->auth = new MetadataAuthentication();
28+
}
29+
}
30+
31+
public function getTokenInfo(): TokenInfo
32+
{
33+
return $this->auth->getTokenInfo();
34+
}
35+
36+
public function getName(): string
37+
{
38+
return $this->auth->getName();
39+
}
40+
}

tests/TestEnvTypes.php

Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?php
2+
namespace YdbPlatform\Ydb\Test;
3+
4+
use PHPUnit\Framework\TestCase;
5+
use YdbPlatform\Ydb\Auth\EnvironCredentials;
6+
use YdbPlatform\Ydb\Ydb;
7+
use YdbPlatform\Ydb\YdbTable;
8+
9+
class TestEnvTypes extends TestCase{
10+
11+
public function testEnvTypes(){
12+
13+
$dataset = [
14+
[
15+
"env" => [
16+
"name" => "YDB_SERVICE_ACCOUNT_KEY_FILE_CREDENTIALS",
17+
"value" => "./some.json"
18+
],
19+
"wait" => "SA JSON key"
20+
],
21+
[
22+
"env" => [
23+
"name" => "YDB_ACCESS_TOKEN_CREDENTIALS",
24+
"value" => "76254876234"
25+
],
26+
"wait" => "Access token"
27+
],
28+
[
29+
"env" => [
30+
"name" => "YDB_ANONYMOUS_CREDENTIALS",
31+
"value" => "1"
32+
],
33+
"wait" => "Anonymous"
34+
],
35+
[
36+
"env" => [
37+
"name" => "YDB_METADATA_CREDENTIALS",
38+
"value" => "1"
39+
],
40+
"wait" => "Metadata URL"
41+
],
42+
[
43+
"env" => [
44+
"name" => "YDB_ANONYMOUS_CREDENTIALS",
45+
"value" => "0"
46+
],
47+
"wait" => "Metadata URL"
48+
],
49+
[
50+
"env" => [
51+
"name" => "none",
52+
"value" => "none"
53+
],
54+
"wait" => "Metadata URL"
55+
],
56+
];
57+
foreach ($dataset as $data){
58+
putenv($data["env"]["name"]."=".$data["env"]["value"]);
59+
self::assertEquals(
60+
$data["wait"],
61+
(new EnvironCredentials())->getName()
62+
);
63+
putenv($data["env"]["name"]);
64+
}
65+
66+
}
67+
}

tests/some.json

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
{
2+
"id": "",
3+
"service_account_id": "",
4+
"created_at": "",
5+
"key_algorithm": "",
6+
"public_key": "",
7+
"private_key": ""
8+
}

0 commit comments

Comments
 (0)