zero: fix bootstrap config path (#5035)

This commit is contained in:
Denis Mishin 2024-03-21 20:39:52 -04:00 committed by GitHub
parent 4193583301
commit d20e99ca8c
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
7 changed files with 36 additions and 33 deletions

View file

@ -20,7 +20,6 @@ import (
"github.com/pomerium/pomerium/internal/log" "github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/retry" "github.com/pomerium/pomerium/internal/retry"
sdk "github.com/pomerium/pomerium/internal/zero/api"
connect_mux "github.com/pomerium/pomerium/internal/zero/connect-mux" connect_mux "github.com/pomerium/pomerium/internal/zero/connect-mux"
) )
@ -34,17 +33,7 @@ const (
) )
// Run initializes the bootstrap config source // Run initializes the bootstrap config source
func (svc *Source) Run( func (svc *Source) Run(ctx context.Context) error {
ctx context.Context,
api *sdk.API,
fileCachePath string,
) error {
log.Ctx(ctx).Info().Str("bootstrap-config-path", fileCachePath).
Msg("initializing bootstrap config source")
svc.api = api
svc.fileCachePath = fileCachePath
svc.tryLoadFromFile(ctx) svc.tryLoadFromFile(ctx)
eg, ctx := errgroup.WithContext(ctx) eg, ctx := errgroup.WithContext(ctx)
@ -109,18 +98,27 @@ func (svc *Source) updateAndSave(ctx context.Context) error {
return fmt.Errorf("load bootstrap config from API: %w", err) return fmt.Errorf("load bootstrap config from API: %w", err)
} }
err = SaveBootstrapConfigToFile(cfg, svc.fileCachePath, svc.fileCipher) svc.UpdateBootstrap(ctx, *cfg)
if svc.fileCachePath == nil {
return nil
}
err = SaveBootstrapConfigToFile(cfg, *svc.fileCachePath, svc.fileCipher)
if err != nil { if err != nil {
log.Ctx(ctx).Error().Err(err). log.Ctx(ctx).Error().Err(err).
Msg("failed to save bootstrap config to file, note it may prevent Pomerium from starting up in case of connectivity issues") Msg("failed to save bootstrap config to file, note it may prevent Pomerium from starting up in case of connectivity issues")
} }
svc.UpdateBootstrap(ctx, *cfg)
return nil return nil
} }
func (svc *Source) tryLoadFromFile(ctx context.Context) { func (svc *Source) tryLoadFromFile(ctx context.Context) {
cfg, err := LoadBootstrapConfigFromFile(svc.fileCachePath, svc.fileCipher) if svc.fileCachePath == nil {
return
}
cfg, err := LoadBootstrapConfigFromFile(*svc.fileCachePath, svc.fileCipher)
if err != nil { if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("failed to load bootstrap config from file") log.Ctx(ctx).Error().Err(err).Msg("failed to load bootstrap config from file")
return return

View file

@ -25,7 +25,7 @@ type Source struct {
api *sdk.API api *sdk.API
fileCachePath string fileCachePath *string
fileCipher cipher.AEAD fileCipher cipher.AEAD
checkForUpdate chan struct{} checkForUpdate chan struct{}
@ -33,7 +33,7 @@ type Source struct {
} }
// New creates a new bootstrap config source // New creates a new bootstrap config source
func New(secret []byte) (*Source, error) { func New(secret []byte, fileCachePath *string, api *sdk.API) (*Source, error) {
cfg := new(config.Config) cfg := new(config.Config)
err := setConfigDefaults(cfg) err := setConfigDefaults(cfg)
@ -54,7 +54,9 @@ func New(secret []byte) (*Source, error) {
} }
svc := &Source{ svc := &Source{
api: api,
source: source{ready: make(chan struct{})}, source: source{ready: make(chan struct{})},
fileCachePath: fileCachePath,
fileCipher: cipher, fileCipher: cipher,
checkForUpdate: make(chan struct{}, 1), checkForUpdate: make(chan struct{}, 1),
} }

View file

@ -11,7 +11,7 @@ import (
func TestConfigDeterministic(t *testing.T) { func TestConfigDeterministic(t *testing.T) {
secret := []byte("secret") secret := []byte("secret")
src, err := bootstrap.New(secret) src, err := bootstrap.New(secret, nil, nil)
require.NoError(t, err) require.NoError(t, err)
cfg := src.GetConfig() cfg := src.GetConfig()
require.NotNil(t, cfg) require.NotNil(t, cfg)
@ -20,7 +20,7 @@ func TestConfigDeterministic(t *testing.T) {
require.NoError(t, cfg.Options.Validate()) require.NoError(t, cfg.Options.Validate())
// test that the config is deterministic // test that the config is deterministic
src2, err := bootstrap.New(secret) src2, err := bootstrap.New(secret, nil, nil)
require.NoError(t, err) require.NoError(t, err)
cfg2 := src2.GetConfig() cfg2 := src2.GetConfig()

View file

@ -18,7 +18,7 @@ func TestConfigChanges(t *testing.T) {
secret := []byte("secret") secret := []byte("secret")
src, err := bootstrap.New(secret) src, err := bootstrap.New(secret, nil, nil)
require.NoError(t, err) require.NoError(t, err)
ptr := func(s string) *string { return &s } ptr := func(s string) *string { return &s }

View file

@ -28,19 +28,22 @@ func Run(ctx context.Context, configFile string) error {
return errors.New("no token provided") return errors.New("no token provided")
} }
bootstrapConfigFileName, err := getBootstrapConfigFileName() opts := []controller.Option{
if err != nil {
return fmt.Errorf("error getting bootstrap config path: %w", err)
}
return controller.Run(
withInterrupt(ctx),
controller.WithAPIToken(token), controller.WithAPIToken(token),
controller.WithClusterAPIEndpoint(getClusterAPIEndpoint()), controller.WithClusterAPIEndpoint(getClusterAPIEndpoint()),
controller.WithConnectAPIEndpoint(getConnectAPIEndpoint()), controller.WithConnectAPIEndpoint(getConnectAPIEndpoint()),
controller.WithOTELAPIEndpoint(getOTELAPIEndpoint()), controller.WithOTELAPIEndpoint(getOTELAPIEndpoint()),
controller.WithBootstrapConfigFileName(bootstrapConfigFileName), }
)
bootstrapConfigFileName, err := getBootstrapConfigFileName()
if err != nil {
log.Ctx(ctx).Error().Err(err).Msg("would not be able to save cluster bootstrap config, that will prevent Pomerium from starting independent from the control plane")
} else {
log.Ctx(ctx).Info().Str("file", bootstrapConfigFileName).Msg("cluster bootstrap config path")
opts = append(opts, controller.WithBootstrapConfigFileName(bootstrapConfigFileName))
}
return controller.Run(withInterrupt(ctx), opts...)
} }
// IsManagedMode returns true if Pomerium should start in managed mode using this command. // IsManagedMode returns true if Pomerium should start in managed mode using this command.

View file

@ -12,7 +12,7 @@ type controllerConfig struct {
otelEndpoint string otelEndpoint string
tmpDir string tmpDir string
bootstrapConfigFileName string bootstrapConfigFileName *string
reconcilerLeaseDuration time.Duration reconcilerLeaseDuration time.Duration
databrokerRequestTimeout time.Duration databrokerRequestTimeout time.Duration
@ -56,7 +56,7 @@ func WithAPIToken(token string) Option {
// WithBootstrapConfigFileName sets the name of the file to store the bootstrap config in. // WithBootstrapConfigFileName sets the name of the file to store the bootstrap config in.
func WithBootstrapConfigFileName(name string) Option { func WithBootstrapConfigFileName(name string) Option {
return func(c *controllerConfig) { return func(c *controllerConfig) {
c.bootstrapConfigFileName = name c.bootstrapConfigFileName = &name
} }
} }

View file

@ -31,7 +31,7 @@ func Run(ctx context.Context, opts ...Option) error {
return fmt.Errorf("init api: %w", err) return fmt.Errorf("init api: %w", err)
} }
src, err := bootstrap.New([]byte(c.cfg.apiToken)) src, err := bootstrap.New([]byte(c.cfg.apiToken), c.cfg.bootstrapConfigFileName, c.api)
if err != nil { if err != nil {
return fmt.Errorf("error creating bootstrap config: %w", err) return fmt.Errorf("error creating bootstrap config: %w", err)
} }
@ -82,7 +82,7 @@ func (c *controller) runBootstrap(ctx context.Context) error {
ctx = log.WithContext(ctx, func(c zerolog.Context) zerolog.Context { ctx = log.WithContext(ctx, func(c zerolog.Context) zerolog.Context {
return c.Str("service", "zero-bootstrap") return c.Str("service", "zero-bootstrap")
}) })
return c.bootstrapConfig.Run(ctx, c.api, c.cfg.bootstrapConfigFileName) return c.bootstrapConfig.Run(ctx)
} }
func (c *controller) runPomeriumCore(ctx context.Context) error { func (c *controller) runPomeriumCore(ctx context.Context) error {