github.com/pingcap/tiflow@v0.0.0-20240520035814-5bf52d54e205/dm/config/security/security.go (about)

     1  // Copyright 2020 PingCAP, Inc.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // See the License for the specific language governing permissions and
    12  // limitations under the License.
    13  
    14  package security
    15  
    16  import (
    17  	"encoding/base64"
    18  	"fmt"
    19  	"os"
    20  )
    21  
    22  // Security config.
    23  type Security struct {
    24  	SSLCA         string   `toml:"ssl-ca" json:"ssl-ca" yaml:"ssl-ca"`
    25  	SSLCert       string   `toml:"ssl-cert" json:"ssl-cert" yaml:"ssl-cert"`
    26  	SSLKey        string   `toml:"ssl-key" json:"ssl-key" yaml:"ssl-key"`
    27  	CertAllowedCN strArray `toml:"cert-allowed-cn" json:"cert-allowed-cn" yaml:"cert-allowed-cn"`
    28  	SSLCABytes    []byte   `toml:"ssl-ca-bytes" json:"-" yaml:"ssl-ca-bytes"`
    29  	SSLKeyBytes   []byte   `toml:"ssl-key-bytes" json:"-" yaml:"ssl-key-bytes"`
    30  	SSLCertBytes  []byte   `toml:"ssl-cert-bytes" json:"-" yaml:"ssl-cert-bytes"`
    31  	SSLCABase64   string   `toml:"ssl-ca-base64" json:"-" yaml:"ssl-ca-base64"`
    32  	SSLKeyBase64  string   `toml:"ssl-key-base64" json:"-" yaml:"ssl-key-base64"`
    33  	SSLCertBase64 string   `toml:"ssl-cert-base64" json:"-" yaml:"ssl-cert-base64"`
    34  }
    35  
    36  // used for parse string slice in flag.
    37  type strArray []string
    38  
    39  func (i *strArray) String() string {
    40  	return fmt.Sprint([]string(*i))
    41  }
    42  
    43  func (i *strArray) Set(value string) error {
    44  	*i = append(*i, value)
    45  	return nil
    46  }
    47  
    48  // LoadTLSContent load all tls config from file or base64 fields.
    49  func (s *Security) LoadTLSContent() error {
    50  	var firstErr error
    51  	convertAndAssign := func(source string, convert func(string) ([]byte, error), target *[]byte) {
    52  		if firstErr != nil {
    53  			return
    54  		}
    55  		// already loaded. And DM does not support certificate rotation.
    56  		if len(*target) > 0 {
    57  			return
    58  		}
    59  
    60  		if source == "" {
    61  			return
    62  		}
    63  
    64  		dat, err := convert(source)
    65  		if err != nil {
    66  			firstErr = err
    67  			return
    68  		}
    69  		*target = dat
    70  	}
    71  
    72  	convertAndAssign(s.SSLCABase64, base64.StdEncoding.DecodeString, &s.SSLCABytes)
    73  	convertAndAssign(s.SSLKeyBase64, base64.StdEncoding.DecodeString, &s.SSLKeyBytes)
    74  	convertAndAssign(s.SSLCertBase64, base64.StdEncoding.DecodeString, &s.SSLCertBytes)
    75  	convertAndAssign(s.SSLCA, os.ReadFile, &s.SSLCABytes)
    76  	convertAndAssign(s.SSLKey, os.ReadFile, &s.SSLKeyBytes)
    77  	convertAndAssign(s.SSLCert, os.ReadFile, &s.SSLCertBytes)
    78  	return firstErr
    79  }
    80  
    81  // ClearSSLBytesData clear all tls config bytes data.
    82  func (s *Security) ClearSSLBytesData() {
    83  	s.SSLCABytes = s.SSLCABytes[:0]
    84  	s.SSLKeyBytes = s.SSLKeyBytes[:0]
    85  	s.SSLCertBytes = s.SSLCertBytes[:0]
    86  }
    87  
    88  // Clone returns a deep copy of Security.
    89  func (s *Security) Clone() *Security {
    90  	if s == nil {
    91  		return nil
    92  	}
    93  	clone := *s
    94  	clone.CertAllowedCN = append(strArray(nil), s.CertAllowedCN...)
    95  	clone.SSLCABytes = append([]byte(nil), s.SSLCABytes...)
    96  	clone.SSLKeyBytes = append([]byte(nil), s.SSLKeyBytes...)
    97  	clone.SSLCertBytes = append([]byte(nil), s.SSLCertBytes...)
    98  	return &clone
    99  }