Skip to content

Commit

Permalink
Process file names in config file correctly
Browse files Browse the repository at this point in the history
If a file name in the config file is not absolute,
it must be processed relative to the location of
the config file.  See cli/server/config.go for calls
to the abs function for making them absolute.

https://jira.hyperledger.org/browse/FAB-1549

Change-Id: If12f12e291cb105efca9e9cb6004b9315eb9e141
Signed-off-by: Keith Smith <[email protected]>
  • Loading branch information
Keith Smith committed Jan 8, 2017
1 parent 72a87e3 commit a5666ff
Show file tree
Hide file tree
Showing 9 changed files with 115 additions and 231 deletions.
2 changes: 1 addition & 1 deletion cli/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -214,7 +214,7 @@ func TestLast(t *testing.T) {

func runServer() {
os.Setenv("COP_DEBUG", "true")
server.Start("../../testdata", "testconfig2.json")
server.Start("../../testdata", "testconfig.json")
}

func startServer() {
Expand Down
90 changes: 0 additions & 90 deletions cli/server/bootstrap_test.go

This file was deleted.

113 changes: 78 additions & 35 deletions cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ import (
"fmt"
"io/ioutil"
"os"
"path/filepath"

"github.com/cloudflare/cfssl/cli"
"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-cop/cli/server/ldap"
"github.com/hyperledger/fabric-cop/idp"
"github.com/hyperledger/fabric-cop/lib/tls"
"github.com/hyperledger/fabric-cop/util"

_ "github.com/mattn/go-sqlite3" // Needed to support sqlite
)
Expand Down Expand Up @@ -79,42 +81,63 @@ var CFG *Config

// Init initializes the COP config given the CFSSL config
func configInit(cfg *cli.Config) {
log.Debugf("config.Init file=%s", cfg.ConfigFile)
CFG = newConfig()

if cfg.ConfigFile != "" {
body, err := ioutil.ReadFile(cfg.ConfigFile)
if err != nil {
panic(err.Error())
}
log.Debugf("config.Init contents=%+v", body)
err = json.Unmarshal(body, CFG)
if err != nil {
panic(fmt.Sprintf("error parsing %s: %s", cfg.ConfigFile, err.Error()))
}

configFile = cfg.ConfigFile
cfg.DBConfigFile = configFile

cfg.CAFile = CFG.CAFile
cfg.CAKeyFile = CFG.KeyFile

if CFG.TLSConf.TLSCertFile != "" {
cfg.TLSCertFile = CFG.TLSConf.TLSCertFile
} else {
cfg.TLSCertFile = CFG.CAFile
}

if CFG.TLSConf.TLSKeyFile != "" {
cfg.TLSKeyFile = CFG.TLSConf.TLSKeyFile
} else {
cfg.TLSKeyFile = CFG.KeyFile
}

if CFG.TLSConf.MutualTLSCAFile != "" {
cfg.MutualTLSCAFile = CFG.TLSConf.MutualTLSCAFile
}
var err error
configFile, err = filepath.Abs(cfg.ConfigFile)
if err != nil {
panic(err.Error())
}
configDir = filepath.Dir(configFile)
log.Debugf("Initializing config file at %s", configFile)
log.Debugf("Inbound CFSSL server config is: %+v", cfg)

CFG = new(Config)
CFG.Authentication = true

body, err := ioutil.ReadFile(configFile)
if err != nil {
panic(err.Error())
}
err = json.Unmarshal(body, CFG)
if err != nil {
panic(fmt.Sprintf("error parsing %s: %s", configFile, err.Error()))
}

CFG.CAFile = abs(CFG.CAFile)
CFG.KeyFile = abs(CFG.KeyFile)
CFG.TLSConf.TLSCertFile = abs(CFG.TLSConf.TLSCertFile)
CFG.TLSConf.TLSKeyFile = abs(CFG.TLSConf.TLSKeyFile)
CFG.TLSConf.MutualTLSCAFile = abs(CFG.TLSConf.MutualTLSCAFile)
absTLSClient(&CFG.TLSConf.DBClient)

if cfg.DBConfigFile == "" {
cfg.DBConfigFile = cfg.ConfigFile
}

if CFG.TLSConf.TLSCertFile != "" {
cfg.TLSCertFile = CFG.TLSConf.TLSCertFile
} else {
cfg.TLSCertFile = CFG.CAFile
}

if CFG.TLSConf.TLSKeyFile != "" {
cfg.TLSKeyFile = CFG.TLSConf.TLSKeyFile
} else {
cfg.TLSKeyFile = CFG.KeyFile
}

if CFG.TLSConf.MutualTLSCAFile != "" {
cfg.MutualTLSCAFile = CFG.TLSConf.MutualTLSCAFile
}

if CFG.DBdriver == "" {
msg := "No database specified, a database is needed to run COP server. Using default - Type: SQLite, Name: cop.db"
log.Info(msg)
CFG.DBdriver = sqlite
CFG.DataSource = "cop.db"
}

if CFG.DBdriver == sqlite {
CFG.DataSource = abs(CFG.DataSource)
}

dbg := os.Getenv("COP_DEBUG")
Expand All @@ -125,4 +148,24 @@ func configInit(cfg *cli.Config) {
log.Level = log.LevelDebug
}

log.Debugf("CFSSL server config is: %+v", cfg)
log.Debugf("COP server config is: %+v", CFG)
}

// Make TLS client files absolute
func absTLSClient(cfg *tls.ClientTLSConfig) {
for i := 0; i < len(cfg.CACertFiles); i++ {
cfg.CACertFiles[i] = abs(cfg.CACertFiles[i])
}
cfg.Client.CertFile = abs(cfg.Client.CertFile)
cfg.Client.KeyFile = abs(cfg.Client.KeyFile)
}

// Make 'file' absolute relative to the configuration directory
func abs(file string) string {
path, err := util.MakeFileAbs(file, configDir)
if err != nil {
panic(err)
}
return path
}
4 changes: 2 additions & 2 deletions cli/server/register_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,8 @@ func prepRegister() error {
configInit(cfg)

regCFG := CFG
home = regPath
datasource := filepath.Join(home, "cop.db")
homeDir = regPath
datasource := filepath.Join(homeDir, "cop.db")
regCFG.DataSource = datasource

err = InitUserRegistry(regCFG)
Expand Down
20 changes: 5 additions & 15 deletions cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,8 @@ var (
enrollSigner signer.Signer
ocspSigner ocsp.Signer
db *sqlx.DB
home string
homeDir string
configDir string
configFile string
userRegistry spi.UserRegistry
certDBAccessor *CertDBAccessor
Expand Down Expand Up @@ -151,28 +152,17 @@ func bootstrapDB() error {
// It sets up a new HTTP server to handle COP requests.
func startMain(args []string, c cli.Config) error {
log.Debug("server.startMain")
var err error

s := new(Server)
home, err := s.CreateHome()
homeDir, err = s.CreateHome()
if err != nil {
return err
}
configInit(&c)
cfg := CFG

if cfg.DataSource == "" {
msg := "No database specified, a database is needed to run COP server. Using default - Type: SQLite, Name: cop.db"
log.Info(msg)
cfg.DBdriver = sqlite
cfg.DataSource = "cop.db"
}

if cfg.DBdriver == sqlite {
cfg.DataSource = filepath.Join(home, cfg.DataSource)
}

// Initialize the user registry
err = InitUserRegistry(cfg)
err = InitUserRegistry(CFG)
if err != nil {
log.Errorf("Failed to initialize user registry [error: %s]", err)
return err
Expand Down
5 changes: 4 additions & 1 deletion cli/server/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,8 +34,9 @@ import (
)

const (
CFGFile = "testconfig2.json"
CFGFile = "testconfig.json"
ClientTLSConfig = "cop_client.json"
COPDB = "../../testdata/cop.db"
)

var serverStarted bool
Expand All @@ -57,6 +58,7 @@ func startServer() {
}

if !serverStarted {
os.Remove(COPDB)
os.RemoveAll(dir)
serverStarted = true
fmt.Println("starting COP server ...")
Expand Down Expand Up @@ -461,6 +463,7 @@ func TestCreateHome(t *testing.T) {

func TestLast(t *testing.T) {
// Cleanup
os.Remove(COPDB)
os.RemoveAll(dir)
}

Expand Down
14 changes: 7 additions & 7 deletions testdata/testconfig.json
Original file line number Diff line number Diff line change
Expand Up @@ -5,15 +5,15 @@
"user_registry": {
"max_enrollments": 1
},
"ca_cert":"../testdata/ec.pem",
"ca_key":"../testdata/ec-key.pem",
"ca_cert":"ec.pem",
"ca_key":"ec-key.pem",
"tls":{
"tls_cert":"../testdata/tls_server-cert.pem",
"tls_key":"../testdata/tls_server-key.pem",
"mutual_tls_ca":"../testdata/root.pem",
"tls_cert":"tls_server-cert.pem",
"tls_key":"tls_server-key.pem",
"mutual_tls_ca":"root.pem",
"db_client":{
"ca_certfiles":["../testdata/root.pem"],
"client":{"keyfile":"../testdata/tls_server-key.pem","certfile":"../testdata/tls_server-cert.pem"}
"ca_certfiles":["root.pem"],
"client":{"keyfile":"tls_server-key.pem","certfile":"tls_server-cert.pem"}
}
},
"users": {
Expand Down
Loading

0 comments on commit a5666ff

Please sign in to comment.