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

Commit 56a7adb

Browse files
author
Baha Shaaban
committed
[FAB-7516] Cert/Key embed & file path combo
Change-Id: I59c3d22cd01034fc7a79f70138dc81401f1820a4 Signed-off-by: Baha Shaaban <baha.shaaban@securekey.com>
1 parent 24f9ecc commit 56a7adb

File tree

3 files changed

+125
-13
lines changed

3 files changed

+125
-13
lines changed

internal/github.com/hyperledger/fabric-ca/util/csp.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,11 @@ func ImportBCCSPKeyFromPEM(keyFile string, myCSP apicryptosuite.CryptoSuite, tem
128128
}
129129

130130
key, err := ImportBCCSPKeyFromPEMBytes(keyBuff, myCSP, temporary)
131-
131+
132132
if err != nil {
133133
return nil, errors.WithMessage(err, fmt.Sprintf("Failed parsing private key from %s", keyFile))
134134
}
135-
135+
136136
return key, nil
137137
}
138138

@@ -159,6 +159,7 @@ func ImportBCCSPKeyFromPEMBytes(keyBuff []byte, myCSP apicryptosuite.CryptoSuite
159159
return nil, errors.Errorf("Failed to import key: invalid secret key type")
160160
}
161161
}
162+
162163
// LoadX509KeyPair reads and parses a public/private key pair from a pair
163164
// of files. The files must contain PEM encoded data. The certificate file
164165
// may contain intermediate certificates following the leaf certificate to

pkg/config/config.go

Lines changed: 51 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -826,25 +826,66 @@ func (c *Config) TLSClientCerts() ([]tls.Certificate, error) {
826826

827827
clientConfig := config.Client
828828
var clientCerts tls.Certificate
829-
829+
var cb, kb []byte
830830
if clientConfig.TLSCerts.Client.CertPem != "" {
831-
clientCerts, err = tls.X509KeyPair([]byte(clientConfig.TLSCerts.Client.CertPem), []byte(clientConfig.TLSCerts.Client.KeyPem))
832-
if err != nil {
833-
return nil, errors.Errorf("Error loading cert/key pair as TLS client credentials: %v", err)
831+
cb = []byte(clientConfig.TLSCerts.Client.CertPem)
832+
if clientConfig.TLSCerts.Client.KeyPem != "" {
833+
kb = []byte(clientConfig.TLSCerts.Client.KeyPem)
834+
} else if clientConfig.TLSCerts.Client.Keyfile != "" {
835+
kb, err = loadByteKeyOrCertFromFile(&clientConfig, true)
836+
if err != nil {
837+
return nil, err
838+
}
839+
} else {
840+
return nil, errors.Errorf("Missing key for cert/key pair TLS client credentials. Ensure either the key file path or the key content is embedded in the client config.")
834841
}
835-
836842
} else if clientConfig.TLSCerts.Client.Certfile != "" {
837-
clientConfig.TLSCerts.Client.Keyfile = substPathVars(clientConfig.TLSCerts.Client.Keyfile)
838-
clientConfig.TLSCerts.Client.Certfile = substPathVars(clientConfig.TLSCerts.Client.Certfile)
839-
clientCerts, err = tls.LoadX509KeyPair(clientConfig.TLSCerts.Client.Certfile, clientConfig.TLSCerts.Client.Keyfile)
840-
if err != nil {
841-
return nil, errors.Errorf("Error loading cert/key pair as TLS client credentials: %v", err)
843+
cb, err = loadByteKeyOrCertFromFile(&clientConfig, false)
844+
if clientConfig.TLSCerts.Client.KeyPem != "" {
845+
kb = []byte(clientConfig.TLSCerts.Client.KeyPem)
846+
if err != nil {
847+
return nil, err
848+
}
849+
} else if clientConfig.TLSCerts.Client.Keyfile != "" {
850+
kb, err = loadByteKeyOrCertFromFile(&clientConfig, true)
851+
if err != nil {
852+
return nil, err
853+
}
854+
} else {
855+
return nil, errors.Errorf("Missing key for cert/key pair TLS client credentials. Ensure either the key file path or the key content is embedded in the client config.")
842856
}
857+
} else {
858+
// if no cert found in the config, return empty cert chain
859+
return []tls.Certificate{clientCerts}, nil
860+
}
861+
862+
// load the key/cert pair from []byte
863+
clientCerts, err = tls.X509KeyPair(cb, kb)
864+
if err != nil {
865+
return nil, errors.Errorf("Error loading cert/key pair as TLS client credentials: %v", err)
843866
}
844867

845868
return []tls.Certificate{clientCerts}, nil
846869
}
847870

871+
func loadByteKeyOrCertFromFile(c *apiconfig.ClientConfig, isKey bool) ([]byte, error) {
872+
var path string
873+
a := "key"
874+
if isKey {
875+
path = substPathVars(c.TLSCerts.Client.Keyfile)
876+
c.TLSCerts.Client.Keyfile = path
877+
} else {
878+
a = "cert"
879+
path = substPathVars(c.TLSCerts.Client.Certfile)
880+
c.TLSCerts.Client.Certfile = path
881+
}
882+
bts, err := ioutil.ReadFile(path)
883+
if err != nil {
884+
return nil, errors.Errorf("Error loading %s file from '%s' err: %v", a, path, err)
885+
}
886+
return bts, nil
887+
}
888+
848889
// loadCAKey
849890
func loadCAKey(rawData []byte) (*x509.Certificate, error) {
850891
block, _ := pem.Decode(rawData)

pkg/config/config_test.go

Lines changed: 71 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -983,6 +983,76 @@ YZjcDi7YEOZ3Fs1hxKmIxR+TTR2vf9I=
983983
}
984984
}
985985

986+
func TestTLSClientCertFromPemAndKeyFromFile(t *testing.T) {
987+
configImpl.networkConfig.Client.TLSCerts.Client.Certfile = ""
988+
configImpl.networkConfig.Client.TLSCerts.Client.Keyfile = "../../test/fixtures/config/mutual_tls/client_sdk_go-key.pem"
989+
990+
configImpl.networkConfig.Client.TLSCerts.Client.CertPem = `-----BEGIN CERTIFICATE-----
991+
MIIC5TCCAkagAwIBAgIUMYhiY5MS3jEmQ7Fz4X/e1Dx33J0wCgYIKoZIzj0EAwQw
992+
gYwxCzAJBgNVBAYTAkNBMRAwDgYDVQQIEwdPbnRhcmlvMRAwDgYDVQQHEwdUb3Jv
993+
bnRvMREwDwYDVQQKEwhsaW51eGN0bDEMMAoGA1UECxMDTGFiMTgwNgYDVQQDEy9s
994+
aW51eGN0bCBFQ0MgUm9vdCBDZXJ0aWZpY2F0aW9uIEF1dGhvcml0eSAoTGFiKTAe
995+
Fw0xNzEyMDEyMTEzMDBaFw0xODEyMDEyMTEzMDBaMGMxCzAJBgNVBAYTAkNBMRAw
996+
DgYDVQQIEwdPbnRhcmlvMRAwDgYDVQQHEwdUb3JvbnRvMREwDwYDVQQKEwhsaW51
997+
eGN0bDEMMAoGA1UECxMDTGFiMQ8wDQYDVQQDDAZzZGtfZ28wdjAQBgcqhkjOPQIB
998+
BgUrgQQAIgNiAAT6I1CGNrkchIAEmeJGo53XhDsoJwRiohBv2PotEEGuO6rMyaOu
999+
pulj2VOj+YtgWw4ZtU49g4Nv6rq1QlKwRYyMwwRJSAZHIUMhYZjcDi7YEOZ3Fs1h
1000+
xKmIxR+TTR2vf9KjgZAwgY0wDgYDVR0PAQH/BAQDAgWgMBMGA1UdJQQMMAoGCCsG
1001+
AQUFBwMCMAwGA1UdEwEB/wQCMAAwHQYDVR0OBBYEFDwS3xhpAWs81OVWvZt+iUNL
1002+
z26DMB8GA1UdIwQYMBaAFLRasbknomawJKuQGiyKs/RzTCujMBgGA1UdEQQRMA+C
1003+
DWZhYnJpY19zZGtfZ28wCgYIKoZIzj0EAwQDgYwAMIGIAkIAk1MxMogtMtNO0rM8
1004+
gw2rrxqbW67ulwmMQzp6EJbm/28T2pIoYWWyIwpzrquypI7BOuf8is5b7Jcgn9oz
1005+
7sdMTggCQgF7/8ZFl+wikAAPbciIL1I+LyCXKwXosdFL6KMT6/myYjsGNeeDeMbg
1006+
3YkZ9DhdH1tN4U/h+YulG/CkKOtUATtQxg==
1007+
-----END CERTIFICATE-----`
1008+
1009+
configImpl.networkConfig.Client.TLSCerts.Client.KeyPem = ""
1010+
1011+
certs, err := configImpl.TLSClientCerts()
1012+
if err != nil {
1013+
t.Fatalf("Expected no errors but got error instead: %s", err)
1014+
}
1015+
1016+
if len(certs) != 1 {
1017+
t.Fatalf("Expected only one tls cert struct")
1018+
}
1019+
1020+
emptyCert := tls.Certificate{}
1021+
1022+
if reflect.DeepEqual(certs[0], emptyCert) {
1023+
t.Fatalf("Actual cert is empty")
1024+
}
1025+
}
1026+
1027+
func TestTLSClientCertFromFileAndKeyFromPem(t *testing.T) {
1028+
configImpl.networkConfig.Client.TLSCerts.Client.Certfile = "../../test/fixtures/config/mutual_tls/client_sdk_go.pem"
1029+
configImpl.networkConfig.Client.TLSCerts.Client.Keyfile = ""
1030+
1031+
configImpl.networkConfig.Client.TLSCerts.Client.CertPem = ""
1032+
1033+
configImpl.networkConfig.Client.TLSCerts.Client.KeyPem = `-----BEGIN EC PRIVATE KEY-----
1034+
MIGkAgEBBDByldj7VTpqTQESGgJpR9PFW9b6YTTde2WN6/IiBo2nW+CIDmwQgmAl
1035+
c/EOc9wmgu+gBwYFK4EEACKhZANiAAT6I1CGNrkchIAEmeJGo53XhDsoJwRiohBv
1036+
2PotEEGuO6rMyaOupulj2VOj+YtgWw4ZtU49g4Nv6rq1QlKwRYyMwwRJSAZHIUMh
1037+
YZjcDi7YEOZ3Fs1hxKmIxR+TTR2vf9I=
1038+
-----END EC PRIVATE KEY-----`
1039+
1040+
certs, err := configImpl.TLSClientCerts()
1041+
if err != nil {
1042+
t.Fatalf("Expected no errors but got error instead: %s", err)
1043+
}
1044+
1045+
if len(certs) != 1 {
1046+
t.Fatalf("Expected only one tls cert struct")
1047+
}
1048+
1049+
emptyCert := tls.Certificate{}
1050+
1051+
if reflect.DeepEqual(certs[0], emptyCert) {
1052+
t.Fatalf("Actual cert is empty")
1053+
}
1054+
}
1055+
9861056
func TestTLSClientCertsPemBeforeFiles(t *testing.T) {
9871057
// files have incorrect paths, but pems are loaded first
9881058
configImpl.networkConfig.Client.TLSCerts.Client.Certfile = "/test/fixtures/config/mutual_tls/client_sdk_go.pem"
@@ -1042,7 +1112,7 @@ func TestTLSClientCertsNoCerts(t *testing.T) {
10421112
}
10431113

10441114
if len(certs) != 1 {
1045-
t.Fatalf("Expected only emppty tls cert struct")
1115+
t.Fatalf("Expected only empty tls cert struct")
10461116
}
10471117

10481118
emptyCert := tls.Certificate{}

0 commit comments

Comments
 (0)