|
8 | 8 | use Keboola\StorageApi\ClientException; |
9 | 9 | use Keboola\StorageApi\WorkspaceLoginType; |
10 | 10 | use Keboola\StorageApi\Workspaces; |
| 11 | +use Keboola\TableBackendUtils\Connection\Exception\ConnectionException; |
11 | 12 | use Keboola\Test\Backend\WorkspaceConnectionTrait; |
12 | 13 | use Keboola\Test\Backend\WorkspaceCredentialsAssertTrait; |
13 | 14 | use Keboola\Test\Backend\Workspaces\Backend\WorkspaceBackendFactory; |
@@ -49,6 +50,64 @@ public static function createWorkspaceProvider(): Generator |
49 | 50 | } |
50 | 51 | } |
51 | 52 |
|
| 53 | + public function legacyLoginTypeProvider(): Generator |
| 54 | + { |
| 55 | + yield 'Login type "snowflake-legacy-service"' => [ |
| 56 | + WorkspaceLoginType::SNOWFLAKE_LEGACY_SERVICE_PASSWORD, |
| 57 | + ]; |
| 58 | + yield 'Login type "default"' => [ |
| 59 | + WorkspaceLoginType::DEFAULT, |
| 60 | + ]; |
| 61 | + } |
| 62 | + |
| 63 | + /** |
| 64 | + * @dataProvider legacyLoginTypeProvider |
| 65 | + */ |
| 66 | + public function testCreateLegacyWsAndMigrateToPersonKeypairTypeBySetPublicKeyEndpoint(WorkspaceLoginType $loginType): void |
| 67 | + { |
| 68 | + $workspaces = new Workspaces($this->workspaceSapiClient); |
| 69 | + $options['loginType'] = $loginType; |
| 70 | + $workspace = $this->initTestWorkspace('snowflake', $options, true); |
| 71 | + |
| 72 | + $connection = $workspace['connection']; |
| 73 | + $this->assertSame('snowflake', $connection['backend']); |
| 74 | + $this->assertSame('snowflake-legacy-service', $connection['loginType']); // it's always snowflake-legacy-service |
| 75 | + |
| 76 | + $key = (new PemKeyCertificateGenerator())->createPemKeyCertificate(null); |
| 77 | + $workspaces->setPublicKey($workspace['id'], new Workspaces\SetPublicKeyRequest(publicKey: $key->getPublicKey())); |
| 78 | + $updatedWs = $workspaces->getWorkspace($workspace['id']); |
| 79 | + |
| 80 | + $connectionWs = $updatedWs['connection']; |
| 81 | + $this->assertSame('snowflake', $connectionWs['backend']); |
| 82 | + assert(array_key_exists('loginType', $connectionWs)); |
| 83 | + $this->assertSame(WorkspaceLoginType::SNOWFLAKE_PERSON_KEYPAIR->value, $connectionWs['loginType']); |
| 84 | + |
| 85 | + try { |
| 86 | + $backendKey = WorkspaceBackendFactory::createWorkspaceForSnowflakeDbal($workspace); |
| 87 | + $backendKey->getDb()->executeQuery('SELECT 1'); |
| 88 | + $this->fail('Original password should not work'); |
| 89 | + } catch (ConnectionException $e) { |
| 90 | + $this->assertSame('An exception occurred in the driver: Incorrect username or password was specified.', $e->getMessage()); |
| 91 | + } |
| 92 | + |
| 93 | + // new key pair should work |
| 94 | + unset($workspace['connection']['password']); |
| 95 | + $workspace['connection']['privateKey'] = $key->getPrivateKey(); |
| 96 | + $backendKey = WorkspaceBackendFactory::createWorkspaceForSnowflakeDbal($workspace); |
| 97 | + $backendKey->getDb()->executeQuery('SELECT 1'); |
| 98 | + |
| 99 | + try { |
| 100 | + // try reset password |
| 101 | + $workspaces->resetWorkspacePassword($workspace['id']); |
| 102 | + $this->fail('Password reset should not be supported for keypair login type'); |
| 103 | + } catch (ClientException $e) { |
| 104 | + $this->assertSame($e->getCode(), 400); |
| 105 | + $this->assertSame('workspace.resetPasswordNotSupported', $e->getStringCode()); |
| 106 | + } |
| 107 | + |
| 108 | + $workspaces->deleteWorkspace($workspace['id'], [], true); |
| 109 | + } |
| 110 | + |
52 | 111 | /** |
53 | 112 | * @dataProvider createWorkspaceProvider |
54 | 113 | */ |
|
0 commit comments