本文整理匯總了Golang中github.com/hyperledger/fabric/core.SecurityEnabled函數的典型用法代碼示例。如果您正苦於以下問題:Golang SecurityEnabled函數的具體用法?Golang SecurityEnabled怎麽用?Golang SecurityEnabled使用的例子?那麽, 這裏精選的函數代碼示例或許可以為您提供幫助。
在下文中一共展示了SecurityEnabled函數的6個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Golang代碼示例。
示例1: getSecHelper
//this should be called exactly once and the result cached
//NOTE- this crypto func might rightly belong in a crypto package
//and universally accessed
func getSecHelper() (crypto.Peer, error) {
var secHelper crypto.Peer
var err error
once.Do(func() {
if core.SecurityEnabled() {
enrollID := viper.GetString("security.enrollID")
enrollSecret := viper.GetString("security.enrollSecret")
if peer.ValidatorEnabled() {
logger.Debugf("Registering validator with enroll ID: %s", enrollID)
if err = crypto.RegisterValidator(enrollID, nil, enrollID, enrollSecret); nil != err {
return
}
logger.Debugf("Initializing validator with enroll ID: %s", enrollID)
secHelper, err = crypto.InitValidator(enrollID, nil)
if nil != err {
return
}
} else {
logger.Debugf("Registering non-validator with enroll ID: %s", enrollID)
if err = crypto.RegisterPeer(enrollID, nil, enrollID, enrollSecret); nil != err {
return
}
logger.Debugf("Initializing non-validator with enroll ID: %s", enrollID)
secHelper, err = crypto.InitPeer(enrollID, nil)
if nil != err {
return
}
}
}
})
return secHelper, err
}
示例2: serve
func serve(args []string) error {
// Parameter overrides must be processed before any paramaters are
// cached. Failures to cache cause the server to terminate immediately.
if chaincodeDevMode {
logger.Info("Running in chaincode development mode")
logger.Info("Set consensus to NOOPS and user starts chaincode")
logger.Info("Disable loading validity system chaincode")
viper.Set("peer.validator.enabled", "true")
viper.Set("peer.validator.consensus", "noops")
viper.Set("chaincode.mode", chaincode.DevModeUserRunsChaincode)
}
if err := peer.CacheConfiguration(); err != nil {
return err
}
peerEndpoint, err := peer.GetPeerEndpoint()
if err != nil {
err = fmt.Errorf("Failed to get Peer Endpoint: %s", err)
return err
}
listenAddr := viper.GetString("peer.listenAddress")
if "" == listenAddr {
logger.Debug("Listen address not specified, using peer endpoint address")
listenAddr = peerEndpoint.Address
}
lis, err := net.Listen("tcp", listenAddr)
if err != nil {
grpclog.Fatalf("Failed to listen: %v", err)
}
ehubLis, ehubGrpcServer, err := createEventHubServer()
if err != nil {
grpclog.Fatalf("Failed to create ehub server: %v", err)
}
logger.Infof("Security enabled status: %t", core.SecurityEnabled())
if viper.GetBool("security.privacy") {
if core.SecurityEnabled() {
logger.Infof("Privacy enabled status: true")
} else {
panic(errors.New("Privacy cannot be enabled as requested because security is disabled"))
}
} else {
logger.Infof("Privacy enabled status: false")
}
var opts []grpc.ServerOption
if comm.TLSEnabled() {
creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"),
viper.GetString("peer.tls.key.file"))
if err != nil {
grpclog.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
grpcServer := grpc.NewServer(opts...)
registerChaincodeSupport(grpcServer)
logger.Debugf("Running peer")
// Register the Admin server
pb.RegisterAdminServer(grpcServer, core.NewAdminServer())
// Register the Endorser server
serverEndorser := endorser.NewEndorserServer()
pb.RegisterEndorserServer(grpcServer, serverEndorser)
// Initialize gossip component
bootstrap := viper.GetStringSlice("peer.gossip.bootstrap")
service.InitGossipService(peerEndpoint.Address, grpcServer, bootstrap...)
defer service.GetGossipService().Stop()
//initialize the env for chainless startup
initChainless()
// Begin startup of default chain
if peerDefaultChain {
chainID := util.GetTestChainID()
block, err := pbutils.MakeConfigurationBlock(chainID)
if nil != err {
panic(fmt.Sprintf("Unable to create genesis block for [%s] due to [%s]", chainID, err))
}
//this creates block and calls JoinChannel on gossip service
if err = peer.CreateChainFromBlock(block); err != nil {
panic(fmt.Sprintf("Unable to create chain block for [%s] due to [%s]", chainID, err))
}
chaincode.DeploySysCCs(chainID)
logger.Infof("Deployed system chaincodes on %s", chainID)
//.........這裏部分代碼省略.........
示例3: serve
func serve(args []string) error {
// Parameter overrides must be processed before any paramaters are
// cached. Failures to cache cause the server to terminate immediately.
if chaincodeDevMode {
logger.Info("Running in chaincode development mode")
logger.Info("Set consensus to NOOPS and user starts chaincode")
logger.Info("Disable loading validity system chaincode")
viper.Set("peer.validator.enabled", "true")
viper.Set("peer.validator.consensus", "noops")
viper.Set("chaincode.mode", chaincode.DevModeUserRunsChaincode)
}
if err := peer.CacheConfiguration(); err != nil {
return err
}
peerEndpoint, err := peer.GetPeerEndpoint()
if err != nil {
err = fmt.Errorf("Failed to get Peer Endpoint: %s", err)
return err
}
listenAddr := viper.GetString("peer.listenAddress")
if "" == listenAddr {
logger.Debug("Listen address not specified, using peer endpoint address")
listenAddr = peerEndpoint.Address
}
lis, err := net.Listen("tcp", listenAddr)
if err != nil {
grpclog.Fatalf("Failed to listen: %v", err)
}
ehubLis, ehubGrpcServer, err := createEventHubServer()
if err != nil {
grpclog.Fatalf("Failed to create ehub server: %v", err)
}
logger.Infof("Security enabled status: %t", core.SecurityEnabled())
if viper.GetBool("security.privacy") {
if core.SecurityEnabled() {
logger.Infof("Privacy enabled status: true")
} else {
panic(errors.New("Privacy cannot be enabled as requested because security is disabled"))
}
} else {
logger.Infof("Privacy enabled status: false")
}
db.Start()
var opts []grpc.ServerOption
if comm.TLSEnabled() {
creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"),
viper.GetString("peer.tls.key.file"))
if err != nil {
grpclog.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
grpcServer := grpc.NewServer(opts...)
secHelper, err := getSecHelper()
if err != nil {
return err
}
secHelperFunc := func() crypto.Peer {
return secHelper
}
registerChaincodeSupport(chaincode.DefaultChain, grpcServer, secHelper)
var peerServer *peer.Impl
// Create the peerServer
if peer.ValidatorEnabled() {
logger.Debug("Running as validating peer - making genesis block if needed")
makeGenesisError := genesis.MakeGenesis()
if makeGenesisError != nil {
return makeGenesisError
}
logger.Debugf("Running as validating peer - installing consensus %s",
viper.GetString("peer.validator.consensus"))
peerServer, err = peer.NewPeerWithEngine(secHelperFunc, helper.GetEngine)
} else {
logger.Debug("Running as non-validating peer")
peerServer, err = peer.NewPeerWithHandler(secHelperFunc, peer.NewPeerHandler)
}
if err != nil {
logger.Fatalf("Failed creating new peer with handler %v", err)
return err
//.........這裏部分代碼省略.........
示例4: chaincodeInvokeOrQuery
// chaincodeInvokeOrQuery invokes or queries the chaincode. If successful, the
// INVOKE form prints the transaction ID on STDOUT, and the QUERY form prints
// the query result on STDOUT. A command-line flag (-r, --raw) determines
// whether the query result is output as raw bytes, or as a printable string.
// The printable form is optionally (-x, --hex) a hexadecimal representation
// of the query response. If the query response is NIL, nothing is output.
func chaincodeInvokeOrQuery(cmd *cobra.Command, args []string, invoke bool) (err error) {
if err = checkChaincodeCmdParams(cmd); err != nil {
return
}
if chaincodeName == "" {
err = errors.New("Name not given for invoke/query")
return
}
devopsClient, err := getDevopsClient(cmd)
if err != nil {
err = fmt.Errorf("Error building %s: %s", chainFuncName, err)
return
}
// Build the spec
input := &pb.ChaincodeInput{}
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {
err = fmt.Errorf("Chaincode argument error: %s", err)
return
}
var attributes []string
if err = json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {
err = fmt.Errorf("Chaincode argument error: %s", err)
return
}
chaincodeLang = strings.ToUpper(chaincodeLang)
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),
ChaincodeID: &pb.ChaincodeID{Name: chaincodeName}, CtorMsg: input, Attributes: attributes}
// If security is enabled, add client login token
if core.SecurityEnabled() {
if chaincodeUsr == undefinedParamValue {
err = errors.New("Must supply username for chaincode when security is enabled")
return
}
// Retrieve the CLI data storage path
// Returns /var/openchain/production/client/
localStore := getCliFilePath()
// Check if the user is logged in before sending transaction
if _, err = os.Stat(localStore + "loginToken_" + chaincodeUsr); err == nil {
logger.Infof("Local user '%s' is already logged in. Retrieving login token.\n", chaincodeUsr)
// Read in the login token
token, err := ioutil.ReadFile(localStore + "loginToken_" + chaincodeUsr)
if err != nil {
panic(fmt.Errorf("Fatal error when reading client login token: %s\n", err))
}
// Add the login token to the chaincodeSpec
spec.SecureContext = string(token)
// If privacy is enabled, mark chaincode as confidential
if viper.GetBool("security.privacy") {
logger.Info("Set confidentiality level to CONFIDENTIAL.\n")
spec.ConfidentialityLevel = pb.ConfidentialityLevel_CONFIDENTIAL
}
} else {
// Check if the token is not there and fail
if os.IsNotExist(err) {
err = fmt.Errorf("User '%s' not logged in. Use the 'login' command to obtain a security token.", chaincodeUsr)
return
}
// Unexpected error
panic(fmt.Errorf("Fatal error when checking for client login token: %s\n", err))
}
} else {
if chaincodeUsr != undefinedParamValue {
logger.Warning("Username supplied but security is disabled.")
}
if viper.GetBool("security.privacy") {
panic(errors.New("Privacy cannot be enabled as requested because security is disabled"))
}
}
// Build the ChaincodeInvocationSpec message
invocation := &pb.ChaincodeInvocationSpec{ChaincodeSpec: spec}
if customIDGenAlg != undefinedParamValue {
invocation.IdGenerationAlg = customIDGenAlg
}
var resp *pb.Response
if invoke {
resp, err = devopsClient.Invoke(context.Background(), invocation)
} else {
resp, err = devopsClient.Query(context.Background(), invocation)
}
if err != nil {
//.........這裏部分代碼省略.........
示例5: chaincodeDeploy
// chaincodeDeploy deploys the chaincode. On success, the chaincode name
// (hash) is printed to STDOUT for use by subsequent chaincode-related CLI
// commands.
func chaincodeDeploy(cmd *cobra.Command, args []string) (err error) {
if err = checkChaincodeCmdParams(cmd); err != nil {
return
}
devopsClient, err := getDevopsClient(cmd)
if err != nil {
err = fmt.Errorf("Error building %s: %s", chainFuncName, err)
return
}
// Build the spec
input := &pb.ChaincodeInput{}
if err = json.Unmarshal([]byte(chaincodeCtorJSON), &input); err != nil {
err = fmt.Errorf("Chaincode argument error: %s", err)
return
}
var attributes []string
if err = json.Unmarshal([]byte(chaincodeAttributesJSON), &attributes); err != nil {
err = fmt.Errorf("Chaincode argument error: %s", err)
return
}
chaincodeLang = strings.ToUpper(chaincodeLang)
spec := &pb.ChaincodeSpec{Type: pb.ChaincodeSpec_Type(pb.ChaincodeSpec_Type_value[chaincodeLang]),
ChaincodeID: &pb.ChaincodeID{Path: chaincodePath, Name: chaincodeName}, CtorMsg: input, Attributes: attributes}
// If security is enabled, add client login token
if core.SecurityEnabled() {
logger.Debug("Security is enabled. Include security context in deploy spec")
if chaincodeUsr == undefinedParamValue {
err = errors.New("Must supply username for chaincode when security is enabled")
return
}
// Retrieve the CLI data storage path
// Returns /var/openchain/production/client/
localStore := getCliFilePath()
// Check if the user is logged in before sending transaction
if _, err = os.Stat(localStore + "loginToken_" + chaincodeUsr); err == nil {
logger.Infof("Local user '%s' is already logged in. Retrieving login token.\n", chaincodeUsr)
// Read in the login token
token, err := ioutil.ReadFile(localStore + "loginToken_" + chaincodeUsr)
if err != nil {
panic(fmt.Errorf("Fatal error when reading client login token: %s\n", err))
}
// Add the login token to the chaincodeSpec
spec.SecureContext = string(token)
// If privacy is enabled, mark chaincode as confidential
if viper.GetBool("security.privacy") {
logger.Info("Set confidentiality level to CONFIDENTIAL.\n")
spec.ConfidentialityLevel = pb.ConfidentialityLevel_CONFIDENTIAL
}
} else {
// Check if the token is not there and fail
if os.IsNotExist(err) {
err = fmt.Errorf("User '%s' not logged in. Use the 'login' command to obtain a security token.", chaincodeUsr)
return
}
// Unexpected error
panic(fmt.Errorf("Fatal error when checking for client login token: %s\n", err))
}
} else {
if chaincodeUsr != undefinedParamValue {
logger.Warning("Username supplied but security is disabled.")
}
if viper.GetBool("security.privacy") {
panic(errors.New("Privacy cannot be enabled as requested because security is disabled"))
}
}
chaincodeDeploymentSpec, err := devopsClient.Deploy(context.Background(), spec)
if err != nil {
err = fmt.Errorf("Error building %s: %s\n", chainFuncName, err)
return
}
logger.Infof("Deploy result: %s", chaincodeDeploymentSpec.ChaincodeSpec)
fmt.Println(chaincodeDeploymentSpec.ChaincodeSpec.ChaincodeID.Name)
return nil
}
示例6: serve
func serve(args []string) error {
// Parameter overrides must be processed before any paramaters are
// cached. Failures to cache cause the server to terminate immediately.
if chaincodeDevMode {
logger.Info("Running in chaincode development mode")
logger.Info("Set consensus to NOOPS and user starts chaincode")
logger.Info("Disable loading validity system chaincode")
viper.Set("peer.validator.enabled", "true")
viper.Set("peer.validator.consensus", "noops")
viper.Set("chaincode.mode", chaincode.DevModeUserRunsChaincode)
// Disable validity system chaincode in dev mode. Also if security is enabled,
// in membersrvc.yaml, manually set pki.validity-period.update to false to prevent
// membersrvc from calling validity system chaincode -- though no harm otherwise
viper.Set("ledger.blockchain.deploy-system-chaincode", "false")
viper.Set("validator.validity-period.verification", "false")
}
if err := peer.CacheConfiguration(); err != nil {
return err
}
//register all system chaincodes. This just registers chaincodes, they must be
//still be deployed and launched
system_chaincode.RegisterSysCCs()
peerEndpoint, err := peer.GetPeerEndpoint()
if err != nil {
err = fmt.Errorf("Failed to get Peer Endpoint: %s", err)
return err
}
listenAddr := viper.GetString("peer.listenAddress")
if "" == listenAddr {
logger.Debug("Listen address not specified, using peer endpoint address")
listenAddr = peerEndpoint.Address
}
lis, err := net.Listen("tcp", listenAddr)
if err != nil {
grpclog.Fatalf("Failed to listen: %v", err)
}
ehubLis, ehubGrpcServer, err := createEventHubServer()
if err != nil {
grpclog.Fatalf("Failed to create ehub server: %v", err)
}
logger.Info("Security enabled status: %t", core.SecurityEnabled())
logger.Info("Privacy enabled status: %t", viper.GetBool("security.privacy"))
var opts []grpc.ServerOption
if peer.TLSEnabled() {
creds, err := credentials.NewServerTLSFromFile(viper.GetString("peer.tls.cert.file"), viper.GetString("peer.tls.key.file"))
if err != nil {
grpclog.Fatalf("Failed to generate credentials %v", err)
}
opts = []grpc.ServerOption{grpc.Creds(creds)}
}
grpcServer := grpc.NewServer(opts...)
secHelper, err := getSecHelper()
if err != nil {
return err
}
secHelperFunc := func() crypto.Peer {
return secHelper
}
registerChaincodeSupport(chaincode.DefaultChain, grpcServer, secHelper)
var peerServer *peer.PeerImpl
//create the peerServer....
if peer.ValidatorEnabled() {
logger.Debug("Running as validating peer - making genesis block if needed")
makeGenesisError := genesis.MakeGenesis()
if makeGenesisError != nil {
return makeGenesisError
}
logger.Debug("Running as validating peer - installing consensus %s", viper.GetString("peer.validator.consensus"))
peerServer, err = peer.NewPeerWithEngine(secHelperFunc, helper.GetEngine)
} else {
logger.Debug("Running as non-validating peer")
peerServer, err = peer.NewPeerWithHandler(secHelperFunc, peer.NewPeerHandler)
}
if err != nil {
logger.Fatalf("Failed creating new peer with handler %v", err)
return err
}
// Register the Peer server
//pb.RegisterPeerServer(grpcServer, openchain.NewPeer())
pb.RegisterPeerServer(grpcServer, peerServer)
// Register the Admin server
//.........這裏部分代碼省略.........