Skip to content

Commit 19ea66f

Browse files
authored
Merge pull request #1520 from keboola/marek-dmd-195-reader-ws-reset-public-key
Update: DMD-195 - Reader WS reset public key
2 parents 96f9571 + bead146 commit 19ea66f

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

tests/Backend/Snowflake/WorkspacesReaderTest.php

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
namespace Backend\Snowflake;
44

5+
use Doctrine\DBAL\Exception\DriverException;
56
use Keboola\Csv\CsvFile;
67
use Keboola\StorageApi\Components;
78
use Keboola\StorageApi\Options\Components\Configuration;
@@ -167,4 +168,83 @@ public function testLoadCloneToReaderAccount(): void
167168
$data = $db->fetchAll('langs');
168169
$this->assertCount(5, $data);
169170
}
171+
172+
public function testResetPublicKeyForReaderWorkspace(): void
173+
{
174+
$this->expectNotToPerformAssertions();
175+
176+
$componentId = 'wr-db';
177+
$configurationId = 'main-1';
178+
$defaultBranchId = $this->getDefaultBranchId($this);
179+
$branchClient = $this->getBranchAwareDefaultClient($defaultBranchId);
180+
// create configuration
181+
$components = new Components($branchClient);
182+
$components->addConfiguration((new Configuration())
183+
->setComponentId('wr-db')
184+
->setConfigurationId('main-1')
185+
->setName('readerWS')
186+
->setDescription('some desc'));
187+
188+
$components = new Components($branchClient);
189+
$workspaces = new Workspaces($branchClient);
190+
191+
$key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null);
192+
193+
$workspace = $components->createConfigurationWorkspace(
194+
$componentId,
195+
$configurationId,
196+
[
197+
'useCase' => 'reader',
198+
'backend' => 'snowflake',
199+
'loginType' => WorkspaceLoginType::SNOWFLAKE_SERVICE_KEYPAIR,
200+
'publicKey' => $key->getPublicKey(),
201+
],
202+
);
203+
204+
//setup test tables
205+
$tableId = $this->_client->createTableAsync(
206+
$this->getTestBucketId(self::STAGE_IN),
207+
'languages',
208+
new CsvFile(__DIR__ . '/../../_data/languages.csv'),
209+
);
210+
211+
$workspaces->cloneIntoWorkspace($workspace['id'], [
212+
'input' => [
213+
[
214+
'source' => $tableId,
215+
'destination' => 'languages',
216+
],
217+
[
218+
'source' => $tableId,
219+
'destination' => 'langs',
220+
],
221+
],
222+
]);
223+
224+
// create the connection after LOAD!! because the schema will be created by LOAD
225+
$workspace['connection']['privateKey'] = $key->getPrivateKey();
226+
$connection = WorkspaceBackendFactory::createWorkspaceForSnowflakeDbal($workspace);
227+
$connection->executeQuery('SELECT 1;');
228+
229+
$newKey = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null);
230+
$workspaces->setPublicKey($workspace['id'], new Workspaces\SetPublicKeyRequest(publicKey: $newKey->getPublicKey()));
231+
232+
// new key should work
233+
$workspace['connection']['privateKey'] = $newKey->getPrivateKey();
234+
$newConnection = WorkspaceBackendFactory::createWorkspaceForSnowflakeDbal($workspace);
235+
$newConnection->executeQuery('SELECT 1;');
236+
237+
// Cannot work with old key
238+
try {
239+
$connection->getDb()->close();
240+
$connection->getDb()->executeQuery('SELECT 1;');
241+
$this->fail('Old key should not work');
242+
} catch (DriverException $driverException) {
243+
// Should throw exception
244+
}
245+
246+
// New should be working always
247+
$newConnection->getDb()->close();
248+
$newConnection->getDb()->executeQuery('SELECT 1;');
249+
}
170250
}

0 commit comments

Comments
 (0)