serverinit: allow jsonconfig expansions in high-level configs.

Change-Id: I600fc56b9324143559ba594ed41a391296871689
This commit is contained in:
Brad Fitzpatrick 2014-08-06 10:49:27 -07:00
parent 27102a044e
commit 0757937177
2 changed files with 42 additions and 21 deletions

View File

@ -43,7 +43,6 @@ import (
"camlistore.org/pkg/jsonconfig"
"camlistore.org/pkg/server/app"
"camlistore.org/pkg/types/serverconfig"
"camlistore.org/pkg/wkfs"
)
const camliPrefix = "/camli/"
@ -412,7 +411,7 @@ func detectConfigChange(conf jsonconfig.Obj) error {
// with boolean value true, the configuration is assumed to be a high-level
// "user config" file, and transformed into a low-level config.
func LoadFile(filename string) (*Config, error) {
return load(filename, nil, nil)
return load(filename, nil)
}
type jsonFileImpl struct {
@ -428,7 +427,7 @@ func (f jsonFileImpl) Name() string { return f.name }
// with boolean value true, the configuration is assumed to be a high-level
// "user config" file, and transformed into a low-level config.
func Load(config []byte) (*Config, error) {
return load("", config, func(filename string) (jsonconfig.File, error) {
return load("", func(filename string) (jsonconfig.File, error) {
if filename != "" {
return nil, errors.New("JSON files with includes not supported with jsonconfig.Load")
}
@ -436,17 +435,7 @@ func Load(config []byte) (*Config, error) {
})
}
func load(filename string, rootConfig []byte, opener func(filename string) (jsonconfig.File, error)) (*Config, error) {
if filename == "" {
if rootConfig == nil || opener == nil {
panic("internal error")
}
} else {
if rootConfig != nil || opener != nil {
panic("internal error")
}
}
func load(filename string, opener func(filename string) (jsonconfig.File, error)) (*Config, error) {
c := &jsonconfig.ConfigParser{Open: opener}
m, err := c.ReadFile(filename)
if err != nil {
@ -461,20 +450,23 @@ func load(filename string, rootConfig []byte, opener func(filename string) (json
return conf, nil
}
// Check whether the high-level config uses the old names.
if err := detectConfigChange(obj); err != nil {
return nil, err
}
if rootConfig == nil {
rootConfig, err = wkfs.ReadFile(filename)
if err != nil {
return nil, fmt.Errorf("Could not read %s: %v", filename, err)
}
// Because the original high-level config might have expanded
// through the use of functions, we re-encode the map back to
// JSON here so we can unmarshal it into the hiLevelConf
// struct later.
highExpandedJSON, err := json.Marshal(m)
if err != nil {
return nil, fmt.Errorf("Can't re-marshal high-level JSON config: %v", err)
}
var hiLevelConf serverconfig.Config
if err := json.Unmarshal(rootConfig, &hiLevelConf); err != nil {
return nil, fmt.Errorf("Could not unmarshal %s into a serverconfig.Config: %v", filename, err)
if err := json.Unmarshal(highExpandedJSON, &hiLevelConf); err != nil {
return nil, fmt.Errorf("Could not unmarshal into a serverconfig.Config: %v", err)
}
conf, err = genLowLevelConfig(&hiLevelConf)

View File

@ -33,6 +33,7 @@ import (
"testing"
"camlistore.org/pkg/jsonconfig"
"camlistore.org/pkg/osutil"
"camlistore.org/pkg/serverinit"
"camlistore.org/pkg/test"
"camlistore.org/pkg/types/serverconfig"
@ -224,3 +225,31 @@ func canonicalizeGolden(t *testing.T, v []byte) []byte {
}
return v
}
func TestExpansionsInHighlevelConfig(t *testing.T) {
camroot, err := osutil.GoPackagePath("camlistore.org")
if err != nil {
t.Fatalf("failed to find camlistore.org GOPATH root: %v", err)
}
const keyID = "26F5ABDA"
os.Setenv("TMP_EXPANSION_TEST", keyID)
os.Setenv("TMP_EXPANSION_SECRING", filepath.Join(camroot, filepath.FromSlash("pkg/jsonsign/testdata/test-secring.gpg")))
conf, err := serverinit.Load([]byte(`
{
"auth": "localhost",
"listen": ":4430",
"https": false,
"identity": ["_env", "${TMP_EXPANSION_TEST}"],
"identitySecretRing": ["_env", "${TMP_EXPANSION_SECRING}"],
"googlecloudstorage": ":camlistore-dev-blobs",
"kvIndexFile": "/tmp/camli-index.kvdb"
}
`))
if err != nil {
t.Fatal(err)
}
got := fmt.Sprintf("%#v", conf)
if !strings.Contains(got, keyID) {
t.Errorf("Expected key %q in resulting low-level config. Got: %s", got)
}
}