pomerium/internal/zero/cmd/run.go
Joe Kralicky 0e13248685
Core-Zero Import (#5288)
* initial core-zero import implementation

* Update /config/import openapi description and use PUT instead of POST

* update import ui tests

* Add 413 as a possible response for /config/import

* Options/Settings type conversion tests and related bugfixes

* Fixes for proto type conversion and tests

* Update core-zero import client

* Update core-zero import client

* Update import api and environment detection

* update go.mod

* remove old testdata

* Remove usage of deleted setting after merge

* remove extra newline from --version output
2024-10-09 18:51:56 -04:00

88 lines
2.3 KiB
Go

// Package cmd implements the pomerium zero command.
package cmd
import (
"context"
"errors"
"fmt"
"os"
"os/signal"
"syscall"
"github.com/rs/zerolog"
"github.com/pomerium/pomerium/internal/log"
"github.com/pomerium/pomerium/internal/zero/controller"
)
// Run runs the pomerium zero command.
func Run(ctx context.Context, configFile string) error {
err := setupLogger()
if err != nil {
return fmt.Errorf("error setting up logger: %w", err)
}
token := getToken(configFile)
if token == "" {
return errors.New("no token provided")
}
opts := []controller.Option{
controller.WithAPIToken(token),
controller.WithClusterAPIEndpoint(getClusterAPIEndpoint()),
controller.WithConnectAPIEndpoint(getConnectAPIEndpoint()),
controller.WithOTELAPIEndpoint(getOTELAPIEndpoint()),
}
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))
if uri := getBootstrapConfigWritebackURI(); uri != "" {
log.Ctx(ctx).Debug().Str("uri", uri).Msg("cluster bootstrap config writeback URI")
opts = append(opts, controller.WithBootstrapConfigWritebackURI(uri))
}
}
return controller.Run(withInterrupt(ctx), opts...)
}
// IsManagedMode returns true if Pomerium should start in managed mode using this command.
func IsManagedMode(configFile string) bool {
return getToken(configFile) != ""
}
func withInterrupt(ctx context.Context) context.Context {
ctx, cancel := context.WithCancelCause(ctx)
go func(ctx context.Context) {
defer cancel(context.Canceled)
ch := make(chan os.Signal, 2)
defer signal.Stop(ch)
signal.Notify(ch, os.Interrupt)
signal.Notify(ch, syscall.SIGTERM)
select {
case sig := <-ch:
cancel(fmt.Errorf("received signal: %s", sig))
case <-ctx.Done():
}
}(ctx)
return ctx
}
func setupLogger() error {
if rawLvl, ok := os.LookupEnv("LOG_LEVEL"); ok {
lvl, err := zerolog.ParseLevel(rawLvl)
if err != nil {
return err
}
log.SetLevel(lvl)
}
return nil
}