2020-08-31 17:12:17 +00:00
|
|
|
package autoconf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
2023-02-17 21:14:46 +00:00
|
|
|
"github.com/hashicorp/consul/proto/private/pbautoconf"
|
2023-01-11 14:39:10 +00:00
|
|
|
"google.golang.org/protobuf/encoding/protojson"
|
2020-08-31 17:12:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// autoConfigFileName is the name of the file that the agent auto-config settings are
|
|
|
|
// stored in within the data directory
|
|
|
|
autoConfigFileName = "auto-config.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2023-01-11 14:39:10 +00:00
|
|
|
pbMarshaler = &protojson.MarshalOptions{
|
|
|
|
UseProtoNames: false,
|
|
|
|
UseEnumNumbers: false,
|
|
|
|
Indent: " ",
|
|
|
|
EmitUnpopulated: true,
|
2020-08-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
|
2023-01-11 14:39:10 +00:00
|
|
|
pbUnmarshaler = &protojson.UnmarshalOptions{
|
|
|
|
DiscardUnknown: false,
|
2020-08-31 17:12:17 +00:00
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func (ac *AutoConfig) readPersistedAutoConfig() (*pbautoconf.AutoConfigResponse, error) {
|
|
|
|
if ac.config.DataDir == "" {
|
|
|
|
// no data directory means we don't have anything to potentially load
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(ac.config.DataDir, autoConfigFileName)
|
|
|
|
ac.logger.Debug("attempting to restore any persisted configuration", "path", path)
|
|
|
|
|
2022-11-10 16:26:01 +00:00
|
|
|
content, err := os.ReadFile(path)
|
2020-08-31 17:12:17 +00:00
|
|
|
if err == nil {
|
|
|
|
var resp pbautoconf.AutoConfigResponse
|
2023-01-11 14:39:10 +00:00
|
|
|
if err := pbUnmarshaler.Unmarshal(content, &resp); err != nil {
|
2020-08-31 17:12:17 +00:00
|
|
|
return nil, fmt.Errorf("failed to decode persisted auto-config data: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.logger.Info("read persisted configuration", "path", path)
|
|
|
|
return &resp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if !os.IsNotExist(err) {
|
|
|
|
return nil, fmt.Errorf("failed to load %s: %w", path, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// ignore non-existence errors as that is an indicator that we haven't
|
|
|
|
// performed the auto configuration before
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (ac *AutoConfig) persistAutoConfig(resp *pbautoconf.AutoConfigResponse) error {
|
|
|
|
// now that we know the configuration is generally fine including TLS certs go ahead and persist it to disk.
|
|
|
|
if ac.config.DataDir == "" {
|
|
|
|
ac.logger.Debug("not persisting auto-config settings because there is no data directory")
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-01-11 14:39:10 +00:00
|
|
|
serialized, err := pbMarshaler.Marshal(resp)
|
2020-08-31 17:12:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to encode auto-config response as JSON: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
path := filepath.Join(ac.config.DataDir, autoConfigFileName)
|
|
|
|
|
2023-01-11 14:39:10 +00:00
|
|
|
err = os.WriteFile(path, serialized, 0660)
|
2020-08-31 17:12:17 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("failed to write auto-config configurations: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ac.logger.Debug("auto-config settings were persisted to disk")
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|