cacert-gosignerclient/internal/config/config.go
Jan Dittberner da17fb69d7 Implement CRL and Health response handling
- add callback support to client and handler
- implement support for updating the CA certificates and profiles from
  health data of the signer
- implement CRL retrieval from the signer including delta CRL support
- implement error response handling
- add configurable start and interval timers for health and CRL data
2022-11-30 18:56:57 +01:00

168 lines
4.4 KiB
Go

/*
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.Second
defaultResponseAnnounceTimeout = 30 * time.Second
defaultResponseDataTimeout = 2 * time.Second
defaultCRLDirectory = "crls"
)
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"`
CRLDirectory string `yaml:"crl-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"`
CRLDirectory string `yaml:"crl-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.CRLDirectory == "" {
data.CRLDirectory = defaultCRLDirectory
}
c.CRLDirectory = data.CRLDirectory
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
}