feat: add common serialize package (#1601)

Signed-off-by: Jim Ma <majinjing3@gmail.com>
This commit is contained in:
Jim Ma 2022-08-29 13:59:56 +08:00 committed by Gaius
parent ecfe467585
commit c5a8512cb6
No known key found for this signature in database
GPG Key ID: 8B4E5D1290FA2FFB
6 changed files with 97 additions and 68 deletions

View File

@ -28,7 +28,6 @@ import (
"os"
"path/filepath"
"regexp"
"strings"
"time"
"gopkg.in/yaml.v3"
@ -40,6 +39,7 @@ import (
logger "d7y.io/dragonfly/v2/internal/dflog"
"d7y.io/dragonfly/v2/pkg/dfnet"
netip "d7y.io/dragonfly/v2/pkg/net/ip"
"d7y.io/dragonfly/v2/pkg/serialize"
"d7y.io/dragonfly/v2/pkg/unit"
)
@ -175,7 +175,7 @@ func ConvertPattern(p string, defaultPattern commonv1.Pattern) commonv1.Pattern
type GlobalSecurityOption struct {
AutoIssueCert bool `mapstructure:"autoIssueCert" yaml:"autoIssueCert"`
CACert PEMContent `mapstructure:"caCert" yaml:"caCert"`
CACert serialize.PEMContent `mapstructure:"caCert" yaml:"caCert"`
TLSVerify bool `mapstructure:"tlsVerify" yaml:"tlsVerify"`
}
@ -528,9 +528,9 @@ type UnixListenOption struct {
type SecurityOption struct {
// Insecure indicate enable tls or not
Insecure bool `mapstructure:"insecure" yaml:"insecure"`
CACert PEMContent `mapstructure:"caCert" yaml:"caCert"`
Cert PEMContent `mapstructure:"cert" yaml:"cert"`
Key PEMContent `mapstructure:"key" yaml:"key"`
CACert serialize.PEMContent `mapstructure:"caCert" yaml:"caCert"`
Cert serialize.PEMContent `mapstructure:"cert" yaml:"cert"`
Key serialize.PEMContent `mapstructure:"key" yaml:"key"`
TLSVerify bool `mapstructure:"tlsVerify" yaml:"tlsVerify"`
TLSConfig *tls.Config `mapstructure:"tlsConfig" yaml:"tlsConfig"`
}
@ -562,58 +562,10 @@ type ReloadOption struct {
Interval util.Duration `mapstructure:"interval" yaml:"interval"`
}
// PEMContent supports load PEM format from file or just inline PEM format content
type PEMContent string
func (p *PEMContent) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
return p.loadPEM(s)
}
func (p *PEMContent) UnmarshalYAML(node *yaml.Node) error {
var s string
switch node.Kind {
case yaml.ScalarNode:
if err := node.Decode(&s); err != nil {
return err
}
default:
return errors.New("invalid pem content")
}
return p.loadPEM(s)
}
func (p *PEMContent) loadPEM(content string) error {
if content == "" {
*p = PEMContent("")
return nil
}
// inline PEM, just return
if strings.HasPrefix(strings.TrimSpace(content), "-----BEGIN ") {
val := strings.TrimSpace(content)
*p = PEMContent(val)
return nil
}
file, err := os.ReadFile(content)
if err != nil {
return err
}
val := strings.TrimSpace(string(file))
*p = PEMContent(val)
return nil
}
type tlsConfigFiles struct {
Cert PEMContent `yaml:"cert" json:"cert"`
Key PEMContent `yaml:"key" json:"key"`
CACert PEMContent `yaml:"caCert" json:"caCert"`
Cert serialize.PEMContent `yaml:"cert" json:"cert"`
Key serialize.PEMContent `yaml:"key" json:"key"`
CACert serialize.PEMContent `yaml:"caCert" json:"caCert"`
}
type TLSConfig struct {

View File

@ -31,6 +31,7 @@ import (
"d7y.io/dragonfly/v2/cmd/dependency/base"
"d7y.io/dragonfly/v2/manager/model"
"d7y.io/dragonfly/v2/pkg/dfnet"
"d7y.io/dragonfly/v2/pkg/serialize"
"d7y.io/dragonfly/v2/pkg/unit"
)
@ -225,9 +226,9 @@ func TestPeerHostOption_Load(t *testing.T) {
_cert, _ := os.ReadFile("./testdata/certs/sca.crt")
_key, _ := os.ReadFile("./testdata/certs/sca.key")
caCert := PEMContent(strings.TrimSpace(string(_caCert)))
cert := PEMContent(strings.TrimSpace(string(_cert)))
key := PEMContent(strings.TrimSpace(string(_key)))
caCert := serialize.PEMContent(strings.TrimSpace(string(_caCert)))
cert := serialize.PEMContent(strings.TrimSpace(string(_cert)))
key := serialize.PEMContent(strings.TrimSpace(string(_key)))
peerHostOption := &DaemonOption{
Options: base.Options{

View File

@ -50,6 +50,7 @@ import (
"d7y.io/dragonfly/v2/pkg/dfpath"
"d7y.io/dragonfly/v2/pkg/net/fqdn"
"d7y.io/dragonfly/v2/pkg/net/ip"
"d7y.io/dragonfly/v2/pkg/serialize"
"d7y.io/dragonfly/v2/pkg/unit"
"d7y.io/dragonfly/v2/version"
)
@ -238,7 +239,7 @@ func initDecoderConfig(dc *mapstructure.DecoderConfig) {
reflect.TypeOf(util.Duration{}),
reflect.TypeOf(&config.ProxyOption{}),
reflect.TypeOf(config.TCPListenPortRange{}),
reflect.TypeOf(config.PEMContent("")),
reflect.TypeOf(serialize.PEMContent("")),
reflect.TypeOf(config.URL{}),
reflect.TypeOf(config.CertPool{}),
reflect.TypeOf(config.Regexp{}):

View File

@ -25,6 +25,7 @@ import (
"d7y.io/dragonfly/v2/cmd/dependency/base"
"d7y.io/dragonfly/v2/pkg/objectstorage"
"d7y.io/dragonfly/v2/pkg/serialize"
)
type Config struct {
@ -250,10 +251,10 @@ type SecurityConfig struct {
Enable bool `yaml:"enable" mapstructure:"enable"`
// CACert is file path PEM-encoded certificate
CACert string `mapstructure:"caCert" yaml:"caCert"`
CACert serialize.PEMContent `mapstructure:"caCert" yaml:"caCert"`
// CAKey is file path of PEM-encoded private key.
CAKey string `mapstructure:"caKey" yaml:"caKey"`
CAKey serialize.PEMContent `mapstructure:"caKey" yaml:"caKey"`
}
// New config instance.

View File

@ -157,7 +157,7 @@ func New(cfg *config.Config, d dfpath.Dfpath) (*Server, error) {
// Initialize global certificate.
var options []rpcserver.Option
if cfg.Security.Enable {
cert, err := tls.LoadX509KeyPair(cfg.Security.CACert, cfg.Security.CAKey)
cert, err := tls.X509KeyPair([]byte(cfg.Security.CACert), []byte(cfg.Security.CAKey))
if err != nil {
return nil, err
}

View File

@ -0,0 +1,74 @@
/*
* Copyright 2022 The Dragonfly Authors
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package serialize
import (
"encoding/json"
"errors"
"os"
"strings"
"gopkg.in/yaml.v3"
)
// PEMContent supports load PEM format from file or just inline PEM format content
type PEMContent string
func (p *PEMContent) UnmarshalJSON(b []byte) error {
var s string
err := json.Unmarshal(b, &s)
if err != nil {
return err
}
return p.loadPEM(s)
}
func (p *PEMContent) UnmarshalYAML(node *yaml.Node) error {
var s string
switch node.Kind {
case yaml.ScalarNode:
if err := node.Decode(&s); err != nil {
return err
}
default:
return errors.New("invalid pem content")
}
return p.loadPEM(s)
}
func (p *PEMContent) loadPEM(content string) error {
if content == "" {
*p = PEMContent("")
return nil
}
// inline PEM, just return
if strings.HasPrefix(strings.TrimSpace(content), "-----BEGIN ") {
val := strings.TrimSpace(content)
*p = PEMContent(val)
return nil
}
file, err := os.ReadFile(content)
if err != nil {
return err
}
val := strings.TrimSpace(string(file))
*p = PEMContent(val)
return nil
}