84 lines
1.7 KiB
Go
84 lines
1.7 KiB
Go
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
||
|
|
||
|
package config
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
"io"
|
||
|
"net/url"
|
||
|
"decl/internal/codec"
|
||
|
"encoding/json"
|
||
|
"gopkg.in/yaml.v3"
|
||
|
"crypto/x509"
|
||
|
)
|
||
|
|
||
|
func init() {
|
||
|
ConfigTypes.Register([]string{"certificate"}, func(u *url.URL) Configuration {
|
||
|
c := NewCertificate()
|
||
|
return c
|
||
|
})
|
||
|
}
|
||
|
|
||
|
type Certificate map[string]*x509.Certificate
|
||
|
|
||
|
func NewCertificate() *Certificate {
|
||
|
c := make(Certificate)
|
||
|
return &c
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) Read(ctx context.Context) ([]byte, error) {
|
||
|
return nil, nil
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) Load(r io.Reader) (err error) {
|
||
|
err = codec.NewYAMLDecoder(r).Decode(c)
|
||
|
if err == nil {
|
||
|
_, err = c.Read(context.Background())
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) LoadYAML(yamlData string) (err error) {
|
||
|
err = codec.NewYAMLStringDecoder(yamlData).Decode(c)
|
||
|
if err == nil {
|
||
|
_, err = c.Read(context.Background())
|
||
|
}
|
||
|
return err
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) UnmarshalJSON(data []byte) error {
|
||
|
if unmarshalErr := json.Unmarshal(data, c); unmarshalErr != nil {
|
||
|
return unmarshalErr
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) UnmarshalYAML(value *yaml.Node) error {
|
||
|
type decodeCertificate Certificate
|
||
|
if unmarshalErr := value.Decode((*decodeCertificate)(c)); unmarshalErr != nil {
|
||
|
return unmarshalErr
|
||
|
}
|
||
|
return nil
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) Clone() Configuration {
|
||
|
jsonGeneric, _ := json.Marshal(c)
|
||
|
clone := NewCertificate()
|
||
|
if unmarshalErr := json.Unmarshal(jsonGeneric, &clone); unmarshalErr != nil {
|
||
|
panic(unmarshalErr)
|
||
|
}
|
||
|
return clone
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) Type() string {
|
||
|
return "certificate"
|
||
|
}
|
||
|
|
||
|
func (c *Certificate) GetValue(name string) (result any, err error) {
|
||
|
var ok bool
|
||
|
if result, ok = (*c)[name]; !ok {
|
||
|
err = ErrUnknownConfigurationKey
|
||
|
}
|
||
|
return
|
||
|
}
|