Skip to content

Commit

Permalink
Fix config and clean up interface
Browse files Browse the repository at this point in the history
  • Loading branch information
adambabik committed Sep 30, 2024
1 parent e0913b5 commit 2007816
Show file tree
Hide file tree
Showing 10 changed files with 163 additions and 123 deletions.
2 changes: 1 addition & 1 deletion internal/cmd/beta/server/grpcurl_utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ func getDescriptorSource(ctx context.Context, cfg *config.Config) (grpcurl.Descr
}

func dialServer(ctx context.Context, cfg *config.Config) (*grpc.ClientConn, error) {
tlsConf, err := runmetls.LoadClientConfig(cfg.Server.Tls.CertFile, cfg.Server.Tls.KeyFile)
tlsConf, err := runmetls.LoadClientConfig(*cfg.Server.Tls.CertFile, *cfg.Server.Tls.KeyFile)
if err != nil {
return nil, err
}
Expand Down
4 changes: 2 additions & 2 deletions internal/cmd/beta/server/server_start_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ func serverStartCmd() *cobra.Command {

serverCfg := &server.Config{
Address: cfg.Server.Address,
CertFile: cfg.Server.Tls.CertFile,
KeyFile: cfg.Server.Tls.KeyFile,
CertFile: *cfg.Server.Tls.CertFile, // guaranteed by autoconfig
KeyFile: *cfg.Server.Tls.KeyFile, // guaranteed by autoconfig
TLSEnabled: cfg.Server.Tls.Enabled,
}

Expand Down
15 changes: 9 additions & 6 deletions internal/config/autoconfig/autoconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,7 +85,8 @@ func getClient(cfg *config.Config, logger *zap.Logger) (*runnerv2client.Client,
var opts []grpc.DialOption

if cfg.Server.Tls != nil && cfg.Server.Tls.Enabled {
tlsConfig, err := runmetls.LoadClientConfig(cfg.Server.Tls.CertFile, cfg.Server.Tls.KeyFile)
// It's ok to dereference TLS fields because they are checked in [getRootConfig].
tlsConfig, err := runmetls.LoadClientConfig(*cfg.Server.Tls.CertFile, *cfg.Server.Tls.KeyFile)
if err != nil {
return nil, errors.WithStack(err)
}
Expand Down Expand Up @@ -268,7 +269,7 @@ func getRootConfig(cfgLoader *config.Loader, userCfgDir UserConfigDir) (*config.
items, err := cfgLoader.RootConfigs()
switch err {
case nil:
if cfg, err = config.ParseMultipleYAMLs(items...); err != nil {
if cfg, err = config.ParseYAML(items...); err != nil {
return nil, err
}
case config.ErrRootConfigNotFound:
Expand All @@ -280,11 +281,13 @@ func getRootConfig(cfgLoader *config.Loader, userCfgDir UserConfigDir) (*config.
if cfg.Server != nil && cfg.Server.Tls != nil && cfg.Server.Tls.Enabled {
tls := cfg.Server.Tls

if tls.CertFile == "" {
tls.CertFile = filepath.Join(string(userCfgDir), "runme", "tls", "cert.pem")
if tls.CertFile == nil {
path := filepath.Join(string(userCfgDir), "runme", "tls", "cert.pem")
tls.CertFile = &path
}
if tls.KeyFile == "" {
tls.KeyFile = filepath.Join(string(userCfgDir), "runme", "tls", "key.pem")
if tls.KeyFile == nil {
path := filepath.Join(string(userCfgDir), "runme", "tls", "key.pem")
tls.KeyFile = &path
}
}

Expand Down
65 changes: 54 additions & 11 deletions internal/config/config.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
package config

import (
_ "embed"
"encoding/json"
"os"
"path/filepath"
"strings"
Expand All @@ -9,15 +11,35 @@ import (
"gopkg.in/yaml.v3"
)

func ParseYAML(data []byte) (*Config, error) {
return ParseMultipleYAMLs(data)
}
//go:embed runme.default.yaml
var defaultRunmeYAML []byte

func ParseMultipleYAMLs(items ...[]byte) (*Config, error) {
if len(items) == 0 {
return nil, errors.New("no configuration items")
func init() {
// Ensure the default configuration is valid.
_, err := newDefault()
if err != nil {
panic(err)
}
}

func newDefault() (*Config, error) {
return ParseYAML(defaultRunmeYAML)
}

func Default() *Config {
cfg, _ := newDefault()
return cfg
}

// ParseYAML parses the given YAML items and returns a configuration object.
// Multiple items are merged into a single configuration. It uses a default
// configuration as a base.
func ParseYAML(items ...[]byte) (*Config, error) {
items = append([][]byte{defaultRunmeYAML}, items...)
return parseYAML(items...)
}

func parseYAML(items ...[]byte) (*Config, error) {
version, err := parseVersionFromYAML(items[0])
if err != nil {
return nil, err
Expand Down Expand Up @@ -64,15 +86,36 @@ func parseVersionFromYAML(data []byte) (string, error) {
return result.Version, nil
}

// parseAndMergeV1alpha1 parses items, which are raw YAML blobs,
// one-by-one into a single map. Then, marshals the map into raw JSON.
// Finally, unmarshals the JSON into a [Config] object.
// Double unmarshaling is required to take advantage of the
// auto-generated [Config.UnmarshalJSON] method which does
// validation.
func parseAndMergeV1alpha1(items ...[]byte) (*Config, error) {
var config Config
m := make(map[string]interface{})

for _, data := range items {
if err := yaml.Unmarshal(data, &config); err != nil {
return nil, errors.Wrap(err, "failed to unmarshal yaml")
if err := yaml.Unmarshal(data, &m); err != nil {
return nil, errors.Wrap(err, "failed to parse v1alpha1 config")
}
}

flatten, err := json.Marshal(m)
if err != nil {
return nil, errors.Wrap(err, "failed to parse v1alpha1 config")
}

var config Config

if err := json.Unmarshal(flatten, &config); err != nil {
return nil, errors.Wrap(err, "failed to parse v1alpha1 config")
}

if err := validateConfig(&config); err != nil {
return nil, errors.Wrap(err, "failed to validate v1alpha1 config")
}

return &config, nil
}

Expand All @@ -83,11 +126,11 @@ func validateConfig(cfg *Config) error {
}

if err := validateInsideCwd(cfg.Project.Root, cwd); err != nil {
return errors.Wrap(err, "failed to validate project dir")
return errors.Wrap(err, "project.root")
}

if err := validateInsideCwd(cfg.Project.Filename, cwd); err != nil {
return errors.Wrap(err, "failed to validate filename")
return errors.Wrap(err, "project.filename")
}

return nil
Expand Down
7 changes: 3 additions & 4 deletions internal/config/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,8 @@
"type": "object",
"properties": {
"use_system_env": {
"type": "boolean"
"type": "boolean",
"default": false
},
"sources": {
"type": "array",
Expand Down Expand Up @@ -129,9 +130,7 @@
}
},
"required": [
"enabled",
"cert_file",
"key_file"
"enabled"
]
}
},
Expand Down
23 changes: 0 additions & 23 deletions internal/config/config_defaults.go

This file was deleted.

22 changes: 0 additions & 22 deletions internal/config/config_defaults_test.go

This file was deleted.

56 changes: 47 additions & 9 deletions internal/config/config_schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

Loading

0 comments on commit 2007816

Please sign in to comment.