gitee.com/zhaochuninhefei/fabric-ca-gm@v0.0.2/cmd/fabric-ca-client/command/reenroll.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  	"fmt"
    11  	"path/filepath"
    12  
    13  	"gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/api"
    14  	"gitee.com/zhaochuninhefei/fabric-ca-gm/lib"
    15  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    16  	"github.com/pkg/errors"
    17  	"github.com/spf13/cobra"
    18  )
    19  
    20  func (c *ClientCmd) newReenrollCommand() *cobra.Command {
    21  	reenrollCmd := &cobra.Command{
    22  		Use:   "reenroll",
    23  		Short: "Reenroll an identity",
    24  		Long:  "Reenroll an identity with Fabric CA server",
    25  		// PreRunE block for this command will check to make sure enrollment
    26  		// information exists before running the command
    27  		PreRunE: func(cmd *cobra.Command, args []string) error {
    28  			if len(args) > 0 {
    29  				return errors.Errorf(extraArgsError, args, cmd.UsageString())
    30  			}
    31  
    32  			err := c.ConfigInit()
    33  			if err != nil {
    34  				return err
    35  			}
    36  
    37  			log.Debugf("Client configuration settings: %+v", c.clientCfg)
    38  
    39  			return nil
    40  		},
    41  		RunE: func(cmd *cobra.Command, args []string) error {
    42  			err := c.runReenroll()
    43  			if err != nil {
    44  				return err
    45  			}
    46  
    47  			return nil
    48  		},
    49  	}
    50  	return reenrollCmd
    51  }
    52  
    53  // The client reenroll main logic
    54  func (c *ClientCmd) runReenroll() error {
    55  	log.Debug("Entered runReenroll")
    56  
    57  	client := lib.Client{
    58  		HomeDir: filepath.Dir(c.cfgFileName),
    59  		Config:  c.clientCfg,
    60  	}
    61  
    62  	id, err := client.LoadMyIdentity()
    63  	if err != nil {
    64  		return err
    65  	}
    66  
    67  	req := &api.ReenrollmentRequest{
    68  		Label:   c.clientCfg.Enrollment.Label,
    69  		Profile: c.clientCfg.Enrollment.Profile,
    70  		CSR:     &c.clientCfg.CSR,
    71  		CAName:  c.clientCfg.CAName,
    72  	}
    73  
    74  	resp, err := id.Reenroll(req)
    75  	if err != nil {
    76  		return errors.WithMessage(err, fmt.Sprintf("Failed to reenroll '%s'", id.GetName()))
    77  	}
    78  
    79  	err = resp.Identity.Store()
    80  	if err != nil {
    81  		return err
    82  	}
    83  
    84  	err = storeCAChain(c.clientCfg, &resp.CAInfo)
    85  	if err != nil {
    86  		return err
    87  	}
    88  
    89  	return nil
    90  }