forked from suddutt1/fabricnetgenerator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcryptoconfiggen.go
110 lines (101 loc) · 3.47 KB
/
cryptoconfiggen.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
package main
import (
"encoding/json"
"fmt"
"io/ioutil"
"strings"
"gopkg.in/yaml.v2"
)
//GenerateCrytoConfig generate the CryptoConfig file
func GenerateCrytoConfig(config []byte, filePath string) bool {
useCA := false
isSuccess := true
rootConfig := make(map[string]interface{})
err := json.Unmarshal(config, &rootConfig)
if err != nil {
fmt.Printf("Error in parsing the config bytes %v", err)
return false
}
useCA = getBoolean(rootConfig["addCA"])
//Perform the orderer part
ordererConfig := getMap(rootConfig["orderers"])
if ordererConfig == nil {
fmt.Println("No orderer specified")
return false
}
cryptoConfig := make(map[string]interface{})
orderOrgs := make([]map[string]interface{}, 0)
orderOrgs = append(orderOrgs, buildOrderConfig(ordererConfig))
cryptoConfig["OrdererOrgs"] = orderOrgs
orgs, orgsExists := rootConfig["orgs"].([]interface{})
if !orgsExists {
fmt.Println("No organizations specified")
return false
}
addNodeOU := IsVersionAbove(rootConfig, "1.3.0")
peerOrgs := make([]map[string]interface{}, 0)
for _, orgConfig := range orgs {
peerOrgs = append(peerOrgs, buildOrgConfig(getMap(orgConfig), useCA, addNodeOU))
}
cryptoConfig["PeerOrgs"] = peerOrgs
outBytes, _ := yaml.Marshal(cryptoConfig)
//fmt.Printf("Crypto config Orderes\n%s\n", string(outBytes))
ioutil.WriteFile(filePath, outBytes, 0666)
return isSuccess
}
func buildOrderConfig(ordererConfig map[string]interface{}) map[string]interface{} {
outputStructure := make(map[string]interface{})
outputStructure["Name"] = getString(ordererConfig["name"])
outputStructure["Domain"] = getString(ordererConfig["domain"])
specs := make([]map[string]interface{}, 0)
//Assuing one as of now
sansInput := strings.Split(getString(ordererConfig["SANS"]), ",")
sansArray := make([]string, len(sansInput))
for indx, sans := range sansInput {
sansArray[indx] = sans
}
sansSpec := make(map[string]interface{})
sansSpec["SANS"] = sansArray
specs = append(specs, sansSpec)
if ifExists(ordererConfig, "haCount") && ifExists(ordererConfig, "type") {
if getString(ordererConfig["type"]) == "kafka" {
template := make(map[string]interface{})
template["Count"] = ordererConfig["haCount"]
template["Hostname"] = fmt.Sprintf("%s{{.Index}}", getString(ordererConfig["ordererHostname"]))
outputStructure["Template"] = template
}
} else {
hostnameSpec := make(map[string]interface{})
hostnameSpec["Hostname"] = getString(ordererConfig["ordererHostname"])
specs = append(specs, hostnameSpec)
}
outputStructure["Specs"] = specs
return outputStructure
}
func buildOrgConfig(orgConfig map[string]interface{}, useCA, addNodeOU bool) map[string]interface{} {
outputStructure := make(map[string]interface{})
outputStructure["Name"] = getString(orgConfig["name"])
outputStructure["Domain"] = getString(orgConfig["domain"])
if addNodeOU {
outputStructure["EnableNodeOUs"] = true
}
if useCA == true {
caTemplate := make(map[string]string)
caTemplate["Hostname"] = "ca"
outputStructure["CA"] = caTemplate
}
template := make(map[string]interface{})
template["Count"] = orgConfig["peerCount"]
//Assuing one as of now
sansInput := strings.Split(getString(orgConfig["SANS"]), ",")
sansArray := make([]string, len(sansInput))
for indx, sans := range sansInput {
sansArray[indx] = sans
}
template["SANS"] = sansArray
outputStructure["Template"] = template
users := make(map[string]interface{})
users["Count"] = orgConfig["userCount"]
outputStructure["Users"] = users
return outputStructure
}