Skip to content

Commit

Permalink
Fix encoding snowflake ID for object identifiers
Browse files Browse the repository at this point in the history
  • Loading branch information
sfc-gh-asawicki committed Dec 12, 2023
1 parent 2d26046 commit 52b0695
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 24 deletions.
16 changes: 12 additions & 4 deletions pkg/helpers/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,10 +65,18 @@ func EncodeSnowflakeID(attributes ...interface{}) string {
// is attribute already an object identifier?
if len(attributes) == 1 {
if id, ok := attributes[0].(sdk.ObjectIdentifier); ok {
// remove quotes and replace dots with pipes
parts := strings.Split(id.FullyQualifiedName(), ".")
for i, part := range parts {
parts[i] = strings.Trim(part, `"`)
parts := make([]string, 0)
switch id.(type) {

Check failure on line 69 in pkg/helpers/helpers.go

View workflow job for this annotation

GitHub Actions / reviewdog

[golangci] reported by reviewdog 🐶 typeSwitchVar: 4 cases can benefit from type switch with assignment (gocritic) Raw Output: pkg/helpers/helpers.go:69:4: typeSwitchVar: 4 cases can benefit from type switch with assignment (gocritic) switch id.(type) { ^
case sdk.AccountObjectIdentifier:
parts = append(parts, id.(sdk.AccountObjectIdentifier).Name())

Check failure on line 71 in pkg/helpers/helpers.go

View workflow job for this annotation

GitHub Actions / reviewdog

[golangci] reported by reviewdog 🐶 S1034(related information): could eliminate this type assertion (gosimple) Raw Output: pkg/helpers/helpers.go:71:27: S1034(related information): could eliminate this type assertion (gosimple) parts = append(parts, id.(sdk.AccountObjectIdentifier).Name()) ^
case sdk.DatabaseObjectIdentifier:
parts = append(parts, id.(sdk.DatabaseObjectIdentifier).DatabaseName(), id.(sdk.DatabaseObjectIdentifier).Name())

Check failure on line 73 in pkg/helpers/helpers.go

View workflow job for this annotation

GitHub Actions / reviewdog

[golangci] reported by reviewdog 🐶 S1034(related information): could eliminate this type assertion (gosimple) Raw Output: pkg/helpers/helpers.go:73:27: S1034(related information): could eliminate this type assertion (gosimple) parts = append(parts, id.(sdk.DatabaseObjectIdentifier).DatabaseName(), id.(sdk.DatabaseObjectIdentifier).Name()) ^
case sdk.SchemaObjectIdentifier:
parts = append(parts, id.(sdk.SchemaObjectIdentifier).DatabaseName(), id.(sdk.SchemaObjectIdentifier).SchemaName(), id.(sdk.SchemaObjectIdentifier).Name())

Check failure on line 75 in pkg/helpers/helpers.go

View workflow job for this annotation

GitHub Actions / reviewdog

[golangci] reported by reviewdog 🐶 S1034(related information): could eliminate this type assertion (gosimple) Raw Output: pkg/helpers/helpers.go:75:27: S1034(related information): could eliminate this type assertion (gosimple) parts = append(parts, id.(sdk.SchemaObjectIdentifier).DatabaseName(), id.(sdk.SchemaObjectIdentifier).SchemaName(), id.(sdk.SchemaObjectIdentifier).Name()) ^
case sdk.TableColumnIdentifier:
parts = append(parts, id.(sdk.TableColumnIdentifier).DatabaseName(), id.(sdk.TableColumnIdentifier).SchemaName(), id.(sdk.TableColumnIdentifier).TableName(), id.(sdk.TableColumnIdentifier).Name())
default:
log.Panicf("Unsupported object identifier: %v", id)

Check failure

Code scanning / CodeQL

Clear-text logging of sensitive information High

Sensitive data returned by an access to passwordPolicy
flows to a logging call.
}
return strings.Join(parts, IDDelimiter)
}
Expand Down
52 changes: 34 additions & 18 deletions pkg/helpers/helpers_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,40 +80,40 @@ func TestEncodeSnowflakeID(t *testing.T) {
expectedEncodedID string
}{
"encodes account object identifier": {
identifier: sdk.NewAccountObjectIdentifier("account"),
expectedEncodedID: `account`,
identifier: sdk.NewAccountObjectIdentifier("database"),
expectedEncodedID: `database`,
},
"encodes quoted account object identifier": {
identifier: sdk.NewAccountObjectIdentifier("\"account\""),
expectedEncodedID: `account`,
identifier: sdk.NewAccountObjectIdentifier("\"database\""),
expectedEncodedID: `database`,
},
"encodes account object identifier with a dot": {
identifier: sdk.NewAccountObjectIdentifier("acc.ount"),
expectedEncodedID: `acc.ount`,
identifier: sdk.NewAccountObjectIdentifier("data.base"),
expectedEncodedID: `data.base`,
},
"encodes database object identifier": {
identifier: sdk.NewDatabaseObjectIdentifier("account", "database"),
expectedEncodedID: `account|database`,
identifier: sdk.NewDatabaseObjectIdentifier("database", "schema"),
expectedEncodedID: `database|schema`,
},
"encodes quoted database object identifier": {
identifier: sdk.NewDatabaseObjectIdentifier("\"account\"", "\"database\""),
expectedEncodedID: `account|database`,
identifier: sdk.NewDatabaseObjectIdentifier("\"database\"", "\"schema\""),
expectedEncodedID: `database|schema`,
},
"encodes database object identifier with dots": {
identifier: sdk.NewDatabaseObjectIdentifier("acc.ount", "data.base"),
expectedEncodedID: `acc.ount|data.base`,
identifier: sdk.NewDatabaseObjectIdentifier("data.base", "sche.ma"),
expectedEncodedID: `data.base|sche.ma`,
},
"encodes schema object identifier": {
identifier: sdk.NewSchemaObjectIdentifier("account", "database", "schema"),
expectedEncodedID: `account|database|schema`,
identifier: sdk.NewSchemaObjectIdentifier("database", "schema", "table"),
expectedEncodedID: `database|schema|table`,
},
"encodes quoted schema object identifier": {
identifier: sdk.NewSchemaObjectIdentifier("\"account\"", "\"database\"", "\"schema\""),
expectedEncodedID: `account|database|schema`,
identifier: sdk.NewSchemaObjectIdentifier("\"database\"", "\"schema\"", "\"table\""),
expectedEncodedID: `database|schema|table`,
},
"encodes schema object identifier with dots": {
identifier: sdk.NewSchemaObjectIdentifier("acc.ount", "data.base", "sche.ma"),
expectedEncodedID: `acc.ount|data.base|sche.ma`,
identifier: sdk.NewSchemaObjectIdentifier("data.base", "sche.ma", "tab.le"),
expectedEncodedID: `data.base|sche.ma|tab.le`,
},
"encodes table column identifier": {
identifier: sdk.NewTableColumnIdentifier("database", "schema", "table", "column"),
Expand All @@ -135,4 +135,20 @@ func TestEncodeSnowflakeID(t *testing.T) {
require.Equal(t, tc.expectedEncodedID, encodedID)
})
}

t.Run("panics for unsupported object identifier", func(t *testing.T) {
require.Panics(t, func() {
EncodeSnowflakeID(unsupportedObjectIdentifier{})
})
})
}

type unsupportedObjectIdentifier struct{}

func (i unsupportedObjectIdentifier) Name() string {
return "name"
}

func (i unsupportedObjectIdentifier) FullyQualifiedName() string {
return "fully qualified name"
}
11 changes: 9 additions & 2 deletions pkg/sdk/identifier_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,9 @@ type AccountObjectIdentifier struct {
}

func NewAccountObjectIdentifier(name string) AccountObjectIdentifier {
return AccountObjectIdentifier{name: name}
return AccountObjectIdentifier{
name: strings.Trim(name, `"`),
}
}

func NewAccountObjectIdentifierFromFullyQualifiedName(fullyQualifiedName string) AccountObjectIdentifier {
Expand Down Expand Up @@ -268,7 +270,12 @@ type TableColumnIdentifier struct {
}

func NewTableColumnIdentifier(databaseName, schemaName, tableName, columnName string) TableColumnIdentifier {
return TableColumnIdentifier{databaseName: databaseName, schemaName: schemaName, tableName: tableName, columnName: columnName}
return TableColumnIdentifier{
databaseName: strings.Trim(databaseName, `"`),
schemaName: strings.Trim(schemaName, `"`),
tableName: strings.Trim(tableName, `"`),
columnName: strings.Trim(columnName, `"`),
}
}

func NewTableColumnIdentifierFromFullyQualifiedName(fullyQualifiedName string) TableColumnIdentifier {
Expand Down

0 comments on commit 52b0695

Please sign in to comment.