-
Notifications
You must be signed in to change notification settings - Fork 720
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
*: support ssl/tls #868
Merged
Merged
*: support ssl/tls #868
Changes from 9 commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
35a5326
support ssl/tls
Connor1996 5388ea3
fix ci
Connor1996 9d24250
fix test
Connor1996 a9f3c1f
use same flag name as etcd
Connor1996 c1ae6a0
client auth always enabled when use tls
Connor1996 9a1dcc3
remove peer auto tls
Connor1996 f9f7091
SecurityOption struct
Connor1996 2f89728
fix ci
Connor1996 ae54098
Merge branch 'master' into support-ssl/tls
Connor1996 942da15
address comment
Connor1996 d65fc49
Merge branch 'master' into support-ssl/tls
Connor1996 7cc7bfb
pd server don't need client cert
Connor1996 261e6c5
support abort when wait for etcd to be ready
Connor1996 871057b
Merge branch 'support-ssl/tls' of https://github.com/Connor1996/pd in…
Connor1996 0475302
fix test
Connor1996 66ad5da
use security section
Connor1996 ca13801
fix typo
Connor1996 File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,6 +15,8 @@ package command | |
|
||
import ( | ||
"bytes" | ||
"crypto/tls" | ||
"crypto/x509" | ||
"encoding/json" | ||
"fmt" | ||
"io" | ||
|
@@ -23,20 +25,52 @@ import ( | |
"net/url" | ||
"os" | ||
|
||
log "github.com/Sirupsen/logrus" | ||
"github.com/juju/errors" | ||
"github.com/pingcap/pd/pd-client" | ||
"github.com/spf13/cobra" | ||
) | ||
|
||
var ( | ||
pdClient pd.Client | ||
dailClient = &http.Client{} | ||
|
||
pingPrefix = "pd/ping" | ||
errInvalidAddr = errors.New("Invalid pd address, Cannot get connect to it") | ||
) | ||
|
||
// InitHTTPSClient creates https client with ca file | ||
func InitHTTPSClient(tlsCAPath, tlsCertPath, tlsKeyPath string) error { | ||
certificates := []tls.Certificate{} | ||
if len(tlsCertPath) != 0 && len(tlsKeyPath) != 0 { | ||
// Load the client certificates from disk | ||
certificate, err := tls.LoadX509KeyPair(tlsCertPath, tlsKeyPath) | ||
if err != nil { | ||
return errors.Errorf("could not load client key pair: %s", err) | ||
} | ||
certificates = append(certificates, certificate) | ||
} | ||
|
||
// Create a certificate pool from the certificate authority | ||
certPool := x509.NewCertPool() | ||
ca, err := ioutil.ReadFile(tlsCAPath) | ||
if err != nil { | ||
return errors.Errorf("could not read ca certificate: %s", err) | ||
} | ||
|
||
// Append the certificates from the CA | ||
if ok := certPool.AppendCertsFromPEM(ca); !ok { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
|
||
return errors.New("failed to append ca certs") | ||
} | ||
|
||
tr := &http.Transport{ | ||
TLSClientConfig: &tls.Config{ | ||
Certificates: certificates, | ||
RootCAs: certPool, | ||
}, | ||
} | ||
dailClient = &http.Client{Transport: tr} | ||
|
||
return nil | ||
} | ||
|
||
func getRequest(cmd *cobra.Command, prefix string, method string, bodyType string, body io.Reader) (*http.Request, error) { | ||
if method == "" { | ||
method = http.MethodGet | ||
|
@@ -82,34 +116,6 @@ func genResponseError(r *http.Response) error { | |
return errors.Errorf("[%d] %s", r.StatusCode, res) | ||
} | ||
|
||
// InitPDClient initialize pd client from cmd | ||
func InitPDClient(cmd *cobra.Command) error { | ||
addr, err := cmd.Flags().GetString("pd") | ||
if err != nil { | ||
return err | ||
} | ||
log.SetOutput(ioutil.Discard) | ||
if pdClient != nil { | ||
return nil | ||
} | ||
err = validPDAddr(addr) | ||
if err != nil { | ||
return err | ||
} | ||
pdClient, err = pd.NewClient([]string{addr}) | ||
if err != nil { | ||
return err | ||
} | ||
return nil | ||
} | ||
|
||
func getClient() (pd.Client, error) { | ||
if pdClient == nil { | ||
return nil, errors.New("Must initialized pdClient firstly") | ||
} | ||
return pdClient, nil | ||
} | ||
|
||
func getAddressFromCmd(cmd *cobra.Command, prefix string) string { | ||
p, err := cmd.Flags().GetString("pd") | ||
if err != nil { | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
if !certPool.AppendCertsFromPEM(ca) {