@@ -18,35 +18,28 @@ package bootstrap
1818
1919import (
2020 "context"
21- "crypto/tls"
22- "crypto/x509"
2321 "encoding/json"
24- "encoding/pem"
2522 "fmt"
2623 configaggregate "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/config/aggregate"
27- "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/config/kube/gateway"
28- "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/features"
29- "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/leaderelection"
30- "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/leaderelection/k8sleaderelection/k8sresourcelock"
31- "github.com/apache/dubbo-kubernetes/pkg/config/schema/gvr"
32- "k8s.io/apimachinery/pkg/api/errors"
33- "net/url"
34- "strings"
35-
36- "github.com/apache/dubbo-kubernetes/api/networking/v1alpha3"
3724 "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/config/kube/crdclient"
3825 "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/config/kube/file"
26+ "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/config/kube/gateway"
3927 "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/config/memory"
4028 dubboCredentials "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/credentials"
4129 "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/credentials/kube"
30+ "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/features"
31+ "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/leaderelection"
32+ "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/leaderelection/k8sleaderelection/k8sresourcelock"
4233 "github.com/apache/dubbo-kubernetes/dubbod/planet/pkg/model"
4334 "github.com/apache/dubbo-kubernetes/pkg/adsc"
4435 "github.com/apache/dubbo-kubernetes/pkg/config/schema/collections"
36+ "github.com/apache/dubbo-kubernetes/pkg/config/schema/gvr"
4537 "github.com/apache/dubbo-kubernetes/pkg/log"
4638 "google.golang.org/grpc"
47- "google.golang.org/grpc/credentials"
4839 "google.golang.org/grpc/credentials/insecure"
40+ "k8s.io/apimachinery/pkg/api/errors"
4941 v1 "k8s.io/apimachinery/pkg/apis/meta/v1"
42+ "net/url"
5043)
5144
5245type ConfigSourceAddressScheme string
@@ -276,93 +269,6 @@ func (s *Server) initConfigController(args *PlanetArgs) error {
276269 return nil
277270}
278271
279- // verifyCert verifies given cert against TLS settings like SANs and CRL.
280- func (s * Server ) verifyCert (certs [][]byte , tlsSettings * v1alpha3.ClientTLSSettings ) error {
281- if len (certs ) == 0 {
282- return fmt .Errorf ("no certificates provided" )
283- }
284- cert , err := x509 .ParseCertificate (certs [0 ])
285- if err != nil {
286- return fmt .Errorf ("failed to parse certificate: %w" , err )
287- }
288-
289- if len (tlsSettings .SubjectAltNames ) > 0 {
290- sanMatchFound := false
291- for _ , san := range cert .DNSNames {
292- if sanMatchFound {
293- break
294- }
295- for _ , name := range tlsSettings .SubjectAltNames {
296- if san == name {
297- sanMatchFound = true
298- break
299- }
300- }
301- }
302- if ! sanMatchFound {
303- return fmt .Errorf ("no matching SAN found" )
304- }
305- }
306-
307- if len (tlsSettings .CaCrl ) > 0 {
308- crlData := []byte (strings .TrimSpace (tlsSettings .CaCrl ))
309- block , _ := pem .Decode (crlData )
310- if block != nil {
311- crlData = block .Bytes
312- }
313- crl , err := x509 .ParseRevocationList (crlData )
314- if err != nil {
315- return fmt .Errorf ("failed to parse CRL: %w" , err )
316- }
317- for _ , revokedCert := range crl .RevokedCertificateEntries {
318- if cert .SerialNumber .Cmp (revokedCert .SerialNumber ) == 0 {
319- return fmt .Errorf ("certificate is revoked" )
320- }
321- }
322- }
323-
324- return nil
325- }
326-
327- // getTransportCredentials attempts to create credentials.TransportCredentials from ClientTLSSettings in mesh config
328- // Implemented only for SIMPLE_TLS mode
329- // TODO:
330- //
331- // Implement for MUTUAL_TLS/DUBBO_MUTUAL_TLS modes
332- func (s * Server ) getTransportCredentials (args * PlanetArgs , tlsSettings * v1alpha3.ClientTLSSettings ) (credentials.TransportCredentials , error ) {
333- // TODO ValidateTLS
334-
335- switch tlsSettings .GetMode () {
336- case v1alpha3 .ClientTLSSettings_SIMPLE :
337- if len (tlsSettings .GetCredentialName ()) > 0 {
338- rootCert , err := s .getRootCertFromSecret (tlsSettings .GetCredentialName (), args .Namespace )
339- if err != nil {
340- return nil , err
341- }
342- tlsSettings .CaCertificates = string (rootCert .Cert )
343- tlsSettings .CaCrl = string (rootCert .CRL )
344- }
345- if tlsSettings .GetInsecureSkipVerify ().GetValue () || len (tlsSettings .GetCaCertificates ()) == 0 {
346- return credentials .NewTLS (& tls.Config {
347- ServerName : tlsSettings .GetSni (),
348- }), nil
349- }
350- certPool := x509 .NewCertPool ()
351- if ! certPool .AppendCertsFromPEM ([]byte (tlsSettings .GetCaCertificates ())) {
352- return nil , fmt .Errorf ("failed to add ca certificate from configSource.tlsSettings to pool" )
353- }
354- return credentials .NewTLS (& tls.Config {
355- ServerName : tlsSettings .GetSni (),
356- RootCAs : certPool ,
357- VerifyPeerCertificate : func (rawCerts [][]byte , verifiedChains [][]* x509.Certificate ) error {
358- return s .verifyCert (rawCerts , tlsSettings )
359- },
360- }), nil
361- default :
362- return insecure .NewCredentials (), nil
363- }
364- }
365-
366272// getRootCertFromSecret fetches a map of keys and values from a secret with name in namespace
367273func (s * Server ) getRootCertFromSecret (name , namespace string ) (* dubboCredentials.CertInfo , error ) {
368274 secret , err := s .kubeClient .Kube ().CoreV1 ().Secrets (namespace ).Get (context .Background (), name , v1.GetOptions {})
0 commit comments