@@ -3,6 +3,7 @@ package gateway
3
3
import (
4
4
"context"
5
5
"crypto/tls"
6
+ "crypto/x509"
6
7
"errors"
7
8
"fmt"
8
9
"net"
@@ -37,8 +38,10 @@ type GatewayRelayConfig struct {
37
38
RelayMaxPort uint16 `yaml:"relay_max_port"`
38
39
TlsCertPath string `yaml:"tls_cert_path"`
39
40
TlsPrivateKeyPath string `yaml:"tls_private_key_path"`
41
+ TlsCaPath string `yaml:"tls_ca_path"`
40
42
41
43
tls tls.Certificate
44
+ tlsCa string
42
45
isTlsEnabled bool
43
46
}
44
47
@@ -79,19 +82,19 @@ func NewGatewayRelay(configFilePath string) (*GatewayRelay, error) {
79
82
return nil , errMissingTlsCert
80
83
}
81
84
82
- tlsCertFile , err := os . ReadFile (cfg .TlsCertPath )
85
+ cert , err := tls . LoadX509KeyPair (cfg .TlsCertPath , cfg . TlsPrivateKeyPath )
83
86
if err != nil {
84
- return nil , err
85
- }
86
- tlsPrivateKeyFile , err := os .ReadFile (cfg .TlsPrivateKeyPath )
87
- if err != nil {
88
- return nil , err
87
+ return nil , fmt .Errorf ("Failed to read load server tls key pair: %w" , err )
89
88
}
90
89
91
- cert , err := tls .LoadX509KeyPair (string (tlsCertFile ), string (tlsPrivateKeyFile ))
92
- if err != nil {
93
- return nil , err
90
+ if cfg .TlsCaPath != "" {
91
+ ca , err := os .ReadFile (cfg .TlsCaPath )
92
+ if err != nil {
93
+ return nil , fmt .Errorf ("Failed to read tls ca: %w" , err )
94
+ }
95
+ cfg .tlsCa = string (ca )
94
96
}
97
+
95
98
cfg .tls = cert
96
99
cfg .isTlsEnabled = true
97
100
}
@@ -140,8 +143,12 @@ func (g *GatewayRelay) Run() error {
140
143
}
141
144
142
145
if g .Config .isTlsEnabled {
146
+ caCertPool := x509 .NewCertPool ()
147
+ caCertPool .AppendCertsFromPEM ([]byte (g .Config .tlsCa ))
148
+
143
149
listenerConfigs [i ].Listener = tls .NewListener (conn , & tls.Config {
144
150
Certificates : []tls.Certificate {g .Config .tls },
151
+ ClientCAs : caCertPool ,
145
152
})
146
153
} else {
147
154
listenerConfigs [i ].Listener = conn
0 commit comments