Package Certificates:

- Allow config to return an old config struct

Package Monitor:
- Add an old config struct / type
- Add method in config (old) to return a new struct config
- Add method in config (new) to return an old config struct
This commit is contained in:
Nicolas JUHEL
2025-04-15 17:36:43 +02:00
parent ec3cc4f859
commit 70b03e32ca
3 changed files with 200 additions and 0 deletions

View File

@@ -28,6 +28,7 @@ package certificates
import (
"fmt"
"strings"
libval "github.com/go-playground/validator/v10"
tlsaut "github.com/nabbar/golib/certificates/auth"
@@ -74,6 +75,70 @@ func (c *Config) Validate() liberr.Error {
return nil
}
func (c *Config) GetConfigOld() ConfigOld {
cfg := ConfigOld{
CurveList: make([]string, 0),
CipherList: make([]string, 0),
RootCAString: make([]string, 0),
RootCAFile: make([]string, 0),
ClientCAString: make([]string, 0),
ClientCAFiles: make([]string, 0),
CertPairString: make([]CertifOld, 0),
CertPairFile: make([]CertifOld, 0),
VersionMin: c.VersionMin.Code(),
VersionMax: c.VersionMax.Code(),
AuthClient: c.AuthClient.Code(),
InheritDefault: c.InheritDefault,
DynamicSizingDisable: c.DynamicSizingDisable,
SessionTicketDisable: c.SessionTicketDisable,
}
for _, i := range c.CurveList {
if i == tlscrv.Unknown {
continue
}
cfg.CurveList = append(cfg.CurveList, i.Code())
}
for _, i := range c.CipherList {
if i == tlscpr.Unknown {
continue
}
cfg.CipherList = append(cfg.CipherList, strings.Join(i.Code(), "_"))
}
for _, i := range c.RootCA {
if i == nil {
continue
}
cfg.RootCAString = append(cfg.RootCAString, i.String())
}
for _, i := range c.ClientCA {
if i == nil {
continue
}
cfg.ClientCAString = append(cfg.ClientCAString, i.String())
}
for _, i := range c.Certs {
if !i.IsPair() {
continue
}
if k, p, e := i.Pair(); e != nil {
continue
} else {
cfg.CertPairString = append(cfg.CertPairString, CertifOld{
Key: k,
Pem: p,
})
}
}
return cfg
}
func (c *Config) New() TLSConfig {
if c.InheritDefault {
return c.NewFrom(Default)

View File

@@ -0,0 +1,120 @@
/*
* MIT License
*
* Copyright (c) 2022 Nicolas JUHEL
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in all
* copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*
*
*/
package types
import (
"fmt"
"time"
libval "github.com/go-playground/validator/v10"
libdur "github.com/nabbar/golib/duration"
liberr "github.com/nabbar/golib/errors"
logcfg "github.com/nabbar/golib/logger/config"
)
type ConfigCompat struct {
// Name define the name of the monitor
Name string `json:"name" yaml:"name" toml:"name" mapstructure:"name"`
// CheckTimeout define the timeout use for healthcheck. Default is 5 second.
CheckTimeout time.Duration `json:"check-timeout" yaml:"check-timeout" toml:"check-timeout" mapstructure:"check-timeout"`
// IntervalCheck define the time waiting between 2 healthcheck. Default is 5 second.
IntervalCheck time.Duration `json:"interval-check" yaml:"interval-check" toml:"interval-check" mapstructure:"interval-check"`
// IntervalFall define the time waiting between 2 healthcheck when last check is KO. Default is 5 second.
IntervalFall time.Duration `json:"interval-fall" yaml:"interval-fall" toml:"interval-fall" mapstructure:"interval-down"`
// IntervalRise define the time waiting between 2 healthcheck when status is KO or Warn but last check is OK. Default is 5 second.
IntervalRise time.Duration `json:"interval-rise" yaml:"interval-rise" toml:"interval-rise" mapstructure:"interval-rise"`
// FallCountKO define the number of KO before considerate the component as down.
FallCountKO uint8 `json:"fall-count-ko" yaml:"fall-count-ko" toml:"fall-count-ko" mapstructure:"fall-count-ko"`
// FallCountWarn define the number of KO before considerate the component as warn.
FallCountWarn uint8 `json:"fall-count-warn" yaml:"fall-count-warn" toml:"fall-count-warn" mapstructure:"fall-count-warn"`
// RiseCountKO define the number of OK when status is KO before considerate the component as up.
RiseCountKO uint8 `json:"rise-count-ko" yaml:"rise-count-ko" toml:"rise-count-ko" mapstructure:"rise-count-ko"`
// RiseCountWarn define the number of OK when status is Warn before considerate the component as up.
RiseCountWarn uint8 `json:"rise-count-warn" yaml:"rise-count-warn" toml:"rise-count-warn" mapstructure:"rise-count-warn"`
// Logger define the logger options for current monitor log
Logger logcfg.Options `json:"logger" yaml:"logger" toml:"logger" mapstructure:"logger"`
}
func (o ConfigCompat) Validate() liberr.Error {
var e = ErrorValidatorError.Error(nil)
if err := libval.New().Struct(o); err != nil {
if er, ok := err.(*libval.InvalidValidationError); ok {
e.Add(er)
}
for _, er := range err.(libval.ValidationErrors) {
//nolint #goerr113
e.Add(fmt.Errorf("config field '%s' is not validated by constraint '%s'", er.Namespace(), er.ActualTag()))
}
}
if !e.HasParent() {
e = nil
}
return e
}
func (o ConfigCompat) Clone() ConfigCompat {
return ConfigCompat{
Name: o.Name,
CheckTimeout: o.CheckTimeout,
IntervalCheck: o.IntervalCheck,
IntervalFall: o.IntervalFall,
IntervalRise: o.IntervalRise,
FallCountKO: o.FallCountKO,
FallCountWarn: o.FallCountWarn,
RiseCountKO: o.RiseCountKO,
RiseCountWarn: o.RiseCountWarn,
Logger: o.Logger.Clone(),
}
}
func (o ConfigCompat) Config() Config {
return Config{
Name: o.Name,
CheckTimeout: libdur.ParseDuration(o.CheckTimeout),
IntervalCheck: libdur.ParseDuration(o.IntervalCheck),
IntervalFall: libdur.ParseDuration(o.IntervalFall),
IntervalRise: libdur.ParseDuration(o.IntervalRise),
FallCountKO: o.FallCountKO,
FallCountWarn: o.FallCountWarn,
RiseCountKO: o.RiseCountKO,
RiseCountWarn: o.RiseCountWarn,
Logger: o.Logger.Clone(),
}
}

View File

@@ -131,3 +131,18 @@ func (o Config) Clone() Config {
Logger: o.Logger.Clone(),
}
}
func (o Config) Compat() ConfigCompat {
return ConfigCompat{
Name: o.Name,
CheckTimeout: o.CheckTimeout.Time(),
IntervalCheck: o.IntervalCheck.Time(),
IntervalFall: o.IntervalFall.Time(),
IntervalRise: o.IntervalRise.Time(),
FallCountKO: o.FallCountKO,
FallCountWarn: o.FallCountWarn,
RiseCountKO: o.RiseCountKO,
RiseCountWarn: o.RiseCountWarn,
Logger: o.Logger.Clone(),
}
}