Skip to content

Commit

Permalink
modify config only for the ingest
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjiangshu committed Dec 16, 2024
1 parent 2df6bcb commit f8f0a46
Show file tree
Hide file tree
Showing 3 changed files with 89 additions and 19 deletions.
2 changes: 1 addition & 1 deletion sda/cmd/ingest/ingest.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,7 +61,7 @@ func main() {
sigc <- syscall.SIGINT
panic(err)
}
keyList, err := config.GetC4GHKey()
keyList, err := config.GetC4GHKeyList()
if err != nil {
log.Error(err)
sigc <- syscall.SIGINT
Expand Down
58 changes: 45 additions & 13 deletions sda/cmd/ingest/ingest_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,13 +36,31 @@ func (suite *TestSuite) SetupTest() {
pubKeyList = append(pubKeyList, publicKey)

tempDir := suite.T().TempDir()
privateKeyFile, err := os.Create(fmt.Sprintf("%s/c4fg.key", tempDir))

// Write the first private key file
privateKeyFile1, err := os.Create(fmt.Sprintf("%s/c4gh1.key", tempDir))
assert.NoError(suite.T(), err)
err = keys.WriteCrypt4GHX25519PrivateKey(privateKeyFile1, privateKey, []byte("c4ghpass1"))
assert.NoError(suite.T(), err)
privateKeyFile1.Close()

// Write the second private key file
privateKeyFile2, err := os.Create(fmt.Sprintf("%s/c4gh2.key", tempDir))
assert.NoError(suite.T(), err)
err = keys.WriteCrypt4GHX25519PrivateKey(privateKeyFile, privateKey, []byte("password"))
err = keys.WriteCrypt4GHX25519PrivateKey(privateKeyFile2, privateKey, []byte("c4ghpass2"))
assert.NoError(suite.T(), err)
viper.Set("c4gh.filepath", fmt.Sprintf("%s/c4fg.key", tempDir))
viper.Set("c4gh.passphrase", "password")
privateKeyFile2.Close()

viper.Set("c4gh.keys", []map[string]string{
{
"filePath": fmt.Sprintf("%s/c4gh1.key", tempDir),
"passphrase": "c4ghpass1",
},
{
"filePath": fmt.Sprintf("%s/c4gh2.key", tempDir),
"passphrase": "c4ghpass2",
},
})
viper.Set("broker.host", "test")
viper.Set("broker.port", 123)
viper.Set("broker.user", "test")
Expand All @@ -67,11 +85,20 @@ func (suite *TestSuite) TestTryDecrypt_wrongFile() {
buf, err := io.ReadAll(file)
assert.NoError(suite.T(), err)

key, err := config.GetC4GHKey()
assert.Nil(suite.T(), err)
b, err := tryDecrypt(key, buf)
assert.Nil(suite.T(), b)
assert.EqualError(suite.T(), err, "not a Crypt4GH file")
keyList, err := config.GetC4GHKeyList()
assert.NoError(suite.T(), err)

var decryptionSuccessful bool
for _, key := range keyList {
b, err := tryDecrypt(key, buf)
if b != nil || err == nil {
decryptionSuccessful = true
break

Check failure on line 96 in sda/cmd/ingest/ingest_test.go

View workflow job for this annotation

GitHub Actions / Lint sda code

break with no blank line before (nlreturn)
}
assert.EqualError(suite.T(), err, "not a Crypt4GH file")
}

assert.False(suite.T(), decryptionSuccessful, "Decryption should not succeed with any key")
}

func (suite *TestSuite) TestTryDecrypt() {
Expand Down Expand Up @@ -102,9 +129,14 @@ func (suite *TestSuite) TestTryDecrypt() {
buf, err := io.ReadAll(file)
assert.NoError(suite.T(), err)

key, err := config.GetC4GHKey()
assert.NoError(suite.T(), err)
header, err := tryDecrypt(key, buf)
keyList, err := config.GetC4GHKeyList()
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), header)
for _, key := range keyList {
header, err := tryDecrypt(key, buf)
if header != nil && err == nil {
break
}
assert.NoError(suite.T(), err)
assert.NotNil(suite.T(), header)
}
}
48 changes: 43 additions & 5 deletions sda/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ type Config struct {

type ReEncConfig struct {
APIConf
Crypt4GHKeyList []*[32]byte
Crypt4GHKey *[32]byte
}

type Sync struct {
Expand Down Expand Up @@ -367,6 +367,7 @@ func NewConfig(app string) (*Config, error) {
requiredConfVars = []string{
"c4gh.filepath",
"c4gh.passphrase",
"c4gh.keys",
}
case "s3inbox":
requiredConfVars = []string{
Expand Down Expand Up @@ -460,7 +461,23 @@ func NewConfig(app string) (*Config, error) {
}

for _, s := range requiredConfVars {
if !viper.IsSet(s) {
if s == "c4gh.keys" {
// Check if at least one key entry has a valid filepath and passphrase
var keysConfig []map[string]string
viper.UnmarshalKey("c4gh.keys", &keysConfig)

Check failure on line 467 in sda/internal/config/config.go

View workflow job for this annotation

GitHub Actions / Lint sda code

Error return value of `viper.UnmarshalKey` is not checked (errcheck)

atLeastOneKeySet := false
for _, key := range keysConfig {
if key["filepath"] != "" && key["passphrase"] != "" {
atLeastOneKeySet = true
break

Check failure on line 473 in sda/internal/config/config.go

View workflow job for this annotation

GitHub Actions / Lint sda code

break with no blank line before (nlreturn)
}
}

if !atLeastOneKeySet {
return nil, fmt.Errorf("at least one valid entry in c4gh.keys must have a filepath and passphrase set")
}
} else if !viper.IsSet(s) {
return nil, fmt.Errorf("%s not set", s)
}
}
Expand Down Expand Up @@ -922,7 +939,7 @@ func (c *Config) configReEncryptServer() (err error) {
c.ReEncrypt.Port = 50443
}

c.ReEncrypt.Crypt4GHKeyList, err = GetC4GHKey()
c.ReEncrypt.Crypt4GHKey, err = GetC4GHKey()
if err != nil {
return err
}
Expand Down Expand Up @@ -1050,8 +1067,29 @@ func (c *Config) configSyncAPI() {

}

// GetC4GHKey reads and decrypts keys and returns a list of c4gh keys
func GetC4GHKey() ([]*[32]byte, error) {
// GetC4GHKey reads and decrypts and returns the c4gh key
func GetC4GHKey() (*[32]byte, error) {
keyPath := viper.GetString("c4gh.filepath")
passphrase := viper.GetString("c4gh.passphrase")

// Make sure the key path and passphrase is valid
keyFile, err := os.Open(keyPath)
if err != nil {
return nil, err
}

key, err := keys.ReadPrivateKey(keyFile, []byte(passphrase))
if err != nil {
return nil, err
}

keyFile.Close()

return &key, nil
}

// GetC4GHKeyList reads and decrypts keys and returns a list of c4gh keys
func GetC4GHKeyList() ([]*[32]byte, error) {
// Retrieve the list of key configurations from the YAML file
var keyConfigs []KeyConfig
if err := viper.UnmarshalKey("c4gh.keys", &keyConfigs); err != nil {
Expand Down

0 comments on commit f8f0a46

Please sign in to comment.