forked from hyperledger/fabric-ca
-
Notifications
You must be signed in to change notification settings - Fork 0
/
config.go
173 lines (146 loc) · 4.85 KB
/
config.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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
/*
Copyright IBM Corp. 2016 All Rights Reserved.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package server
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"github.com/cloudflare/cfssl/cli"
"github.com/cloudflare/cfssl/log"
"github.com/hyperledger/fabric-ca/api"
"github.com/hyperledger/fabric-ca/cli/server/ldap"
libcsp "github.com/hyperledger/fabric-ca/lib/csp"
"github.com/hyperledger/fabric-ca/lib/tls"
"github.com/hyperledger/fabric-ca/util"
_ "github.com/mattn/go-sqlite3" // Needed to support sqlite
)
// Config is the fabric-ca config structure
type Config struct {
Debug bool `json:"debug,omitempty"`
Authentication bool `json:"authentication,omitempty"`
Users map[string]*User `json:"users,omitempty"`
DBdriver string `json:"driver"`
DataSource string `json:"data_source"`
UsrReg UserReg `json:"user_registry"`
LDAP *ldap.Config `json:"ldap,omitempty"`
CAFile string `json:"ca_cert"`
KeyFile string `json:"ca_key"`
TLSConf TLSConfig `json:"tls,omitempty"`
TLSDisable bool `json:"tls_disable,omitempty"`
CSP *libcsp.Config `json:"csp,omitempty"`
}
// UserReg defines the user registry properties
type UserReg struct {
MaxEnrollments int `json:"max_enrollments"`
}
// TLSConfig defines the files needed for a TLS connection
type TLSConfig struct {
TLSCertFile string `json:"tls_cert,omitempty"`
TLSKeyFile string `json:"tls_key,omitempty"`
MutualTLSCAFile string `json:"mutual_tls_ca,omitempty"`
DBClient tls.ClientTLSConfig `json:"db_client,omitempty"`
}
// User information
type User struct {
Pass string `json:"pass"` // enrollment secret
Type string `json:"type"`
Group string `json:"group"`
Attributes []api.Attribute `json:"attrs,omitempty"`
}
// Constructor for fabric-ca config
func newConfig() *Config {
c := new(Config)
c.Authentication = true
return c
}
// CFG is the fabric-ca specific config
var CFG *Config
// Init initializes the fabric-ca config given the CFSSL config
func configInit(cfg *cli.Config) {
var err error
configFile, err = filepath.Abs(cfg.ConfigFile)
if err != nil {
panic(err.Error())
}
configDir = filepath.Dir(configFile)
log.Debugf("Initializing config file at %s", configFile)
log.Debugf("Inbound CFSSL server config is: %+v", cfg)
CFG = new(Config)
CFG.Authentication = true
body, err := ioutil.ReadFile(configFile)
if err != nil {
panic(err.Error())
}
err = json.Unmarshal(body, CFG)
if err != nil {
panic(fmt.Sprintf("error parsing %s: %s", configFile, err.Error()))
}
CFG.CAFile = abs(CFG.CAFile)
CFG.KeyFile = abs(CFG.KeyFile)
CFG.TLSConf.TLSCertFile = abs(CFG.TLSConf.TLSCertFile)
CFG.TLSConf.TLSKeyFile = abs(CFG.TLSConf.TLSKeyFile)
CFG.TLSConf.MutualTLSCAFile = abs(CFG.TLSConf.MutualTLSCAFile)
absTLSClient(&CFG.TLSConf.DBClient)
if cfg.DBConfigFile == "" {
cfg.DBConfigFile = cfg.ConfigFile
}
if CFG.TLSConf.TLSCertFile != "" {
cfg.TLSCertFile = CFG.TLSConf.TLSCertFile
} else {
cfg.TLSCertFile = CFG.CAFile
}
if CFG.TLSConf.TLSKeyFile != "" {
cfg.TLSKeyFile = CFG.TLSConf.TLSKeyFile
} else {
cfg.TLSKeyFile = CFG.KeyFile
}
if CFG.TLSConf.MutualTLSCAFile != "" {
cfg.MutualTLSCAFile = CFG.TLSConf.MutualTLSCAFile
}
if CFG.DBdriver == "" {
msg := "No database specified, a database is needed to run fabric-ca server. Using default - Type: SQLite, Name: fabric-ca.db"
log.Info(msg)
CFG.DBdriver = sqlite
CFG.DataSource = "fabric-ca.db"
}
if CFG.DBdriver == sqlite {
CFG.DataSource = abs(CFG.DataSource)
}
dbg := os.Getenv("FABRIC_CA_DEBUG")
if dbg != "" {
CFG.Debug = dbg == "true"
}
if CFG.Debug {
log.Level = log.LevelDebug
}
log.Debugf("CFSSL server config is: %+v", cfg)
log.Debugf("Fabric CA server config is: %+v", CFG)
}
// Make TLS client files absolute
func absTLSClient(cfg *tls.ClientTLSConfig) {
for i := 0; i < len(cfg.CACertFiles); i++ {
cfg.CACertFiles[i] = abs(cfg.CACertFiles[i])
}
cfg.Client.CertFile = abs(cfg.Client.CertFile)
cfg.Client.KeyFile = abs(cfg.Client.KeyFile)
}
// Make 'file' absolute relative to the configuration directory
func abs(file string) string {
path, err := util.MakeFileAbs(file, configDir)
if err != nil {
panic(err)
}
return path
}