Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support drop-in config files #240

Merged
merged 2 commits into from
Dec 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
33 changes: 32 additions & 1 deletion internal/broker/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package broker
import (
"errors"
"fmt"
"io/fs"
"os"
"path/filepath"
"strings"

"gopkg.in/ini.v1"
Expand All @@ -27,11 +30,39 @@ const (
sshSuffixesKey = "ssh_allowed_suffixes"
)

func getDropInFiles(cfgPath string) ([]any, error) {
// Check if a .d directory exists and return the paths to the files in it.
dropInDir := cfgPath + ".d"
files, err := os.ReadDir(dropInDir)
if errors.Is(err, fs.ErrNotExist) {
return nil, nil
}
if err != nil {
return nil, err
}

var dropInFiles []any
denisonbarbosa marked this conversation as resolved.
Show resolved Hide resolved
// files is empty if the directory does not exist
for _, file := range files {
if file.IsDir() {
continue
}
dropInFiles = append(dropInFiles, filepath.Join(dropInDir, file.Name()))
}

return dropInFiles, nil
}

// parseConfigFile parses the config file and returns a map with the configuration keys and values.
func parseConfigFile(cfgPath string) (userConfig, error) {
cfg := userConfig{}

iniCfg, err := ini.Load(cfgPath)
dropInFiles, err := getDropInFiles(cfgPath)
if err != nil {
return cfg, err
}

iniCfg, err := ini.Load(cfgPath, dropInFiles...)
if err != nil {
return cfg, err
}
Expand Down
46 changes: 43 additions & 3 deletions internal/broker/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,17 @@ client_id = <CLIENT_ID
[oidc]
issuer = https://<ISSUER_URL>
client_id = <CLIENT_ID>
`,

"overwrite_lower_precedence": `
[oidc]
issuer = https://lower-precedence-issuer.url.com
client_id = lower_precedence_client_id
`,

"overwrite_higher_precedence": `
[oidc]
issuer = https://higher-precedence-issuer.url.com
`,
}

Expand All @@ -48,17 +59,21 @@ func TestParseConfig(t *testing.T) {

tests := map[string]struct {
configType string
dropInType string

wantErr bool
}{
"Successfully_parse_config_file": {},
"Successfully_parse_config_file_with_optional_values": {configType: "valid+optional"},
"Successfully_parse_config_with_drop_in_files": {dropInType: "valid"},

"Do_not_fail_if_values_contain_a_single_template_delimiter": {configType: "singles"},

"Error_if_file_does_not_exist": {configType: "inexistent", wantErr: true},
"Error_if_file_is_unreadable": {configType: "unreadable", wantErr: true},
"Error_if_file_is_not_updated": {configType: "template", wantErr: true},
"Error_if_file_does_not_exist": {configType: "inexistent", wantErr: true},
"Error_if_file_is_unreadable": {configType: "unreadable", wantErr: true},
"Error_if_file_is_not_updated": {configType: "template", wantErr: true},
"Error_if_drop_in_directory_is_unreadable": {dropInType: "unreadable-dir", wantErr: true},
"Error_if_drop_in_file_is_unreadable": {dropInType: "unreadable-file", wantErr: true},
}
for name, tc := range tests {
t.Run(name, func(t *testing.T) {
Expand All @@ -81,6 +96,31 @@ func TestParseConfig(t *testing.T) {
require.NoError(t, err, "Setup: Failed to make config file unreadable")
}

dropInDir := confPath + ".d"
if tc.dropInType != "" {
err = os.Mkdir(dropInDir, 0700)
require.NoError(t, err, "Setup: Failed to create drop-in directory")
}

switch tc.dropInType {
case "valid":
// Create multiple drop-in files to test that they are loaded in the correct order.
err = os.WriteFile(filepath.Join(dropInDir, "00-drop-in.conf"), []byte(configTypes["overwrite_lower_precedence"]), 0600)
require.NoError(t, err, "Setup: Failed to write drop-in file")
err = os.WriteFile(filepath.Join(dropInDir, "01-drop-in.conf"), []byte(configTypes["overwrite_higher_precedence"]), 0600)
require.NoError(t, err, "Setup: Failed to write drop-in file")
// Create the main config file, to test that the options which are not overwritten by the drop-in files
// are still present.
err = os.WriteFile(confPath, []byte(configTypes["valid+optional"]), 0600)
require.NoError(t, err, "Setup: Failed to write config file")
case "unreadable-dir":
err = os.Chmod(dropInDir, 0000)
require.NoError(t, err, "Setup: Failed to make drop-in directory unreadable")
case "unreadable-file":
err = os.WriteFile(filepath.Join(dropInDir, "00-drop-in.conf"), []byte(configTypes["valid"]), 0000)
require.NoError(t, err, "Setup: Failed to make drop-in file unreadable")
}

cfg, err := parseConfigFile(confPath)
if tc.wantErr {
require.Error(t, err)
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
clientID=lower_precedence_client_id
clientSecret=
issuerURL=https://higher-precedence-issuer.url.com
homeBaseDir=/home
allowedSSHSuffixes=[]
Loading