From c09197a1ca95486db225634d82772ed1fad60806 Mon Sep 17 00:00:00 2001 From: Toby Padilla Date: Sat, 2 Oct 2021 17:28:43 -0500 Subject: [PATCH] Auto set anon access if there's an init public key --- config/auth.go | 2 +- config/config.go | 36 ++++++++++++++++++++---------------- config/defaults.go | 2 +- main.go | 13 +++---------- 4 files changed, 25 insertions(+), 28 deletions(-) diff --git a/config/auth.go b/config/auth.go index 999262c5a07821e8e08a0b3417814c007977ab41..78f6b2d6e27a5def15e9947b4d861c3f94e1c21b 100644 --- a/config/auth.go +++ b/config/auth.go @@ -11,7 +11,7 @@ func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel { } func (cfg *Config) PasswordHandler(ctx ssh.Context, password string) bool { - return cfg.AnonReadOnly && cfg.AllowNoKeys + return (cfg.AnonAccess != "no-access") && cfg.AllowNoKeys } func (cfg *Config) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool { diff --git a/config/config.go b/config/config.go index b64974d8be1017ab8c78129d1c27dd9fa0275dfe..46f7ba7ea87f014db60c0b07ecab34639e7b448c 100644 --- a/config/config.go +++ b/config/config.go @@ -16,14 +16,14 @@ import ( ) type Config struct { - Name string `yaml:"name"` - Host string `yaml:"host"` - Port int `yaml:"port"` - AnonReadOnly bool `yaml:"anon-access"` - AllowNoKeys bool `yaml:"allow-no-keys"` - Users []User `yaml:"users"` - Repos []Repo `yaml:"repos"` - Source *git.RepoSource + Name string `yaml:"name"` + Host string `yaml:"host"` + Port int `yaml:"port"` + AnonAccess string `yaml:"anon-access"` + AllowNoKeys bool `yaml:"allow-no-keys"` + Users []User `yaml:"users"` + Repos []Repo `yaml:"repos"` + Source *git.RepoSource } type User struct { @@ -39,21 +39,25 @@ type Repo struct { Note string `yaml:"note"` } -func NewConfig(host string, port int, anon bool, pk string, rs *git.RepoSource) (*Config, error) { +func NewConfig(host string, port int, pk string, rs *git.RepoSource) (*Config, error) { + var anonAccess string + var yamlUsers string + var displayHost string cfg := &Config{} cfg.Host = host cfg.Port = port - cfg.AnonReadOnly = anon cfg.Source = rs - - var yamlUsers string - var h string + if pk == "" { + anonAccess = "read-write" + } else { + anonAccess = "no-access" + } if host == "" { - h = "localhost" + displayHost = "localhost" } else { - h = host + displayHost = host } - yamlConfig := fmt.Sprintf(defaultConfig, h, port, anon) + yamlConfig := fmt.Sprintf(defaultConfig, displayHost, port, anonAccess) if pk != "" { yamlUsers = fmt.Sprintf(hasKeyUserConfig, pk) } else { diff --git a/config/defaults.go b/config/defaults.go index 1a7cbb3866df8db37deef9ac6e2bff02f0bc45e0..49b1235b6ec8289c28d1e623be783f6702b0476b 100644 --- a/config/defaults.go +++ b/config/defaults.go @@ -8,7 +8,7 @@ host: %s port: %d # Set the access level for anonymous users. Options are: read-write, read-only and no-access -anon-access: %v +anon-access: %s # Allow read only even if they don't have private keys, any password will work allow-no-keys: false diff --git a/main.go b/main.go index 92c1eaaccce8147781f2eca97b717ca0eac3cbe7..3992f3b28707080bf6b2a471f5319afbe83a7195 100644 --- a/main.go +++ b/main.go @@ -33,16 +33,9 @@ func main() { log.Fatalln(err) } rs := git.NewRepoSource(scfg.RepoPath) - if scfg.InitKey == "" { - cfg, err = config.NewConfig(scfg.Host, scfg.Port, true, "", rs) - if err != nil { - log.Fatalln(err) - } - } else { - cfg, err = config.NewConfig(scfg.Host, scfg.Port, false, scfg.InitKey, rs) - if err != nil { - log.Fatalln(err) - } + cfg, err = config.NewConfig(scfg.Host, scfg.Port, scfg.InitKey, rs) + if err != nil { + log.Fatalln(err) } s, err := wish.NewServer( ssh.PublicKeyAuth(cfg.PublicKeyHandler),