Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chore: optimize hijack ca format #3418

Merged
merged 3 commits into from
Aug 6, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading