Skip to content

Commit

Permalink
Clean up.
Browse files Browse the repository at this point in the history
  • Loading branch information
Tang8330 committed Jul 24, 2024
1 parent eb56ba4 commit 575c4d2
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 31 deletions.
39 changes: 8 additions & 31 deletions lib/config/snowflake.go
Original file line number Diff line number Diff line change
@@ -1,28 +1,24 @@
package config

import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"

"github.com/artie-labs/transfer/lib/crypto"

"github.com/artie-labs/transfer/lib/ptr"
"github.com/snowflakedb/gosnowflake"
)

type Snowflake struct {
AccountID string `yaml:"account"`

Username string `yaml:"username"`
Username string `yaml:"username"`
// If pathToPrivateKey is specified, the password field will be ignored
PathToPrivateKey string `json:"pathToPrivateKey,omitempty"`
PathToPrivateKey string `yaml:"pathToPrivateKey,omitempty"`
Password string `yaml:"password,omitempty"`

Warehouse string `yaml:"warehouse"`
Region string `yaml:"region"`
Host string `yaml:"host"`

Warehouse string `yaml:"warehouse"`
Region string `yaml:"region"`
Host string `yaml:"host"`
Application string `yaml:"application"`
}

Expand All @@ -40,7 +36,7 @@ func (s Snowflake) ToConfig() (*gosnowflake.Config, error) {
}

if s.PathToPrivateKey != "" {
key, err := loadPrivateKey(s.PathToPrivateKey)
key, err := crypto.LoadRSAKey(s.PathToPrivateKey)
if err != nil {
return nil, fmt.Errorf("failed to load private key: %w", err)
}
Expand All @@ -59,22 +55,3 @@ func (s Snowflake) ToConfig() (*gosnowflake.Config, error) {

return cfg, nil
}

func loadPrivateKey(path string) (*rsa.PrivateKey, error) {
keyBytes, err := os.ReadFile(path)
if err != nil {
return nil, err
}

block, _ := pem.Decode(keyBytes)
if block == nil || block.Type != "RSA PRIVATE KEY" {
return nil, fmt.Errorf("failed to decode PEM block containing private key")
}

key, err := x509.ParsePKCS1PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
}

return key, nil
}
37 changes: 37 additions & 0 deletions lib/crypto/rsa.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
package crypto

import (
"crypto/rsa"
"crypto/x509"
"encoding/pem"
"fmt"
"os"
)

func LoadRSAKey(filePath string) (*rsa.PrivateKey, error) {
keyBytes, err := os.ReadFile(filePath)
if err != nil {
return nil, fmt.Errorf("failed to read file: %w", err)
}

return ParseRSAPrivateKey(keyBytes)
}

func ParseRSAPrivateKey(keyBytes []byte) (*rsa.PrivateKey, error) {
block, _ := pem.Decode(keyBytes)
if block == nil {
return nil, fmt.Errorf("failed to decode PEM block containing private key")
}

key, err := x509.ParsePKCS8PrivateKey(block.Bytes)
if err != nil {
return nil, fmt.Errorf("failed to parse private key: %v", err)
}

rsaKey, ok := key.(*rsa.PrivateKey)
if !ok {
return nil, fmt.Errorf("not an RSA private key, rather: %T", key)
}

return rsaKey, nil
}

0 comments on commit 575c4d2

Please sign in to comment.