cacert-gosignerclient/internal/config/config.go

177 lines
4.8 KiB
Go
Raw Normal View History

/*
Copyright 2022 CAcert Inc.
SPDX-License-Identifier: Apache-2.0
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 config
import (
"fmt"
"io"
"os"
"time"
"gopkg.in/yaml.v3"
)
const (
defaultSerialTimeout = 5 * time.Second
defaultHealthInterval = 30 * time.Second
defaultHealthStart = 2 * time.Second
defaultFetchCRLInterval = 5 * time.Minute
defaultFetchCRLStart = 5 * time.Minute
defaultResponseAnnounceTimeout = 30 * time.Second
defaultResponseDataTimeout = 2 * time.Second
defaultFilesDirectory = "public"
)
type SettingsError struct {
msg string
}
func (se SettingsError) Error() string {
return fmt.Sprintf("invalid Settings %s", se.msg)
}
type Serial struct {
Device string `yaml:"device"`
Baud int `yaml:"baud"`
Timeout time.Duration `yaml:"timeout"`
}
type ClientConfig struct {
Serial Serial `yaml:"serial"`
HealthInterval time.Duration `yaml:"health-interval"`
HealthStart time.Duration `yaml:"health-start"`
FetchCRLStart time.Duration `yaml:"fetch-crl-start"`
FetchCRLInterval time.Duration `yaml:"fetch-crl-interval"`
ResponseAnnounceTimeout time.Duration `yaml:"response-announce-timeout"`
ResponseDataTimeout time.Duration `yaml:"response-data-timeout"`
PublicCRLDirectory string `yaml:"public-crl-directory"`
PublicCertificateDirectory string `yaml:"public-certificate-directory"`
}
func (c *ClientConfig) UnmarshalYAML(n *yaml.Node) error {
data := struct {
Serial Serial `yaml:"serial"`
HealthInterval time.Duration `yaml:"health-interval"`
HealthStart time.Duration `yaml:"health-start"`
FetchCRLStart time.Duration `yaml:"fetch-crl-start"`
FetchCRLInterval time.Duration `yaml:"fetch-crl-interval"`
ResponseAnnounceTimeout time.Duration `yaml:"response-announce-timeout"`
ResponseDataTimeout time.Duration `yaml:"response-data-timeout"`
PublicCRLDirectory string `yaml:"public-crl-directory"`
PublicCertificateDirectory string `yaml:"public-certificate-directory"`
}{}
err := n.Decode(&data)
if err != nil {
return fmt.Errorf("could not decode YAML: %w", err)
}
if data.Serial.Device == "" {
return SettingsError{"you must specify a serial 'device'"}
}
if data.Serial.Baud == 0 {
data.Serial.Baud = 115200
}
if data.Serial.Timeout == 0 {
data.Serial.Timeout = defaultSerialTimeout
}
c.Serial = data.Serial
if data.HealthInterval == 0 {
data.HealthInterval = defaultHealthInterval
}
c.HealthInterval = data.HealthInterval
if data.HealthStart == 0 {
data.HealthStart = defaultHealthStart
}
c.HealthStart = data.HealthStart
if data.FetchCRLInterval == 0 {
data.FetchCRLInterval = defaultFetchCRLInterval
}
c.FetchCRLInterval = data.FetchCRLInterval
if data.FetchCRLStart == 0 {
data.FetchCRLStart = defaultFetchCRLStart
}
c.FetchCRLStart = data.FetchCRLStart
if data.ResponseAnnounceTimeout == 0 {
data.ResponseAnnounceTimeout = defaultResponseAnnounceTimeout
}
c.ResponseAnnounceTimeout = data.ResponseAnnounceTimeout
if data.ResponseDataTimeout == 0 {
data.ResponseDataTimeout = defaultResponseDataTimeout
}
c.ResponseDataTimeout = data.ResponseDataTimeout
if data.PublicCRLDirectory == "" {
data.PublicCRLDirectory = defaultFilesDirectory
}
c.PublicCRLDirectory = data.PublicCRLDirectory
if data.PublicCertificateDirectory == "" {
data.PublicCertificateDirectory = defaultFilesDirectory
}
c.PublicCertificateDirectory = data.PublicCRLDirectory
return nil
}
func New(clientConfigFile string) (*ClientConfig, error) {
configFile, err := os.Open(clientConfigFile)
if err != nil {
return nil, fmt.Errorf("could not open signer client configuration file %s: %w", clientConfigFile, err)
}
defer func() { _ = configFile.Close() }()
clientConfig, err := LoadConfiguration(configFile)
if err != nil {
return nil, fmt.Errorf("could not load client configuration from %s: %w", clientConfigFile, err)
}
return clientConfig, nil
}
func LoadConfiguration(r io.Reader) (*ClientConfig, error) {
var config *ClientConfig
decoder := yaml.NewDecoder(r)
err := decoder.Decode(&config)
if err != nil {
return nil, fmt.Errorf("could not parse YAML configuration: %w", err)
}
return config, nil
}