-
Notifications
You must be signed in to change notification settings - Fork 727
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
client: support pass cert bytes in SecurityOption (#4203)
* client: support pass cert bytes in SecurityOption Signed-off-by: ehco1996 <zh19960202@gmail.com> * add more test Signed-off-by: ehco1996 <zh19960202@gmail.com> * fix lint Signed-off-by: ehco1996 <zh19960202@gmail.com> * address comment Signed-off-by: ehco1996 <zh19960202@gmail.com> * address comment Signed-off-by: ehco1996 <zh19960202@gmail.com> Co-authored-by: Ti Chi Robot <ti-community-prow-bot@tidb.io>
- Loading branch information
1 parent
4e41c8d
commit b4c1804
Showing
6 changed files
with
130 additions
and
0 deletions.
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
package grpcutil | ||
|
||
import ( | ||
"os" | ||
"testing" | ||
|
||
. "github.com/pingcap/check" | ||
"github.com/pingcap/errors" | ||
"github.com/tikv/pd/pkg/errs" | ||
) | ||
|
||
func Test(t *testing.T) { | ||
TestingT(t) | ||
} | ||
|
||
var _ = Suite(&gRPCUtilSuite{}) | ||
|
||
type gRPCUtilSuite struct{} | ||
|
||
func loadTLSContent(c *C, caPath, certPath, keyPath string) (caData, certData, keyData []byte) { | ||
var err error | ||
caData, err = os.ReadFile(caPath) | ||
c.Assert(err, IsNil) | ||
certData, err = os.ReadFile(certPath) | ||
c.Assert(err, IsNil) | ||
keyData, err = os.ReadFile(keyPath) | ||
c.Assert(err, IsNil) | ||
return | ||
} | ||
|
||
func (s *gRPCUtilSuite) TestToTLSConfig(c *C) { | ||
tlsConfig := TLSConfig{ | ||
KeyPath: "../../tests/client/cert/pd-server-key.pem", | ||
CertPath: "../../tests/client/cert/pd-server.pem", | ||
CAPath: "../../tests/client/cert/ca.pem", | ||
} | ||
// test without bytes | ||
_, err := tlsConfig.ToTLSConfig() | ||
c.Assert(err, IsNil) | ||
|
||
// test with bytes | ||
caData, certData, keyData := loadTLSContent(c, tlsConfig.CAPath, tlsConfig.CertPath, tlsConfig.KeyPath) | ||
tlsConfig.SSLCABytes = caData | ||
tlsConfig.SSLCertBytes = certData | ||
tlsConfig.SSLKEYBytes = keyData | ||
_, err = tlsConfig.ToTLSConfig() | ||
c.Assert(err, IsNil) | ||
|
||
// test wrong cert bytes | ||
tlsConfig.SSLCertBytes = []byte("invalid cert") | ||
_, err = tlsConfig.ToTLSConfig() | ||
c.Assert(errors.ErrorEqual(err, errs.ErrCryptoX509KeyPair), IsTrue) | ||
|
||
//test wrong ca bytes | ||
tlsConfig.SSLCertBytes = certData | ||
tlsConfig.SSLCABytes = []byte("invalid ca") | ||
_, err = tlsConfig.ToTLSConfig() | ||
c.Assert(errors.ErrorEqual(err, errs.ErrCryptoAppendCertsFromPEM), IsTrue) | ||
} |
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