some ground work to support authentcation on the control channel
This commit is contained in:
parent
d3afe29d5a
commit
a97be385ec
@ -2,12 +2,14 @@ package main
|
|||||||
|
|
||||||
import "crypto/tls"
|
import "crypto/tls"
|
||||||
import "crypto/x509"
|
import "crypto/x509"
|
||||||
|
import "encoding/base64"
|
||||||
import "errors"
|
import "errors"
|
||||||
import "fmt"
|
import "fmt"
|
||||||
import "hodu"
|
import "hodu"
|
||||||
import "io"
|
import "io"
|
||||||
import "io/ioutil"
|
import "io/ioutil"
|
||||||
import "os"
|
import "os"
|
||||||
|
import "strings"
|
||||||
import "time"
|
import "time"
|
||||||
|
|
||||||
import "gopkg.in/yaml.v3"
|
import "gopkg.in/yaml.v3"
|
||||||
@ -45,8 +47,7 @@ type ClientTLSConfig struct {
|
|||||||
type BasicAuthConfig struct {
|
type BasicAuthConfig struct {
|
||||||
Enabled bool `yaml:"enabled"`
|
Enabled bool `yaml:"enabled"`
|
||||||
Realm string `yaml:"realm"`
|
Realm string `yaml:"realm"`
|
||||||
Users []string `yaml:"users"`
|
Creds []string `yaml:"credentials"`
|
||||||
UserFile string `yaml:"user-file"`
|
|
||||||
}
|
}
|
||||||
|
|
||||||
type CTLServiceConfig struct {
|
type CTLServiceConfig struct {
|
||||||
@ -342,3 +343,30 @@ func make_tls_client_config(cfg *ClientTLSConfig) (*tls.Config, error) {
|
|||||||
|
|
||||||
return tlscfg, nil
|
return tlscfg, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// --------------------------------------------------------------------
|
||||||
|
func make_server_basic_auth_config(cfg *BasicAuthConfig) (*hodu.ServerBasicAuth, error) {
|
||||||
|
var config hodu.ServerBasicAuth
|
||||||
|
var cred string
|
||||||
|
var b []byte
|
||||||
|
var x []string
|
||||||
|
var err error
|
||||||
|
|
||||||
|
config.Enabled = cfg.Enabled
|
||||||
|
config.Realm = cfg.Realm
|
||||||
|
|
||||||
|
for _, cred = range cfg.Creds {
|
||||||
|
b, err = base64.StdEncoding.DecodeString(cred)
|
||||||
|
if err == nil { cred = string(b) }
|
||||||
|
|
||||||
|
// each entry must be of the form username:password
|
||||||
|
x = strings.Split(cred, ":")
|
||||||
|
if len(x) != 2 {
|
||||||
|
return nil, fmt.Errorf("invalid basic auth credential - %s", cred)
|
||||||
|
}
|
||||||
|
|
||||||
|
config.Creds = append(config.Creds, hodu.ServerBasicAuthCred{ Username: x[0], Password: x[1] })
|
||||||
|
}
|
||||||
|
|
||||||
|
return &config, nil
|
||||||
|
}
|
||||||
|
31
cmd/main.go
31
cmd/main.go
@ -93,7 +93,7 @@ func server_main(ctl_addrs []string, rpc_addrs []string, pxy_addrs []string, wpx
|
|||||||
var s *hodu.Server
|
var s *hodu.Server
|
||||||
var config *hodu.ServerConfig
|
var config *hodu.ServerConfig
|
||||||
var logger *AppLogger
|
var logger *AppLogger
|
||||||
var log_mask hodu.LogMask
|
var logmask hodu.LogMask
|
||||||
var logfile string
|
var logfile string
|
||||||
var logfile_maxsize int64
|
var logfile_maxsize int64
|
||||||
var logfile_rotate int
|
var logfile_rotate int
|
||||||
@ -101,7 +101,7 @@ func server_main(ctl_addrs []string, rpc_addrs []string, pxy_addrs []string, wpx
|
|||||||
var xterm_html string
|
var xterm_html string
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
log_mask = hodu.LOG_ALL
|
logmask = hodu.LOG_ALL
|
||||||
|
|
||||||
config = &hodu.ServerConfig{
|
config = &hodu.ServerConfig{
|
||||||
CtlAddrs: ctl_addrs,
|
CtlAddrs: ctl_addrs,
|
||||||
@ -125,12 +125,15 @@ func server_main(ctl_addrs []string, rpc_addrs []string, pxy_addrs []string, wpx
|
|||||||
if len(config.PxyAddrs) <= 0 { config.PxyAddrs = cfg.PXY.Service.Addrs }
|
if len(config.PxyAddrs) <= 0 { config.PxyAddrs = cfg.PXY.Service.Addrs }
|
||||||
if len(config.WpxAddrs) <= 0 { config.WpxAddrs = cfg.WPX.Service.Addrs }
|
if len(config.WpxAddrs) <= 0 { config.WpxAddrs = cfg.WPX.Service.Addrs }
|
||||||
|
|
||||||
|
config.CtlBasicAuth, err = make_server_basic_auth_config(&cfg.CTL.Service.BasicAuth)
|
||||||
|
if err != nil { return err }
|
||||||
|
|
||||||
config.CtlPrefix = cfg.CTL.Service.Prefix
|
config.CtlPrefix = cfg.CTL.Service.Prefix
|
||||||
config.RpcMaxConns = cfg.APP.MaxRpcConns
|
config.RpcMaxConns = cfg.APP.MaxRpcConns
|
||||||
config.MaxPeers = cfg.APP.MaxPeers
|
config.MaxPeers = cfg.APP.MaxPeers
|
||||||
xterm_html_file = cfg.APP.XtermHtmlFile
|
xterm_html_file = cfg.APP.XtermHtmlFile
|
||||||
|
|
||||||
log_mask = log_strings_to_mask(cfg.APP.LogMask)
|
logmask = log_strings_to_mask(cfg.APP.LogMask)
|
||||||
logfile = cfg.APP.LogFile
|
logfile = cfg.APP.LogFile
|
||||||
logfile_maxsize = cfg.APP.LogMaxSize
|
logfile_maxsize = cfg.APP.LogMaxSize
|
||||||
logfile_rotate = cfg.APP.LogRotate
|
logfile_rotate = cfg.APP.LogRotate
|
||||||
@ -141,9 +144,9 @@ func server_main(ctl_addrs []string, rpc_addrs []string, pxy_addrs []string, wpx
|
|||||||
}
|
}
|
||||||
|
|
||||||
if logfile == "" {
|
if logfile == "" {
|
||||||
logger = NewAppLogger("server", os.Stderr, log_mask)
|
logger = NewAppLogger("server", os.Stderr, logmask)
|
||||||
} else {
|
} else {
|
||||||
logger, err = NewAppLoggerToFile("server", logfile, logfile_maxsize, logfile_rotate, log_mask)
|
logger, err = NewAppLoggerToFile("server", logfile, logfile_maxsize, logfile_rotate, logmask)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to initialize logger - %s", err.Error())
|
return fmt.Errorf("failed to initialize logger - %s", err.Error())
|
||||||
}
|
}
|
||||||
@ -158,13 +161,9 @@ func server_main(ctl_addrs []string, rpc_addrs []string, pxy_addrs []string, wpx
|
|||||||
xterm_html = string(tmp)
|
xterm_html = string(tmp)
|
||||||
}
|
}
|
||||||
|
|
||||||
s, err = hodu.NewServer(
|
s, err = hodu.NewServer(context.Background(), HODU_NAME, logger, config)
|
||||||
context.Background(),
|
|
||||||
HODU_NAME,
|
|
||||||
logger,
|
|
||||||
config)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to create new server - %s", err.Error())
|
return fmt.Errorf("failed to create server - %s", err.Error())
|
||||||
}
|
}
|
||||||
|
|
||||||
if xterm_html != "" { s.SetXtermHtml(xterm_html) }
|
if xterm_html != "" { s.SetXtermHtml(xterm_html) }
|
||||||
@ -251,7 +250,7 @@ func client_main(ctl_addrs []string, rpc_addrs []string, route_configs []string,
|
|||||||
var ctl_prefix string
|
var ctl_prefix string
|
||||||
var cc hodu.ClientConfig
|
var cc hodu.ClientConfig
|
||||||
var logger *AppLogger
|
var logger *AppLogger
|
||||||
var log_mask hodu.LogMask
|
var logmask hodu.LogMask
|
||||||
var logfile string
|
var logfile string
|
||||||
var logfile_maxsize int64
|
var logfile_maxsize int64
|
||||||
var logfile_rotate int
|
var logfile_rotate int
|
||||||
@ -261,7 +260,7 @@ func client_main(ctl_addrs []string, rpc_addrs []string, route_configs []string,
|
|||||||
var i int
|
var i int
|
||||||
var err error
|
var err error
|
||||||
|
|
||||||
log_mask = hodu.LOG_ALL
|
logmask = hodu.LOG_ALL
|
||||||
if cfg != nil {
|
if cfg != nil {
|
||||||
ctltlscfg, err = make_tls_server_config(&cfg.CTL.TLS)
|
ctltlscfg, err = make_tls_server_config(&cfg.CTL.TLS)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@ -278,7 +277,7 @@ func client_main(ctl_addrs []string, rpc_addrs []string, route_configs []string,
|
|||||||
|
|
||||||
cc.ServerSeedTmout = cfg.RPC.Endpoint.SeedTmout
|
cc.ServerSeedTmout = cfg.RPC.Endpoint.SeedTmout
|
||||||
cc.ServerAuthority = cfg.RPC.Endpoint.Authority
|
cc.ServerAuthority = cfg.RPC.Endpoint.Authority
|
||||||
log_mask = log_strings_to_mask(cfg.APP.LogMask)
|
logmask = log_strings_to_mask(cfg.APP.LogMask)
|
||||||
logfile = cfg.APP.LogFile
|
logfile = cfg.APP.LogFile
|
||||||
logfile_maxsize = cfg.APP.LogMaxSize
|
logfile_maxsize = cfg.APP.LogMaxSize
|
||||||
logfile_rotate = cfg.APP.LogRotate
|
logfile_rotate = cfg.APP.LogRotate
|
||||||
@ -299,9 +298,9 @@ func client_main(ctl_addrs []string, rpc_addrs []string, route_configs []string,
|
|||||||
}
|
}
|
||||||
|
|
||||||
if logfile == "" {
|
if logfile == "" {
|
||||||
logger = NewAppLogger("client", os.Stderr, log_mask)
|
logger = NewAppLogger("client", os.Stderr, logmask)
|
||||||
} else {
|
} else {
|
||||||
logger, err = NewAppLoggerToFile("client", logfile, logfile_maxsize, logfile_rotate, log_mask)
|
logger, err = NewAppLoggerToFile("client", logfile, logfile_maxsize, logfile_rotate, logmask)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return fmt.Errorf("failed to initialize logger - %s", err.Error())
|
return fmt.Errorf("failed to initialize logger - %s", err.Error())
|
||||||
}
|
}
|
||||||
|
@ -62,6 +62,17 @@ func (ctl *server_ctl) Id() string {
|
|||||||
return ctl.id
|
return ctl.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (ctl *server_ctl) Authenticate(req *http.Request) bool {
|
||||||
|
var s *Server
|
||||||
|
|
||||||
|
s = ctl.s
|
||||||
|
if s.cfg.CtlBasicAuth != nil && s.cfg.CtlBasicAuth.Enabled {
|
||||||
|
// perform basic authentication
|
||||||
|
}
|
||||||
|
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
|
|
||||||
func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) {
|
func (ctl *server_ctl_server_conns) ServeHTTP(w http.ResponseWriter, req *http.Request) (int, error) {
|
||||||
|
@ -184,10 +184,16 @@ func mutate_proxy_req_headers(req *http.Request, newreq *http.Request, path_pref
|
|||||||
return upgrade_required
|
return upgrade_required
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// ------------------------------------
|
||||||
|
|
||||||
func (pxy *server_proxy) Id() string {
|
func (pxy *server_proxy) Id() string {
|
||||||
return pxy.id
|
return pxy.id
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (pxy *server_proxy) Authenticate(req *http.Request) bool {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
// ------------------------------------
|
// ------------------------------------
|
||||||
|
|
||||||
func prevent_follow_redirect (req *http.Request, via []*http.Request) error {
|
func prevent_follow_redirect (req *http.Request, via []*http.Request) error {
|
||||||
|
@ -42,7 +42,7 @@ type ServerSvcPortMap = map[PortId]ConnRouteId
|
|||||||
type ServerWpxResponseTransformer func(r *ServerRouteProxyInfo, resp *http.Response) io.Reader
|
type ServerWpxResponseTransformer func(r *ServerRouteProxyInfo, resp *http.Response) io.Reader
|
||||||
type ServerWpxForeignPortProxyMaker func(wpx_type string, port_id string) (*ServerRouteProxyInfo, error)
|
type ServerWpxForeignPortProxyMaker func(wpx_type string, port_id string) (*ServerRouteProxyInfo, error)
|
||||||
|
|
||||||
type ServerBasicAuthUser struct {
|
type ServerBasicAuthCred struct {
|
||||||
Username string
|
Username string
|
||||||
Password string
|
Password string
|
||||||
}
|
}
|
||||||
@ -50,7 +50,7 @@ type ServerBasicAuthUser struct {
|
|||||||
type ServerBasicAuth struct {
|
type ServerBasicAuth struct {
|
||||||
Enabled bool
|
Enabled bool
|
||||||
Realm string
|
Realm string
|
||||||
User []ServerBasicAuthUser
|
Creds []ServerBasicAuthCred
|
||||||
}
|
}
|
||||||
|
|
||||||
type ServerConfig struct {
|
type ServerConfig struct {
|
||||||
@ -62,7 +62,7 @@ type ServerConfig struct {
|
|||||||
CtlAddrs []string
|
CtlAddrs []string
|
||||||
CtlTls *tls.Config
|
CtlTls *tls.Config
|
||||||
CtlPrefix string
|
CtlPrefix string
|
||||||
CtlBasicAuth ServerBasicAuth
|
CtlBasicAuth *ServerBasicAuth
|
||||||
|
|
||||||
PxyAddrs []string
|
PxyAddrs []string
|
||||||
PxyTls *tls.Config
|
PxyTls *tls.Config
|
||||||
@ -953,6 +953,7 @@ func (hlw *server_http_log_writer) Write(p []byte) (n int, err error) {
|
|||||||
|
|
||||||
type ServerHttpHandler interface {
|
type ServerHttpHandler interface {
|
||||||
Id() string
|
Id() string
|
||||||
|
Authenticate(req *http.Request) bool
|
||||||
ServeHTTP (w http.ResponseWriter, req *http.Request) (int, error)
|
ServeHTTP (w http.ResponseWriter, req *http.Request) (int, error)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user