104 lines
2.1 KiB
Go
104 lines
2.1 KiB
Go
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
package config
|
|
|
|
import (
|
|
"context"
|
|
"io"
|
|
"fmt"
|
|
"net/url"
|
|
"decl/internal/codec"
|
|
"decl/internal/data"
|
|
"decl/internal/folio"
|
|
"encoding/json"
|
|
"gopkg.in/yaml.v3"
|
|
"crypto/x509"
|
|
)
|
|
|
|
func init() {
|
|
folio.DocumentRegistry.ConfigurationTypes.Register([]string{"certificate"}, func(u *url.URL) data.Configuration {
|
|
c := NewCertificate()
|
|
return c
|
|
})
|
|
}
|
|
|
|
type Certificate map[string]*x509.Certificate
|
|
|
|
func NewCertificate() *Certificate {
|
|
c := make(Certificate)
|
|
return &c
|
|
}
|
|
|
|
func (c *Certificate) URI() string {
|
|
return fmt.Sprintf("%s://%s", c.Type(), "")
|
|
}
|
|
|
|
func (c *Certificate) SetURI(uri string) error {
|
|
return nil
|
|
}
|
|
|
|
func (c *Certificate) SetParsedURI(uri data.URIParser) error {
|
|
return nil
|
|
}
|
|
|
|
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() data.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 = data.ErrUnknownConfigurationKey
|
|
}
|
|
return
|
|
}
|
|
|
|
func (c *Certificate) Has(key string) (ok bool) {
|
|
_, ok = (*c)[key]
|
|
return
|
|
}
|