Skip to content

Commit

Permalink
[FAB-3061] Persist the ca.name
Browse files Browse the repository at this point in the history
When generating the default configuration file, specified ca.name (via
env variable or --ca.name command line option) is not persisted to the
config file. Also, when ca.name is not specified throw an error if
ca.name cannot be derived from the hostname (either because domain
name is not part of hostname or hostname cannot be retrieved)

Change-Id: Ia71193fccaa43b5c2c0e46897d59be6aa8261aba
Signed-off-by: Anil Ambati <[email protected]>
  • Loading branch information
Anil Ambati committed Apr 19, 2017
1 parent e583181 commit 6d5ae41
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 11 deletions.
35 changes: 28 additions & 7 deletions cmd/fabric-ca-server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -335,16 +335,17 @@ func createDefaultConfigFile() error {
if len(pass) == 0 {
return errors.New("An empty password in the '-b user:pass' option is not permitted")
}
// Get hostname
myhost, err := os.Hostname()

var myhost, caName string
var err error
myhost, err = os.Hostname()
if err != nil {
return err
}
// Get domain name
caName := strings.Join(strings.Split(myhost, ".")[1:], ".")
if caName == "" {
caName = myhost
}

// Get hostname
caName = getCAName(myhost)

// Do string subtitution to get the default config
cfg := strings.Replace(defaultCfgTemplate, "<<<ADMIN>>>", user, 1)
cfg = strings.Replace(cfg, "<<<ADMINPW>>>", pass, 1)
Expand All @@ -358,3 +359,23 @@ func createDefaultConfigFile() error {
// Now write the file
return ioutil.WriteFile(cfgFileName, []byte(cfg), 0644)
}

// getCAName returns CA Name
// If ca.name property is specified (via the environment variable
// 'FABRIC_CA_SERVER_CA_NAME' or the command line option '--ca.name' or
// in the configuration file), then its value is returned
// If ca.name property is not specified, domain is extracted from the hostname and is
// returned
// If domain is empty, then hostname is returned
func getCAName(hostname string) (caName string) {
caName = viper.GetString("ca.name")
if caName != "" {
return caName
}

caName = strings.Join(strings.Split(hostname, ".")[1:], ".")
if caName == "" {
caName = hostname
}
return caName
}
30 changes: 26 additions & 4 deletions cmd/fabric-ca-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -73,6 +73,28 @@ func errorTest(in *TestData, t *testing.T) {
}
}

// Tests for the getCAName function
func TestGetCAName(t *testing.T) {
var testCases = []struct {
input string // input
expected string // expected result
}{
{"server1.acme.com", "acme.com"},
{"server1.net1.acme.com", "net1.acme.com"},
{".com", "com"},
{"server2", "server2"},
{"foo.", "foo."},
{".", "."},
}
for _, tc := range testCases {
n := getCAName(tc.input)
if n != tc.expected {
t.Errorf("getCAName returned unexpected value '%s' for '%s', expected value is '%s'",
n, tc.input, tc.expected)
}
}
}

func TestErrors(t *testing.T) {
os.Unsetenv(homeEnvVar)
_ = ioutil.WriteFile(badSyntaxYaml, []byte("signing: true\n"), 0644)
Expand All @@ -82,8 +104,9 @@ func TestErrors(t *testing.T) {

errorCases := []TestData{
{[]string{cmdName, "init", "-c", initYaml}, "option is required"},
{[]string{cmdName, "init", "-b", "user:pass", "ca.key"}, "too many arguments"},
{[]string{cmdName, "init", "-b", "user::"}, "Failed to read"},
{[]string{cmdName, "init", "-n", "acme.com", "-b", "user::"}, "Failed to read"},
{[]string{cmdName, "init", "-c", ymlWithoutCAName, "-n", "", "-b", "user:pass"}, caNameReqMsg},
{[]string{cmdName, "init", "-b", "user:pass", "-n", "acme.com", "ca.key"}, "too many arguments"},
{[]string{cmdName, "init", "-c", badSyntaxYaml, "-b", "user:pass"}, "Incorrect format"},
{[]string{cmdName, "init", "-c", initYaml, "-b", fmt.Sprintf("%s:foo", longUserName)}, "than 1024 characters"},
{[]string{cmdName, "init", "-c", fmt.Sprintf("%s.yaml", longFileName), "-b", "user:pass"}, "file name too long"},
Expand All @@ -92,8 +115,7 @@ 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, "-d", "-b", "user:pass", "ca.key"}, "too many arguments"},
{[]string{cmdName, "start", "-c", ymlWithoutCAName, "-b", "user:pass"}, caNameReqMsg},
{[]string{cmdName, "start", "-c", startYaml, "-b", "user:pass", "ca.key"}, "too many arguments"},
}

// Explicitly set the default for ca.name to "", this is to test if server
Expand Down

0 comments on commit 6d5ae41

Please sign in to comment.