@@ -5,13 +5,17 @@ import (
55 "crypto/x509"
66 "fmt"
77 "net"
8+ "os"
89 "time"
10+
11+ "golang.org/x/term"
912)
1013
1114const (
1215 TLSTimeout = 3 * time .Second
1316 CertExpWarnDays = 40
1417 privateKeyPwEnvVar = "CERTINFO_PKEY_PW"
18+ emptyString = ""
1519)
1620
1721type CertinfoConfig struct {
@@ -32,15 +36,34 @@ type CertinfoConfig struct {
3236 TLSInsecure bool
3337}
3438
39+ type (
40+ Reader interface {
41+ ReadFile (name string ) ([]byte , error )
42+ ReadPassword (fd int ) ([]byte , error )
43+ }
44+
45+ InputReader struct {}
46+ )
47+
3548var (
36- // TODO: remove
37- // certsBundle []*x509.Certificate
38- // privKey any
39- // tlsEndpoint string
4049 TlsServerName string
4150 TlsInsecure bool
51+ inputReader InputReader
4252)
4353
54+ func (InputReader ) ReadFile (name string ) ([]byte , error ) {
55+ file , err := os .ReadFile (name )
56+ if err != nil {
57+ return nil , err
58+ }
59+
60+ return file , nil
61+ }
62+
63+ func (InputReader ) ReadPassword (fd int ) ([]byte , error ) {
64+ return term .ReadPassword (fd )
65+ }
66+
4467func NewCertinfoConfig () (* CertinfoConfig , error ) {
4568 defaultCertPool , err := x509 .SystemCertPool ()
4669 if err != nil {
@@ -54,9 +77,12 @@ func NewCertinfoConfig() (*CertinfoConfig, error) {
5477 return & c , nil
5578}
5679
57- func (c * CertinfoConfig ) SetCaPoolFromFile (filePath string ) error {
58- if filePath != "" {
59- caCertsPool , err := GetRootCertsFromFile (filePath )
80+ func (c * CertinfoConfig ) SetCaPoolFromFile (filePath string , fileReader Reader ) error {
81+ if filePath != emptyString {
82+ caCertsPool , err := GetRootCertsFromFile (
83+ filePath ,
84+ fileReader ,
85+ )
6086 if err != nil {
6187 return err
6288 }
@@ -68,9 +94,9 @@ func (c *CertinfoConfig) SetCaPoolFromFile(filePath string) error {
6894 return nil
6995}
7096
71- func (c * CertinfoConfig ) SetCertsFromFile (filePath string ) error {
72- if filePath != "" {
73- certs , err := GetCertsFromBundle (filePath )
97+ func (c * CertinfoConfig ) SetCertsFromFile (filePath string , fileReader Reader ) error {
98+ if filePath != emptyString {
99+ certs , err := GetCertsFromBundle (filePath , fileReader )
74100 if err != nil {
75101 return err
76102 }
@@ -82,9 +108,13 @@ func (c *CertinfoConfig) SetCertsFromFile(filePath string) error {
82108 return nil
83109}
84110
85- func (c * CertinfoConfig ) SetPrivateKeyFromFile (filePath string ) error {
86- if filePath != "" {
87- keyFromFile , err := GetKeyFromFile (filePath )
111+ func (c * CertinfoConfig ) SetPrivateKeyFromFile (filePath string , fileReader Reader ) error {
112+ if filePath != emptyString {
113+ keyFromFile , err := GetKeyFromFile (
114+ filePath ,
115+ privateKeyPwEnvVar ,
116+ fileReader ,
117+ )
88118 if err != nil {
89119 return err
90120 }
0 commit comments