Skip to content

Commit

Permalink
validate settings for c4gh.keys as a temp solution
Browse files Browse the repository at this point in the history
  • Loading branch information
nanjiangshu committed Dec 17, 2024
1 parent 3ae8dac commit 93dcc15
Showing 1 changed file with 22 additions and 23 deletions.
45 changes: 22 additions & 23 deletions sda/internal/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -367,7 +367,27 @@ func NewConfig(app string) (*Config, error) {
requiredConfVars = []string{
"c4gh.filepath",
"c4gh.passphrase",
"c4gh.keys",
}
// This should be refactored once the old method for reading a single key file is removed
if viper.IsSet("c4gh.keys") {
// Check if at least one key entry has a valid filepath and passphrase
var keyConfigs []map[string]string
if err := viper.UnmarshalKey("c4gh.keys", &keyConfigs); err != nil {
return nil, fmt.Errorf("failed to parse key configurations: %v", err)
}

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

break
}
}

if !atLeastOneKeySet {
return nil, fmt.Errorf("at least one valid entry in c4gh.keys must have a filepath and passphrase set")
}
}
case "s3inbox":
requiredConfVars = []string{
Expand Down Expand Up @@ -461,28 +481,7 @@ func NewConfig(app string) (*Config, error) {
}

for _, s := range requiredConfVars {
if s == "c4gh.keys" {
if viper.IsSet("c4gh.keys") {
// Check if at least one key entry has a valid filepath and passphrase
var keyConfigs []map[string]string
if err := viper.UnmarshalKey("c4gh.keys", &keyConfigs); err != nil {
return nil, fmt.Errorf("failed to parse key configurations: %v", err)
}

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

break
}
}

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) {
if !viper.IsSet(s) {
return nil, fmt.Errorf("%s not set", s)
}
}
Expand Down

0 comments on commit 93dcc15

Please sign in to comment.