Skip to content

Commit 9a37217

Browse files
mbrandenburgerbvavala
authored andcommitted
Add k8s-based demo network
Signed-off-by: Marcus brandenburger <[email protected]>
1 parent a50c9e1 commit 9a37217

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

56 files changed

+4230
-81
lines changed

client_sdk/go/sample/Makefile

+3-10
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,7 @@
66
TOP = ../../..
77
include $(TOP)/build.mk
88

9-
CC_ID ?= echo
9+
SUB_DIRS = cli_app simple
1010

11-
build:
12-
$(GO) build
13-
14-
clean:
15-
$(GO) clean
16-
rm -rf keystore wallet
17-
18-
run:
19-
env CC_ID=${CC_ID} $(GO) run .
11+
all build test clean clobber:
12+
$(foreach DIR, $(SUB_DIRS), $(MAKE) -C $(DIR) $@ || exit ;)
+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
# SPDX-License-Identifier: Apache-2.0
2+
**/wallet/*
3+
**/keystore/*
4+
fpcclient
5+
cli_app
+9
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
# Copyright IBM Corp. All Rights Reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
FROM hyperledger/fabric-private-chaincode-ccenv:latest
6+
7+
COPY fpcclient /usr/local/bin
8+
9+
WORKDIR /opt/gopath/src/github.com/hyperledger/fabric/peer

client_sdk/go/sample/cli_app/Makefile

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
# Copyright IBM Corp. All Rights Reserved.
2+
#
3+
# SPDX-License-Identifier: Apache-2.0
4+
5+
TOP = ../../../..
6+
include $(TOP)/build.mk
7+
8+
build:
9+
$(GO) build $(GOTAGS) -o fpcclient
10+
11+
docker:
12+
$(DOCKER) build $(DOCKER_BUILD_OPTS) \
13+
--no-cache \
14+
-t fpc/fpcclient:latest \
15+
-f Dockerfile \
16+
.
+27
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/sample/cli_app/pkg"
11+
"github.com/spf13/cobra"
12+
)
13+
14+
func init() {
15+
rootCmd.AddCommand(initCmd)
16+
}
17+
18+
var initCmd = &cobra.Command{
19+
Use: "init [target_peer]",
20+
Short: "initialize enclave",
21+
Args: cobra.ExactArgs(1),
22+
RunE: func(cmd *cobra.Command, args []string) error {
23+
admin := pkg.NewAdmin(config)
24+
defer admin.Close()
25+
return admin.InitEnclave(args[0])
26+
},
27+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"fmt"
11+
12+
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/sample/cli_app/pkg"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
func init() {
17+
rootCmd.AddCommand(invokeCmd)
18+
}
19+
20+
var invokeCmd = &cobra.Command{
21+
Use: "invoke [function] [arg1] [arg2] ...",
22+
Short: "invoke FPC Chaincode with function and arguments",
23+
Args: cobra.MinimumNArgs(1),
24+
Run: func(cmd *cobra.Command, args []string) {
25+
client := pkg.NewClient(config)
26+
res := client.Call(args[0], args[1:]...)
27+
fmt.Println("> " + res)
28+
},
29+
}
+29
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"fmt"
11+
12+
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/sample/cli_app/pkg"
13+
"github.com/spf13/cobra"
14+
)
15+
16+
func init() {
17+
rootCmd.AddCommand(queryCmd)
18+
}
19+
20+
var queryCmd = &cobra.Command{
21+
Use: "query [function] [arg1] [arg2] ...",
22+
Short: "query FPC Chaincode with function and arguments",
23+
Args: cobra.MinimumNArgs(1),
24+
Run: func(cmd *cobra.Command, args []string) {
25+
client := pkg.NewClient(config)
26+
res := client.Call(args[0], args[1:]...)
27+
fmt.Println("> " + res)
28+
},
29+
}
+70
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package cmd
8+
9+
import (
10+
"fmt"
11+
"os"
12+
"strconv"
13+
14+
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/sample/cli_app/pkg"
15+
"github.com/spf13/cobra"
16+
)
17+
18+
var (
19+
rootCmd = &cobra.Command{
20+
Use: "fpcclient",
21+
Short: "A demo app that calls FPC chaincode",
22+
}
23+
config *pkg.Config
24+
)
25+
26+
// Execute executes the root command.
27+
func Execute() error {
28+
return rootCmd.Execute()
29+
}
30+
31+
func init() {
32+
cobra.OnInitialize(initConfig)
33+
}
34+
35+
func initConfig() {
36+
37+
getStrEnv := func(key string) string {
38+
val := os.Getenv(key)
39+
if val == "" {
40+
panic(fmt.Sprintf("%s not set", key))
41+
}
42+
return val
43+
}
44+
45+
getBoolEnv := func(key string) bool {
46+
val := getStrEnv(key)
47+
ret, err := strconv.ParseBool(val)
48+
if err != nil {
49+
if val == "" {
50+
panic(fmt.Sprintf("invalid bool value for %s", key))
51+
}
52+
}
53+
return ret
54+
}
55+
56+
config = &pkg.Config{
57+
CorePeerAddress: getStrEnv("CORE_PEER_ADDRESS"),
58+
CorePeerId: getStrEnv("CORE_PEER_ID"),
59+
CorePeerLocalMSPID: getStrEnv("CORE_PEER_LOCALMSPID"),
60+
CorePeerMSPConfigPath: getStrEnv("CORE_PEER_MSPCONFIGPATH"),
61+
CorePeerTLSCertFile: getStrEnv("CORE_PEER_TLS_CERT_FILE"),
62+
CorePeerTLSEnabled: getBoolEnv("CORE_PEER_TLS_ENABLED"),
63+
CorePeerTLSKeyFile: getStrEnv("CORE_PEER_TLS_KEY_FILE"),
64+
CorePeerTLSRootCertFile: getStrEnv("CORE_PEER_TLS_ROOTCERT_FILE"),
65+
OrdererCA: getStrEnv("ORDERER_CA"),
66+
ChaincodeId: getStrEnv("CC_NAME"),
67+
ChannelId: getStrEnv("CHANNEL_NAME"),
68+
GatewayConfigPath: getStrEnv("GATEWAY_CONFIG"),
69+
}
70+
}

client_sdk/go/sample/cli_app/main.go

+16
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
Copyright 2020 Intel Corporation
4+
5+
SPDX-License-Identifier: Apache-2.0
6+
*/
7+
8+
package main
9+
10+
import (
11+
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/sample/cli_app/cmd"
12+
)
13+
14+
func main() {
15+
cmd.Execute()
16+
}
+23
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,23 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package pkg
8+
9+
type Config struct {
10+
CorePeerAddress string
11+
CorePeerId string
12+
CorePeerLocalMSPID string
13+
CorePeerMSPConfigPath string
14+
CorePeerTLSCertFile string
15+
CorePeerTLSEnabled bool
16+
CorePeerTLSKeyFile string
17+
CorePeerTLSRootCertFile string
18+
OrdererCA string
19+
FpcPath string
20+
ChaincodeId string
21+
ChannelId string
22+
GatewayConfigPath string
23+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package pkg
8+
9+
import (
10+
"os"
11+
12+
"gopkg.in/yaml.v2"
13+
)
14+
15+
type Connections struct {
16+
Peers map[string]struct {
17+
Url string
18+
}
19+
20+
Orderers map[string]struct {
21+
Url string
22+
}
23+
}
24+
25+
func NewConnections(path string) (*Connections, error) {
26+
connections := &Connections{}
27+
28+
file, err := os.Open(path)
29+
if err != nil {
30+
return nil, err
31+
}
32+
defer file.Close()
33+
34+
d := yaml.NewDecoder(file)
35+
36+
if err := d.Decode(&connections); err != nil {
37+
return nil, err
38+
}
39+
40+
return connections, nil
41+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,86 @@
1+
/*
2+
Copyright IBM Corp. All Rights Reserved.
3+
4+
SPDX-License-Identifier: Apache-2.0
5+
*/
6+
7+
package pkg
8+
9+
import (
10+
"fmt"
11+
"path/filepath"
12+
13+
fpcmgmt "github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/pkg/client/resmgmt"
14+
"github.com/hyperledger-labs/fabric-private-chaincode/client_sdk/go/pkg/sgx"
15+
"github.com/hyperledger/fabric-sdk-go/pkg/client/resmgmt"
16+
"github.com/hyperledger/fabric-sdk-go/pkg/common/errors/retry"
17+
cfg "github.com/hyperledger/fabric-sdk-go/pkg/core/config"
18+
"github.com/hyperledger/fabric-sdk-go/pkg/fabsdk"
19+
)
20+
21+
type Admin struct {
22+
sdk *fabsdk.FabricSDK
23+
client *fpcmgmt.Client
24+
config *Config
25+
connections *Connections
26+
}
27+
28+
func (a *Admin) Close() {
29+
a.sdk.Close()
30+
}
31+
32+
func NewAdmin(config *Config) *Admin {
33+
connections, err := NewConnections(filepath.Clean(config.GatewayConfigPath))
34+
if err != nil {
35+
logger.Fatalf("failed to parse connections: %v", err)
36+
}
37+
38+
sdk, err := fabsdk.New(cfg.FromFile(filepath.Clean(config.GatewayConfigPath)))
39+
if err != nil {
40+
logger.Fatalf("failed to create sdk: %v", err)
41+
}
42+
//defer sdk.Close()
43+
44+
orgAdmin := "Admin"
45+
orgName := "org1"
46+
adminContext := sdk.Context(fabsdk.WithUser(orgAdmin), fabsdk.WithOrg(orgName))
47+
48+
client, err := fpcmgmt.New(adminContext)
49+
if err != nil {
50+
logger.Fatalf("failed to create context: %v", err)
51+
}
52+
53+
return &Admin{sdk: sdk, client: client, config: config, connections: connections}
54+
}
55+
56+
func (a *Admin) InitEnclave(targetPeer string) error {
57+
58+
logger.Infof("--> Collection attestation params ")
59+
attestationParams, err := sgx.CreateAttestationParamsFromEnvironment()
60+
if err != nil {
61+
return fmt.Errorf("failed to load attestation params from environment: %v", err)
62+
}
63+
64+
initReq := fpcmgmt.LifecycleInitEnclaveRequest{
65+
ChaincodeID: a.config.ChaincodeId,
66+
EnclavePeerEndpoint: targetPeer, // define the peer where we wanna init our enclave
67+
AttestationParams: attestationParams,
68+
}
69+
70+
peers := []string{"peer0-org1", "peer0-org2", "peer0-org3"}
71+
orderer := "orderer0"
72+
73+
logger.Infof("--> LifecycleInitEnclave ")
74+
_, err = a.client.LifecycleInitEnclave(a.config.ChannelId, initReq,
75+
// Note that these options are currently ignored by our implementation
76+
resmgmt.WithRetry(retry.DefaultResMgmtOpts),
77+
resmgmt.WithTargetEndpoints(peers...), // peers that are responsible for enclave registration
78+
resmgmt.WithOrdererEndpoint(orderer),
79+
)
80+
81+
if err != nil {
82+
return err
83+
}
84+
85+
return nil
86+
}

0 commit comments

Comments
 (0)