Database governance built for humans and agents — controlling changes and access across every major database.
Go cicddata-governancedata-maskingdata-securitydatabase-accessdatabase-governancedevopsdevsecopsflywaygitopsliquibasemongodbmysqloraclepostgresqlschema-migrationssecuritysql-clientsqlservertidb
fix(snowflake)!: require an encrypted private key's passphrase (#21880)
Danny Xucff5e388e628
diff --git a/backend/plugin/db/snowflake/snowflake.go b/backend/plugin/db/snowflake/snowflake.goindex f6c95149b2..5e80fcefe2 100644--- a/backend/plugin/db/snowflake/snowflake.go+++ b/backend/plugin/db/snowflake/snowflake.go@@ -443,9 +443,13 @@ func decodeRSAPrivateKey(key, passphrase string) (*rsa.PrivateKey, error) {}return rsaKey, nilcase "ENCRYPTED PRIVATE KEY":- // NOTE: As of Jun 2024, Golang's official library does not support passcode-encrypted PKCS8 private key. And- // Snowflake do not introduce an external library to achieve this due to the security purposes.- // We introduce https://pkg.go.dev/github.com/youmark/pkcs8 to help us to achieve this goal.+ // crypto/x509 cannot decrypt PKCS#8 keys and gosnowflake does not either, so+ // youmark/pkcs8 covers the PBES2 scheme. It reads an empty password as "the key+ // is unencrypted" and would misparse the ciphertext, so reject a missing+ // passphrase before calling it.+ if passphrase == "" {+ return nil, errors.Errorf("private key is encrypted, but no passphrase was provided")+ }pk, err := pkcs8.ParsePKCS8PrivateKeyRSA(block.Bytes, []byte(passphrase))if err != nil {return nil, errors.Wrapf(err, "failed to parse pkcs8 private key to rsa private key with passphrase")diff --git a/backend/plugin/db/snowflake/snowflake_test.go b/backend/plugin/db/snowflake/snowflake_test.goindex b68d380a5b..5c949a75bd 100644--- a/backend/plugin/db/snowflake/snowflake_test.go+++ b/backend/plugin/db/snowflake/snowflake_test.go@@ -2,9 +2,15 @@package snowflakeimport (+ "crypto/ed25519"+ "crypto/rand"+ "crypto/rsa"+ "crypto/x509"+ "encoding/pem""testing""github.com/stretchr/testify/require"+ "github.com/youmark/pkcs8"storepb "github.com/bytebase/bytebase/backend/generated-go/store""github.com/bytebase/bytebase/backend/plugin/db"@@ -76,3 +82,80 @@ func TestBuildSnowflakeDSN(t *testing.T) {require.Equal(t, testCase.want, got)}}++func TestDecodeRSAPrivateKey(t *testing.T) {+ key, err := rsa.GenerateKey(rand.Reader, 2048)+ require.NoError(t, err)+ unencrypted, err := x509.MarshalPKCS8PrivateKey(key)+ require.NoError(t, err)+ encrypted, err := pkcs8.MarshalPrivateKey(key, []byte("passphrase"), nil)+ require.NoError(t, err)+ _, edKey, err := ed25519.GenerateKey(rand.Reader)+ require.NoError(t, err)+ notRSA, err := x509.MarshalPKCS8PrivateKey(edKey)+ require.NoError(t, err)++ encodePEM := func(blockType string, der []byte) string {+ return string(pem.EncodeToMemory(&pem.Block{Type: blockType, Bytes: der}))+ }++ testCases := []struct {+ name string+ key string+ passphrase string+ wantErr string+ }{+ {+ name: "unencrypted key",+ key: encodePEM("PRIVATE KEY", unencrypted),+ },+ {+ name: "unencrypted key with an unnecessary passphrase",+ key: encodePEM("PRIVATE KEY", unencrypted),+ passphrase: "passphrase",+ },+ {+ name: "encrypted key with its passphrase",+ key: encodePEM("ENCRYPTED PRIVATE KEY", encrypted),+ passphrase: "passphrase",+ },+ {+ name: "encrypted key without a passphrase",+ key: encodePEM("ENCRYPTED PRIVATE KEY", encrypted),+ wantErr: "private key is encrypted, but no passphrase was provided",+ },+ {+ name: "encrypted key with the wrong passphrase",+ key: encodePEM("ENCRYPTED PRIVATE KEY", encrypted),+ passphrase: "wrong",+ wantErr: "incorrect password",+ },+ {+ name: "key that is not RSA",+ key: encodePEM("PRIVATE KEY", notRSA),+ wantErr: "expected RSA private key",+ },+ {+ name: "PKCS#1 block",+ key: encodePEM("RSA PRIVATE KEY", x509.MarshalPKCS1PrivateKey(key)),+ wantErr: "unsupported pem block type: RSA PRIVATE KEY",+ },+ {+ name: "not PEM at all",+ key: "rsa_key.p8",+ wantErr: "failed to get private key PEM block",+ },+ }+ for _, testCase := range testCases {+ t.Run(testCase.name, func(t *testing.T) {+ got, err := decodeRSAPrivateKey(testCase.key, testCase.passphrase)+ if testCase.wantErr != "" {+ require.ErrorContains(t, err, testCase.wantErr)+ require.Nil(t, got)+ return+ }+ require.NoError(t, err)+ require.True(t, got.Equal(key))+ })+ }+}diff --git a/go.mod b/go.modindex 709785aed4..50fddb0e51 100644--- a/go.mod+++ b/go.mod@@ -378,6 +378,4 @@ replace (github.com/pingcap/tidb => github.com/bytebase/tidb v0.0.0-20251104040057-d29df9dd1b3bgithub.com/pingcap/tidb/pkg/parser => github.com/bytebase/tidb/pkg/parser v0.0.0-20251104040057-d29df9dd1b3b-- github.com/youmark/pkcs8 => github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94)diff --git a/go.sum b/go.sumindex b7b6334849..db7a99ba66 100644--- a/go.sum+++ b/go.sum@@ -265,8 +265,6 @@ github.com/bytebase/omni v0.0.0-20261102094319-61f070707dc7 h1:3rwQ9dpUCGv19YJJfgithub.com/bytebase/omni v0.0.0-20261102094319-61f070707dc7/go.mod h1:9XZE1hqtvr09Oc+DdLu1/yCb8MDcoy/EPdA4AONDHlE=github.com/bytebase/pgx/v5 v5.0.0-20261103142809-663c807705c4 h1:CVgGkwGGV1wLd9Fju0XcY1cac2sf7IKOLA80bWGUwG8=github.com/bytebase/pgx/v5 v5.0.0-20261103142809-663c807705c4/go.mod h1:u2Fi6no03HCSBYE+ZVdLxbwdu3xyi2vU+K/YNJioSds=-github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94 h1:6PNsuNmSqCuR8Z616uQLzDIvujWCFsKzeVc3ECmVubk=-github.com/bytebase/pkcs8 v0.0.0-20240612095628-fcd0a7484c94/go.mod h1:SQliXeA7Dhkt//vS29v3zpbEwoa+zb2Cn5xj5uO4K5U=github.com/bytebase/tidb v0.0.0-20251104040057-d29df9dd1b3b h1:FIMLXCmledlGHw7NzIT2BGVflB2ZNamudc+QtTNITH0=github.com/bytebase/tidb v0.0.0-20251104040057-d29df9dd1b3b/go.mod h1:sXlW/IJbfyluk3KFfjOCNpN5CAtQos4/nxmRs2tCyn8=github.com/bytebase/tidb/pkg/parser v0.0.0-20251104040057-d29df9dd1b3b h1:TJweAUQzFg8ZqIVk780o7k7nTDkYtKQU/44s4Ukv7vk=@@ -1033,6 +1031,8 @@ github.com/xyproto/randomstring v1.0.5 h1:YtlWPoRdgMu3NZtP45drfy1GKoojuR7hmRcnhZgithub.com/xyproto/randomstring v1.0.5/go.mod h1:rgmS5DeNXLivK7YprL0pY+lTuhNQW3iGxZ18UQApw/E=github.com/yosida95/uritemplate/v3 v3.0.2 h1:Ed3Oyj9yrmi9087+NczuL5BwkIc4wvTb5zIM+UJPGz4=github.com/yosida95/uritemplate/v3 v3.0.2/go.mod h1:ILOh0sOhIJR3+L/8afwt/kE++YT040gmv5BQTMR2HP4=+github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78 h1:ilQV1hzziu+LLM3zUTJ0trRztfwgjqKnBWNtSRkbmwM=+github.com/youmark/pkcs8 v0.0.0-20240726163527-a2c0da244d78/go.mod h1:aL8wCCfTfSfmXjznFBSZNN13rSJjlIOI1fUNAtF7rmI=github.com/yterajima/go-sitemap v0.4.0 h1:JVgmfq4ch/XXIfKjSNlGqZudlPTBagNUgE6B9uBurm4=github.com/yterajima/go-sitemap v0.4.0/go.mod h1:CRU0eiLdQIFhxLoTQRgXlsPMNbENABe7h/l5+NGFNFw=github.com/yuin/goldmark v1.1.27/go.mod h1:3hX8gzYuyVAZsxl0MRgGTJEmQBFcNTphYh9decYSb74=