-
Notifications
You must be signed in to change notification settings - Fork 708
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add support for client getcacert command
See https://jira.hyperledger.org/browse/FAB-2602 This change set implements the "fabric-ca-client getcacert" command as described in FAB-2602. A follow on change set will implement the "-M" option of the enroll command. Change-Id: Ida82e7c4ecf8a46a52d15cb58546185d869561c4 Signed-off-by: Keith Smith <[email protected]>
- Loading branch information
Keith Smith
committed
Mar 11, 2017
1 parent
df922a1
commit 2e51747
Showing
18 changed files
with
420 additions
and
67 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,89 @@ | ||
/* | ||
Copyright IBM Corp. 2017 All Rights Reserved. | ||
Licensed under the Apache License, Version 2.0 (the "License"); | ||
you may not use this file except in compliance with the License. | ||
You may obtain a copy of the License at | ||
http://www.apache.org/licenses/LICENSE-2.0 | ||
Unless required by applicable law or agreed to in writing, software | ||
distributed under the License is distributed on an "AS IS" BASIS, | ||
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
See the License for the specific language governing permissions and | ||
limitations under the License. | ||
*/ | ||
|
||
package main | ||
|
||
import ( | ||
"fmt" | ||
"os" | ||
"path" | ||
"path/filepath" | ||
"strings" | ||
|
||
"github.com/cloudflare/cfssl/log" | ||
"github.com/hyperledger/fabric-ca/lib" | ||
"github.com/hyperledger/fabric-ca/util" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
// getCACertCmd represents the "getcacert" command | ||
var getCACertCmd = &cobra.Command{ | ||
Use: "getcacert -u http://serverAddr:serverPort -M <MSP-directory>", | ||
Short: "Get CA certificate chain", | ||
RunE: func(cmd *cobra.Command, args []string) error { | ||
if len(args) > 0 { | ||
cmd.Help() | ||
return nil | ||
} | ||
err := runGetCACert() | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
}, | ||
} | ||
|
||
func init() { | ||
rootCmd.AddCommand(getCACertCmd) | ||
} | ||
|
||
// The client "getcacert" main logic | ||
func runGetCACert() error { | ||
log.Debug("Entered runGetCACert") | ||
|
||
client := &lib.Client{ | ||
HomeDir: filepath.Dir(cfgFileName), | ||
Config: clientCfg, | ||
} | ||
|
||
si, err := client.GetServerInfo() | ||
if err != nil { | ||
return err | ||
} | ||
|
||
return storeCAChain(client, si) | ||
} | ||
|
||
// Store the CAChain in the CACerts folder of MSP (Membership Service Provider) | ||
func storeCAChain(client *lib.Client, si *lib.GetServerInfoResponse) error { | ||
mspDir := client.Config.MSPDir | ||
if !util.FileExists(mspDir) { | ||
return fmt.Errorf("Directory does not exist: %s", mspDir) | ||
} | ||
caCertsDir := path.Join(mspDir, "cacerts") | ||
err := os.MkdirAll(caCertsDir, 0755) | ||
if err != nil { | ||
return fmt.Errorf("Failed creating CA certificates directory: %s", err) | ||
} | ||
fname := strings.Replace(si.CAName, ".", "-", -1) + ".pem" | ||
path := path.Join(caCertsDir, fname) | ||
err = util.WriteFile(path, si.CAChain, 0644) | ||
if err != nil { | ||
return fmt.Errorf("Failed to create CA root file: %s", err) | ||
} | ||
log.Infof("Stored CA certificate chain at %s", path) | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.