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

     1  // Copyright 2021 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 config
    15  
    16  import (
    17  	"github.com/pingcap/tiflow/dm/config/dbconfig"
    18  	"github.com/pingcap/tiflow/dm/config/security"
    19  	"github.com/pingcap/tiflow/dm/openapi"
    20  )
    21  
    22  // SourceCfgToOpenAPISource converter SourceConfig to openapi.Source.
    23  func SourceCfgToOpenAPISource(cfg *SourceConfig) openapi.Source {
    24  	source := openapi.Source{
    25  		Enable:     cfg.Enable,
    26  		EnableGtid: cfg.EnableGTID,
    27  		Host:       cfg.From.Host,
    28  		Password:   &ObfuscatedPasswordForFeedback, // PM's requirement, we always return obfuscated password to users
    29  		Port:       cfg.From.Port,
    30  		SourceName: cfg.SourceID,
    31  		User:       cfg.From.User,
    32  		Purge: &openapi.Purge{
    33  			Expires:     &cfg.Purge.Expires,
    34  			Interval:    &cfg.Purge.Interval,
    35  			RemainSpace: &cfg.Purge.RemainSpace,
    36  		},
    37  		RelayConfig: &openapi.RelayConfig{
    38  			EnableRelay:     &cfg.EnableRelay,
    39  			RelayBinlogGtid: &cfg.RelayBinlogGTID,
    40  			RelayBinlogName: &cfg.RelayBinLogName,
    41  			RelayDir:        &cfg.RelayDir,
    42  		},
    43  	}
    44  	if cfg.Flavor != "" {
    45  		source.Flavor = &cfg.Flavor
    46  	}
    47  	if cfg.From.Security != nil {
    48  		// NOTE we don't return security content here, because we don't want to expose it to the user.
    49  		var certAllowedCn []string
    50  		certAllowedCn = append(certAllowedCn, cfg.From.Security.CertAllowedCN...)
    51  		source.Security = &openapi.Security{CertAllowedCn: &certAllowedCn}
    52  	}
    53  	return source
    54  }
    55  
    56  // OpenAPISourceToSourceCfg converter openapi.Source to SourceConfig.
    57  func OpenAPISourceToSourceCfg(source openapi.Source) *SourceConfig {
    58  	cfg := NewSourceConfig()
    59  	from := dbconfig.DBConfig{
    60  		Host: source.Host,
    61  		Port: source.Port,
    62  		User: source.User,
    63  	}
    64  	if source.Password != nil {
    65  		from.Password = *source.Password
    66  	}
    67  	if source.Security != nil {
    68  		from.Security = &security.Security{
    69  			SSLCABytes:   []byte(source.Security.SslCaContent),
    70  			SSLKeyBytes:  []byte(source.Security.SslKeyContent),
    71  			SSLCertBytes: []byte(source.Security.SslCertContent),
    72  		}
    73  		if source.Security.CertAllowedCn != nil {
    74  			from.Security.CertAllowedCN = *source.Security.CertAllowedCn
    75  		}
    76  	}
    77  	cfg.From = from
    78  	if source.Flavor != nil {
    79  		cfg.Flavor = *source.Flavor
    80  	}
    81  	cfg.Enable = source.Enable
    82  	cfg.EnableGTID = source.EnableGtid
    83  	cfg.SourceID = source.SourceName
    84  	if purge := source.Purge; purge != nil {
    85  		if purge.Expires != nil {
    86  			cfg.Purge.Expires = *purge.Expires
    87  		}
    88  		if purge.Interval != nil {
    89  			cfg.Purge.Interval = *purge.Interval
    90  		}
    91  		if purge.RemainSpace != nil {
    92  			cfg.Purge.RemainSpace = *purge.RemainSpace
    93  		}
    94  	}
    95  	if relayConfig := source.RelayConfig; relayConfig != nil {
    96  		if relayConfig.EnableRelay != nil {
    97  			cfg.EnableRelay = *relayConfig.EnableRelay
    98  		}
    99  		if relayConfig.RelayBinlogGtid != nil {
   100  			cfg.RelayBinlogGTID = *relayConfig.RelayBinlogGtid
   101  		}
   102  		if relayConfig.RelayBinlogName != nil {
   103  			cfg.RelayBinLogName = *relayConfig.RelayBinlogName
   104  		}
   105  		if relayConfig.RelayDir != nil {
   106  			cfg.RelayDir = *relayConfig.RelayDir
   107  		}
   108  	}
   109  	return cfg
   110  }