Skip to content

Commit 78373e9

Browse files
committed
[FAB-7674] use buffers instead of temporary files
GenerateCertificatesOrPanic was using temporary files when a simple buffer is sufficient. This resulted in untracked files getting left behind when tests failed. Change-Id: I2bdcafde387509a00563315ddd11eccbb7e95584 Signed-off-by: Matthew Sykes <sykesmat@us.ibm.com>
1 parent 3178dbf commit 78373e9

File tree

1 file changed

+3
-27
lines changed

1 file changed

+3
-27
lines changed

gossip/comm/crypto.go

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -13,39 +13,21 @@ import (
1313
"crypto/tls"
1414
"crypto/x509"
1515
"encoding/pem"
16-
"fmt"
1716
"math/big"
18-
"os"
1917

2018
"github.com/hyperledger/fabric/common/util"
21-
gutil "github.com/hyperledger/fabric/gossip/util"
2219
"golang.org/x/net/context"
2320
"google.golang.org/grpc/credentials"
2421
"google.golang.org/grpc/peer"
2522
)
2623

27-
func writeFile(filename string, keyType string, data []byte) error {
28-
f, err := os.Create(filename)
29-
if err != nil {
30-
return err
31-
}
32-
defer f.Close()
33-
return pem.Encode(f, &pem.Block{Type: keyType, Bytes: data})
34-
}
35-
3624
// GenerateCertificatesOrPanic generates a a random pair of public and private keys
3725
// and return TLS certificate
3826
func GenerateCertificatesOrPanic() tls.Certificate {
39-
privKeyFile := fmt.Sprintf("key.%d.priv", gutil.RandomUInt64())
40-
certKeyFile := fmt.Sprintf("cert.%d.pub", gutil.RandomUInt64())
41-
42-
defer os.Remove(privKeyFile)
43-
defer os.Remove(certKeyFile)
4427
privateKey, err := ecdsa.GenerateKey(elliptic.P256(), rand.Reader)
4528
if err != nil {
4629
panic(err)
4730
}
48-
4931
sn, err := rand.Int(rand.Reader, new(big.Int).Lsh(big.NewInt(1), 128))
5032
if err != nil {
5133
panic(err)
@@ -59,19 +41,13 @@ func GenerateCertificatesOrPanic() tls.Certificate {
5941
if err != nil {
6042
panic(err)
6143
}
62-
err = writeFile(certKeyFile, "CERTIFICATE", rawBytes)
63-
if err != nil {
64-
panic(err)
65-
}
6644
privBytes, err := x509.MarshalECPrivateKey(privateKey)
6745
if err != nil {
6846
panic(err)
6947
}
70-
err = writeFile(privKeyFile, "EC PRIVATE KEY", privBytes)
71-
if err != nil {
72-
panic(err)
73-
}
74-
cert, err := tls.LoadX509KeyPair(certKeyFile, privKeyFile)
48+
encodedCert := pem.EncodeToMemory(&pem.Block{Type: "CERTIFICATE", Bytes: rawBytes})
49+
encodedKey := pem.EncodeToMemory(&pem.Block{Type: "EC PRIVATE KEY", Bytes: privBytes})
50+
cert, err := tls.X509KeyPair(encodedCert, encodedKey)
7551
if err != nil {
7652
panic(err)
7753
}

0 commit comments

Comments
 (0)