Files

535 lines
14 KiB
Go

package ctl
import (
"cmp"
"context"
"fmt"
"io"
"os"
"path/filepath"
"strings"
"time"
"github.com/alecthomas/kong"
"github.com/cockroachdb/errors"
"github.com/effective-security/porto/pkg/retriable"
"github.com/effective-security/porto/pkg/rpcclient"
"github.com/effective-security/porto/pkg/tlsconfig"
"github.com/effective-security/porto/xhttp/correlation"
"github.com/effective-security/trustyca/api/pb"
"github.com/effective-security/trustyca/api/pb/proxypb"
"github.com/effective-security/trustyca/api/version"
"github.com/effective-security/trustyca/privpb"
privproxypb "github.com/effective-security/trustyca/privpb/proxypb"
"github.com/effective-security/x/configloader"
"github.com/effective-security/x/ctl"
"github.com/effective-security/x/fileutil/resolve"
"github.com/effective-security/x/format"
"github.com/effective-security/x/print"
"github.com/effective-security/xlog"
)
var logger = xlog.NewPackageLogger("github.com/effective-security/trustyca/pkg", "ctl")
var (
// DefaultStoragePath specifies default storage path
DefaultStoragePath = "~/.config/trustyca"
ServerAlias = map[string]string{
"local": "https://localhost:8880",
"dev": "https://wfe.dev.trustyca.io",
"prod": "https://wfe.prod.trustyca.io",
}
)
// Cli provides CLI context to run commands
type Cli struct {
Server string `short:"s" help:"Address of the remote server to connect. Use TRUSTYCA_SERVER environment to override"`
Debug bool `short:"D" help:"Enable debug mode"`
Version ctl.VersionFlag `name:"version" help:"Print version information and quit" hidden:""`
O string `help:"Print output format: json|yaml"`
Cfg string `help:"Configuration file" default:"~/.config/trustyca/config.yaml"`
Storage string `help:"flag specifies to override default location: ~/.config/trustyca. Use TRUSTYCA_STORAGE environment to override"`
HTTP bool `short:"H" help:"Use HTTP client"`
//STS bool `help:"Use STS Caller Identity authentication"`
TimeFormat string `name:"time" help:"Print time format: utc|local|ago" hidden:"" default:"utc"`
Timeout int `help:"Connection timeout" default:"6"`
Cert string `short:"c" help:"Client certificate file for mTLS"`
CertKey string `short:"k" help:"Client certificate key for mTLS"`
TrustedCA string `short:"r" help:"Trusted CA store for server TLS"`
// Stdin is the source to read from, typically set to os.Stdin
stdin io.Reader
// Output is the destination for all output from the command, typically set to os.Stdout
output io.Writer
// ErrOutput is the destination for errors.
// If not set, errors will be written to os.StdError
errOutput io.Writer
rpcClient *rpcclient.Client
httpClient *retriable.Client
ctx context.Context
}
// Context for requests
func (c *Cli) Context() context.Context {
if c.ctx == nil {
c.ctx = correlation.WithMetaFromContext(context.Background())
logger.ContextKV(c.ctx, xlog.DEBUG, "context", "created")
}
return c.ctx
}
// IsJSON returns true if the output format us JSON
func (c *Cli) IsJSON() bool {
return c.O == "json"
}
// Reader is the source to read from, typically set to os.Stdin
func (c *Cli) Reader() io.Reader {
if c.stdin != nil {
return c.stdin
}
return os.Stdin
}
// WithReader allows to specify a custom reader
func (c *Cli) WithReader(reader io.Reader) *Cli {
c.stdin = reader
return c
}
// Writer returns a writer for control output
func (c *Cli) Writer() io.Writer {
if c.output != nil {
return c.output
}
return os.Stdout
}
// WithWriter allows to specify a custom writer
func (c *Cli) WithWriter(out io.Writer) *Cli {
c.output = out
return c
}
// ErrWriter returns a writer for control output
func (c *Cli) ErrWriter() io.Writer {
if c.errOutput != nil {
return c.errOutput
}
return os.Stderr
}
// WithErrWriter allows to specify a custom error writer
func (c *Cli) WithErrWriter(out io.Writer) *Cli {
c.errOutput = out
return c
}
// // WithHTTPClient allows to specify an http client
// func (c *Cli) WithHTTPClient(httpClient *httpclient.Client) *Cli {
// c.httpClient = httpClient
// return c
// }
// AfterApply hook loads config. It sets the process-wide log formatter, log
// level and time format, so two CLIs must not be parsed concurrently in one
// process.
func (c *Cli) AfterApply(app *kong.Kong, vars kong.Vars) error {
xlog.SetFormatter(xlog.NewPrettyFormatter(c.ErrWriter()))
if c.Debug {
xlog.SetGlobalLogLevel(xlog.DEBUG)
} else {
xlog.SetGlobalLogLevel(xlog.ERROR)
}
if c.CertKey != "" && c.Cert == "" {
return errors.Errorf("--cert-key option specified without --cert")
}
if c.Server != "" && ServerAlias[c.Server] != "" {
c.Server = ServerAlias[c.Server]
}
format.DefaultTimePrintFormat = c.TimeFormat
pb.RegisterPrintOnce()
return nil
}
/*
type callerIdentity struct {
sts awsprov.STS
}
func (c *callerIdentity) GetCallerIdentity(ctx context.Context) (*credentials.Token, error) {
return c.sts.PresignGetCallerIdentity(ctx, 5*time.Minute, nil)
}
// NewCallerIdentity returns CallerIdentity
func NewCallerIdentity() (credentials.CallerIdentity, error) {
key := os.Getenv("AWS_ACCESS_KEY_ID")
secret := os.Getenv("AWS_SECRET_ACCESS_KEY")
session := os.Getenv("AWS_SESSION_TOKEN")
region := cmp.Or(os.Getenv("AWS_DEFAULT_REGION"), os.Getenv("AWS_REGION"))
if key == "" || secret == "" {
return nil, errors.New("AWS_ACCESS_KEY_ID and AWS_SECRET_KEY must be set to use --sts flag")
}
if region == "" {
return nil, errors.New("AWS_DEFAULT_REGION or AWS_REGION must be set to use --sts flag")
}
cfg := &aws.Config{
Region: region,
Credentials: awscreds.NewStaticCredentialsProvider(key, secret, session),
}
sts := awsprov.NewSTS(cfg)
return &callerIdentity{sts: sts}, nil
}
*/
const defaultConfig = `
---
clients:
local_wfe:
host: https://localhost:8880
tls:
trusted_ca: ~/.trustyca/certs/trustyca_root.pem
request:
retry_limit: 3
timeout: 6s
storage_folder: ~/.trustyca
remote_wfe_dev:
host: https://wfe.dev.trustyca.io
request:
retry_limit: 3
timeout: 6s
storage_folder: ~/.trustyca
remote_wfe_prod:
host: https://wfe.prod.trustyca.io
request:
retry_limit: 3
timeout: 6s
storage_folder: ~/.trustyca
`
func (c *Cli) configFileName() string {
cfg := c.Cfg
if cfg == "" {
storage := cmp.Or(
c.Storage,
os.Getenv("TRUSTYCA_STORAGE"),
DefaultStoragePath,
)
cfg = filepath.Join(storage, "config.yaml")
}
return resolve.ExpandPath(cfg)
}
func (c *Cli) ensureConfigFile() (string, error) {
fn := c.configFileName()
fi, err := os.Stat(fn)
if err != nil {
// if not found, create it
if os.IsNotExist(err) {
dir := filepath.Dir(fn)
if err := os.MkdirAll(dir, 0755); err != nil {
return fn, errors.WithMessagef(err, "unable to create config directory: %s", dir)
}
err = os.WriteFile(fn, []byte(defaultConfig), 0644)
if err != nil {
return fn, errors.WithMessagef(err, "unable to create config file: %s", fn)
}
}
return fn, err
}
if fi.IsDir() {
return fn, errors.Newf("config file %s is a directory", fn)
}
return fn, nil
}
// RPCClient returns gRPC client
func (c *Cli) RPCClient(skipAuth bool) (*rpcclient.Client, error) {
if c.rpcClient != nil {
return c.rpcClient, nil
}
host := cmp.Or(c.Server, os.Getenv("TRUSTYCA_SERVER"))
if host == "" {
return nil, errors.New("no server specified. Use -s flag or TRUSTYCA_SERVER env var")
}
if ServerAlias[host] != "" {
host = ServerAlias[host]
}
var err error
// a host the config file does not list, such as the backend listener,
// keeps its token and DPoP keys in the storage folder too
storage := cmp.Or(
c.Storage,
os.Getenv("TRUSTYCA_STORAGE"),
DefaultStoragePath,
)
timeout := time.Duration(c.Timeout) * time.Second
clientCfg := &rpcclient.Config{
DialTimeout: timeout,
DialKeepAliveTimeout: timeout,
DialKeepAliveTime: timeout,
Endpoint: host,
UserAgent: fmt.Sprintf("trustycactl %s", version.Current().String()),
StorageFolder: resolve.ExpandPath(storage),
}
if strings.HasPrefix(host, "https://") {
cert, key, ca := resolve.ExpandPath(c.Cert), resolve.ExpandPath(c.CertKey), resolve.ExpandPath(c.TrustedCA)
cfg, err := c.ensureConfigFile()
if err != nil {
return nil, err
}
f, err := retriable.LoadFactory(cfg)
if err == nil {
rc := f.ConfigForHost(host)
if rc != nil {
storage := cmp.Or(
c.Storage,
os.Getenv("TRUSTYCA_STORAGE"),
rc.StorageFolder,
DefaultStoragePath,
)
clientCfg.StorageFolder = resolve.ExpandPath(storage)
if rc.TLS != nil {
if cert == "" {
cert = resolve.ExpandPath(rc.TLS.CertFile)
key = resolve.ExpandPath(rc.TLS.KeyFile)
}
if ca == "" {
ca = resolve.ExpandPath(rc.TLS.TrustedCAFile)
}
}
}
}
if cert != "" || ca != "" {
logger.KV(xlog.DEBUG, "tls-cert", cert, "tls-trusted-ca", ca)
}
clientCfg.TLS, err = tlsconfig.NewClientTLSFromFiles(cert, key, ca)
if err != nil {
return nil, errors.WithMessage(err, "unable to build TLS configuration")
}
if !skipAuth {
// if c.STS {
// clientCfg.CallerIdentity, err = NewCallerIdentity()
// if err != nil {
// return nil, err
// }
// } else {
// the operator is a DPoP token, on the backend listener too: a
// client certificate is a service identity, never the operator
err = clientCfg.LoadAuthTokenOrFromEnv("TRUSTYCA_AUTH_TOKEN")
if err != nil {
return nil, errors.WithMessage(err, "unable to load auth token")
}
//}
}
}
grpcClient, err := rpcclient.New(clientCfg)
if err != nil {
return nil, errors.WithMessage(err, "unable to create client")
}
c.rpcClient = grpcClient
// TODO: add Timeout and retries
c.ctx = c.Context()
return c.rpcClient, nil
}
// HTTPClient returns client
func (c *Cli) HTTPClient(skipAuth bool) (*retriable.Client, error) {
if c.httpClient != nil {
return c.httpClient, nil
}
server := cmp.Or(c.Server, os.Getenv("TRUSTYCA_SERVER"))
if server == "" {
return nil, errors.New("no server specified. Use -s flag or TRUSTYCA_SERVER env var")
}
if ServerAlias[server] != "" {
server = ServerAlias[server]
}
cfg, err := c.ensureConfigFile()
if err != nil {
return nil, err
}
client, err := retriable.NewForHost(cfg, server)
if err != nil {
return nil, err
}
// expand Storage in order of priorities: flag, Env, config, default
storage := cmp.Or(
c.Storage,
os.Getenv("TRUSTYCA_STORAGE"),
client.Config.StorageFolder,
DefaultStoragePath,
)
c.Storage = resolve.ExpandPath(storage)
client.Config.StorageFolder = c.Storage
if strings.HasPrefix(server, "https://") {
if c.TrustedCA != "" || c.Cert != "" {
cert, key, ca := resolve.ExpandPath(c.Cert), resolve.ExpandPath(c.CertKey), resolve.ExpandPath(c.TrustedCA)
logger.KV(xlog.DEBUG, "tls-cert", cert, "tls-trusted-ca", ca)
tlscfg, err := tlsconfig.NewClientTLSFromFiles(
cert,
key,
ca)
if err != nil {
return nil, errors.WithMessage(err, "unable to build TLS configuration")
}
client.WithTLS(tlscfg)
}
if !skipAuth {
// if c.STS {
// ci, err := NewCallerIdentity()
// if err != nil {
// return nil, err
// }
// c.httpClient.WithCallerIdentity(ci)
// } else {
err = client.Config.LoadAuthTokenOrFromEnv("TRUSTYCA_AUTH_TOKEN")
if err != nil {
return nil, errors.WithMessage(err, "unable to load auth token")
}
err = client.SetAuthorization()
if err != nil {
return nil, err
}
//}
}
}
if c.Timeout > 0 {
client.WithTimeout(time.Second * time.Duration(c.Timeout))
}
c.httpClient = client.WithUserAgent("trustyca " + version.Current().String())
return c.httpClient, nil
}
// StatusClient returns StatusClient client from connection
func (c *Cli) StatusClient() (pb.StatusServer, error) {
if c.HTTP {
h, err := c.HTTPClient(true)
if err != nil {
return nil, err
}
return proxypb.NewHTTPStatusClient(h), nil
}
r, err := c.RPCClient(true)
if err != nil {
return nil, err
}
return proxypb.NewStatusClient(r.Conn(), r.Opts()), nil
}
// AuthClient returns Auth client from connection
func (c *Cli) AuthClient(skipAuth bool) (pb.AuthServer, error) {
if c.HTTP {
h, err := c.HTTPClient(skipAuth)
if err != nil {
return nil, err
}
return proxypb.NewHTTPAuthClient(h), nil
}
r, err := c.RPCClient(skipAuth)
if err != nil {
return nil, err
}
return proxypb.NewAuthClient(r.Conn(), r.Opts()), nil
}
// OrgsClient returns Orgs client from connection
func (c *Cli) OrgsClient() (pb.OrgsServer, error) {
if c.HTTP {
h, err := c.HTTPClient(false)
if err != nil {
return nil, err
}
return proxypb.NewHTTPOrgsClient(h), nil
}
r, err := c.RPCClient(false)
if err != nil {
return nil, err
}
return proxypb.NewOrgsClient(r.Conn(), r.Opts()), nil
}
// CISClient returns the client of the Certificate Information Service,
// the public, read-only side of the CA: it needs no token
func (c *Cli) CISClient() (pb.CISServer, error) {
if c.HTTP {
h, err := c.HTTPClient(true)
if err != nil {
return nil, err
}
return proxypb.NewHTTPCISClient(h), nil
}
r, err := c.RPCClient(true)
if err != nil {
return nil, err
}
return proxypb.NewCISClient(r.Conn(), r.Opts()), nil
}
// BackendClient returns the CA client of the connection: the CA of the
// service identities (privpb.Backend), which names the org of every
// request. It is served by the backend listener over gRPC only, to the
// operator's DPoP token (the trustyca-admin role of its email).
func (c *Cli) BackendClient() (privpb.BackendServer, error) {
if c.HTTP {
return nil, errors.New("CA endpoint is allowed only over gRPC")
}
r, err := c.RPCClient(false)
if err != nil {
return nil, err
}
return privproxypb.NewBackendClient(r.Conn(), r.Opts()), nil
}
// AdminClient returns Admin client from connection
func (c *Cli) AdminClient() (privpb.AdminServer, error) {
if c.HTTP {
return nil, errors.New("admin endpoint is allowed only over gRPC")
}
r, err := c.RPCClient(false)
if err != nil {
return nil, err
}
return privproxypb.NewAdminClient(r.Conn(), r.Opts()), nil
}
// Print response to out
func (c *Cli) Print(value any) error {
print.Object(c.Writer(), c.O, value)
return nil
}
func (c *Cli) Resolve(val string) (string, error) {
return configloader.ResolveValue(val)
}