-
Notifications
You must be signed in to change notification settings - Fork 123
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Toby Padilla
committed
Oct 2, 2021
1 parent
0e4d562
commit 074eada
Showing
9 changed files
with
287 additions
and
106 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,20 @@ | ||
package config | ||
|
||
import ( | ||
gm "github.com/charmbracelet/wish/git" | ||
"github.com/gliderlabs/ssh" | ||
) | ||
|
||
func (cfg *Config) AuthRepo(repo string, pk ssh.PublicKey) gm.AccessLevel { | ||
// TODO: check yaml for access rules | ||
return gm.ReadWriteAccess | ||
} | ||
|
||
func (cfg *Config) PasswordHandler(ctx ssh.Context, password string) bool { | ||
return cfg.AnonReadOnly && cfg.AllowNoKeys | ||
} | ||
|
||
func (cfg *Config) PublicKeyHandler(ctx ssh.Context, pk ssh.PublicKey) bool { | ||
// TODO: check yaml for access rules | ||
return true | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,164 @@ | ||
package config | ||
|
||
import ( | ||
"log" | ||
|
||
"gopkg.in/yaml.v2" | ||
|
||
"fmt" | ||
"os" | ||
"path/filepath" | ||
"soft-serve/git" | ||
|
||
"github.com/gliderlabs/ssh" | ||
gg "github.com/go-git/go-git/v5" | ||
"github.com/go-git/go-git/v5/plumbing/object" | ||
) | ||
|
||
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 | ||
} | ||
|
||
type User struct { | ||
Name string `yaml:"name"` | ||
Admin bool `yaml:"admin"` | ||
PublicKey string `yaml:"pk"` | ||
CollabRepos []string `yaml:"collab_repos"` | ||
} | ||
|
||
type Repo struct { | ||
Name string `yaml:"name"` | ||
Repo string `yaml:"repo"` | ||
Note string `yaml:"note"` | ||
} | ||
|
||
func NewConfig(host string, port int, anon bool, pk string, rs *git.RepoSource) (*Config, error) { | ||
cfg := &Config{} | ||
cfg.Host = host | ||
cfg.Port = port | ||
cfg.AnonReadOnly = anon | ||
cfg.Source = rs | ||
|
||
var yamlUsers string | ||
var h string | ||
if host == "" { | ||
h = "localhost" | ||
} else { | ||
h = host | ||
} | ||
yamlConfig := fmt.Sprintf(defaultConfig, h, port, anon) | ||
if pk != "" { | ||
yamlUsers = fmt.Sprintf(hasKeyUserConfig, pk) | ||
} else { | ||
yamlUsers = defaultUserConfig | ||
} | ||
yaml := fmt.Sprintf("%s%s%s", yamlConfig, yamlUsers, exampleUserConfig) | ||
err := cfg.createDefaultConfigRepo(yaml) | ||
if err != nil { | ||
return nil, err | ||
} | ||
return cfg, nil | ||
} | ||
|
||
func (cfg *Config) Pushed(repo string, pk ssh.PublicKey) { | ||
err := cfg.Reload() | ||
if err != nil { | ||
log.Printf("error reloading after push: %s", err) | ||
} | ||
} | ||
|
||
func (cfg *Config) Reload() error { | ||
err := cfg.Source.LoadRepos() | ||
if err != nil { | ||
return err | ||
} | ||
cr, err := cfg.Source.GetRepo("config") | ||
if err != nil { | ||
return err | ||
} | ||
cs, err := cr.LatestFile("config.yaml") | ||
if err != nil { | ||
return err | ||
} | ||
err = yaml.Unmarshal([]byte(cs), cfg) | ||
if err != nil { | ||
return fmt.Errorf("bad yaml in config.yaml: %s", err) | ||
} | ||
return nil | ||
} | ||
|
||
func createFile(path string, content string) error { | ||
f, err := os.Create(path) | ||
if err != nil { | ||
return err | ||
} | ||
defer f.Close() | ||
_, err = f.WriteString(content) | ||
if err != nil { | ||
return err | ||
} | ||
return f.Sync() | ||
} | ||
|
||
func (cfg *Config) createDefaultConfigRepo(yaml string) error { | ||
cn := "config" | ||
rs := cfg.Source | ||
err := rs.LoadRepos() | ||
if err != nil { | ||
return err | ||
} | ||
_, err = rs.GetRepo(cn) | ||
if err == git.ErrMissingRepo { | ||
cr, err := rs.InitRepo(cn, false) | ||
if err != nil { | ||
return err | ||
} | ||
|
||
rp := filepath.Join(rs.Path, cn, "README.md") | ||
err = createFile(rp, defaultReadme) | ||
if err != nil { | ||
return err | ||
} | ||
cp := filepath.Join(rs.Path, cn, "config.yaml") | ||
err = createFile(cp, yaml) | ||
if err != nil { | ||
return err | ||
} | ||
wt, err := cr.Repository.Worktree() | ||
if err != nil { | ||
return err | ||
} | ||
_, err = wt.Add("README.md") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = wt.Add("config.yaml") | ||
if err != nil { | ||
return err | ||
} | ||
_, err = wt.Commit("Default init", &gg.CommitOptions{ | ||
All: true, | ||
Author: &object.Signature{ | ||
Name: "Soft Serve Server", | ||
Email: "vt100@charm.sh", | ||
}, | ||
}) | ||
if err != nil { | ||
return err | ||
} | ||
err = rs.LoadRepos() | ||
if err != nil { | ||
return err | ||
} | ||
} else if err != nil { | ||
return err | ||
} | ||
return nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,42 @@ | ||
package config | ||
|
||
const defaultReadme = "# Soft Serve\n\n Welcome! You can configure your Soft Serve server by cloning this repo and pushing changes.\n\n## Repos\n\n{{ range .Repos }}* {{ .Name }}{{ if .Note }} - {{ .Note }} {{ end }}\n - `git clone ssh://{{$.Host}}:{{$.Port}}/{{.Repo}}`\n{{ end }}" | ||
|
||
const defaultConfig = ` | ||
name: Soft Serve | ||
host: %s | ||
port: %d | ||
# Set the access level for anonymous users. Options are: read-write, read-only and no-access | ||
anon-access: %v | ||
# Allow read only even if they don't have private keys, any password will work | ||
allow-no-keys: false | ||
# Customize repo display in menu | ||
repos: | ||
- name: Home | ||
repo: config | ||
note: "Configuration and content repo for this server"` | ||
|
||
const hasKeyUserConfig = ` | ||
# Users can read all repos, and push to collab-repos, admin can push to all repos | ||
users: | ||
- name: admin | ||
admin: true | ||
public-key: | | ||
%s` | ||
|
||
const defaultUserConfig = ` | ||
# users: | ||
# - name: admin | ||
# admin: true | ||
# public-key: | | ||
# KEY TEXT` | ||
|
||
const exampleUserConfig = ` | ||
# - name: little-buddy | ||
# collab-repos: | ||
# - soft-serve | ||
# public-key: | | ||
# KEY TEXT` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.