forked from suddutt1/fabricnetgenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathhuntcertificates.go
103 lines (94 loc) · 3.13 KB
/
huntcertificates.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
yaml "gopkg.in/yaml.v2"
)
//HuntCertificates hunts the certificates of the users and private keys
func HuntCertificates(baseDir string, isJSON bool) {
fmt.Println("Hunting user certificates at ", baseDir)
orgMainPath := baseDir + "/crypto-config/peerOrganizations"
orgs, err := ioutil.ReadDir(baseDir + "/crypto-config/peerOrganizations")
if err != nil {
fmt.Printf("\nError is reading %s %+v", orgMainPath, err)
return
}
for _, orgDirectory := range orgs {
if orgDirectory.IsDir() {
outputFileName := fmt.Sprintf("./%s_users.yaml", orgDirectory.Name())
if isJSON {
outputFileName = fmt.Sprintf("./%s_users.json", orgDirectory.Name())
}
usersEntry := make(map[string]interface{})
fmt.Println("Going to get the users of the org ", orgDirectory.Name())
userDir := fmt.Sprintf("%s/%s/users", orgMainPath, orgDirectory.Name())
userDirs, err := ioutil.ReadDir(userDir)
if err != nil {
fmt.Printf("\nError is reading %s %+v", userDir, err)
return
}
for _, userDir := range userDirs {
fmt.Println("Going to get the certificates of ", userDir.Name())
certPath := fmt.Sprintf("%s/%s/users/%s/msp/signcerts", orgMainPath, orgDirectory.Name(), userDir.Name())
certFiles, err := ioutil.ReadDir(certPath)
if err != nil {
fmt.Printf("\nError is reading %s %+v", certPath, err)
return
}
certFileName := ""
for _, certFile := range certFiles {
if !certFile.IsDir() {
fmt.Println("Reading file ", certFile.Name())
certFileName = fmt.Sprintf("%s/%s", certPath, certFile.Name())
}
}
keyPath := fmt.Sprintf("%s/%s/users/%s/msp/keystore", orgMainPath, orgDirectory.Name(), userDir.Name())
keyFiles, err := ioutil.ReadDir(keyPath)
if err != nil {
fmt.Printf("\nError is reading %s %+v", keyPath, err)
return
}
keyFileName := ""
for _, keyFile := range keyFiles {
if !keyFile.IsDir() {
fmt.Println("Reading file ", keyFile.Name())
keyFileName = fmt.Sprintf("%s/%s", keyPath, keyFile.Name())
}
}
GenerateCertKeyEntry(certFileName, keyFileName, userDir.Name(), isJSON, usersEntry)
}
finalEntry := make(map[string]interface{})
finalEntry["users"] = usersEntry
if isJSON {
finalOutput, _ := json.MarshalIndent(finalEntry, "", " ")
ioutil.WriteFile(outputFileName, finalOutput, 0666)
} else {
finalOutput, _ := yaml.Marshal(finalEntry)
ioutil.WriteFile(outputFileName, finalOutput, 0666)
}
}
}
}
func GenerateCertKeyEntry(certPath, privKeyPath string, userID string, isJson bool, userEntryMap map[string]interface{}) {
certBytes, _ := ioutil.ReadFile(certPath)
keyBytes, _ := ioutil.ReadFile(privKeyPath)
output := make(map[string]interface{})
pemCert := make(map[string]string)
if isJson {
pemCert["pem"] = string(certBytes)
} else {
pemCert["path"] = certPath
}
output["cert"] = pemCert
pemKey := make(map[string]interface{})
if isJson {
pemKey["pem"] = string(keyBytes)
} else {
pemKey["path"] = privKeyPath
}
output["key"] = pemKey
userIDOnly := strings.Split(userID, "@")[0]
userEntryMap[userIDOnly] = output
}