Skip to content

Commit a74c8ab

Browse files
authored
Merge pull request #7 from ApplauseOSS/feat/update-snowflake-connection
2 parents c5d72eb + 5b81c16 commit a74c8ab

3 files changed

Lines changed: 20 additions & 21 deletions

File tree

README.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ CREATE USER IF NOT EXISTS SNOWFLIZZLE_SVC
6666
DISPLAY_NAME = 'SNOWFLIZZLE_SVC'
6767
COMMENT = 'Service user for automation'
6868
RSA_PUBLIC_KEY = '<paste contents of rsa_key.pub>'
69-
DEFAULT_ROLE = 'SECURITYADMIN'
69+
DEFAULT_ROLE = 'ACCOUNTADMIN'
7070
DEFAULT_WAREHOUSE = 'SNOWFLIZZLE'
7171
TYPE = 'SERVICE';
7272

config/snowflake.go

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -63,13 +63,12 @@ func ConnectToSnowflake() (*sql.DB, error) {
6363
return nil, err
6464
}
6565

66-
// Create a Snowflake configuration
66+
// Do NOT specify a Role, so the user's default role is used
6767
cfg := &sf.Config{
6868
Account: account,
6969
User: user,
7070
Authenticator: sf.AuthTypeJwt,
7171
PrivateKey: rsaPrivateKey,
72-
Role: "SECURITYADMIN",
7372
}
7473

7574
dsn, err := sf.DSN(cfg)
@@ -98,9 +97,9 @@ func ConnectToSnowflake() (*sql.DB, error) {
9897
return nil, err
9998
}
10099

101-
if currentRole != "SECURITYADMIN" {
102-
logger.Error("Role must be SECURITYADMIN.")
103-
return nil, errors.New("user must be SECURITYADMIN")
100+
// Warn if role is not SYSADMIN or ACCOUNTADMIN
101+
if currentRole != "SYSADMIN" && currentRole != "ACCOUNTADMIN" {
102+
logger.Warn("Role needs to have privileges to manage users, warehouses, databases. Current role: " + currentRole)
104103
}
105104

106105
logger.Debug("Current role:", "role", currentRole)

internal/role/role.go

Lines changed: 15 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -343,37 +343,37 @@ func (rp *RoleProcessor) createRoleIfNotExists() error {
343343
}
344344

345345
// createWarehouseIfNotExists creates a warehouse with the same name as the role, uppercased and postfixed with _WH, if it does not exist.
346-
func (rp *RoleProcessor) createWarehouseIfNotExists() error {
346+
func (rp *RoleProcessor) createWarehouseIfNotExists() (string, error) {
347347
warehouseName := rp.roleName + "_WAREHOUSE"
348348
qWarehouse := quoteIdentifier(warehouseName)
349349

350350
// Use cached warehouses instead of fetching every time
351351
if _, exists := rp.existingWarehouses[warehouseName]; exists {
352352
rp.logger.Info("Warehouse already exists", "warehouse", warehouseName)
353-
return nil
353+
return warehouseName, nil
354354
}
355355

356356
createWarehouseQuery := fmt.Sprintf(
357357
`CREATE WAREHOUSE IF NOT EXISTS %s
358-
WITH
359-
WAREHOUSE_SIZE = XSMALL
360-
WAREHOUSE_TYPE = STANDARD
361-
AUTO_SUSPEND = 60
362-
AUTO_RESUME = TRUE
363-
INITIALLY_SUSPENDED = TRUE`,
358+
WITH
359+
WAREHOUSE_SIZE = XSMALL
360+
WAREHOUSE_TYPE = STANDARD
361+
AUTO_SUSPEND = 60
362+
AUTO_RESUME = TRUE
363+
INITIALLY_SUSPENDED = TRUE`,
364364
qWarehouse,
365365
)
366366
if rp.dryRun {
367367
rp.logger.Info("[DryRun] Would create warehouse", "query", createWarehouseQuery, "warehouse", warehouseName)
368-
return nil
368+
return warehouseName, nil
369369
}
370370
rp.logger.Info("Creating warehouse", "query", createWarehouseQuery, "warehouse", warehouseName)
371371
if _, err := rp.db.Exec(createWarehouseQuery); err != nil {
372-
return fmt.Errorf("failed to create warehouse %s: %w", warehouseName, err)
372+
return warehouseName, fmt.Errorf("failed to create warehouse %s: %w", warehouseName, err)
373373
}
374374
// Add to cache so subsequent calls know it exists
375375
rp.existingWarehouses[warehouseName] = warehouseName
376-
return nil
376+
return warehouseName, nil
377377
}
378378

379379
func (rp *RoleProcessor) grantRoleToUser() error {
@@ -389,8 +389,7 @@ func (rp *RoleProcessor) grantRoleToUser() error {
389389
return nil
390390
}
391391

392-
func (rp *RoleProcessor) grantWarehouseToRole() error {
393-
warehouseName := rp.roleName + "_WH"
392+
func (rp *RoleProcessor) grantWarehouseToRole(warehouseName string) error {
394393
qWarehouse := quoteIdentifier(warehouseName)
395394
grantQuery := fmt.Sprintf("GRANT USAGE ON WAREHOUSE %s TO ROLE %s", qWarehouse, rp.qRole)
396395
if rp.dryRun {
@@ -653,10 +652,11 @@ func (rp *RoleProcessor) Process() error {
653652

654653
// process warehouse creation and grants, except for system-defined roles
655654
if _, isSystem := systemRoles[rp.roleName]; !isSystem {
656-
if err := rp.createWarehouseIfNotExists(); err != nil {
655+
warehouseName, err := rp.createWarehouseIfNotExists()
656+
if err != nil {
657657
return err
658658
}
659-
if err := rp.grantWarehouseToRole(); err != nil {
659+
if err := rp.grantWarehouseToRole(warehouseName); err != nil {
660660
return err
661661
}
662662
}

0 commit comments

Comments
 (0)