|
2 | 2 |
|
3 | 3 | namespace Backend\Snowflake; |
4 | 4 |
|
| 5 | +use Doctrine\DBAL\Exception\DriverException; |
5 | 6 | use Keboola\Csv\CsvFile; |
6 | 7 | use Keboola\StorageApi\Components; |
7 | 8 | use Keboola\StorageApi\Options\Components\Configuration; |
@@ -167,4 +168,83 @@ public function testLoadCloneToReaderAccount(): void |
167 | 168 | $data = $db->fetchAll('langs'); |
168 | 169 | $this->assertCount(5, $data); |
169 | 170 | } |
| 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 | + } |
170 | 250 | } |
0 commit comments