sshpoke/internal/config/model.go

133 lines
3.7 KiB
Go
Raw Normal View History

2023-11-16 20:09:40 +03:00
package config
import (
"net/http"
"path/filepath"
"github.com/Neur0toxine/sshpoke/pkg/smarttypes"
2023-11-16 20:09:40 +03:00
"github.com/docker/docker/client"
"github.com/docker/go-connections/tlsconfig"
)
2023-11-17 20:39:00 +03:00
var Default Config
2023-11-16 20:09:40 +03:00
type Config struct {
Debug bool `mapstructure:"debug" json:"debug"`
API API `mapstructure:"api" json:"api"`
Docker DockerConfig `mapstructure:"docker" json:"docker"`
DefaultServer string `mapstructure:"default_server" json:"default_server"`
Services []Service `mapstructure:"services" json:"services,omitempty"`
Servers []Server `mapstructure:"servers" json:"servers"`
2023-11-16 20:09:40 +03:00
}
type Service struct {
Name smarttypes.MatchableString `mapstructure:"name" json:"name"`
Params ServiceLabels `mapstructure:"params" json:"params"`
}
type ServiceLabels struct {
Enable smarttypes.BoolStr `mapstructure:"enable" json:"enable"`
Network string `mapstructure:"network" json:"network,omitempty"`
Server string `mapstructure:"server" json:"server,omitempty"`
Port string `mapstructure:"port" json:"port,omitempty"`
RemoteHost string `mapstructure:"remote_host" json:"remote_host,omitempty"`
}
type API struct {
2023-11-22 23:31:35 +03:00
Rest WebAPI `mapstructure:"rest" json:"rest"`
Plugin PluginAPI `mapstructure:"plugin" json:"plugin"`
}
type WebAPI struct {
Port int `mapstructure:"port" json:"port" validate:"gte=0,lte=65535"`
Token string `mapstructure:"token" json:"token"`
}
type PluginAPI struct {
Port int `mapstructure:"port" json:"port" validate:"gte=0,lte=65535"`
}
2023-11-16 20:09:40 +03:00
type DockerConfig struct {
FromEnv *bool `mapstructure:"from_env,omitempty" json:"from_env"`
CertPath string `mapstructure:"cert_path" json:"cert_path,omitempty"`
TLSVerify *bool `mapstructure:"tls_verify,omitempty" json:"tls_verify,omitempty"`
Host string `mapstructure:"host" json:"host,omitempty"`
Version string `mapstructure:"version" json:"version,omitempty"`
2023-11-16 20:09:40 +03:00
}
2023-11-17 20:39:00 +03:00
type DriverParams map[string]interface{}
type DriverType string
const (
DriverSSH DriverType = "ssh"
DriverPlugin DriverType = "plugin"
DriverNil DriverType = "nil"
2023-11-17 20:39:00 +03:00
)
type Server struct {
Name string `mapstructure:"name" json:"name" validate:"required"`
Driver DriverType `mapstructure:"driver" json:"driver"`
Params DriverParams `mapstructure:"params" json:"params,omitempty"`
2023-11-17 20:39:00 +03:00
}
2023-11-16 20:09:40 +03:00
func (d DockerConfig) Opts(c *client.Client) error {
2023-11-17 20:39:00 +03:00
if d.FromEnv == nil || *d.FromEnv {
2023-11-16 20:09:40 +03:00
return client.FromEnv(c)
}
ops := []client.Opt{
d.withTLSClientConfig(),
d.withHost(),
d.withVersion(),
}
for _, op := range ops {
if err := op(c); err != nil {
return err
}
}
return nil
}
func (d DockerConfig) withTLSClientConfig() client.Opt {
return func(c *client.Client) error {
dockerCertPath := d.CertPath
if dockerCertPath == "" {
return nil
}
skipTLSVerify := false
if d.TLSVerify != nil && !(*d.TLSVerify) {
skipTLSVerify = true
}
options := tlsconfig.Options{
CAFile: filepath.Join(dockerCertPath, "ca.pem"),
CertFile: filepath.Join(dockerCertPath, "cert.pem"),
KeyFile: filepath.Join(dockerCertPath, "key.pem"),
InsecureSkipVerify: skipTLSVerify,
}
tlsConfig, err := tlsconfig.Client(options)
if err != nil {
return err
}
return client.WithHTTPClient(&http.Client{
Transport: &http.Transport{TLSClientConfig: tlsConfig},
CheckRedirect: client.CheckRedirect,
})(c)
}
}
func (d DockerConfig) withHost() client.Opt {
return func(c *client.Client) error {
if host := d.Host; host != "" {
return client.WithHost(host)(c)
}
return nil
}
}
func (d DockerConfig) withVersion() client.Opt {
return func(c *client.Client) error {
return client.WithVersion(d.Version)(c)
}
}