-
Notifications
You must be signed in to change notification settings - Fork 0
/
test-util.go
241 lines (212 loc) · 6 KB
/
test-util.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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
/*
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 lib
import (
"crypto/rand"
"crypto/x509"
"crypto/x509/pkix"
"encoding/pem"
"errors"
"fmt"
"io"
"io/ioutil"
"math/big"
"os"
"path"
"strconv"
"testing"
"time"
"github.com/cloudflare/cfssl/config"
)
const (
rootPort = 7075
rootDir = "rootDir"
rootClientDir = "rootClientDir"
intermediatePort = 7076
intermediateDir = "intDir"
testdataDir = "../testdata"
)
func getRootServerURL() string {
return fmt.Sprintf("http://admin:adminpw@localhost:%d", rootPort)
}
// TestGetRootServer creates a server with root configuration
func TestGetRootServer(t *testing.T) *Server {
return TestGetServer(rootPort, rootDir, "", -1, t)
}
// TestGetIntermediateServer creates a server with intermediate server configuration
func TestGetIntermediateServer(idx int, t *testing.T) *Server {
return TestGetServer(
intermediatePort,
path.Join(intermediateDir, strconv.Itoa(idx)),
getRootServerURL(),
-1,
t)
}
// TestGetServer creates and returns a pointer to a server struct
func TestGetServer(port int, home, parentURL string, maxEnroll int, t *testing.T) *Server {
return TestGetServer2(home != testdataDir, port, home, parentURL, maxEnroll, t)
}
// TestGetServer2 creates and returns a pointer to a server struct, with an option of
// whether or not to remove the home directory first
func TestGetServer2(deleteHome bool, port int, home, parentURL string, maxEnroll int, t *testing.T) *Server {
if deleteHome {
os.RemoveAll(home)
}
affiliations := map[string]interface{}{
"hyperledger": map[string]interface{}{
"fabric": []string{"ledger", "orderer", "security"},
"fabric-ca": nil,
"sdk": nil,
},
"org2": []string{"dept1"},
"org1": nil,
"org2dept1": nil,
}
profiles := map[string]*config.SigningProfile{
"tls": &config.SigningProfile{
Usage: []string{"signing", "key encipherment", "server auth", "client auth", "key agreement"},
ExpiryString: "8760h",
},
"ca": &config.SigningProfile{
Usage: []string{"cert sign", "crl sign"},
ExpiryString: "8760h",
CAConstraint: config.CAConstraint{
IsCA: true,
MaxPathLen: 0,
},
},
}
defaultProfile := &config.SigningProfile{
Usage: []string{"cert sign"},
ExpiryString: "8760h",
}
srv := &Server{
Config: &ServerConfig{
Port: port,
Debug: true,
},
CA: CA{
Config: &CAConfig{
Intermediate: IntermediateCA{
ParentServer: ParentServer{
URL: parentURL,
},
},
Affiliations: affiliations,
Registry: CAConfigRegistry{
MaxEnrollments: maxEnroll,
},
Signing: &config.Signing{
Profiles: profiles,
Default: defaultProfile,
},
Version: "1.1.0", // The default test server/ca should use the latest version
},
},
HomeDir: home,
}
// The bootstrap user's affiliation is the empty string, which
// means the user is at the affiliation root
err := srv.RegisterBootstrapUser("admin", "adminpw", "")
if err != nil {
t.Errorf("Failed to register bootstrap user: %s", err)
return nil
}
return srv
}
// CopyFile copies a file
func CopyFile(src, dst string) error {
srcFile, err := os.Open(src)
if err != nil {
return err
}
defer srcFile.Close()
destFile, err := os.Create(dst)
if err != nil {
return err
}
defer destFile.Close()
_, err = io.Copy(destFile, srcFile)
if err != nil {
return err
}
err = destFile.Sync()
if err != nil {
return err
}
return nil
}
// GenerateECDSATestCert generates EC based certificate for testing purposes
func GenerateECDSATestCert() error {
template := &x509.Certificate{
IsCA: true,
BasicConstraintsValid: true,
SubjectKeyId: []byte{1, 2, 3},
SerialNumber: big.NewInt(1234),
Subject: pkix.Name{
Country: []string{"US"},
Organization: []string{"IBM"},
},
NotBefore: time.Now(),
NotAfter: time.Now().AddDate(15, 0, 0),
ExtKeyUsage: []x509.ExtKeyUsage{x509.ExtKeyUsageClientAuth, x509.ExtKeyUsageServerAuth},
KeyUsage: x509.KeyUsageDigitalSignature | x509.KeyUsageCertSign,
}
privKey, err := ioutil.ReadFile("../testdata/ec_key.pem")
if err != nil {
return err
}
decoded, _ := pem.Decode(privKey)
if decoded == nil {
return errors.New("Failed to decode the PEM-encoded ECDSA key")
}
privateKey, err := x509.ParseECPrivateKey(decoded.Bytes)
if err != nil {
return err
}
publicKey := &privateKey.PublicKey
var parent = template
cert, err := x509.CreateCertificate(rand.Reader, template, parent, publicKey, privateKey)
if err != nil {
return err
}
certOut, err := os.Create("../testdata/ec_cert.pem")
if err != nil {
return err
}
pem.Encode(certOut, &pem.Block{Type: "CERTIFICATE", Bytes: cert})
return nil
}
// Currently not being used anywhere, commenting it out for right now
// it was just bringing test coverage.
// StopAndCleanupServer stops the server and removes the server's home directory
// func StopAndCleanupServer(t *testing.T, srv *Server) {
// if srv != nil {
// defer os.RemoveAll(srv.HomeDir)
// err := srv.Stop()
// if err != nil {
// t.Errorf("Server stop failed: %s", err)
// }
// }
// }
// TestGetRootClient returns a Fabric CA client that is meant for a root Fabric CA server
func TestGetRootClient() *Client {
return TestGetClient(rootPort, rootClientDir)
}
// TestGetClient returns a Fabric CA client
func TestGetClient(port int, home string) *Client {
return &Client{
Config: &ClientConfig{URL: fmt.Sprintf("http://localhost:%d", port)},
HomeDir: home,
}
}