Skip to content

Commit

Permalink
chore: optimize hijack ca format (#3418)
Browse files Browse the repository at this point in the history
* chore: optimize hijack ca format

Signed-off-by: Jim Ma <[email protected]>

* chore: fix cert leaf

Signed-off-by: Jim Ma <[email protected]>

* fix: unit test

Signed-off-by: Jim Ma <[email protected]>

---------

Signed-off-by: Jim Ma <[email protected]>
  • Loading branch information
jim3ma committed Aug 6, 2024
1 parent 89e06a8 commit 9698903
Show file tree
Hide file tree
Showing 5 changed files with 15 additions and 14 deletions.
4 changes: 2 additions & 2 deletions client/config/peerhost.go
Original file line number Diff line number Diff line change
Expand Up @@ -920,8 +920,8 @@ func (r *Regexp) MarshalYAML() (any, error) {

// HijackConfig represents how dfdaemon hijacks http requests.
type HijackConfig struct {
Cert string `yaml:"cert" mapstructure:"cert"`
Key string `yaml:"key" mapstructure:"key"`
Cert types.PEMContent `yaml:"cert" mapstructure:"cert"`
Key types.PEMContent `yaml:"key" mapstructure:"key"`
Hosts []*HijackHost `yaml:"hosts" mapstructure:"hosts"`
SNI []*TCPListenOption `yaml:"sni" mapstructure:"sni"`
}
Expand Down
4 changes: 2 additions & 2 deletions client/config/peerhost_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -470,8 +470,8 @@ func TestPeerHostOption_Load(t *testing.T) {
},
},
HijackHTTPS: &HijackConfig{
Cert: "./testdata/certs/sca.crt",
Key: "./testdata/certs/sca.key",
Cert: types.PEMContent(_cert),
Key: types.PEMContent(_key),
Hosts: []*HijackHost{
{
Regx: hijackExp,
Expand Down
2 changes: 1 addition & 1 deletion client/config/testdata/certs/sca.crt
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,4 @@ A5l000dtHekhk+DO2tjQgEKg5+EYMYoki5mEkSbyHkMMY8D6w5A130fpw10ZeN1z
B/v/1PiVkZfu1kbnTZICQDsb4xI/2Sw2x0qKXp1oYzIDt8fZATNJgWhzv47xLLXF
XQM7Yj0HQ3txAi6qOMDw1sYf/TEc1k4VC9J//QJb5/kNnWcAheLPCm3D1+CnAxcD
vL928p4GmUIGbzxm3/WbWfLosSwxq5y4P5bbEd3niM4=
-----END CERTIFICATE-----
-----END CERTIFICATE-----
2 changes: 1 addition & 1 deletion client/config/testdata/certs/sca.key
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,4 @@ fbR5XmFsuzmdL0zRIt6+mtDjfqHHYA2avzwvRaBWVprzS8/ISTqJSEs/NWSYuAsP
tjPw2QKBgQCB+sS2lio/sTAQzsYTe/GNmxsL1lKO+yRsTPRRjzcm3ZdOsPgkFDx/
ZCL9Lsp7TqOLOghLGdYj9a45GrXwmEeJo5P9c1y+G9PSzFDMBUyseWmDvrcvYwWo
JMfrfs6pHtZ828AbnT2kfnFv6zok2ns6vE2gme/a9Z/RCjVXyJwF5w==
-----END RSA PRIVATE KEY-----
-----END RSA PRIVATE KEY-----
17 changes: 9 additions & 8 deletions client/daemon/proxy/proxy_manager.go
Original file line number Diff line number Diff line change
Expand Up @@ -98,20 +98,20 @@ func NewProxyManager(peerHost *schedulerv1.PeerHost, peerTaskManager peer.TaskMa
if r.Direct {
method = "directly"
}
scheme := ""
prompt := ""
if r.UseHTTPS {
scheme = "and force https"
prompt = " and force https"
}
logger.Infof("[%d] proxy %s %s %s", i+1, r.Regx, method, scheme)
logger.Infof("[%d] proxy %s %s%s", i+1, r.Regx, method, prompt)
}
}

if hijackHTTPS != nil {
options = append(options, WithHTTPSHosts(hijackHTTPS.Hosts...))
if hijackHTTPS.Cert != "" && hijackHTTPS.Key != "" {
cert, err := certFromFile(hijackHTTPS.Cert, hijackHTTPS.Key)
cert, err := certFromFile(string(hijackHTTPS.Cert), string(hijackHTTPS.Key))
if err != nil {
return nil, fmt.Errorf("cert from file: %w", err)
return nil, fmt.Errorf("load cert error: %w", err)
}
if cert.Leaf != nil && cert.Leaf.IsCA {
logger.Debugf("hijack https request with CA <%s>", cert.Leaf.Subject.CommonName)
Expand Down Expand Up @@ -174,13 +174,14 @@ func (pm *proxyManager) Watch(opt *config.ProxyOption) {
}
}

func certFromFile(certFile string, keyFile string) (*tls.Certificate, error) {
func certFromFile(certPEM string, keyPEM string) (*tls.Certificate, error) {
// cert.Certificate is a chain of one or more certificates, leaf first.
cert, err := tls.LoadX509KeyPair(certFile, keyFile)
cert, err := tls.X509KeyPair([]byte(certPEM), []byte(keyPEM))
if err != nil {
return nil, fmt.Errorf("load cert: %w", err)
}
logger.Infof("use self-signed certificate (%s, %s) for https hijacking", certFile, keyFile)

logger.Infof("use self-signed certificate for https hijacking")

// leaf is CA cert or server cert
leaf, err := x509.ParseCertificate(cert.Certificate[0])
Expand Down

0 comments on commit 9698903

Please sign in to comment.