Add basic authentication

This commit is contained in:
2021-12-06 06:08:17 +01:00
parent 651de90839
commit aae3d9a217
15 changed files with 1039 additions and 804 deletions

View File

@@ -17,6 +17,8 @@ import (
"google.golang.org/grpc/credentials"
)
var ErrNotFound = fmt.Errorf("config not found")
type Config struct {
Server *ServerConfig `toml:"Server"`
Client *ClientConfig `toml:"Client"`
@@ -28,19 +30,26 @@ type CertificatePaths struct {
}
type ServerConfig struct {
LogLevel string `toml:"LogLevel"`
Hostname string `toml:"Hostname"`
StoreConfig *ServerStoreConfig `toml:"Store"`
GRPC *ServerGRPCConfig `toml:"GRPC"`
HTTP *ServerHTTPConfig `toml:"HTTP"`
LogLevel string `toml:"LogLevel"`
Hostname string `toml:"Hostname"`
GRPCEndpoint string `toml:"GRPCEndpoint"`
UserStoreConfig *ServerUserStoreConfig `toml:"UserStore"`
FileStoreConfig *ServerFileStoreConfig `toml:"FileStore"`
GRPC *ServerGRPCConfig `toml:"GRPC"`
HTTP *ServerHTTPConfig `toml:"HTTP"`
}
type ServerStoreConfig struct {
type ServerFileStoreConfig struct {
Type string `toml:"Type"`
FSStoreConfig *FSStoreConfig `toml:"Filesystem"`
BoltStoreConfig *BoltStoreConfig `toml:"Bolt"`
}
type ServerUserStoreConfig struct {
Type string `toml:"Type"`
BoltStoreConfig *BoltStoreConfig `toml:"Bolt"`
}
type BoltStoreConfig struct {
Path string `toml:"Path"`
}
@@ -78,7 +87,9 @@ func FromDefault() *Config {
ListenAddr: ":8089",
},
},
Client: &ClientConfig{},
Client: &ClientConfig{
Certs: &CertificatePaths{},
},
}
return cfg
@@ -97,6 +108,9 @@ func FromReader(r io.Reader) (*Config, error) {
func FromFile(path string) (*Config, error) {
f, err := os.Open(path)
if err != nil {
if errors.Is(err, os.ErrNotExist) {
return nil, ErrNotFound
}
return nil, fmt.Errorf("unable to open config-file: %w", err)
}
defer f.Close()
@@ -188,7 +202,7 @@ func (cc *ClientConfig) Creds() (credentials.TransportCredentials, error) {
return credentials.NewTLS(config), nil
}
func (c *Config) ToDefaultFile() error {
func CreateDefaultConfigDir() error {
userConfigDir, err := os.UserConfigDir()
if err != nil {
return err
@@ -207,8 +221,25 @@ func (c *Config) ToDefaultFile() error {
return fmt.Errorf("config-directory is not a directory")
}
}
return nil
}
func DefaultConfigFilePath() (string, error) {
userConfigDir, err := os.UserConfigDir()
if err != nil {
return "", err
}
return filepath.Join(userConfigDir, "ezshare", "ezshare.toml"), nil
configFilePath := filepath.Join(configDirPath, "ezshare.toml")
}
func (c *Config) ToDefaultFile() error {
if err := CreateDefaultConfigDir(); err != nil {
return err
}
configFilePath, err := DefaultConfigFilePath()
if err != nil {
return err
}
_, err = os.Stat(configFilePath)
if err != nil {
if !errors.Is(err, fs.ErrNotExist) {
@@ -226,7 +257,7 @@ func (c *Config) ToDefaultFile() error {
return fmt.Errorf("config-file already exists")
}
func (sc *ServerStoreConfig) GetStore() (store.FileStore, func() error, error) {
func (sc *ServerFileStoreConfig) GetStore() (store.FileStore, func() error, error) {
nopCloseFunc := func() error { return nil }
if strings.EqualFold(sc.Type, "bolt") {
s, err := store.NewBoltStore(sc.BoltStoreConfig.Path)
@@ -243,6 +274,39 @@ func (sc *ServerStoreConfig) GetStore() (store.FileStore, func() error, error) {
if strings.EqualFold(sc.Type, "memory") {
return store.NewMemoryStore(), nopCloseFunc, nil
}
if strings.EqualFold(sc.Type, "bolt") {
s, err := store.NewBoltStore(sc.BoltStoreConfig.Path)
if err != nil {
return nil, nil, err
}
closeFunc := func() error { return s.Close() }
return s, closeFunc, nil
}
return nil, nil, fmt.Errorf("invalid store config")
}
func (sc *ServerUserStoreConfig) GetStore() (store.UserStore, func() error, error) {
nopCloseFunc := func() error { return nil }
if strings.EqualFold(sc.Type, "bolt") {
s, err := store.NewBoltStore(sc.BoltStoreConfig.Path)
if err != nil {
return nil, nil, err
}
return s, s.Close, err
}
if strings.EqualFold(sc.Type, "memory") {
return store.NewMemoryStore(), nopCloseFunc, nil
}
if strings.EqualFold(sc.Type, "bolt") {
s, err := store.NewBoltStore(sc.BoltStoreConfig.Path)
if err != nil {
return nil, nil, err
}
closeFunc := func() error { return s.Close() }
return s, closeFunc, nil
}
return nil, nil, fmt.Errorf("invalid store config")
}