Skip to content

Commit

Permalink
[FAB-7596] Modify enroll cmd to read env var
Browse files Browse the repository at this point in the history
Currently, fabric-ca-client enroll command does not
read FABRIC_CA_CLIENT_URL environment variable when
invoked without -u option. With this change, it will
use -u parameter value if specified, else use
FABRIC_CA_CLIENT_URL env variable value. If both are
not specified, it will read the value from the client
config file.

Change-Id: Ibeee86553db3836dc0a1b7e95455ffd0b0fdec1f
Signed-off-by: Anil Ambati <[email protected]>
  • Loading branch information
Anil Ambati committed Jan 5, 2018
1 parent ae82824 commit c219a5e
Show file tree
Hide file tree
Showing 4 changed files with 97 additions and 10 deletions.
6 changes: 4 additions & 2 deletions cmd/fabric-ca-client/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -209,6 +209,10 @@ func (c *ClientCmd) configInit() error {

log.Debugf("Home directory: %s", c.homeDirectory)

// Set configuration file name for viper and configure it read env variables
c.myViper.SetConfigFile(c.cfgFileName)
c.myViper.AutomaticEnv() // read in environment variables that match

// If the config file doesn't exist, create a default one if enroll
// command being executed. Enroll should be the first command to be
// executed, and furthermore the default configuration file requires
Expand All @@ -227,8 +231,6 @@ func (c *ClientCmd) configInit() error {
}

// Call viper to read the config
c.myViper.SetConfigFile(c.cfgFileName)
c.myViper.AutomaticEnv() // read in environment variables that match
if util.FileExists(c.cfgFileName) {
err = c.myViper.ReadInConfig()
if err != nil {
Expand Down
8 changes: 1 addition & 7 deletions cmd/fabric-ca-client/enroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@ import (
"github.com/pkg/errors"

"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-ca/util"
"github.com/spf13/cobra"
)

Expand All @@ -41,12 +40,7 @@ func (c *ClientCmd) newEnrollCommand() *cobra.Command {
return errors.Errorf(extraArgsError, args, cmd.UsageString())
}

_, _, err := util.GetUser(c.myViper)
if err != nil {
return err
}

err = c.configInit()
err := c.configInit()
if err != nil {
return err
}
Expand Down
91 changes: 91 additions & 0 deletions cmd/fabric-ca-client/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -236,6 +236,64 @@ func TestClientCommandsNoTLS(t *testing.T) {
}
}

func TestEnroll(t *testing.T) {
t.Log("Testing Enroll")
adminHome := filepath.Join(tdDir, "enrolladminhome")

// Remove admin home directory if it exists
err := os.RemoveAll(adminHome)
if err != nil {
t.Fatalf("Failed to remove directory %s: %s", adminHome, err)
}

// Remove admin home directory that this test is going to create before
// exiting the test case
defer os.RemoveAll(adminHome)

srv := setupEnrollTest(t)

// Cleanup before exiting the test case
defer stopAndCleanupServer(t, srv)

// Enroll with -u parameter. Value of the -u parameter is used as server URL
err = RunMain([]string{cmdName, "enroll", "-d", "-u", enrollURL, "-H", adminHome})
if err != nil {
t.Errorf("client enroll -u failed: %s", err)
}

// Enroll without -u parameter, should fail as the server URL is picked
// from the configuration file but userid and password are not part of the
// URL
err = RunMain([]string{cmdName, "enroll", "-d", "-H", adminHome})
if err == nil {
t.Errorf("No username/password provided, should have errored")
}

// Remove admin home
err = os.RemoveAll(adminHome)
if err != nil {
t.Fatalf("Failed to remove directory %s: %s", adminHome, err)
}

// Enroll without -u parameter but with FABRIC_CA_CLIENT_URL env variable
// Default client configuration file will be generated. Value of the
// FABRIC_CA_CLIENT_URL env variable is used as server URL
os.Setenv("FABRIC_CA_CLIENT_URL", enrollURL1)
defer os.Unsetenv("FABRIC_CA_CLIENT_URL")
err = RunMain([]string{cmdName, "enroll", "-d", "-H", adminHome})
if err != nil {
t.Errorf("client enroll with FABRIC_CA_CLIENT_URL env variable failed: %s", err)
}

// Enroll without -u parameter but with FABRIC_CA_CLIENT_URL env variable
// Existing client configuration file will be used. Value of the
// FABRIC_CA_CLIENT_URL env variable is used as server URL
err = RunMain([]string{cmdName, "enroll", "-d", "-H", adminHome})
if err != nil {
t.Errorf("client enroll with FABRIC_CA_CLIENT_URL env variable failed: %s", err)
}
}

// Test cases for gencrl command
func TestGenCRL(t *testing.T) {
t.Log("Testing GenCRL")
Expand Down Expand Up @@ -1861,6 +1919,39 @@ func getSerialAKIByID(id string) (serial, aki string, err error) {
return
}

func setupEnrollTest(t *testing.T) *lib.Server {
srvHome := filepath.Join(tdDir, "enrollsrvhome")
err := os.RemoveAll(srvHome)
if err != nil {
t.Fatalf("Failed to remove home directory %s: %s", srvHome, err)
}
srv = lib.TestGetServer(serverPort, srvHome, "", -1, t)
srv.Config.Debug = true

err = srv.RegisterBootstrapUser("admin", "adminpw", "")
if err != nil {
t.Errorf("Failed to register bootstrap user: %s", err)
}

err = srv.RegisterBootstrapUser("admin2", "adminpw2", "hyperledger")
if err != nil {
t.Errorf("Failed to register bootstrap user: %s", err)
}

aff := make(map[string]interface{})
aff["hyperledger"] = []string{"org1", "org2", "org3"}
aff["company1"] = []string{"dept1"}
aff["company2"] = []string{}

srv.CA.Config.Affiliations = aff

err = srv.Start()
if err != nil {
t.Errorf("Server start failed: %s", err)
}
return srv
}

func setupGenCRLTest(t *testing.T, adminHome string) *lib.Server {
srvHome := filepath.Join(tdDir, "gencrlsrvhom")
err := os.RemoveAll(srvHome)
Expand Down
2 changes: 1 addition & 1 deletion util/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -582,7 +582,7 @@ func GetUser(v *viper.Viper) (string, string, error) {

user := URL.User
if user == nil {
return "", "", errors.New("No username and password provided as part of URL")
return "", "", errors.New("No username and password provided as part of the Fabric CA server URL")
}

eid := user.Username()
Expand Down

0 comments on commit c219a5e

Please sign in to comment.