|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +/* |
| 6 | + * @author Aaron Scherer <aequasi@gmail.com> |
| 7 | + * @date 2019 |
| 8 | + * @license https://opensource.org/licenses/MIT |
| 9 | + */ |
| 10 | + |
| 11 | +namespace Secretary\Adapter\AWS\SSM\Tests; |
| 12 | + |
| 13 | +use Aws\Result; |
| 14 | +use Aws\Ssm\Exception\SsmException; |
| 15 | +use Aws\Ssm\SsmClient; |
| 16 | +use Mockery; |
| 17 | +use Mockery\Adapter\Phpunit\MockeryTestCase; |
| 18 | +use Secretary\Adapter\AWS\SSM\AWSSSMParameterStoreAdapter; |
| 19 | +use Secretary\Exception\SecretNotFoundException; |
| 20 | +use Secretary\Secret; |
| 21 | + |
| 22 | +final class AWSSSMParameterStoreAdapterTest extends MockeryTestCase |
| 23 | +{ |
| 24 | + private function adapterWithClient(SsmClient $client): AWSSSMParameterStoreAdapter |
| 25 | + { |
| 26 | + $adapter = new AWSSSMParameterStoreAdapter([]); |
| 27 | + |
| 28 | + $ref = new \ReflectionObject($adapter); |
| 29 | + $prop = $ref->getProperty('client'); |
| 30 | + $prop->setAccessible(true); |
| 31 | + $prop->setValue($adapter, $client); |
| 32 | + |
| 33 | + return $adapter; |
| 34 | + } |
| 35 | + |
| 36 | + private function notFound(): SsmException |
| 37 | + { |
| 38 | + $exception = Mockery::mock(SsmException::class); |
| 39 | + $exception->shouldReceive('getAwsErrorCode')->andReturn('ParameterNotFound'); |
| 40 | + |
| 41 | + return $exception; |
| 42 | + } |
| 43 | + |
| 44 | + public function testGetSecretReturnsPlainString(): void |
| 45 | + { |
| 46 | + $client = Mockery::mock(SsmClient::class); |
| 47 | + $client->shouldReceive('getParameter') |
| 48 | + ->once() |
| 49 | + ->with(['Name' => 'foo', 'WithDecryption' => true]) |
| 50 | + ->andReturn(new Result(['Parameter' => ['Value' => 'bar']])); |
| 51 | + |
| 52 | + $secret = $this->adapterWithClient($client)->getSecret('foo'); |
| 53 | + |
| 54 | + $this->assertSame('foo', $secret->getKey()); |
| 55 | + $this->assertSame('bar', $secret->getValue()); |
| 56 | + } |
| 57 | + |
| 58 | + public function testGetSecretDecodesJson(): void |
| 59 | + { |
| 60 | + $client = Mockery::mock(SsmClient::class); |
| 61 | + $client->shouldReceive('getParameter') |
| 62 | + ->once() |
| 63 | + ->with(['Name' => 'baz', 'WithDecryption' => true]) |
| 64 | + ->andReturn(new Result(['Parameter' => ['Value' => json_encode(['foobar' => 'baz'])]])); |
| 65 | + |
| 66 | + $secret = $this->adapterWithClient($client)->getSecret('baz'); |
| 67 | + |
| 68 | + $this->assertSame(['foobar' => 'baz'], $secret->getValue()); |
| 69 | + } |
| 70 | + |
| 71 | + public function testGetSecretThrowsWhenMissing(): void |
| 72 | + { |
| 73 | + $client = Mockery::mock(SsmClient::class); |
| 74 | + $client->shouldReceive('getParameter') |
| 75 | + ->once() |
| 76 | + ->andThrow($this->notFound()); |
| 77 | + |
| 78 | + $this->expectException(SecretNotFoundException::class); |
| 79 | + $this->adapterWithClient($client)->getSecret('bar'); |
| 80 | + } |
| 81 | + |
| 82 | + public function testPutSecretWritesSecureStringByDefault(): void |
| 83 | + { |
| 84 | + $client = Mockery::mock(SsmClient::class); |
| 85 | + $client->shouldReceive('putParameter') |
| 86 | + ->once() |
| 87 | + ->with([ |
| 88 | + 'Type' => 'SecureString', |
| 89 | + 'Overwrite' => true, |
| 90 | + 'Value' => 'foobar', |
| 91 | + 'Name' => 'test', |
| 92 | + ]) |
| 93 | + ->andReturn(new Result([])); |
| 94 | + |
| 95 | + $secret = new Secret('test', 'foobar'); |
| 96 | + $result = $this->adapterWithClient($client)->putSecret($secret); |
| 97 | + |
| 98 | + $this->assertSame($secret->getKey(), $result->getKey()); |
| 99 | + } |
| 100 | + |
| 101 | + public function testPutSecretEncodesArrayValue(): void |
| 102 | + { |
| 103 | + $client = Mockery::mock(SsmClient::class); |
| 104 | + $client->shouldReceive('putParameter') |
| 105 | + ->once() |
| 106 | + ->with([ |
| 107 | + 'Type' => 'SecureString', |
| 108 | + 'Overwrite' => true, |
| 109 | + 'Value' => json_encode(['a' => 'b']), |
| 110 | + 'Name' => 'obj', |
| 111 | + ]) |
| 112 | + ->andReturn(new Result([])); |
| 113 | + |
| 114 | + $result = $this->adapterWithClient($client)->putSecret(new Secret('obj', ['a' => 'b'])); |
| 115 | + |
| 116 | + $this->assertSame('obj', $result->getKey()); |
| 117 | + } |
| 118 | + |
| 119 | + public function testDeleteSecretCallsDeleteParameter(): void |
| 120 | + { |
| 121 | + $client = Mockery::mock(SsmClient::class); |
| 122 | + $client->shouldReceive('deleteParameter') |
| 123 | + ->once() |
| 124 | + ->with(['Name' => 'test']) |
| 125 | + ->andReturn(new Result([])); |
| 126 | + |
| 127 | + $this->adapterWithClient($client)->deleteSecret(new Secret('test', 'foobar')); |
| 128 | + $this->assertTrue(true); |
| 129 | + } |
| 130 | +} |
0 commit comments