Skip to content

Commit

Permalink
Auto set anon access if there's an init public key
Browse files Browse the repository at this point in the history
  • Loading branch information
Toby Padilla committed Oct 2, 2021
1 parent 074eada commit c09197a
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 28 deletions.
2 changes: 1 addition & 1 deletion config/auth.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down
36 changes: 20 additions & 16 deletions config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
2 changes: 1 addition & 1 deletion config/defaults.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
13 changes: 3 additions & 10 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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),
Expand Down

0 comments on commit c09197a

Please sign in to comment.