Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

cleanup: use common config loader to simplify code #125

Merged
merged 8 commits into from
Sep 23, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions client-lib/go/go.mod
Original file line number Diff line number Diff line change
@@ -4,8 +4,8 @@ go 1.18

require (
github.com/abcxyz/jvs v0.0.2-0.20220915004312-db26d444b9ae
github.com/abcxyz/pkg v0.0.0-20220719233420-62c7b76c10e9
github.com/google/go-cmp v0.5.8
github.com/abcxyz/pkg v0.0.0-20220923185910-81a29fe7ad35
github.com/google/go-cmp v0.5.9
github.com/hashicorp/go-multierror v1.1.1
github.com/lestrrat-go/jwx/v2 v2.0.6
github.com/sethvargo/go-envconfig v0.8.2
@@ -25,10 +25,10 @@ require (
github.com/lestrrat-go/option v1.0.0 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 // indirect
golang.org/x/net v0.0.0-20220921155015-db77216a4ee9 // indirect
golang.org/x/net v0.0.0-20220921203646-d300de134e69 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/genproto v0.0.0-20220920201722-2b89144ce006 // indirect
google.golang.org/genproto v0.0.0-20220921223823-23cae91e6737 // indirect
google.golang.org/grpc v1.49.0 // indirect
google.golang.org/protobuf v1.28.1 // indirect
gopkg.in/check.v1 v1.0.0-20180628173108-788fd7840127 // indirect
16 changes: 8 additions & 8 deletions client-lib/go/go.sum
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
github.com/abcxyz/pkg v0.0.0-20220719233420-62c7b76c10e9 h1:ROwNYAzdaFBjS9dTzmTjwgW84QaKQWxUFm3zlSbP77I=
github.com/abcxyz/pkg v0.0.0-20220719233420-62c7b76c10e9/go.mod h1:Sz/e+0vW0XIIeeIS4KDLIeh1Bb/CPAvQiZOdOTBiW4k=
github.com/abcxyz/pkg v0.0.0-20220923185910-81a29fe7ad35 h1:wirYJM8YVdiA9tnPuXO3/CSS4Lbb6XPUvjWylWDTuKI=
github.com/abcxyz/pkg v0.0.0-20220923185910-81a29fe7ad35/go.mod h1:ByiJv4X3q6AzjbXrgYBZx9iVMGfSK/EFND6lstF0RL8=
github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
github.com/davecgh/go-spew v1.1.1 h1:vj9j/u1bqnvCEfJOwUhtlOARqs3+rkHYY13jYWTU97c=
github.com/davecgh/go-spew v1.1.1/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38=
@@ -12,8 +12,8 @@ github.com/golang/protobuf v1.5.0/go.mod h1:FsONVRAS9T7sI+LIUmWTfcYkHO4aIWwzhcaS
github.com/golang/protobuf v1.5.2 h1:ROPKBNFfQgOUMifHyP+KYbvpjbdoFNs+aK7DXlji0Tw=
github.com/golang/protobuf v1.5.2/go.mod h1:XVQd3VNwM+JqD3oG2Ue2ip4fOMUkwXdXDdiuN0vRsmY=
github.com/google/go-cmp v0.5.5/go.mod h1:v8dTdLbMG2kIc/vJvl+f65V22dbkXbowE6jgT/gNBxE=
github.com/google/go-cmp v0.5.8 h1:e6P7q2lk1O+qJJb4BtCQXlK8vWEO8V1ZeuEdJNOqZyg=
github.com/google/go-cmp v0.5.8/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/google/go-cmp v0.5.9 h1:O2Tfq5qg4qc4AmwVlvv0oLiVAGB7enBSJ2x2DqQFi38=
github.com/google/go-cmp v0.5.9/go.mod h1:17dUlkBOakJ0+DkrSSNjCkIjxS6bF9zb3elmeNGIjoY=
github.com/hashicorp/errwrap v1.0.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
github.com/hashicorp/errwrap v1.1.0 h1:OxrOeh75EUXMY8TBjag2fzXGZ40LB6IKw45YeGUDY2I=
github.com/hashicorp/errwrap v1.1.0/go.mod h1:YH+1FKiLXxHSkmPseP+kNlulaMuP3n2brvKWEqk/Jc4=
@@ -52,8 +52,8 @@ golang.org/x/crypto v0.0.0-20220427172511-eb4f295cb31f/go.mod h1:IxCIyHEi3zRg3s0
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 h1:a5Yg6ylndHHYJqIPrdq0AhvR6KTvDTAvgBtaidhEevY=
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0/go.mod h1:IxCIyHEi3zRg3s0A5j5BB6A9Jmi73HwBIUl50j+osU4=
golang.org/x/net v0.0.0-20211112202133-69e39bad7dc2/go.mod h1:9nx3DQGgdP8bBQD5qxJ1jj9UTztislL4KSBs9R2vV5Y=
golang.org/x/net v0.0.0-20220921155015-db77216a4ee9 h1:SdDGdqRuKrF2R4XGcnPzcvZ63c/55GvhoHUus0o+BNI=
golang.org/x/net v0.0.0-20220921155015-db77216a4ee9/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/net v0.0.0-20220921203646-d300de134e69 h1:hUJpGDpnfwdJW8iNypFjmSY0sCBEL+spFTZ2eO+Sfps=
golang.org/x/net v0.0.0-20220921203646-d300de134e69/go.mod h1:YDH+HFinaLZZlnHAfSS6ZXJJ9M9t4Dl22yv3iI2vPwk=
golang.org/x/sys v0.0.0-20201119102817-f84b799fce68/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210423082822-04245dca01da/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs=
golang.org/x/sys v0.0.0-20210615035016-665e8c7367d1/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg=
@@ -65,8 +65,8 @@ golang.org/x/text v0.3.7 h1:olpwvP2KacW1ZWvsR7uQhoyTYvKAupfQrRGBFM352Gk=
golang.org/x/text v0.3.7/go.mod h1:u+2+/6zg+i71rQMx5EYifcz6MCKuco9NR6JIITiCfzQ=
golang.org/x/tools v0.0.0-20180917221912-90fa682c2a6e/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ=
golang.org/x/xerrors v0.0.0-20191204190536-9bdfabe68543/go.mod h1:I/5z698sn9Ka8TeJc9MKroUUfqBBauWjQqLJ2OPfmY0=
google.golang.org/genproto v0.0.0-20220920201722-2b89144ce006 h1:mmbq5q8M1t7dhkLw320YK4PsOXm6jdnUAkErImaIqOg=
google.golang.org/genproto v0.0.0-20220920201722-2b89144ce006/go.mod h1:ht8XFiar2npT/g4vkk7O0WYS1sHOHbdujxbEp7CJWbw=
google.golang.org/genproto v0.0.0-20220921223823-23cae91e6737 h1:K1zaaMdYBXRyX+cwFnxj7M6zwDyumLQMZ5xqwGvjreQ=
google.golang.org/genproto v0.0.0-20220921223823-23cae91e6737/go.mod h1:2r/26NEF3bFmT3eC3aZreahSal0C3Shl8Gi6vyDYqOQ=
google.golang.org/grpc v1.49.0 h1:WTLtQzmQori5FUH25Pq4WT22oCsv8USpQ+F6rqtsmxw=
google.golang.org/grpc v1.49.0/go.mod h1:ZgQEeidpAuNRZ8iRrlBKXZQP1ghovWIVhdJRyCDK+GI=
google.golang.org/protobuf v1.26.0-rc.1/go.mod h1:jlhhOSvTdKEhbULTjvd4ARK9grFBp09yW+WbY/TyQbw=
7 changes: 4 additions & 3 deletions cmd/cert-rotation/main.go
Original file line number Diff line number Diff line change
@@ -27,6 +27,7 @@ import (
kms "cloud.google.com/go/kms/apiv1"
"github.com/abcxyz/jvs/pkg/config"
"github.com/abcxyz/jvs/pkg/jvscrypto"
"github.com/abcxyz/pkg/cfgloader"
"github.com/abcxyz/pkg/logging"
"github.com/hashicorp/go-multierror"
"go.uber.org/zap"
@@ -83,14 +84,14 @@ func realMain(ctx context.Context) error {
}
defer kmsClient.Close()

config, err := config.LoadCryptoConfig(ctx, []byte{})
if err != nil {
var cfg config.CryptoConfig
if err := cfgloader.Load(ctx, cfg); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

handler := &jvscrypto.RotationHandler{
KMSClient: kmsClient,
CryptoConfig: config,
CryptoConfig: &cfg,
}

mux := http.NewServeMux()
9 changes: 5 additions & 4 deletions cmd/justification/main.go
Original file line number Diff line number Diff line change
@@ -25,6 +25,7 @@ import (
jvspb "github.com/abcxyz/jvs/apis/v0"
"github.com/abcxyz/jvs/pkg/config"
"github.com/abcxyz/jvs/pkg/justification"
"github.com/abcxyz/pkg/cfgloader"
"github.com/abcxyz/pkg/grpcutil"
"github.com/abcxyz/pkg/logging"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
@@ -54,9 +55,9 @@ func realMain(ctx context.Context) error {
otelgrpc.UnaryServerInterceptor(),
))

cfg, err := config.LoadJustificationConfig(ctx, []byte{})
if err != nil {
logger.Fatal("failed to load config", zap.Error(err))
var cfg config.JustificationConfig
if err := cfgloader.Load(ctx, &cfg); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

kmsClient, err := kms.NewKeyManagementClient(ctx)
@@ -69,7 +70,7 @@ func realMain(ctx context.Context) error {
return fmt.Errorf("failed to setup grpc auth handler: %w", err)
}

p := justification.NewProcessor(kmsClient, cfg, authHandler)
p := justification.NewProcessor(kmsClient, &cfg, authHandler)
jvsAgent := justification.NewJVSAgent(p)
jvspb.RegisterJVSServiceServer(s, jvsAgent)
reflection.Register(s)
7 changes: 4 additions & 3 deletions cmd/manual-cert-actions/main.go
Original file line number Diff line number Diff line change
@@ -26,6 +26,7 @@ import (
jvspb "github.com/abcxyz/jvs/apis/v0"
"github.com/abcxyz/jvs/pkg/config"
"github.com/abcxyz/jvs/pkg/jvscrypto"
"github.com/abcxyz/pkg/cfgloader"
"github.com/abcxyz/pkg/logging"
"go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc"
"go.uber.org/zap"
@@ -54,8 +55,8 @@ func realMain(ctx context.Context) error {
otelgrpc.UnaryServerInterceptor(),
))

cfg, err := config.LoadCryptoConfig(ctx, []byte{})
if err != nil {
var cfg config.CryptoConfig
if err := cfgloader.Load(ctx, cfg); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

@@ -66,7 +67,7 @@ func realMain(ctx context.Context) error {

handler := &jvscrypto.RotationHandler{
KMSClient: kmsClient,
CryptoConfig: cfg,
CryptoConfig: &cfg,
}

cas := &jvscrypto.CertificateActionService{
13 changes: 7 additions & 6 deletions cmd/public-key/main.go
Original file line number Diff line number Diff line change
@@ -28,6 +28,7 @@ import (
"github.com/abcxyz/jvs/pkg/config"
"github.com/abcxyz/jvs/pkg/jvscrypto"
"github.com/abcxyz/pkg/cache"
"github.com/abcxyz/pkg/cfgloader"
"github.com/abcxyz/pkg/logging"
"go.uber.org/zap"
)
@@ -57,26 +58,26 @@ func realMain(ctx context.Context) error {
}
defer kmsClient.Close()

config, err := config.LoadPublicKeyConfig(ctx, []byte{})
if err != nil {
var cfg config.PublicKeyConfig
if err := cfgloader.Load(ctx, &cfg); err != nil {
return fmt.Errorf("failed to load config: %w", err)
}

cache := cache.New[string](config.CacheTimeout)
cache := cache.New[string](cfg.CacheTimeout)

ks := &jvscrypto.KeyServer{
KMSClient: kmsClient,
PublicKeyConfig: config,
PublicKeyConfig: &cfg,
Cache: cache,
}

mux := http.NewServeMux()
mux.Handle("/.well-known/jwks", ks)

// Create the server and listen in a goroutine.
logger.Debug("starting server on port", zap.String("port", config.Port))
logger.Debug("starting server on port", zap.String("port", cfg.Port))
server := &http.Server{
Addr: ":" + config.Port,
Addr: ":" + cfg.Port,
Handler: mux,
ReadHeaderTimeout: 2 * time.Second,
}
32 changes: 17 additions & 15 deletions go.mod
Original file line number Diff line number Diff line change
@@ -4,39 +4,40 @@ go 1.18

require (
cloud.google.com/go/kms v1.4.0
github.com/abcxyz/pkg v0.0.0-20220719233420-62c7b76c10e9
github.com/abcxyz/pkg v0.0.0-20220923185910-81a29fe7ad35
github.com/golang/protobuf v1.5.2
github.com/google/go-cmp v0.5.8
github.com/google/go-cmp v0.5.9
github.com/google/uuid v1.3.0
github.com/hashicorp/go-multierror v1.1.1
github.com/lestrrat-go/jwx/v2 v2.0.6
github.com/mitchellh/mapstructure v1.5.0
github.com/sethvargo/go-envconfig v0.8.0
github.com/sethvargo/go-envconfig v0.8.2
github.com/sethvargo/go-gcpkms v0.1.0
github.com/sethvargo/go-retry v0.2.3
github.com/spf13/cobra v1.4.0
github.com/spf13/viper v1.12.0
go.opentelemetry.io/contrib/instrumentation/google.golang.org/grpc/otelgrpc v0.32.0
go.uber.org/zap v1.21.0
golang.org/x/oauth2 v0.0.0-20220524215830-622c5d57e401
go.uber.org/zap v1.23.0
golang.org/x/oauth2 v0.0.0-20220909003341-f21342109be1
golang.org/x/sync v0.0.0-20220601150217-0de741cfad7f
google.golang.org/api v0.82.0
google.golang.org/genproto v0.0.0-20220602131408-e326c6e8e9c8
google.golang.org/grpc v1.47.0
google.golang.org/protobuf v1.28.0
gopkg.in/yaml.v2 v2.4.0
google.golang.org/api v0.96.0
google.golang.org/genproto v0.0.0-20220921223823-23cae91e6737
google.golang.org/grpc v1.49.0
google.golang.org/protobuf v1.28.1
)

require (
cloud.google.com/go v0.102.0 // indirect
cloud.google.com/go/compute v1.6.1 // indirect
cloud.google.com/go v0.102.1 // indirect
cloud.google.com/go/compute v1.10.0 // indirect
cloud.google.com/go/iam v0.3.0 // indirect
github.com/benbjohnson/clock v1.3.0 // indirect
github.com/decred/dcrd/dcrec/secp256k1/v4 v4.1.0 // indirect
github.com/fsnotify/fsnotify v1.5.4 // indirect
github.com/go-logr/logr v1.2.3 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/goccy/go-json v0.9.11 // indirect
github.com/golang/groupcache v0.0.0-20210331224755-41bb18bfe9da // indirect
github.com/googleapis/enterprise-certificate-proxy v0.1.0 // indirect
github.com/googleapis/gax-go/v2 v2.4.0 // indirect
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/hcl v1.0.0 // indirect
@@ -57,13 +58,14 @@ require (
go.opencensus.io v0.23.0 // indirect
go.opentelemetry.io/otel v1.7.0 // indirect
go.opentelemetry.io/otel/trace v1.7.0 // indirect
go.uber.org/atomic v1.9.0 // indirect
go.uber.org/atomic v1.10.0 // indirect
go.uber.org/multierr v1.8.0 // indirect
golang.org/x/crypto v0.0.0-20220919173607-35f4265a4bc0 // indirect
golang.org/x/net v0.0.0-20220531201128-c960675eff93 // indirect
golang.org/x/sys v0.0.0-20220520151302-bc2c85ada10a // indirect
golang.org/x/net v0.0.0-20220921203646-d300de134e69 // indirect
golang.org/x/sys v0.0.0-20220919091848-fb04ddd9f9c8 // indirect
golang.org/x/text v0.3.7 // indirect
google.golang.org/appengine v1.6.7 // indirect
gopkg.in/ini.v1 v1.66.4 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
)
78 changes: 47 additions & 31 deletions go.sum

Large diffs are not rendered by default.

27 changes: 0 additions & 27 deletions pkg/config/crypto_config.go
Original file line number Diff line number Diff line change
@@ -16,13 +16,10 @@
package config

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-multierror"
"github.com/sethvargo/go-envconfig"
"gopkg.in/yaml.v2"
)

// CryptoConfigVersions is the list of allowed versions for the CryptoConfig.
@@ -92,27 +89,3 @@ func (cfg *CryptoConfig) RotationAge() time.Duration {
func (cfg *CryptoConfig) DestroyAge() time.Duration {
return cfg.KeyTTL + cfg.DisabledPeriod
}

// LoadConfig calls the necessary methods to load in config using the OsLookuper which finds env variables specified on the host.
func LoadCryptoConfig(ctx context.Context, b []byte) (*CryptoConfig, error) {
return loadCryptoConfigFromLookuper(ctx, b, envconfig.OsLookuper())
}

// loadConfigFromLooker reads in a yaml file, applies ENV config overrides from the lookuper, and finally validates the config.
func loadCryptoConfigFromLookuper(ctx context.Context, b []byte, lookuper envconfig.Lookuper) (*CryptoConfig, error) {
cfg := &CryptoConfig{}
if err := yaml.Unmarshal(b, cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
}

// Process overrides from env vars.
if err := envconfig.ProcessWith(ctx, cfg, lookuper); err != nil {
return nil, fmt.Errorf("failed to process environment: %w", err)
}

if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf("failed validating config: %w", err)
}

return cfg, nil
}
8 changes: 7 additions & 1 deletion pkg/config/crypto_config_test.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/abcxyz/pkg/cfgloader"
"github.com/abcxyz/pkg/testutil"
"github.com/google/go-cmp/cmp"
"github.com/sethvargo/go-envconfig"
@@ -211,10 +212,15 @@ propagation_delay: 1h

lookuper := envconfig.MapLookuper(tc.envs)
content := bytes.NewBufferString(tc.cfg).Bytes()
gotConfig, err := loadCryptoConfigFromLookuper(ctx, content, lookuper)
gotConfig := &CryptoConfig{}
err := cfgloader.Load(ctx, gotConfig,
cfgloader.WithLookuper(lookuper), cfgloader.WithYAML(content))
if diff := testutil.DiffErrString(err, tc.wantErr); diff != "" {
t.Errorf("Unexpected err: %s", diff)
}
if err != nil {
return
}
if diff := cmp.Diff(tc.wantConfig, gotConfig); diff != "" {
t.Errorf("Config unexpected diff (-want,+got):\n%s", diff)
}
27 changes: 0 additions & 27 deletions pkg/config/justification_config.go
Original file line number Diff line number Diff line change
@@ -16,13 +16,10 @@
package config

import (
"context"
"fmt"
"time"

"github.com/hashicorp/go-multierror"
"github.com/sethvargo/go-envconfig"
"gopkg.in/yaml.v2"
)

// JustificationConfigVersions is the list of allowed versions for the
@@ -63,27 +60,3 @@ func (cfg *JustificationConfig) Validate() error {
}
return err.ErrorOrNil()
}

// LoadJustificationConfig calls the necessary methods to load in config using the OsLookuper which finds env variables specified on the host.
func LoadJustificationConfig(ctx context.Context, b []byte) (*JustificationConfig, error) {
return loadJustificationConfigFromLookuper(ctx, b, envconfig.OsLookuper())
}

// loadConfigFromLooker reads in a yaml file, applies ENV config overrides from the lookuper, and finally validates the config.
func loadJustificationConfigFromLookuper(ctx context.Context, b []byte, lookuper envconfig.Lookuper) (*JustificationConfig, error) {
cfg := &JustificationConfig{}
if err := yaml.Unmarshal(b, cfg); err != nil {
return nil, err
}

// Process overrides from env vars.
if err := envconfig.ProcessWith(ctx, cfg, lookuper); err != nil {
return nil, err
}

if err := cfg.Validate(); err != nil {
return nil, fmt.Errorf("failed validating config: %w", err)
}

return cfg, nil
}
8 changes: 7 additions & 1 deletion pkg/config/justification_config_test.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/abcxyz/pkg/cfgloader"
"github.com/abcxyz/pkg/testutil"
"github.com/google/go-cmp/cmp"
"github.com/sethvargo/go-envconfig"
@@ -130,10 +131,15 @@ issuer: jvs

lookuper := envconfig.MapLookuper(tc.envs)
content := bytes.NewBufferString(tc.cfg).Bytes()
gotConfig, err := loadJustificationConfigFromLookuper(ctx, content, lookuper)
gotConfig := &JustificationConfig{}
err := cfgloader.Load(ctx, gotConfig,
cfgloader.WithLookuper(lookuper), cfgloader.WithYAML(content))
if diff := testutil.DiffErrString(err, tc.wantErr); diff != "" {
t.Errorf("Unexpected err: %s", diff)
}
if err != nil {
return
}
if diff := cmp.Diff(tc.wantConfig, gotConfig); diff != "" {
t.Errorf("Config unexpected diff (-want,+got):\n%s", diff)
}
26 changes: 0 additions & 26 deletions pkg/config/public_key_config.go
Original file line number Diff line number Diff line change
@@ -15,12 +15,7 @@
package config

import (
"context"
"fmt"
"time"

"github.com/sethvargo/go-envconfig"
"gopkg.in/yaml.v2"
)

// PublicKeyConfig is the config used for public key hosting.
@@ -33,24 +28,3 @@ type PublicKeyConfig struct {
CacheTimeout time.Duration `yaml:"cache_timeout" env:"CACHE_TIMEOUT"`
Port string `env:"PORT,default=8080"`
}

// LoadConfig calls the necessary methods to load in config using the OsLookuper which finds env variables specified on the host.
func LoadPublicKeyConfig(ctx context.Context, b []byte) (*PublicKeyConfig, error) {
return loadPublicKeyConfigFromLookuper(ctx, b, envconfig.OsLookuper())
}

// loadConfigFromLooker reads in a yaml file, applies ENV config overrides from the lookuper, and finally validates the config.
func loadPublicKeyConfigFromLookuper(ctx context.Context, b []byte, lookuper envconfig.Lookuper) (*PublicKeyConfig, error) {
cfg := &PublicKeyConfig{}
if err := yaml.Unmarshal(b, cfg); err != nil {
return nil, fmt.Errorf("failed to unmarshal yaml: %w", err)
}

// Process overrides from env vars.
l := envconfig.PrefixLookuper("", lookuper)
if err := envconfig.ProcessWith(ctx, cfg, l); err != nil {
return nil, fmt.Errorf("failed to process environment: %w", err)
}

return cfg, nil
}
13 changes: 9 additions & 4 deletions pkg/config/public_key_config_test.go
Original file line number Diff line number Diff line change
@@ -20,6 +20,7 @@ import (
"testing"
"time"

"github.com/abcxyz/pkg/cfgloader"
"github.com/google/go-cmp/cmp"
"github.com/sethvargo/go-envconfig"
)
@@ -35,9 +36,11 @@ func TestLoadPublicKeyConfig(t *testing.T) {

lookuper := envconfig.MapLookuper(envs)
content := bytes.NewBufferString("").Bytes()
gotConfig, err := loadPublicKeyConfigFromLookuper(ctx, content, lookuper)
gotConfig := &PublicKeyConfig{}
err := cfgloader.Load(ctx, gotConfig,
cfgloader.WithLookuper(lookuper), cfgloader.WithYAML(content))
if err != nil {
t.Error(err)
t.Fatal(err)
}

wantConfig := &PublicKeyConfig{
@@ -60,9 +63,11 @@ func TestLoadPublicKeyConfig_Default(t *testing.T) {

lookuper := envconfig.MapLookuper(envs)
content := bytes.NewBufferString("").Bytes()
gotConfig, err := loadPublicKeyConfigFromLookuper(ctx, content, lookuper)
gotConfig := &PublicKeyConfig{}
err := cfgloader.Load(ctx, gotConfig,
cfgloader.WithLookuper(lookuper), cfgloader.WithYAML(content))
if err != nil {
t.Error(err)
t.Fatal(err)
}

wantConfig := &PublicKeyConfig{