Skip to content

Commit

Permalink
fabric-ca-server start for cobra/viper CLI
Browse files Browse the repository at this point in the history
This change set is a continuation of the rebase of fabric-ca CLI
on cobra/viper.  This change set implements the first part of the
   fabric-ca-server start
command.  From a configuration perspective, the server start
command is the most intrusive because the cfssl CLI permeated
the code.  The lib/server.go now is a proper library API which
is independent of cfssl or even viper config.  The cobra/viper
specific calls are intentionally all in cmd/fabric-ca-server.

See https://jira.hyperledger.org/browse/FAB-2012

Change-Id: Iac6413c2de115fd66aada121cbfdbce22b11d4e9
Signed-off-by: Keith Smith <[email protected]>
  • Loading branch information
Keith Smith committed Feb 15, 2017
1 parent 70ae36d commit 5a35b72
Show file tree
Hide file tree
Showing 14 changed files with 390 additions and 58 deletions.
4 changes: 2 additions & 2 deletions cli/server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -175,8 +175,8 @@ func configInit(cfg *cli.Config) {

// 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])
for i := 0; i < len(cfg.CertFiles); i++ {
cfg.CertFiles[i] = abs(cfg.CertFiles[i])
}
cfg.Client.CertFile = abs(cfg.Client.CertFile)
cfg.Client.KeyFile = abs(cfg.Client.KeyFile)
Expand Down
4 changes: 2 additions & 2 deletions cli/server/dbutil/dbutil.go
Original file line number Diff line number Diff line change
Expand Up @@ -102,8 +102,8 @@ func NewUserRegistryPostgres(datasource string, clientTLSConfig *tls.ClientTLSCo
connStr := getConnStr(datasource)

if clientTLSConfig != nil {
if len(clientTLSConfig.CACertFiles) > 0 {
root := clientTLSConfig.CACertFiles[0]
if len(clientTLSConfig.CertFiles) > 0 {
root := clientTLSConfig.CertFiles[0]
connStr = fmt.Sprintf("%s sslrootcert=%s", connStr, root)
}

Expand Down
3 changes: 2 additions & 1 deletion cli/server/ldap/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -36,8 +36,9 @@ var (
)

// Config is the configuration object for this LDAP client
// URL is of the form: ldap://adminDN:adminPassword@host:port/base
type Config struct {
// URL is of the form: ldap://adminDN:adminPassword@host:port/base
Enabled bool `json:"enabled"`
URL string `json:"url"`
Base string `json:"base,omitempty"`
UserFilter string `json:"userfilter,omitempty"`
Expand Down
2 changes: 0 additions & 2 deletions cli/server/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,6 @@ import (
apiocsp "github.com/cloudflare/cfssl/api/ocsp"
"github.com/cloudflare/cfssl/api/scan"
"github.com/cloudflare/cfssl/api/signhandler"
"github.com/cloudflare/cfssl/bundler"
"github.com/cloudflare/cfssl/cli"
"github.com/cloudflare/cfssl/cli/ocspsign"
"github.com/cloudflare/cfssl/config"
Expand Down Expand Up @@ -175,7 +174,6 @@ func (s *Server) serverMain(args []string, c cli.Config) error {
return errors.New("argument is provided but not defined; please refer to the usage by flag -h")
}

bundler.IntermediateStash = conf.IntDir
var err error

if err = ubiquity.LoadPlatforms(conf.Metadata); err != nil {
Expand Down
12 changes: 7 additions & 5 deletions cmd/fabric-ca-server/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -285,18 +285,20 @@ func createDefaultConfigFile() error {
if len(pass) == 0 {
return errors.New("An empty password in the '-u user:pass' option is not permitted")
}
// Get hostname
myhost, err := os.Hostname()
if err != nil {
return err
}
// 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)
// Now write the file
err := os.MkdirAll(filepath.Dir(cfgFileName), 0644)
err = os.MkdirAll(filepath.Dir(cfgFileName), 0644)
if err != nil {
return err
}
// Now write the file
return ioutil.WriteFile(cfgFileName, []byte(cfg), 0644)
}

func getDefaultListeningPort() int {
return 7054
}
1 change: 1 addition & 0 deletions cmd/fabric-ca-server/main_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,4 +82,5 @@ func TestClean(t *testing.T) {
os.Remove(testYaml)
os.Remove("ca-key.pem")
os.Remove("ca-cert.pem")
os.Remove("fabric-ca-server.db")
}
21 changes: 12 additions & 9 deletions cmd/fabric-ca-server/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,11 +18,11 @@ package main

import (
"fmt"
"path/filepath"

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

// startCmd represents the enroll command
Expand All @@ -35,8 +35,8 @@ func init() {
startCmd.RunE = runStart
rootCmd.AddCommand(startCmd)
flags := startCmd.Flags()
util.FlagInt(flags, "port", "p", getDefaultListeningPort(),
"Listening port")
util.FlagString(flags, "addr", "a", lib.DefaultServerAddr, "Listening address")
util.FlagInt(flags, "port", "p", lib.DefaultServerPort, "Listening port")
registerCommonFlags(flags)
}

Expand All @@ -45,10 +45,13 @@ func runStart(cmd *cobra.Command, args []string) error {
if len(args) > 0 {
return fmt.Errorf("Usage: too many arguments.\n%s", startCmd.UsageString())
}
log.Infof("Starting the %s", shortName)
log.Debugf("tls.key: '%s'", viper.GetString("tls.key"))
log.Debugf("tls.cert: '%s'", viper.GetString("tls.cert"))
log.Debugf("tls.enabled: %v", viper.GetBool("tls.enabled"))
log.Infof("Listening on port %v ...", viper.GetInt("port"))
server := lib.Server{
HomeDir: filepath.Dir(cfgFileName),
Config: serverCfg,
}
err := server.Start()
if err != nil {
return err
}
return nil
}
15 changes: 15 additions & 0 deletions lib/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,21 @@ func testLoadBadCSRInfo(c *Client, t *testing.T) {
}
}

func TestNormalizeUrl(t *testing.T) {
_, err := normalizeURL("")
if err != nil {
t.Errorf("normalizeURL empty: %s", err)
}
_, err = normalizeURL("http://host:7054:x/path")
if err != nil {
t.Errorf("normalizeURL colons: %s", err)
}
_, err = normalizeURL("http://host:7054/path")
if err != nil {
t.Errorf("normalizeURL failed: %s", err)
}
}

func TestSendBadPost(t *testing.T) {
c := new(Client)
curl := "fake"
Expand Down
Loading

0 comments on commit 5a35b72

Please sign in to comment.