Skip to content

Commit

Permalink
FAB-3007:Make CA name required
Browse files Browse the repository at this point in the history
Currently, if ca.name property is not specified in the server
configuration file, server's root ca cert is saved to '.pem' file,
which is hidden by default. This change set will fix this problem
by making ca.name property required.

Change-Id: Ia1a8234a284885dc14fdd8ad97e383ff495ffa7e
Signed-off-by: Anil Ambati <[email protected]>
  • Loading branch information
Anil Ambati committed Apr 10, 2017
1 parent 77dc0ce commit 4c3189b
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 3 deletions.
12 changes: 10 additions & 2 deletions cmd/fabric-ca-server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ const (
cmdName = "fabric-ca-server"
envVarPrefix = "FABRIC_CA_SERVER"
homeEnvVar = "FABRIC_CA_SERVER_HOME"
caNameReqMsg = "ca.name property is required but is missing from the configuration file"
)

const (
Expand Down Expand Up @@ -290,6 +291,10 @@ func configInit() (err error) {
return fmt.Errorf("Incorrect format in file '%s': %s", cfgFileName, err)
}

if serverCfg.CA.Name == "" {
return fmt.Errorf(caNameReqMsg)
}

return nil
}

Expand Down Expand Up @@ -321,12 +326,15 @@ func createDefaultConfigFile() error {
return err
}
// Get domain name
mydomain := strings.Join(strings.Split(myhost, ".")[1:], ".")
caName := strings.Join(strings.Split(myhost, ".")[1:], ".")
if caName == "" {
caName = myhost
}
// Do string subtitution to get the default config
cfg := strings.Replace(defaultCfgTemplate, "<<<ADMIN>>>", user, 1)
cfg = strings.Replace(cfg, "<<<ADMINPW>>>", pass, 1)
cfg = strings.Replace(cfg, "<<<MYHOST>>>", myhost, 1)
cfg = strings.Replace(cfg, "<<<CANAME>>>", mydomain, 1)
cfg = strings.Replace(cfg, "<<<CANAME>>>", caName, 1)
// Now write the file
err = os.MkdirAll(filepath.Dir(cfgFileName), 0755)
if err != nil {
Expand Down
15 changes: 14 additions & 1 deletion cmd/fabric-ca-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (
"testing"

"github.com/hyperledger/fabric-ca/util"
"github.com/spf13/viper"
)

const (
Expand All @@ -41,6 +42,7 @@ var (

// Create a config element in unexpected format
var badSyntaxYaml = "bad.yaml"
var ymlWithoutCAName = "noCAName.yml"

// Unsupported file type
var unsupportedFileType = "config.txt"
Expand Down Expand Up @@ -74,6 +76,9 @@ func errorTest(in *TestData, t *testing.T) {
func TestErrors(t *testing.T) {
os.Unsetenv(homeEnvVar)
_ = ioutil.WriteFile(badSyntaxYaml, []byte("signing: true\n"), 0644)
exp := regexp.MustCompile(".*<<<CANAME>>>.*")
cfg := exp.ReplaceAllString(defaultCfgTemplate, "")
_ = ioutil.WriteFile(ymlWithoutCAName, []byte(cfg), 0644)

errorCases := []TestData{
{[]string{cmdName, "init", "-c", initYaml}, "option is required"},
Expand All @@ -87,13 +92,20 @@ func TestErrors(t *testing.T) {
{[]string{cmdName, "init", "-c", initYaml, "-b", "user:"}, "empty password"},
{[]string{cmdName, "bogus", "-c", initYaml, "-b", "user:pass"}, "unknown command"},
{[]string{cmdName, "start", "-c"}, "needs an argument:"},
{[]string{cmdName, "start", "-c", startYaml, "-b", "user:pass", "ca.key"}, "too many arguments"},
{[]string{cmdName, "start", "-c", startYaml, "-d", "-b", "user:pass", "ca.key"}, "too many arguments"},
{[]string{cmdName, "start", "-c", ymlWithoutCAName, "-b", "user:pass"}, caNameReqMsg},
}

// Explicitly set the default for ca.name to "", this is to test if server
// does not start if ca.name is not specified
viper.SetDefault("ca.name", "")
for _, e := range errorCases {
errorTest(&e, t)
_ = os.Remove(initYaml)
}
// We are done with all error cases. Now, set the ca.name default value to
// "acme.com", as ca.name is a required property for server to start
viper.SetDefault("ca.name", "acme.com")
}

func TestValid(t *testing.T) {
Expand Down Expand Up @@ -188,6 +200,7 @@ func TestClean(t *testing.T) {
os.Remove(initYaml)
os.Remove(startYaml)
os.Remove(badSyntaxYaml)
os.Remove(ymlWithoutCAName)
os.Remove(unsupportedFileType)
os.Remove("ca-key.pem")
os.Remove("ca-cert.pem")
Expand Down

0 comments on commit 4c3189b

Please sign in to comment.