teleport/lib/service/cfg.go

314 lines
9.3 KiB
Go
Raw Normal View History

2015-10-31 18:56:49 +00:00
/*
Copyright 2015 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package service
import (
"encoding/json"
"fmt"
2016-03-11 01:03:01 +00:00
"io"
"net"
"os"
"path/filepath"
2016-03-11 01:03:01 +00:00
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/auth"
2016-03-11 01:03:01 +00:00
"github.com/gravitational/teleport/lib/backend/etcdbk"
"github.com/gravitational/teleport/lib/defaults"
2015-12-03 09:26:34 +00:00
"github.com/gravitational/teleport/lib/limiter"
"github.com/gravitational/teleport/lib/services"
"github.com/gravitational/teleport/lib/utils"
2016-03-11 01:03:01 +00:00
log "github.com/Sirupsen/logrus"
2016-01-20 15:52:25 +00:00
"github.com/gravitational/trace"
2016-03-11 01:03:01 +00:00
"gopkg.in/yaml.v2"
)
// Config structure is used to initialize _all_ services Teleporot can run.
// Some settings are globl (like DataDir) while others are grouped into
// sections, like AuthConfig
type Config struct {
// DataDir provides directory where teleport stores it's permanent state
// (in case of auth server backed by BoltDB) or local state, e.g. keys
DataDir string
// Hostname is a node host name
Hostname string
// AuthServers is a list of auth servers nodes, proxies and peer auth servers
// connect to
AuthServers []utils.NetAddr
// Identities is an optional list of pre-generated key pairs
// for teleport roles, this is helpful when server is preconfigured
Identities []*auth.Identity
// AdvertiseIP is used to "publish" an alternative IP address this node
// can be reached on, if running behind NAT
AdvertiseIP net.IP
// SSH role an SSH endpoint server
SSH SSHConfig
// Auth server authentication and authorizatin server config
Auth AuthConfig
// Proxy is SSH proxy that manages incoming and outbound connections
// via multiple reverse tunnels
Proxy ProxyConfig
// HostUUID is a unique UUID of this host (it will be known via this UUID within
2016-03-04 02:02:48 +00:00
// a teleport cluster). It's automatically generated on 1st start
HostUUID string
// Console writer to speak to a user
Console io.Writer
// ReverseTunnels is a list of reverse tunnels to create on the
// first cluster start
ReverseTunnels []services.ReverseTunnel
2016-04-02 00:58:41 +00:00
// PidFile is a full path of the PID file for teleport daemon
PidFile string
}
// ApplyToken assigns a given token to all internal services but only if token
// is not an empty string.
//
// Returns 'true' if token was modified
func (cfg *Config) ApplyToken(token string) bool {
if token != "" {
cfg.SSH.Token = token
cfg.Proxy.Token = token
cfg.Auth.Token = token
return true
}
return false
}
2016-03-11 01:03:01 +00:00
// ConfigureBolt configures Bolt back-ends with a data dir.
func (cfg *Config) ConfigureBolt(dataDir string) {
2016-03-11 01:03:01 +00:00
a := &cfg.Auth
2016-03-11 01:03:01 +00:00
if a.EventsBackend.Type == teleport.BoltBackendType {
a.EventsBackend.Params = boltParams(dataDir, defaults.EventsBoltFile)
}
2016-03-11 01:03:01 +00:00
if a.KeysBackend.Type == teleport.BoltBackendType {
a.KeysBackend.Params = boltParams(dataDir, defaults.KeysBoltFile)
}
2016-03-11 01:03:01 +00:00
if a.RecordsBackend.Type == teleport.BoltBackendType {
a.RecordsBackend.Params = boltParams(dataDir, defaults.RecordsBoltFile)
}
}
2016-03-11 01:03:01 +00:00
// ConfigureETCD configures ETCD backend (still uses BoltDB for some cases)
2016-03-17 01:30:00 +00:00
func (cfg *Config) ConfigureETCD(dataDir string, etcdCfg etcdbk.Config) error {
2016-03-11 01:03:01 +00:00
a := &cfg.Auth
2016-03-17 01:30:00 +00:00
params, err := etcdParams(etcdCfg)
2016-03-11 01:03:01 +00:00
if err != nil {
return trace.Wrap(err)
}
a.KeysBackend.Type = teleport.ETCDBackendType
a.KeysBackend.Params = params
// We can't store records and events in ETCD
a.EventsBackend.Type = teleport.BoltBackendType
a.EventsBackend.Params = boltParams(dataDir, defaults.EventsBoltFile)
a.RecordsBackend.Type = teleport.BoltBackendType
a.RecordsBackend.Params = boltParams(dataDir, defaults.RecordsBoltFile)
return nil
}
// RoleConfig is a config for particular Teleport role
2015-10-27 00:58:39 +00:00
func (cfg *Config) RoleConfig() RoleConfig {
return RoleConfig{
DataDir: cfg.DataDir,
2016-03-05 00:27:52 +00:00
HostUUID: cfg.HostUUID,
HostName: cfg.Hostname,
2015-10-27 00:58:39 +00:00
AuthServers: cfg.AuthServers,
Auth: cfg.Auth,
Console: cfg.Console,
2015-10-27 00:58:39 +00:00
}
}
2016-03-11 01:03:01 +00:00
// DebugDumpToYAML is useful for debugging: it dumps the Config structure into
// a string
func (cfg *Config) DebugDumpToYAML() string {
shallow := *cfg
// do not copy sensitive data to stdout
shallow.Identities = nil
shallow.Auth.Authorities = nil
out, err := yaml.Marshal(shallow)
if err != nil {
return err.Error()
}
return string(out)
}
// ProxyConfig configures proy service
type ProxyConfig struct {
// Enabled turns proxy role on or off for this process
Enabled bool
// Token is a provisioning token for new proxy server registering with auth
Token string
// ReverseTunnelListenAddr is address where reverse tunnel dialers connect to
ReverseTunnelListenAddr utils.NetAddr
// WebAddr is address for web portal of the proxy
WebAddr utils.NetAddr
2015-11-02 21:02:34 +00:00
// SSHAddr is address of ssh proxy
SSHAddr utils.NetAddr
// AssetsDir is a directory with proxy website assets
AssetsDir string
// TLSKey is a base64 encoded private key used by web portal
TLSKey string
// TLSCert is a base64 encoded certificate used by web portal
TLSCert string
2015-12-02 18:51:32 +00:00
Limiter limiter.LimiterConfig
}
// AuthConfig is a configuration of the auth server
type AuthConfig struct {
// Enabled turns auth role on or off for this process
Enabled bool
// SSHAddr is the listening address of SSH tunnel to HTTP service
SSHAddr utils.NetAddr
// Token is a provisioning token for new proxy server registering with auth
Token string
// Authorities is a set of trusted certificate authorities
// that will be added by this auth server on the first start
Authorities []services.CertAuthority
// DomainName is a name that identifies this authority and all
// host nodes in the cluster that will share this authority domain name
// as a base name, e.g. if authority domain name is example.com,
// all nodes in the cluster will have UUIDs in the form: <uuid>.example.com
DomainName string
// KeysBackend configures backend that stores auth keys, certificates, tokens ...
KeysBackend struct {
// Type is a backend type - etcd or boltdb
Type string
// Params is map with backend specific parameters
Params string
}
// EventsBackend configures backend that stores cluster events (login attempts, etc)
EventsBackend struct {
// Type is a backend type, etcd or bolt
Type string
// Params is map with backend specific parameters
Params string
}
// RecordsBackend configures backend that stores live SSH sessions recordings
RecordsBackend struct {
// Type is a backend type, currently only bolt
Type string
// Params is map with backend specific parameters
Params string
}
2015-12-02 18:51:32 +00:00
Limiter limiter.LimiterConfig
}
// SSHConfig configures SSH server node role
type SSHConfig struct {
Enabled bool
Token string
Addr utils.NetAddr
Shell string
Limiter limiter.LimiterConfig
Labels map[string]string
CmdLabels services.CommandLabels
}
// MakeDefaultConfig creates a new Config structure and populates it with defaults
func MakeDefaultConfig() (config *Config) {
config = &Config{}
ApplyDefaults(config)
return config
}
2016-02-17 19:58:28 +00:00
// ApplyDefaults applies default values to the existing config structure
func ApplyDefaults(cfg *Config) {
hostname, err := os.Hostname()
if err != nil {
hostname = "localhost"
log.Errorf("Failed to determine hostname: %v", err)
}
// defaults for the auth service:
cfg.Auth.Enabled = true
cfg.Auth.SSHAddr = *defaults.AuthListenAddr()
cfg.Auth.EventsBackend.Type = defaults.BackendType
cfg.Auth.EventsBackend.Params = boltParams(defaults.DataDir, defaults.EventsBoltFile)
cfg.Auth.KeysBackend.Type = defaults.BackendType
cfg.Auth.KeysBackend.Params = boltParams(defaults.DataDir, defaults.KeysBoltFile)
cfg.Auth.RecordsBackend.Type = defaults.BackendType
cfg.Auth.RecordsBackend.Params = boltParams(defaults.DataDir, defaults.RecordsBoltFile)
defaults.ConfigureLimiter(&cfg.Auth.Limiter)
// defaults for the SSH proxy service:
cfg.Proxy.Enabled = true
cfg.Proxy.AssetsDir = defaults.DataDir
cfg.Proxy.SSHAddr = *defaults.ProxyListenAddr()
cfg.Proxy.WebAddr = *defaults.ProxyWebListenAddr()
2016-02-10 02:52:39 +00:00
cfg.Proxy.ReverseTunnelListenAddr = *defaults.ReverseTunnellListenAddr()
defaults.ConfigureLimiter(&cfg.Proxy.Limiter)
// defaults for the SSH service:
cfg.SSH.Enabled = true
cfg.SSH.Addr = *defaults.SSHServerListenAddr()
2016-02-16 21:18:58 +00:00
cfg.SSH.Shell = defaults.DefaultShell
defaults.ConfigureLimiter(&cfg.SSH.Limiter)
// global defaults
cfg.Hostname = hostname
cfg.DataDir = defaults.DataDir
if cfg.Auth.Enabled {
cfg.AuthServers = []utils.NetAddr{cfg.Auth.SSHAddr}
}
cfg.Console = os.Stdout
}
// Generates a string accepted by the BoltDB driver, like this:
// `{"path": "/var/lib/teleport/records.db"}`
func boltParams(storagePath, dbFile string) string {
return fmt.Sprintf(`{"path": "%s"}`, filepath.Join(storagePath, dbFile))
}
2016-03-11 01:03:01 +00:00
// etcdParams generates a string accepted by the ETCD driver, like this:
2016-03-17 01:30:00 +00:00
func etcdParams(cfg etcdbk.Config) (string, error) {
out, err := json.Marshal(cfg)
2016-03-11 01:03:01 +00:00
if err != nil { // don't know what to do seriously
return "", trace.Wrap(err)
}
return string(out), nil
}