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

     1  /*
     2  Copyright IBM Corp. 2017 All Rights Reserved.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8                   http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package command
    18  
    19  import (
    20  	"path/filepath"
    21  
    22  	"gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/api"
    23  	"gitee.com/zhaochuninhefei/fabric-ca-gm/internal/pkg/util"
    24  	"gitee.com/zhaochuninhefei/fabric-ca-gm/lib"
    25  	log "gitee.com/zhaochuninhefei/zcgolog/zclog"
    26  	"github.com/pkg/errors"
    27  	"github.com/spf13/cobra"
    28  )
    29  
    30  var errInput = errors.New("Invalid usage; either --revoke.name and/or both --revoke.serial and --revoke.aki are required")
    31  
    32  func (c *ClientCmd) newRevokeCommand() *cobra.Command {
    33  	revokeCmd := &cobra.Command{
    34  		Use:   "revoke",
    35  		Short: "Revoke an identity",
    36  		Long:  "Revoke an identity with Fabric CA server",
    37  		// PreRunE block for this command will check to make sure enrollment
    38  		// information exists before running the command
    39  		PreRunE: func(cmd *cobra.Command, args []string) error {
    40  			if len(args) > 0 {
    41  				return errors.Errorf(extraArgsError, args, cmd.UsageString())
    42  			}
    43  
    44  			err := c.ConfigInit()
    45  			if err != nil {
    46  				return err
    47  			}
    48  
    49  			log.Debugf("Client configuration settings: %+v", c.clientCfg)
    50  
    51  			return nil
    52  		},
    53  		RunE: func(cmd *cobra.Command, args []string) error {
    54  			err := c.runRevoke(cmd)
    55  			if err != nil {
    56  				return err
    57  			}
    58  
    59  			return nil
    60  		},
    61  	}
    62  	util.RegisterFlags(c.myViper, revokeCmd.Flags(), &c.revokeParams, nil)
    63  	return revokeCmd
    64  }
    65  
    66  // The client revoke main logic
    67  func (c *ClientCmd) runRevoke(cmd *cobra.Command) error {
    68  	log.Debug("Entered runRevoke")
    69  
    70  	var err error
    71  
    72  	client := lib.Client{
    73  		HomeDir: filepath.Dir(c.cfgFileName),
    74  		Config:  c.clientCfg,
    75  	}
    76  
    77  	id, err := client.LoadMyIdentity()
    78  	if err != nil {
    79  		return err
    80  	}
    81  
    82  	// aki and serial # are required to revoke a certificate. The enrollment ID
    83  	// is required to revoke an identity. So, either aki and serial must be
    84  	// specified OR enrollment ID must be specified, else return an error.
    85  	// Note that all three can be specified, in which case server will revoke
    86  	// certificate associated with the specified aki, serial number.
    87  	if (c.clientCfg.Revoke.Name == "") && (c.clientCfg.Revoke.AKI == "" ||
    88  		c.clientCfg.Revoke.Serial == "") {
    89  		cmd.Usage()
    90  		return errInput
    91  	}
    92  
    93  	req := &api.RevocationRequest{
    94  		Name:   c.clientCfg.Revoke.Name,
    95  		Serial: c.clientCfg.Revoke.Serial,
    96  		AKI:    c.clientCfg.Revoke.AKI,
    97  		Reason: c.clientCfg.Revoke.Reason,
    98  		GenCRL: c.revokeParams.GenCRL,
    99  		CAName: c.clientCfg.CAName,
   100  	}
   101  	result, err := id.Revoke(req)
   102  
   103  	if err != nil {
   104  		return err
   105  	}
   106  	log.Infof("Successfully revoked certificates: %+v", result.RevokedCerts)
   107  
   108  	if req.GenCRL {
   109  		return storeCRL(c.clientCfg, result.CRL)
   110  	}
   111  	return nil
   112  }