-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathcryptoconfiggen.go
More file actions
127 lines (116 loc) · 4.37 KB
/
cryptoconfiggen.go
File metadata and controls
127 lines (116 loc) · 4.37 KB
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
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"])
caSpec := make(map[string]string)
caSpec["Country"] = getString(ordererConfig["caCountry"])
caSpec["Province"] = getString(ordererConfig["caProvince"])
caSpec["Locality"] = getString(ordererConfig["caLocality"])
caSpec["OrganizationalUnit"] = getString(ordererConfig["caOrganizationalUnit"])
caSpec["StreetAddress"] = getString(ordererConfig["caStreetAddress"])
caSpec["PostalCode"] = getString(ordererConfig["caPostalCode"])
outputStructure["CA"] = caSpec
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") {
orderType := getString(ordererConfig["type"])
if orderType == "kafka" || orderType == "raft" {
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"
caTemplate["Country"] = getString(orgConfig["caCountry"])
caTemplate["Province"] = getString(orgConfig["caProvince"])
caTemplate["Locality"] = getString(orgConfig["caLocality"])
caTemplate["OrganizationalUnit"] = getString(orgConfig["caOrganizationalUnit"])
caTemplate["StreetAddress"] = getString(orgConfig["caStreetAddress"])
caTemplate["PostalCode"] = getString(orgConfig["caPostalCode"])
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
}