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