2024-07-17 08:34:57 +00:00
|
|
|
// Copyright 2024 Matthew Rich <matthewrich.conf@gmail.com>. All rights reserved.
|
|
|
|
|
|
|
|
package config
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io"
|
2024-09-24 19:15:47 +00:00
|
|
|
"fmt"
|
2024-07-17 08:34:57 +00:00
|
|
|
"net/url"
|
|
|
|
"decl/internal/codec"
|
2024-09-24 19:15:47 +00:00
|
|
|
"decl/internal/data"
|
|
|
|
"decl/internal/folio"
|
2024-07-17 08:34:57 +00:00
|
|
|
"encoding/json"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
"crypto/x509"
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
2024-09-24 19:15:47 +00:00
|
|
|
folio.DocumentRegistry.ConfigurationTypes.Register([]string{"certificate"}, func(u *url.URL) data.Configuration {
|
2024-07-17 08:34:57 +00:00
|
|
|
c := NewCertificate()
|
|
|
|
return c
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
type Certificate map[string]*x509.Certificate
|
|
|
|
|
|
|
|
func NewCertificate() *Certificate {
|
|
|
|
c := make(Certificate)
|
|
|
|
return &c
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:15:47 +00:00
|
|
|
func (c *Certificate) URI() string {
|
|
|
|
return fmt.Sprintf("%s://%s", c.Type(), "")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (c *Certificate) SetURI(uri string) error {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-10-16 17:26:42 +00:00
|
|
|
func (c *Certificate) SetParsedURI(uri data.URIParser) error {
|
2024-09-24 19:15:47 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2024-07-17 08:34:57 +00:00
|
|
|
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
|
|
|
|
}
|
|
|
|
|
2024-09-24 19:15:47 +00:00
|
|
|
func (c *Certificate) Clone() data.Configuration {
|
2024-07-17 08:34:57 +00:00
|
|
|
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 {
|
2024-09-24 19:15:47 +00:00
|
|
|
err = data.ErrUnknownConfigurationKey
|
2024-07-17 08:34:57 +00:00
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2024-09-24 19:15:47 +00:00
|
|
|
|
|
|
|
func (c *Certificate) Has(key string) (ok bool) {
|
|
|
|
_, ok = (*c)[key]
|
|
|
|
return
|
|
|
|
}
|