Skip to content
This repository was archived by the owner on Apr 25, 2025. It is now read-only.

Commit 2ecb4a5

Browse files
[FAB-2979]Fixed TLS Config for fabric CA client
Change-Id: I4425d49b692ed578e41247769b46c75b93b2e480 Signed-off-by: biljana lukovic <biljana.lukovic@securekey.com>
1 parent d36e7eb commit 2ecb4a5

File tree

5 files changed

+72
-43
lines changed

5 files changed

+72
-43
lines changed

config/config.go

Lines changed: 31 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,6 @@ package config
2121

2222
import (
2323
"crypto/x509"
24-
"encoding/json"
2524
"encoding/pem"
2625
"fmt"
2726
"io/ioutil"
@@ -43,15 +42,6 @@ type PeerConfig struct {
4342
TLSServerHostOverride string
4443
}
4544

46-
type fabricCAConfig struct {
47-
ServerURL string `json:"serverURL"`
48-
Certfiles []string `json:"certfiles"`
49-
Client struct {
50-
Keyfile string `json:"keyfile"`
51-
Certfile string `json:"certfile"`
52-
} `json:"client"`
53-
}
54-
5545
var myViper = viper.New()
5646
var log = logging.MustGetLogger("fabric_sdk_go")
5747
var format = logging.MustStringFormatter(
@@ -74,7 +64,7 @@ func InitConfig(configFile string) error {
7464
return fmt.Errorf("Fatal error config file: %v", err)
7565
}
7666
}
77-
67+
log.Debug(myViper.GetString("client.fabricCA.serverURL"))
7868
backend := logging.NewLogBackend(os.Stderr, "", 0)
7969
backendFormatter := logging.NewBackendFormatter(backend, format)
8070

@@ -93,6 +83,36 @@ func InitConfig(configFile string) error {
9383
return nil
9484
}
9585

86+
//GetServerURL Read configuration option for the fabric CA server URL
87+
func GetServerURL() string {
88+
return strings.Replace(myViper.GetString("client.fabricCA.serverURL"), "$GOPATH", os.Getenv("GOPATH"), -1)
89+
}
90+
91+
//GetServerCertFiles Read configuration option for the server certificate files
92+
func GetServerCertFiles() []string {
93+
certFiles := myViper.GetStringSlice("client.fabricCA.certfiles")
94+
certFileModPath := make([]string, len(certFiles))
95+
for i, v := range certFiles {
96+
certFileModPath[i] = strings.Replace(v, "$GOPATH", os.Getenv("GOPATH"), -1)
97+
}
98+
return certFileModPath
99+
}
100+
101+
//GetFabricCAClientKeyFile Read configuration option for the fabric CA client key file
102+
func GetFabricCAClientKeyFile() string {
103+
return strings.Replace(myViper.GetString("client.fabricCA.client.keyfile"), "$GOPATH", os.Getenv("GOPATH"), -1)
104+
}
105+
106+
//GetFabricCAClientCertFile Read configuration option for the fabric CA client cert file
107+
func GetFabricCAClientCertFile() string {
108+
return strings.Replace(myViper.GetString("client.fabricCA.client.keyfile"), "$GOPATH", os.Getenv("GOPATH"), -1)
109+
}
110+
111+
//GetFabricCATLSEnabledFlag Read configuration option for the fabric CA TLS flag
112+
func GetFabricCATLSEnabledFlag() bool {
113+
return myViper.GetBool("client.fabricCA.tlsEnabled")
114+
}
115+
96116
// GetFabricClientViper returns the internal viper instance used by the
97117
// SDK to read configuration options
98118
func GetFabricClientViper() *viper.Viper {
@@ -221,25 +241,6 @@ func GetFabricCAID() string {
221241
return myViper.GetString("client.fabricCA.id")
222242
}
223243

224-
// GetFabricCAClientPath This method will read the fabric-ca configurations from the
225-
// config yaml file and return the path to a json client config file
226-
// in the format that is expected by the fabric-ca client
227-
func GetFabricCAClientPath() (string, error) {
228-
filePath := "/tmp/client-config.json"
229-
fabricCAConf := fabricCAConfig{}
230-
err := myViper.UnmarshalKey("client.fabricCA", &fabricCAConf)
231-
if err != nil {
232-
return "", err
233-
}
234-
jsonConfig, err := json.Marshal(fabricCAConf)
235-
if err != nil {
236-
return "", err
237-
}
238-
239-
err = ioutil.WriteFile(filePath, jsonConfig, 0644)
240-
return filePath, err
241-
}
242-
243244
// GetKeyStorePath ...
244245
func GetKeyStorePath() string {
245246
return myViper.GetString("client.keystore.path")

fabric-ca-client/fabricca.go

Lines changed: 31 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -21,13 +21,15 @@ package fabricca
2121

2222
import (
2323
"fmt"
24-
"os"
24+
"strings"
2525

2626
"github.com/hyperledger/fabric-ca/api"
2727
fabric_ca "github.com/hyperledger/fabric-ca/lib"
2828
"github.com/hyperledger/fabric-sdk-go/config"
2929
fabricclient "github.com/hyperledger/fabric-sdk-go/fabric-client"
3030

31+
"io/ioutil"
32+
3133
"github.com/op/go-logging"
3234
)
3335

@@ -85,18 +87,40 @@ type Attribute struct {
8587
* @param {string} clientConfigFile for fabric-ca services"
8688
*/
8789
func NewFabricCAClient() (Services, error) {
88-
configPath, err := config.GetFabricCAClientPath()
90+
91+
// Create new Fabric-ca client without configs
92+
c, err := fabric_ca.NewClient("")
8993
if err != nil {
90-
return nil, fmt.Errorf("error setting up fabric-ca configurations: %s", err.Error())
94+
return nil, fmt.Errorf("New fabricCAClient failed: %s", err)
95+
}
96+
97+
certFile := config.GetFabricCAClientCertFile()
98+
keyFile := config.GetFabricCAClientKeyFile()
99+
serverCertFiles := config.GetServerCertFiles()
100+
101+
//set server URL
102+
c.Config.URL = config.GetServerURL()
103+
//certs file list
104+
c.Config.TLS.CertFilesList = serverCertFiles
105+
//concat cert files
106+
c.Config.TLS.CertFiles = strings.Join(serverCertFiles[:], ",")
107+
//set cert file into TLS context
108+
file, err := ioutil.ReadFile(certFile)
109+
if err != nil {
110+
logger.Errorf("Error reading fabric ca client propertiy certfile: %v", err)
111+
return nil, fmt.Errorf("New fabricCAClient failed: %s", err)
91112
}
92-
//Remove temporary config file after setup
93-
defer os.Remove(configPath)
94-
// Create new Fabric-ca client with configs
95-
c, err := fabric_ca.NewClient(configPath)
113+
c.Config.TLS.Client.CertFile = string(file)
114+
//set key file into TLS context
115+
keyfile, err := ioutil.ReadFile(keyFile)
96116
if err != nil {
117+
logger.Errorf("Error reading fabric ca client property keyfile: %v", err)
97118
return nil, fmt.Errorf("New fabricCAClient failed: %s", err)
98119
}
120+
c.Config.TLS.Client.KeyFile = string(keyfile)
99121

122+
//TLS falg enabled/disabled
123+
c.Config.TLS.Enabled = config.GetFabricCATLSEnabledFlag()
100124
fabricCAClient := &services{fabricCAClient: c}
101125
logger.Infof("Constructed fabricCAClient instance: %v", fabricCAClient)
102126

fabric-ca-client/fabricca_test.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,7 @@ import (
2828
)
2929

3030
func TestEnrollWithMissingParameters(t *testing.T) {
31+
3132
fabricCAClient, err := NewFabricCAClient()
3233
if err != nil {
3334
t.Fatalf("NewFabricCAClient return error: %v", err)
@@ -49,6 +50,7 @@ func TestEnrollWithMissingParameters(t *testing.T) {
4950
}
5051

5152
func TestRegister(t *testing.T) {
53+
5254
fabricCAClient, err := NewFabricCAClient()
5355
if err != nil {
5456
t.Fatalf("NewFabricCAClient returned error: %v", err)
@@ -94,6 +96,7 @@ func TestRegister(t *testing.T) {
9496
}
9597

9698
func TestRevoke(t *testing.T) {
99+
97100
fabricCAClient, err := NewFabricCAClient()
98101
if err != nil {
99102
t.Fatalf("NewFabricCAClient returned error: %v", err)

test/fixtures/config/config_test.yaml

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -44,13 +44,14 @@ client:
4444
level: info
4545

4646
fabricCA:
47+
tlsEnabled: true
4748
id: "Org1MSP"
48-
serverURL: "http://localhost:7054"
49+
serverURL: "http://localhost:9054"
4950
certfiles :
50-
- "../test/fixtures/root.pem"
51+
- "$GOPATH/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/root.pem"
5152
client:
52-
keyfile: "../test/fixtures/tls_client-key.pem"
53-
certfile: "../test/fixtures/tls_client-cert.pem"
53+
keyfile: "$GOPATH/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/tls_client-key.pem"
54+
certfile: "$GOPATH/src/github.com/hyperledger/fabric-sdk-go/test/fixtures/tls_client-cert.pem"
5455

5556
keystore:
56-
path: "/tmp/keystore"
57+
path: "/tmp/keystore"

test/fixtures/docker-compose.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ services:
66
environment:
77
- FABRIC_CA_HOME=/etc/hyperledger/fabric-ca-server
88
ports:
9-
- "7054:7054"
9+
- "9054:7054"
1010
command: sh -c 'fabric-ca-server start --ca.certfile /etc/hyperledger/fabric-ca-server-config/peerOrg1-cert.pem --ca.keyfile /etc/hyperledger/fabric-ca-server-config/d8a5b3cac1b821f6e4b487ceaf1fd239cdcfc310894150908b90f05e9179556a_sk -b admin:adminpw' -d
1111
volumes:
1212
- ./channel/crypto-config/peerOrganizations/peerOrg1/ca/:/etc/hyperledger/fabric-ca-server-config

0 commit comments

Comments
 (0)