@@ -15,7 +15,9 @@ package manager
15
15
16
16
import (
17
17
"context"
18
+ "encoding/pem"
18
19
"fmt"
20
+ "os"
19
21
"path/filepath"
20
22
"strings"
21
23
@@ -835,6 +837,36 @@ func buildTLSTask(
835
837
return builder .Build (), nil
836
838
}
837
839
840
+ func genTiProxySessionCerts (dir string ) error {
841
+ if _ , err := os .Stat (filepath .Join (dir , "tiproxy-session.crt" )); err == nil {
842
+ return nil
843
+ }
844
+
845
+ ca , err := crypto .NewCA ("tiproxy" )
846
+ if err != nil {
847
+ return err
848
+ }
849
+ privKey , err := crypto .NewKeyPair (crypto .KeyTypeRSA , crypto .KeySchemeRSASSAPSSSHA256 )
850
+ if err != nil {
851
+ return err
852
+ }
853
+ csr , err := privKey .CSR ("tiproxy" , "tiproxy" , nil , nil )
854
+ if err != nil {
855
+ return err
856
+ }
857
+ cert , err := ca .Sign (csr )
858
+ if err != nil {
859
+ return err
860
+ }
861
+ if err := utils .SaveFileWithBackup (filepath .Join (dir , "tiproxy-session.key" ), privKey .Pem (), "" ); err != nil {
862
+ return err
863
+ }
864
+ return utils .SaveFileWithBackup (filepath .Join (dir , "tiproxy-session.crt" ), pem .EncodeToMemory (& pem.Block {
865
+ Type : "CERTIFICATE" ,
866
+ Bytes : cert ,
867
+ }), "" )
868
+ }
869
+
838
870
// buildCertificateTasks generates certificate for instance and transfers it to the server
839
871
func buildCertificateTasks (
840
872
m * Manager ,
@@ -848,37 +880,61 @@ func buildCertificateTasks(
848
880
certificateTasks []* task.StepDisplay // tasks which are used to copy certificate to remote host
849
881
)
850
882
851
- if topo .BaseTopo ().GlobalOptions .TLSEnabled {
852
- // copy certificate to remote host
853
- topo .IterInstance (func (inst spec.Instance ) {
854
- deployDir := spec .Abs (base .User , inst .DeployDir ())
855
- tlsDir := filepath .Join (deployDir , spec .TLSCertKeyDir )
883
+ // check if there is tiproxy
884
+ // if there is tiproxy, whether or not TLS, we must issue a self-signed cert
885
+ hasTiProxy := false
886
+ topo .IterInstance (func (inst spec.Instance ) {
887
+ if inst .ComponentName () == spec .ComponentTiProxy {
888
+ hasTiProxy = true
889
+ }
890
+ })
891
+ if hasTiProxy {
892
+ if err := genTiProxySessionCerts (m .specManager .Path (name , spec .TempConfigPath )); err != nil {
893
+ return certificateTasks , err
894
+ }
895
+ }
856
896
897
+ // copy certificate to remote host
898
+ topo .IterInstance (func (inst spec.Instance ) {
899
+ deployDir := spec .Abs (base .User , inst .DeployDir ())
900
+ tlsDir := filepath .Join (deployDir , spec .TLSCertKeyDir )
901
+
902
+ needSessionCert := hasTiProxy && inst .ComponentName () == spec .ComponentTiDB
903
+ if needSessionCert || topo .BaseTopo ().GlobalOptions .TLSEnabled {
857
904
tb := task .NewSimpleUerSSH (m .logger , inst .GetManageHost (), inst .GetSSHPort (), base .User , gOpt , p , topo .BaseTopo ().GlobalOptions .SSHType ).
858
905
Mkdir (base .User , inst .GetManageHost (), topo .BaseTopo ().GlobalOptions .SystemdMode != spec .UserMode , deployDir , tlsDir )
859
906
860
- ca , err := crypto .ReadCA (
861
- name ,
862
- m .specManager .Path (name , spec .TLSCertKeyDir , spec .TLSCACert ),
863
- m .specManager .Path (name , spec .TLSCertKeyDir , spec .TLSCAKey ),
864
- )
865
- if err != nil {
866
- iterErr = err
867
- return
907
+ if needSessionCert {
908
+ tb = tb .
909
+ CopyFile (filepath .Join (m .specManager .Path (name , spec .TempConfigPath ), "tiproxy-session.key" ), filepath .Join (deployDir , spec .TLSCertKeyDir , "tiproxy-session.key" ), inst .GetHost (), false , 0 , false ).
910
+ CopyFile (filepath .Join (m .specManager .Path (name , spec .TempConfigPath ), "tiproxy-session.crt" ), filepath .Join (deployDir , spec .TLSCertKeyDir , "tiproxy-session.crt" ), inst .GetHost (), false , 0 , false )
868
911
}
869
- t := tb .TLSCert (
870
- inst .GetHost (),
871
- inst .ComponentName (),
872
- inst .Role (),
873
- inst .GetMainPort (),
874
- ca ,
875
- meta.DirPaths {
876
- Deploy : deployDir ,
877
- Cache : m .specManager .Path (name , spec .TempConfigPath ),
878
- }).
879
- BuildAsStep (fmt .Sprintf (" - Generate certificate %s -> %s" , inst .ComponentName (), inst .ID ()))
912
+
913
+ if topo .BaseTopo ().GlobalOptions .TLSEnabled {
914
+ ca , err := crypto .ReadCA (
915
+ name ,
916
+ m .specManager .Path (name , spec .TLSCertKeyDir , spec .TLSCACert ),
917
+ m .specManager .Path (name , spec .TLSCertKeyDir , spec .TLSCAKey ),
918
+ )
919
+ if err != nil {
920
+ iterErr = err
921
+ return
922
+ }
923
+ tb = tb .TLSCert (
924
+ inst .GetHost (),
925
+ inst .ComponentName (),
926
+ inst .Role (),
927
+ inst .GetMainPort (),
928
+ ca ,
929
+ meta.DirPaths {
930
+ Deploy : deployDir ,
931
+ Cache : m .specManager .Path (name , spec .TempConfigPath ),
932
+ })
933
+ }
934
+
935
+ t := tb .BuildAsStep (fmt .Sprintf (" - Generate certificate %s -> %s" , inst .ComponentName (), inst .ID ()))
880
936
certificateTasks = append (certificateTasks , t )
881
- })
882
- }
937
+ }
938
+ })
883
939
return certificateTasks , iterErr
884
940
}
0 commit comments