gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/certificate.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package command
     8  
     9  import (
    10  	"path/filepath"
    11  	"strings"
    12  	"time"
    13  
    14  	"gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/api"
    15  	calog "gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/log"
    16  	"gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/util"
    17  	"gitee.com/zhaochuninhefei/fabric-ca-gm/lib"
    18  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    19  	"github.com/pkg/errors"
    20  	"github.com/spf13/cobra"
    21  )
    22  
    23  type certificateCommand struct {
    24  	command  Command
    25  	list     api.GetCertificatesRequest
    26  	timeArgs timeArgs
    27  	store    string
    28  }
    29  
    30  type timeArgs struct {
    31  	// Get certificates that were revoked between the UTC timestamp (RFC3339 format) or duration specified
    32  	Revocation string `help:"Get certificates that were revoked between the UTC timestamp (RFC3339 format) or duration specified (e.g. <begin_time>::<end_time>)"`
    33  	// Get certificates which expire between the UTC timestamp (RFC3339 format) or duration specified
    34  	Expiration string `help:"Get certificates which expire between the UTC timestamp (RFC3339 format) or duration specified (e.g. <begin_time>::<end_time>)"`
    35  }
    36  
    37  // createCertificateCommand will create the certificate cobra command
    38  func createCertificateCommand(clientCmd Command) *cobra.Command {
    39  	return addCertificateCommand(newCertificateCommand(clientCmd))
    40  }
    41  
    42  func newCertificateCommand(clientCmd Command) *certificateCommand {
    43  	return &certificateCommand{
    44  		command: clientCmd,
    45  	}
    46  }
    47  
    48  func addCertificateCommand(c *certificateCommand) *cobra.Command {
    49  	certificateCmd := &cobra.Command{
    50  		Use:   "certificate",
    51  		Short: "Manage certificates",
    52  		Long:  "Manage certificates",
    53  	}
    54  	certificateCmd.AddCommand(newListCertificateCommand(c))
    55  	return certificateCmd
    56  }
    57  
    58  func newListCertificateCommand(c *certificateCommand) *cobra.Command {
    59  	certificateListCmd := &cobra.Command{
    60  		Use:     "list",
    61  		Short:   "List certificates",
    62  		Long:    "List all certificates which are visible to the caller and match the flags",
    63  		Example: "fabric-ca-client certificate list --id admin --expiration 2018-01-01::2018-01-30\nfabric-ca-client certificate list --id admin --expiration 2018-01-01T01:30:00z::2018-01-30T11:30:00z\nfabric-ca-client certificate list --id admin --expiration -30d::-15d",
    64  		PreRunE: c.preRunCertificate,
    65  		RunE:    c.runListCertificate,
    66  	}
    67  	flags := certificateListCmd.Flags()
    68  	flags.StringVarP(&c.list.ID, "id", "", "", "Get certificates for this enrollment ID")
    69  	flags.StringVarP(&c.store, "store", "", "", "Store requested certificates in this location")
    70  	viper := c.command.GetViper()
    71  	util.RegisterFlags(viper, flags, &c.list, nil)
    72  	util.RegisterFlags(viper, flags, &c.timeArgs, nil)
    73  	return certificateListCmd
    74  }
    75  
    76  func (c *certificateCommand) preRunCertificate(cmd *cobra.Command, args []string) error {
    77  	c.command.SetDefaultLogLevel(calog.WARNING)
    78  	err := c.command.ConfigInit()
    79  	if err != nil {
    80  		return err
    81  	}
    82  
    83  	log.Debugf("Client configuration settings: %+v", c.command.GetClientCfg())
    84  
    85  	return nil
    86  }
    87  
    88  // The client side logic for executing list certificates command
    89  func (c *certificateCommand) runListCertificate(cmd *cobra.Command, args []string) error {
    90  	log.Debug("Entered runListCertificate")
    91  
    92  	id, err := c.command.LoadMyIdentity()
    93  	if err != nil {
    94  		return err
    95  	}
    96  
    97  	err = c.getCertListReq()
    98  	if err != nil {
    99  		return err
   100  	}
   101  
   102  	req := &c.list
   103  	req.CAName = c.command.GetClientCfg().CAName
   104  
   105  	if c.store != "" {
   106  		if !filepath.IsAbs(c.store) {
   107  			c.store = filepath.Join(c.command.GetHomeDirectory(), c.store)
   108  		}
   109  		log.Infof("Certificates stored at: %s", c.store)
   110  	}
   111  
   112  	certDecoder := lib.NewCertificateDecoder(c.store)
   113  	return id.GetCertificates(req, certDecoder.CertificateDecoder)
   114  }
   115  
   116  func (c *certificateCommand) getCertListReq() error {
   117  	log.Debug("Parse expiration/revocation time range and generate certificate list request")
   118  	listReq := &c.list
   119  	expirationRange := c.timeArgs.Expiration
   120  	revocationRange := c.timeArgs.Revocation
   121  
   122  	if expirationRange != "" {
   123  		timeArgs, err := parseTimeRange(expirationRange, "expiration")
   124  		if err != nil {
   125  			return err
   126  		}
   127  		listReq.Expired.StartTime = getTime(timeArgs[0])
   128  		listReq.Expired.EndTime = getTime(timeArgs[1])
   129  	}
   130  
   131  	if revocationRange != "" {
   132  		timeArgs, err := parseTimeRange(revocationRange, "revocation")
   133  		if err != nil {
   134  			return err
   135  		}
   136  		listReq.Revoked.StartTime = getTime(timeArgs[0])
   137  		listReq.Revoked.EndTime = getTime(timeArgs[1])
   138  	}
   139  
   140  	return nil
   141  }
   142  
   143  func parseTimeRange(str, name string) ([]string, error) {
   144  	log.Debugf("Parsing %s time range: %s", name, str)
   145  	if !strings.Contains(str, "::") {
   146  		return nil, errors.Errorf("Invalid %s format, expecting '<start>::<end>' but found %s, missing '::' sepatator", name, str)
   147  	}
   148  
   149  	timeArgs := strings.Split(str, "::")
   150  	for _, timeArg := range timeArgs {
   151  		if strings.Contains(timeArg, "/") {
   152  			return nil, errors.Errorf("Invalid %s format, use '-' instead of '/' in time format: %s", name, str)
   153  		}
   154  	}
   155  
   156  	return timeArgs, nil
   157  }
   158  
   159  func getTime(timeArg string) string {
   160  	if strings.ToLower(timeArg) == "now" {
   161  		currentTime := time.Now().UTC()
   162  		return currentTime.Format(time.RFC3339)
   163  	}
   164  	return timeArg
   165  }