Skip to content

Commit

Permalink
Deprecate SecretsManager and test new client (#99)
Browse files Browse the repository at this point in the history
* Deprecate SecretManager

* Update secrets to use new client
  • Loading branch information
withinboredom authored Jul 16, 2021
1 parent 6fb3015 commit debcd71
Show file tree
Hide file tree
Showing 4 changed files with 76 additions and 9 deletions.
4 changes: 2 additions & 2 deletions src/lib/Client/DaprClient.php
Original file line number Diff line number Diff line change
Expand Up @@ -404,12 +404,12 @@ abstract public function getSecretAsync(string $storeName, string $key, array $m
* @param array<array-key, string> $metadata
* @return array<array-key, string>
*/
abstract public function getSecret(string $storeName, string $key, array $metadata = []): array;
abstract public function getSecret(string $storeName, string $key, array $metadata = []): array|null;

/**
* @param string $storeName
* @param array<array-key, string> $metadata
* @return PromiseInterface<array<array-key,array<array-key, string>>>
* @return PromiseInterface<null|array<array-key,array<array-key, string>>>
*/
abstract public function getBulkSecretAsync(string $storeName, array $metadata = []): PromiseInterface;

Expand Down
23 changes: 20 additions & 3 deletions src/lib/Client/HttpSecretsTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ trait HttpSecretsTrait
public ISerializer $serializer;
private Client $httpClient;

public function getSecret(string $storeName, string $key, array $metadata = []): array
public function getSecret(string $storeName, string $key, array $metadata = []): array|null
{
return $this->getSecretAsync($storeName, $key, $metadata)->wait();
}
Expand All @@ -30,7 +30,18 @@ public function getSecretAsync(string $storeName, string $key, array $metadata =
$storeName = rawurlencode($storeName);
$key = rawurlencode($key);
return $this->handlePromise(
$this->httpClient->getAsync("/v1.0/secrets/$storeName/$key", ['query' => $metadata]),
$this->httpClient->getAsync(
"/v1.0/secrets/$storeName/$key",
[
'query' => array_merge(
...array_map(
fn($key, $value) => ["metadata.$key" => $value],
array_keys($metadata),
$metadata
)
)
]
),
fn(ResponseInterface $response) => $this->deserializer->from_json(
'array',
$response->getBody()->getContents()
Expand All @@ -50,7 +61,13 @@ public function getBulkSecretAsync(string $storeName, array $metadata = []): Pro
$this->httpClient->getAsync(
"/v1.0/secrets/$storeName/bulk",
[
'query' => $metadata
'query' => array_merge(
...array_map(
fn($key, $value) => ["metadata.$key" => $value],
array_keys($metadata),
$metadata
)
)
]
),
fn(ResponseInterface $response) => $this->deserializer->from_json(
Expand Down
2 changes: 2 additions & 0 deletions src/lib/SecretManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,15 @@
namespace Dapr;

use Dapr\exceptions\DaprException;
use JetBrains\PhpStorm\Deprecated;
use Psr\Log\LoggerInterface;

/**
* Enables reading Dapr secrets
* @see https://v1-rc1.docs.dapr.io/reference/api/secrets_api/
* @package Dapr
*/
#[Deprecated(since: '1.2.0')]
class SecretManager
{
public function __construct(protected DaprClient $client, protected LoggerInterface $logger)
Expand Down
56 changes: 52 additions & 4 deletions tests/SecretTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
use DI\DependencyException;
use DI\NotFoundException;

require_once __DIR__.'/DaprTests.php';
require_once __DIR__ . '/DaprTests.php';

/**
* Class SecretTest
Expand All @@ -19,6 +19,21 @@ class SecretTest extends DaprTests
*/
public function testRetrieveSecret()
{
$container = $this->get_http_client_stack(
[
new \GuzzleHttp\Psr7\Response(200, body: json_encode(['secret' => 'my_secret']))
]
);
$client = $this->get_new_client_with_http($container->client);
$secret = $client->getSecret('store', 'test', ['meta' => 'data']);
$this->assertSame(['secret' => 'my_secret'], $secret);

$request = $container->history[0]['request'];
$this->assertRequestQueryString('metadata.meta=data', $request);
$this->assertRequestUri('/v1.0/secrets/store/test', $request);
$this->assertRequestMethod('GET', $request);
$this->assertRequestBody('', $request);

$this->get_client()->register_get(
'/secrets/store/test',
200,
Expand All @@ -27,7 +42,7 @@ public function testRetrieveSecret()
]
);
$secretManager = $this->container->get(SecretManager::class);
$secret = $secretManager->retrieve('store', 'test');
$secret = $secretManager->retrieve('store', 'test');
$this->assertSame(['secret' => 'my_secret'], $secret);
}

Expand All @@ -38,13 +53,30 @@ public function testRetrieveSecret()
*/
public function testGetAllSecrets()
{
$container = $this->get_http_client_stack(
[
new \GuzzleHttp\Psr7\Response(
200,
body: json_encode(['secret1' => ['key1' => 'value1'], 'secret2' => ['key2' => 'value2']])
)
]
);
$client = $this->get_new_client_with_http($container->client);
$secrets = $client->getBulkSecret('store', ['meta' => 'data']);
$this->assertSame(['secret1' => ['key1' => 'value1'], 'secret2' => ['key2' => 'value2']], $secrets);
$request = $container->history[0]['request'];
$this->assertRequestQueryString('metadata.meta=data', $request);
$this->assertRequestUri('/v1.0/secrets/store/bulk', $request);
$this->assertRequestMethod('GET', $request);
$this->assertRequestBody('', $request);

$this->get_client()->register_get(
'/secrets/store/bulk',
200,
['secret1' => ['key1' => 'value1'], 'secret2' => ['key2' => 'value2']]
);
$secretManager = $this->container->get(SecretManager::class);
$secrets = $secretManager->all('store');
$secrets = $secretManager->all('store');
$this->assertSame(['secret1' => ['key1' => 'value1'], 'secret2' => ['key2' => 'value2']], $secrets);
}

Expand All @@ -55,9 +87,25 @@ public function testGetAllSecrets()
*/
public function testSecretNoExist()
{
$container = $this->get_http_client_stack(
[
new \GuzzleHttp\Psr7\Response(
204
)
]
);
$client = $this->get_new_client_with_http($container->client);
$secret = $client->getSecret('store', 'test', ['meta' => 'data']);
$this->assertNull($secret);
$request = $container->history[0]['request'];
$this->assertRequestQueryString('metadata.meta=data', $request);
$this->assertRequestUri('/v1.0/secrets/store/test', $request);
$this->assertRequestMethod('GET', $request);
$this->assertRequestBody('', $request);

$this->get_client()->register_get('/secrets/store/test', 204, null);
$secretManager = $this->container->get(SecretManager::class);
$secret = $secretManager->retrieve('store', 'test');
$secret = $secretManager->retrieve('store', 'test');
$this->assertSame(null, $secret);
}
}

0 comments on commit debcd71

Please sign in to comment.