...

Source file src/github.com/sigstore/timestamp-authority/pkg/ntpmonitor/config.go

Documentation: github.com/sigstore/timestamp-authority/pkg/ntpmonitor

     1  // Copyright 2022 The Sigstore Authors.
     2  //
     3  // Licensed under the Apache License, Version 2.0 (the "License");
     4  // you may not use this file except in compliance with the License.
     5  // You may obtain a copy of the License at
     6  //
     7  //     http://www.apache.org/licenses/LICENSE-2.0
     8  //
     9  // Unless required by applicable law or agreed to in writing, software
    10  // distributed under the License is distributed on an "AS IS" BASIS,
    11  // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    12  // See the License for the specific language governing permissions and
    13  // limitations under the License.
    14  
    15  package ntpmonitor
    16  
    17  import (
    18  	// a blank import is recommended by the Go docs
    19  	// when using embed with byte slices
    20  	_ "embed"
    21  	"fmt"
    22  	"os"
    23  
    24  	"gopkg.in/yaml.v3"
    25  )
    26  
    27  //go:embed ntpsync.yaml
    28  var defaultConfigData []byte
    29  
    30  // Config holds the configuration for a NTPMonitor
    31  type Config struct {
    32  	RequestAttempts int      `yaml:"request_attempts"`
    33  	RequestTimeout  int      `yaml:"request_timeout"`
    34  	NumServers      int      `yaml:"num_servers"`
    35  	MaxTimeDelta    int      `yaml:"max_time_delta"`
    36  	ServerThreshold int      `yaml:"server_threshold"`
    37  	Period          int      `yaml:"period"`
    38  	Servers         []string `yaml:"servers"`
    39  }
    40  
    41  // LoadConfig reads a yaml file from a provided path, instantiating a new
    42  // Config object with the vales found. No sanity checking is made of the
    43  // loaded values.
    44  func LoadConfig(path string) (*Config, error) {
    45  	var configData []byte
    46  	if path == "" {
    47  		configData = defaultConfigData
    48  	} else {
    49  		data, err := os.ReadFile(path)
    50  		if err != nil {
    51  			return nil, fmt.Errorf("failed to read file: %s %w",
    52  				path, err)
    53  		}
    54  		configData = data
    55  	}
    56  
    57  	var cfg Config
    58  	if err := yaml.Unmarshal(configData, &cfg); err != nil {
    59  		return nil, fmt.Errorf("failed to parse YAML: %w", err)
    60  	}
    61  
    62  	return &cfg, nil
    63  }
    64  

View as plain text