Skip to content

Commit 65dab12

Browse files
committed
update cfssl usage, remove some ioutil
1 parent d6774fd commit 65dab12

File tree

9 files changed

+30
-37
lines changed

9 files changed

+30
-37
lines changed

certmetrics/metrics.go

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,7 @@ package certmetrics
33

44
import (
55
"crypto/x509"
6-
"io/ioutil"
6+
"io"
77
"os"
88
"sort"
99
"strings"
@@ -34,7 +34,7 @@ func certSourceFromFile(path string) ([]CertSource, error) {
3434
if err != nil {
3535
return nil, err
3636
}
37-
pemData, err := ioutil.ReadAll(file)
37+
pemData, err := io.ReadAll(file)
3838
if err != nil {
3939
return nil, err
4040
}

client/client.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,6 @@ import (
1010
"encoding/pem"
1111
"errors"
1212
"fmt"
13-
"io/ioutil"
1413
"net"
1514
"os"
1615
"path/filepath"
@@ -71,7 +70,7 @@ func NewClientFromFile(certFile, keyFile, caFile string) (*Client, error) {
7170
return nil, err
7271
}
7372

74-
pemCerts, err := ioutil.ReadFile(caFile)
73+
pemCerts, err := os.ReadFile(caFile)
7574
if err != nil {
7675
return nil, err
7776
}
@@ -321,7 +320,7 @@ func (c *Client) ScanDir(server, dir string, LoadPubKey func([]byte) (crypto.Pub
321320
if !info.IsDir() && (isPubKey || isCert) {
322321
log.Infof("Loading %s...\n", path)
323322

324-
in, err := ioutil.ReadFile(path)
323+
in, err := os.ReadFile(path)
325324
if err != nil {
326325
return err
327326
}
@@ -358,7 +357,7 @@ func (c *Client) LoadTLSCertificate(server, certFile string) (cert tls.Certifica
358357
var certPEMBlock []byte
359358
var certDERBlock *pem.Block
360359

361-
if certPEMBlock, err = ioutil.ReadFile(certFile); err != nil {
360+
if certPEMBlock, err = os.ReadFile(certFile); err != nil {
362361
return fail(err)
363362
}
364363

client/remote_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto"
66
"crypto/x509"
77
"encoding/pem"
8-
"io/ioutil"
98
"log"
109
"net"
1110
"os"
@@ -243,7 +242,7 @@ func TestSlowServer(t *testing.T) {
243242

244243
// helper function reads a cert from a file and convert it to a signer
245244
func NewRemoteSignerByCertFile(filepath string) (crypto.Signer, error) {
246-
pemBytes, err := ioutil.ReadFile(filepath)
245+
pemBytes, err := os.ReadFile(filepath)
247246
if err != nil {
248247
return nil, err
249248
}

cmd/gokeyless/gokeyless.go

+3-4
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"crypto/tls"
66
"crypto/x509"
77
"fmt"
8-
"io/ioutil"
98
"net"
109
"os"
1110
"strconv"
@@ -366,7 +365,7 @@ func needNewCertAndKey() bool {
366365
}
367366

368367
// error is ignore because tls.LoadX509KeyPair already verify the existence of the file
369-
certBytes, _ := ioutil.ReadFile(config.CertFile)
368+
certBytes, _ := os.ReadFile(config.CertFile)
370369
// error is ignore because tls.LoadX509KeyPair already verify the file can be parsed
371370
cert, _ := helpers.ParseCertificatePEM(certBytes)
372371
// verify the leaf certificate
@@ -380,7 +379,7 @@ func needNewCertAndKey() bool {
380379

381380
// verifyCSRAndKey checks if csr and key files exist and if they match
382381
func verifyCSRAndKey() bool {
383-
csrBytes, err := ioutil.ReadFile(config.CSRFile)
382+
csrBytes, err := os.ReadFile(config.CSRFile)
384383
if err != nil {
385384
log.Errorf("cannot read csr file: %v", err)
386385
return false
@@ -403,7 +402,7 @@ func verifyCSRAndKey() bool {
403402
return false
404403
}
405404

406-
keyBytes, err := ioutil.ReadFile(config.KeyFile)
405+
keyBytes, err := os.ReadFile(config.KeyFile)
407406
if err != nil {
408407
log.Errorf("cannot read private key file: %v", err)
409408
return false

cmd/gokeyless/initialize.go

+7-9
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@ import (
55
"encoding/json"
66
"fmt"
77
"io"
8-
"io/ioutil"
98
"net/http"
109
"os"
1110

@@ -65,7 +64,7 @@ func initAPICall(token, hostname, zoneID, csr string) ([]byte, error) {
6564
}
6665
defer resp.Body.Close()
6766

68-
bodyBytes, err := ioutil.ReadAll(resp.Body)
67+
bodyBytes, err := io.ReadAll(resp.Body)
6968
if err != nil {
7069
return nil, fmt.Errorf("certificate API returned an invalid response body for HTTP %d", resp.StatusCode)
7170
}
@@ -120,12 +119,12 @@ func initializeServerCertAndKey() {
120119
log.Fatal("failed to generate csr and key: ", err)
121120
}
122121

123-
if err := ioutil.WriteFile(config.KeyFile, key, 0600); err != nil {
122+
if err := os.WriteFile(config.KeyFile, key, 0600); err != nil {
124123
log.Fatal("failed to write to key file: ", err)
125124
}
126125
log.Infof("key is generated and saved to %s", config.KeyFile)
127126

128-
if err := ioutil.WriteFile(config.CSRFile, csr, 0600); err != nil {
127+
if err := os.WriteFile(config.CSRFile, csr, 0600); err != nil {
129128
log.Fatal("failed to write to csr file:", err)
130129
}
131130
log.Infof("csr is generated and saved to %s", config.CSRFile)
@@ -141,12 +140,11 @@ func initializeServerCertAndKey() {
141140
log.Fatal("couldn't remove old certificate file: ", err)
142141
}
143142

144-
if err := ioutil.WriteFile(config.CertFile, cert, 0644); err != nil {
143+
if err := os.WriteFile(config.CertFile, cert, 0644); err != nil {
145144
log.Fatal("couldn't write to certificate file: ", err)
146145
}
147146
log.Infof("certificate saved to %s", config.CertFile)
148147

149-
return
150148
}
151149

152150
// generateCSR generates a private key and a CSR for the given host. The
@@ -155,7 +153,7 @@ func generateCSR(host string) ([]byte, []byte, error) {
155153
csr, key, err := csr.ParseRequest(&csr.CertificateRequest{
156154
CN: "Keyless Server Authentication Certificate",
157155
Hosts: []string{host},
158-
KeyRequest: &csr.BasicKeyRequest{
156+
KeyRequest: &csr.KeyRequest{
159157
A: "ecdsa",
160158
S: 384,
161159
},
@@ -173,12 +171,12 @@ func manualActivation() {
173171
log.Fatal("failed to generate csr and key: ", err)
174172
}
175173

176-
if err := ioutil.WriteFile(config.KeyFile, key, 0600); err != nil {
174+
if err := os.WriteFile(config.KeyFile, key, 0600); err != nil {
177175
log.Fatal("failed to write to key file:", err)
178176
}
179177
log.Infof("key is generated and saved to %s", config.KeyFile)
180178

181-
if err := ioutil.WriteFile(config.CSRFile, csr, 0600); err != nil {
179+
if err := os.WriteFile(config.CSRFile, csr, 0600); err != nil {
182180
log.Fatal("failed to write to csr file:", err)
183181
}
184182
log.Infof("csr is generated and saved to %s", config.CSRFile)

server/keystore.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,6 @@ import (
44
"context"
55
"crypto"
66
"fmt"
7-
"io/ioutil"
87
"os"
98
"path/filepath"
109
"regexp"
@@ -78,7 +77,7 @@ func (keys *DefaultKeystore) AddFromDir(dir string, LoadKey func([]byte) (crypto
7877
func (keys *DefaultKeystore) AddFromFile(path string, LoadKey func([]byte) (crypto.Signer, error)) error {
7978
log.Infof("loading %s...", path)
8079

81-
in, err := ioutil.ReadFile(path)
80+
in, err := os.ReadFile(path)
8281
if err != nil {
8382
return err
8483
}

server/server.go

+8-8
Original file line numberDiff line numberDiff line change
@@ -12,9 +12,9 @@ import (
1212
"errors"
1313
"fmt"
1414
"io"
15-
"io/ioutil"
1615
"net"
1716
"net/rpc"
17+
"os"
1818
"sync"
1919
"time"
2020

@@ -85,7 +85,7 @@ func NewServerFromFile(config *ServeConfig, certFile, keyFile, caFile string) (*
8585
return nil, err
8686
}
8787

88-
pemCerts, err := ioutil.ReadFile(caFile)
88+
pemCerts, err := os.ReadFile(caFile)
8989
if err != nil {
9090
return nil, err
9191
}
@@ -706,12 +706,12 @@ const (
706706

707707
// DefaultServeConfig constructs a default ServeConfig with the following
708708
// values:
709-
// - The number of ECDSA workers is max(2, runtime.NumCPU())
710-
// - The number of RSA workers is max(2, runtime.NumCPU())
711-
// - The number of other workers is 2
712-
// - The TCP connection timeout is 30 seconds
713-
// - The Unix connection timeout is 1 hour
714-
// - All connections have full power
709+
// - The number of ECDSA workers is max(2, runtime.NumCPU())
710+
// - The number of RSA workers is max(2, runtime.NumCPU())
711+
// - The number of other workers is 2
712+
// - The TCP connection timeout is 30 seconds
713+
// - The Unix connection timeout is 1 hour
714+
// - All connections have full power
715715
func DefaultServeConfig() *ServeConfig {
716716
return &ServeConfig{
717717
tcpTimeout: defaultTCPTimeout,

tests/common_test.go

+1-2
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,6 @@ import (
99
"flag"
1010
"fmt"
1111
"io"
12-
"io/ioutil"
1312
"net"
1413
"os"
1514
"strconv"
@@ -131,7 +130,7 @@ func (d DummyRPC) PipeRead(in int, out *[]byte) (err error) {
131130

132131
// helper function reads a pub key from a file and convert it to a signer
133132
func (s *IntegrationTestSuite) NewRemoteSignerByPubKeyFile(filepath string) (crypto.Signer, error) {
134-
pemBytes, err := ioutil.ReadFile(filepath)
133+
pemBytes, err := os.ReadFile(filepath)
135134
if err != nil {
136135
return nil, err
137136
}

tests/proxy_test.go

+4-4
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@ import (
77
"encoding/pem"
88
"errors"
99
"io"
10-
"io/ioutil"
10+
"os"
1111
"testing"
1212
"time"
1313

@@ -41,7 +41,7 @@ func clientFunc(conn *tls.Conn) error {
4141
return err
4242
}
4343

44-
output, err := ioutil.ReadAll(conn)
44+
output, err := io.ReadAll(conn)
4545
if err != nil {
4646
return err
4747
}
@@ -83,7 +83,7 @@ func (s *IntegrationTestSuite) TestTLSProxy() {
8383

8484
keys := server.NewDefaultKeystore()
8585
s.server.SetKeystore(keys)
86-
pemKey, err := ioutil.ReadFile(tlsKey)
86+
pemKey, err := os.ReadFile(tlsKey)
8787
require.NoError(err)
8888
p, _ := pem.Decode(pemKey)
8989
rsaKey, err := x509.ParseECPrivateKey(p.Bytes)
@@ -97,7 +97,7 @@ func (s *IntegrationTestSuite) TestTLSProxy() {
9797
RootCAs: x509.NewCertPool(),
9898
}
9999

100-
caBytes, err := ioutil.ReadFile(caCert)
100+
caBytes, err := os.ReadFile(caCert)
101101
require.NoError(err)
102102
clientConfig.RootCAs.AppendCertsFromPEM(caBytes)
103103

0 commit comments

Comments
 (0)