Skip to content

Commit 1643adc

Browse files
authored
Merge pull request #56 from keboola/marek-CT-2030-key-pair-root
Update: CT-2030 - Key pair for root user
2 parents e44403b + 31ac7d5 commit 1643adc

File tree

8 files changed

+132
-18
lines changed

8 files changed

+132
-18
lines changed

.env.dist

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ KBC_RUNID=
22
SNOWFLAKE_HOST=
33
SNOWFLAKE_USER=
44
SNOWFLAKE_PASSWORD=
5+
SNOWFLAKE_KEYPAIR=
56
SNOWFLAKE_SCHEMA_KEYPAIR=
67
SNOWFLAKE_DATABASE=
78
SNOWFLAKE_WAREHOUSE=

.github/workflows/push.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,7 @@ env:
2020
SNOWFLAKE_WAREHOUSE: "DEV"
2121
SNOWFLAKE_USER: "DWHM_TRAVIS_TESTS"
2222
SNOWFLAKE_PASSWORD: "${{ secrets.SNOWFLAKE_PASSWORD }}"
23+
SNOWFLAKE_KEYPAIR: "${{ secrets.SNOWFLAKE_KEYPAIR }}"
2324
SNOWFLAKE_SCHEMA_KEYPAIR: "${{ secrets.SNOWFLAKE_SCHEMA_KEYPAIR }}"
2425
SNOWFLAKE_DATABASE: "DWHM_TRAVIS_TESTS"
2526
KBC_RUNID: "runId"
@@ -98,6 +99,7 @@ jobs:
9899
-e SNOWFLAKE_HOST \
99100
-e SNOWFLAKE_USER \
100101
-e SNOWFLAKE_PASSWORD \
102+
-e SNOWFLAKE_KEYPAIR \
101103
-e SNOWFLAKE_SCHEMA_KEYPAIR \
102104
-e SNOWFLAKE_DATABASE \
103105
-e SNOWFLAKE_WAREHOUSE \

docker-compose.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ services:
88
- SNOWFLAKE_HOST
99
- SNOWFLAKE_USER
1010
- SNOWFLAKE_PASSWORD
11+
- SNOWFLAKE_KEYPAIR
1112
- SNOWFLAKE_DATABASE
1213
- SNOWFLAKE_WAREHOUSE
1314
- SNOWFLAKE_SCHEMA_KEYPAIR

src/Config.php

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,35 @@
66

77
use Couchbase\UserSettings;
88
use Keboola\Component\Config\BaseConfig;
9+
use Keboola\Component\UserException;
910
use Keboola\SnowflakeDwhManager\Configuration\Schema;
1011
use Keboola\SnowflakeDwhManager\Configuration\SchemaDefinition;
1112
use Keboola\SnowflakeDwhManager\Configuration\User;
1213
use Keboola\SnowflakeDwhManager\Configuration\UserDefinition;
14+
use Symfony\Component\Config\Definition\ConfigurationInterface;
1315

1416
class Config extends BaseConfig
1517
{
18+
/**
19+
* @inheritDoc
20+
*/
21+
public function __construct(array $config, ?ConfigurationInterface $configDefinition = null)
22+
{
23+
/** @var array<string, array<string, mixed>> $config */
24+
$password = $config['parameters']['#master_password'] ?? '';
25+
$keyPair = $config['parameters']['#master_key_pair'] ?? null;
26+
27+
if (empty($password) && $keyPair === null) {
28+
throw new UserException('Either "password" or "keyPair" must be provided.');
29+
}
30+
31+
if (!empty($password) && !empty($keyPair)) {
32+
throw new UserException('Both "password" and "keyPair" cannot be set at the same time.');
33+
}
34+
35+
parent::__construct($config, $configDefinition);
36+
}
37+
1638
/**
1739
* @return string[]
1840
*/
@@ -22,6 +44,7 @@ public function getSnowflakeConnectionOptions(): array
2244
'host' => $this->getValue(['parameters', 'master_host']),
2345
'user' => $this->getValue(['parameters', 'master_user']),
2446
'password' => $this->getValue(['parameters', '#master_password']),
47+
'keyPair' => $this->getValue(['parameters', '#master_key_pair']),
2548
'database' => $this->getValue(['parameters', 'master_database']),
2649
'warehouse' => $this->getValue(['parameters', 'warehouse']),
2750
];

src/ConfigDefinition.php

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -28,8 +28,10 @@ protected function getParametersDefinition(): ArrayNodeDefinition
2828
->isRequired()
2929
->end()
3030
->scalarNode('#master_password')
31-
->cannotBeEmpty()
32-
->isRequired()
31+
->defaultValue('')
32+
->end()
33+
->scalarNode('#master_key_pair')
34+
->defaultNull()
3335
->end()
3436
->scalarNode('master_database')
3537
->cannotBeEmpty()

tests/functional/DatadirScenarioTest.php

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,26 @@ private static function getSchema3Config(): array
142142
];
143143
}
144144

145+
/**
146+
* @return array<string, array<mixed>>
147+
*/
148+
private static function getSchema4Config(): array
149+
{
150+
return [
151+
'parameters' => [
152+
'master_host' => getenv('SNOWFLAKE_HOST'),
153+
'master_user' => getenv('SNOWFLAKE_USER'),
154+
'#master_key_pair' => getenv('SNOWFLAKE_KEYPAIR'),
155+
'master_database' => getenv('SNOWFLAKE_DATABASE'),
156+
'warehouse' => getenv('SNOWFLAKE_WAREHOUSE'),
157+
'business_schema' => [
158+
'schema_name' => 'my_dwh_schema4',
159+
'key_pair' => getenv('SNOWFLAKE_SCHEMA_KEYPAIR'),
160+
],
161+
],
162+
];
163+
}
164+
145165
protected function getScript(): string
146166
{
147167
return $this->getTestFileDir() . '/../../src/run.php';
@@ -168,6 +188,23 @@ private static function getTestConfigs(): array
168188
];
169189
}
170190

191+
public function testCreateSchemaAsMasterUserWithKeyPair(): void
192+
{
193+
$schema4config = $this->getConfigFromConfigArray(self::getSchema4Config());
194+
$connection = $this->getConnectionForConfig($schema4config);
195+
196+
self::dropCreatedSchema($connection, $schema4config->getDatabase(), $schema4config->getSchema());
197+
198+
$this->runAppWithConfig(self::getSchema4Config());
199+
200+
$userName = implode('_', [$schema4config->getDatabase(), $schema4config->getSchema()->getName()]);
201+
202+
/** @var array<int, array<string, string|int>> $users */
203+
$users = $connection->fetchAll('SHOW USERS LIKE \'%' . $userName . '%\' LIMIT 1');
204+
205+
self::assertSame('SERVICE', $users[0]['type']);
206+
}
207+
171208
public function testCreateSchemaWithKeyPairUser(): void
172209
{
173210
$schema3config = $this->getConfigFromConfigArray(self::getSchema3Config());

tests/phpunit/ConfigDefinitionTest.php

Lines changed: 7 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -54,6 +54,7 @@ public function provideValidConfigs(): array
5454
'disabled' => false,
5555
'reset_password' => false,
5656
],
57+
'#master_key_pair' => null,
5758
],
5859
],
5960
[
@@ -92,6 +93,7 @@ public function provideValidConfigs(): array
9293
'statement_timeout' => 10800,
9394
'disabled' => false,
9495
],
96+
'#master_key_pair' => null,
9597
],
9698
],
9799
[
@@ -133,6 +135,7 @@ public function provideValidConfigs(): array
133135
'disabled' => false,
134136
'reset_password' => false,
135137
],
138+
'#master_key_pair' => null,
136139
],
137140
],
138141
[
@@ -174,6 +177,7 @@ public function provideValidConfigs(): array
174177
'disabled' => false,
175178
'reset_password' => false,
176179
],
180+
'#master_key_pair' => null,
177181
],
178182
],
179183
[
@@ -208,6 +212,7 @@ public function provideValidConfigs(): array
208212
'reset_password' => false,
209213
'key_pair' => null,
210214
],
215+
'#master_key_pair' => null,
211216
],
212217
],
213218
[
@@ -237,6 +242,7 @@ public function provideValidConfigs(): array
237242
'reset_password' => false,
238243
'key_pair' => null,
239244
],
245+
'#master_key_pair' => null,
240246
],
241247
],
242248
[
@@ -267,6 +273,7 @@ public function provideValidConfigs(): array
267273
'statement_timeout' => 10800,
268274
'key_pair' => null,
269275
],
276+
'#master_key_pair' => null,
270277
],
271278
],
272279
[
@@ -468,22 +475,6 @@ public function provideInvalidConfigs(): array
468475
],
469476
],
470477
],
471-
'empty master_password' => [
472-
InvalidConfigurationException::class,
473-
'The path "root.parameters.#master_password" cannot contain an empty value',
474-
[
475-
'parameters' => [
476-
'master_host' => 'host',
477-
'master_user' => 'user',
478-
'#master_password' => '',
479-
'master_database' => 'database',
480-
'warehouse' => 'warehouse',
481-
'user' => [
482-
'email' => '[email protected]',
483-
],
484-
],
485-
],
486-
],
487478
'empty master_database' => [
488479
InvalidConfigurationException::class,
489480
'The path "root.parameters.master_database" cannot contain an empty value',

tests/phpunit/ConfigTest.php

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,13 @@
44

55
namespace Keboola\SnowflakeDwhManager\Tests;
66

7+
use Keboola\Component\UserException;
78
use Keboola\SnowflakeDwhManager\Config;
89
use Keboola\SnowflakeDwhManager\ConfigDefinition;
910
use Keboola\SnowflakeDwhManager\Configuration\Schema;
1011
use Keboola\SnowflakeDwhManager\Configuration\User;
1112
use PHPUnit\Framework\TestCase;
13+
use Throwable;
1214

1315
class ConfigTest extends TestCase
1416
{
@@ -61,4 +63,59 @@ public function testGetSchema(): void
6163
$this->assertInstanceOf(Schema::class, $schema);
6264
$this->assertSame('dwh1', $schema->getName());
6365
}
66+
67+
/**
68+
* @param class-string<Throwable> $exceptionClass
69+
* @param array<string, array<class-string|string|array<mixed>>> $rawConfig
70+
*
71+
* @dataProvider invalidConfigsDataProvider
72+
*/
73+
public function testInvalidConfigs(string $exceptionClass, string $exceptionMessage, array $rawConfig): void
74+
{
75+
self::expectException($exceptionClass);
76+
self::expectExceptionMessage($exceptionMessage);
77+
new Config($rawConfig, new ConfigDefinition());
78+
}
79+
80+
/**
81+
* @return array<string, array<class-string|string|array<mixed>>>
82+
*/
83+
public static function invalidConfigsDataProvider(): array
84+
{
85+
return [
86+
'empty master_password & master_key_pair' => [
87+
UserException::class,
88+
'Either "password" or "keyPair" must be provided.',
89+
[
90+
'parameters' => [
91+
'master_host' => 'host',
92+
'master_user' => 'user',
93+
'#master_password' => '',
94+
'master_database' => 'database',
95+
'warehouse' => 'warehouse',
96+
'user' => [
97+
'email' => '[email protected]',
98+
],
99+
],
100+
],
101+
],
102+
'master_password & master_key_pair' => [
103+
UserException::class,
104+
'Both "password" and "keyPair" cannot be set at the same time.',
105+
[
106+
'parameters' => [
107+
'master_host' => 'host',
108+
'master_user' => 'user',
109+
'#master_password' => 'gr3eatpassword',
110+
'#master_key_pair' => getenv('SNOWFLAKE_KEYPAIR'),
111+
'master_database' => 'database',
112+
'warehouse' => 'warehouse',
113+
'user' => [
114+
'email' => '[email protected]',
115+
],
116+
],
117+
],
118+
],
119+
];
120+
}
64121
}

0 commit comments

Comments
 (0)