Skip to content

Commit

Permalink
Merge pull request #53 from keboola/roman-pst-2436
Browse files Browse the repository at this point in the history
PST-2436: Ignore secret version when retrieving secrets from AKV
  • Loading branch information
romantmb authored Feb 4, 2025
2 parents fc8555f + a818fb2 commit faf6f55
Show file tree
Hide file tree
Showing 7 changed files with 36 additions and 11 deletions.
6 changes: 3 additions & 3 deletions .github/workflows/push.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ concurrency: ci

env:
TEST_TENANT_ID: 9b85ee6f-4fb0-4a46-8cb7-4dcc6b262a89
TEST_CLIENT_ID: 3a4162fa-43d4-4de7-a8c1-be3bc7765a8c
TEST_CLIENT_ID: bbd79ac8-74a2-4853-8c8c-99af4c614492
TEST_CLIENT_SECRET: ${{ secrets.TEST_CLIENT_SECRET }}
TEST_KEY_VAULT_URL: https://ci-object-encryptor.vault.azure.net/
TEST_AWS_REGION: eu-central-1
Expand All @@ -29,7 +29,7 @@ jobs:
- name: Build image
run: |
docker login --username "$DOCKERHUB_USER" --password "$DOCKERHUB_TOKEN"
docker-compose build
docker compose build
- name: Run tests
run: |
docker-compose run ci
docker compose run ci
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -74,7 +74,7 @@ Prerequisites:
* installed AWS CLI `aws` (and run `aws configure --profile YOUR_AWS_PROFILE_NAME`)
* installed GCP CLI `gcloud` (and run `gcloud auth login` or `gcloud auth application-default login`)
* installed `terraform` (https://www.terraform.io) and `jq` (https://stedolan.github.io/jq) to setup local env
* installed `docker` and `docker-compose` to run & develop the app
* installed `docker` to run & develop the app

```bash
export NAME_PREFIX= # your name/nickname to make your resource unique & recognizable
Expand All @@ -88,7 +88,7 @@ terraform -chdir=./provisioning/local init -backend-config="key=object-encryptor
terraform -chdir=./provisioning/local apply
./provisioning/local/update-env.sh aws # or azure or gcp

docker-compose run --rm tests
docker compose run --rm tests
```

## License
Expand Down
1 change: 0 additions & 1 deletion docker-compose.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,3 @@
version: "3"
services:
# for development purposes
tests: &tests
Expand Down
4 changes: 3 additions & 1 deletion phpstan.neon
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
parameters:
level: max
checkMissingIterableValueType: false
paths:
- src
- tests
ignoreErrors:
-
identifier: missingType.iterableValue
includes:
- vendor/phpstan/phpstan-phpunit/extension.neon
4 changes: 4 additions & 0 deletions provisioning/ci/.terraform.lock.hcl

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

7 changes: 3 additions & 4 deletions src/Wrapper/GenericAKVWrapper.php
Original file line number Diff line number Diff line change
Expand Up @@ -150,10 +150,9 @@ public function decrypt(string $encryptedData): string
}
try {
$decryptedContext = $this->getRetryProxy()->call(function () use ($encrypted) {
return $this->getClient()->getSecret(
$encrypted[self::SECRET_NAME],
$encrypted[self::SECRET_VERSION],
)->getValue();
return $this->getClient()
->getSecret($encrypted[self::SECRET_NAME])
->getValue();
});
assert(is_string($decryptedContext));
$decryptedContext = $this->decode($decryptedContext);
Expand Down
21 changes: 21 additions & 0 deletions tests/GenericAKVWrapperTest.php
Original file line number Diff line number Diff line change
Expand Up @@ -122,6 +122,27 @@ public function testEncryptEmptyValue(?string $secret): void
self::assertEquals($secret, $wrapper->decrypt($encrypted));
}

public function testIgnoreSecretVersionWhenRetrievingSecret(): void
{
$secret = 'mySecretValue';
$wrapper = $this->getWrapper();
$encrypted = $wrapper->encrypt($secret);

// decode the encrypted secret and manually change the version
$decoded = unserialize((string) gzuncompress(base64_decode($encrypted)));
$secretVersionIndex = 4; // = GenericAKVWrapper::SECRET_VERSION
/** @var array<int, string> $decoded */
$decoded[$secretVersionIndex] = bin2hex(random_bytes(16));

// encode back with the changed version
$encrypted = base64_encode((string) gzcompress(serialize($decoded)));

// decrypt should succeed regardless of changed version
$decrypted = $wrapper->decrypt($encrypted);

self::assertSame($secret, $decrypted);
}

private function getMockWrapper(Client $mockClient): GenericAKVWrapper
{
$mockWrapper = $this->createPartialMock(GenericAKVWrapper::class, ['getClient']);
Expand Down

0 comments on commit faf6f55

Please sign in to comment.