/
main.go
144 lines (125 loc) · 3.78 KB
/
main.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
package main
import (
"bytes"
"flag"
"fmt"
"io/ioutil"
"net/http"
"net/http/httputil"
"os"
"os/exec"
"time"
)
var (
listenAddress = "127.0.0.1:21700"
requestMade = false
)
const caClientConfigContentSw = `
bccsp:
default: SW
sw:
hash: SHA2
security: 256
filekeystore:
keystore: msp/keystore
`
const caClientConfigContentPkcs11 = `
bccsp:
default: PKCS11
pkcs11:
Library:
Pin:
Label:
hash: SHA2
security: 256
`
func main() {
// flags to parse
caClientBin := flag.String("b", "./fabric-ca-client", "path to fabric-ca-client binary")
homeDir := flag.String("m", "./testdata", "base folder of msp directory")
caProfile := flag.String("p", "", "CA profile to use")
caName := flag.String("n", "", "CA instance name")
debugEnabled := flag.Bool("d", false, "enable debug")
pkcs11Enabled := flag.Bool("pkcs11", false, "enable pkcs11")
pkcs11Library := flag.String("lib", "", "path to pkcs11 library")
pkcs11Label := flag.String("label", "", "name of pkcs11 label/slot")
pkcs11Pin := flag.String("pin", "", "pin for pkcs11 label/slot")
flag.Parse()
// confirm that the ca bin exists
printBinVersion(*caClientBin)
// by default use SW BCCSP config
caClientConfigContent := caClientConfigContentSw
// set up pkcs11 related environment variables and config if enabled
if *pkcs11Enabled {
caClientConfigContent = caClientConfigContentPkcs11
fmt.Println("PKCS11 is enabled. Setting up variables")
os.Setenv("FABRIC_CA_CLIENT_BCCSP_DEFAULT", "PKCS11")
os.Setenv("FABRIC_CA_CLIENT_BCCSP_PKCS11_LIBRARY", *pkcs11Library)
os.Setenv("FABRIC_CA_CLIENT_BCCSP_PKCS11_LABEL", *pkcs11Label)
os.Setenv("FABRIC_CA_CLIENT_BCCSP_PKCS11_PIN", *pkcs11Pin)
}
// write fabric-ca-client config file
configFilePath := *homeDir + "/fabric-ca-client-config.yaml"
err := createCAClientConfig(configFilePath, caClientConfigContent)
if err != nil {
panic(fmt.Sprintf("unable to create config file %s: %v", configFilePath, err))
}
// start http server to catch response
go startHttpServer()
time.Sleep(500 * time.Millisecond)
// execute reenroll
fmt.Printf("Using MSP path: %s/msp\n", *homeDir)
cmd := exec.Command(
*caClientBin, "reenroll",
"--home", *homeDir,
"--url", fmt.Sprintf("http://%s", listenAddress),
"--enrollment.profile", *caProfile,
"--caname", *caName,
"--debug",
)
var out, errOut bytes.Buffer
cmd.Stdout = &out
cmd.Stderr = &errOut
// run command and ignore error (since the fabric-ca-client binary won't like the response)
fmt.Printf("Expecting Request...\n\n")
err = cmd.Run()
time.Sleep(200 * time.Millisecond)
// if our server didn't receive a request then log the stdout and stderr
if requestMade == false || *debugEnabled {
fmt.Printf("\nDEBUG OUTPUT:\n> err: %s\n> stdOut: %s\n> stdErr: %s\n", err, out.String(), errOut.String())
}
}
func startHttpServer() {
// override default httpFunc
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// dump request to log
requestDump, err := httputil.DumpRequest(r, true)
if err != nil {
fmt.Println(err)
}
fmt.Println(string(requestDump))
requestMade = true
fmt.Fprint(w, "INVALID FABRIC-CA SERVER RESPONSE")
})
// start the server
fmt.Println("Starting HTTP server...")
err := http.ListenAndServe(listenAddress, nil)
if err != nil {
panic(fmt.Sprintln("Error starting HTTP server:", err))
}
}
func printBinVersion(bin string) {
fmt.Println("using bin:", bin)
cmd := exec.Command(bin, "version")
var out bytes.Buffer
cmd.Stdout = &out
err := cmd.Run()
fmt.Println(out.String())
if err != nil {
fmt.Println("Error executing fabric-ca-client binary:", err)
os.Exit(1)
}
}
func createCAClientConfig(filePath, fileContent string) error {
return ioutil.WriteFile(filePath, []byte(fileContent), 0644)
}