cacert-gosignerclient/internal/config/config.go
Jan Dittberner f3c0e1379f Improve robustness and concurrency handling
- Rename client.CertInfo to CACertificateInfo
- declare commands channel inside client.Run, there is no need to inject it
  from the outside
- let command generating code in client.commandLoop run in goroutines to
  allow parallel handling of queued commands and avoid blocking operations
- pass context to command generating functions to allow cancellation
- guard access to c.knownCACertificates by mutex.Lock and mutex.Unlock
- make command channel capacity configurable
- update to latest cacert-gosigner dependency for channel direction support
- improve handling of closed input channel
- reduce client initialization to serial connection setup, move callback and
  handler parameters to client.Run invocation
2022-12-04 14:20:34 +01:00

185 lines
5.2 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.Minute
defaultResponseAnnounceTimeout = 30 * time.Second
defaultResponseDataTimeout = 2 * time.Second
defaultFilesDirectory = "public"
defaultCommandChannelCapacity = 100
)
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"`
CommandChannelCapacity int `yaml:"command-channel-capacity"`
}
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"`
CommandChannelCapacity int `yaml:"command-channel-capacity"`
}{}
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
}
if data.CommandChannelCapacity == 0 {
data.CommandChannelCapacity = defaultCommandChannelCapacity
}
c.CommandChannelCapacity = data.CommandChannelCapacity
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
}