grim/hgkeeper

68855a21fd28
Parents 76eb73c033fc
Children 8098dd6d3587
merging in my previous attempts at getting auth working
--- a/commands/commands.go Tue Jul 23 16:52:18 2019 -0500
+++ b/commands/commands.go Sun Sep 08 04:55:34 2019 -0500
@@ -23,7 +23,7 @@
}
type Command interface {
- Run(conn ssh.Channel, serverConn *ssh.ServerConn, req *ssh.Request) error
+ Run(conn ssh.Channel, serverConn *ssh.ServerConn, username string, req *ssh.Request) error
CheckAccess(access *access.Access, username string) bool
String() string
}
@@ -46,7 +46,7 @@
return values, ctx.Command(), nil
}
-func Find(cmd, reposPath string) (Command, error) {
+func Find(cmd, reposPath string, access *access.Access) (Command, error) {
values, pcmd, err := parse(cmd)
if err != nil {
return nil, err
@@ -54,9 +54,9 @@
switch pcmd {
case "hg serve":
- return NewServe(reposPath, values.Hg.Repo), nil
+ return NewServe(reposPath, values.Hg.Repo, access), nil
case "hg init <repo>":
- return NewInit(reposPath, values.Hg.Init.Repo), nil
+ return NewInit(reposPath, values.Hg.Init.Repo, access), nil
default:
return nil, fmt.Errorf("unknown command %s", cmd)
}
--- a/commands/init.go Tue Jul 23 16:52:18 2019 -0500
+++ b/commands/init.go Sun Sep 08 04:55:34 2019 -0500
@@ -11,16 +11,18 @@
type Init struct {
repoPath string
repoName string
+ access *access.Access
}
-func NewInit(reposPath, repoName string) *Init {
+func NewInit(reposPath, repoName string, access *access.Access) *Init {
return &Init{
repoPath: filepath.Join(reposPath, repoName),
repoName: repoName,
+ access: access,
}
}
-func (i *Init) Run(conn ssh.Channel, serverConn *ssh.ServerConn, req *ssh.Request) error {
+func (i *Init) Run(conn ssh.Channel, serverConn *ssh.ServerConn, username string, req *ssh.Request) error {
return run(hg.Init(i.repoPath), conn, serverConn, req)
}
--- a/commands/serve.go Tue Jul 23 16:52:18 2019 -0500
+++ b/commands/serve.go Sun Sep 08 04:55:34 2019 -0500
@@ -12,17 +12,21 @@
type Serve struct {
repoPath string
repoName string
+ access *access.Access
}
-func NewServe(reposPath, repoName string) *Serve {
+func NewServe(reposPath, repoName string, access *access.Access) *Serve {
return &Serve{
repoPath: filepath.Join(reposPath, repoName),
repoName: repoName,
+ access: access,
}
}
-func (s *Serve) Run(conn ssh.Channel, serverConn *ssh.ServerConn, req *ssh.Request) error {
- return run(hg.Serve(s.repoPath), conn, serverConn, req)
+func (s *Serve) Run(conn ssh.Channel, serverConn *ssh.ServerConn, username string, req *ssh.Request) error {
+ writeable := s.access.Global.CanWrite(username)
+
+ return run(hg.Serve(s.repoPath, writeable), conn, serverConn, req)
}
func (s *Serve) CheckAccess(access *access.Access, username string) bool {
--- a/hg/hg.go Tue Jul 23 16:52:18 2019 -0500
+++ b/hg/hg.go Sun Sep 08 04:55:34 2019 -0500
@@ -15,8 +15,18 @@
return common(exec.Command("hg", "init", path))
}
-func Serve(path string) *exec.Cmd {
- return common(exec.Command("hg", "-R", path, "serve", "--stdio"))
+func Serve(path string, writeable bool) *exec.Cmd {
+ args := []string{
+ "-R", path,
+ "serve",
+ "--stdio",
+ }
+
+ if !writeable {
+ args = append(args, "--config", "hooks.pretxnchangegroup=/bin/false")
+ }
+
+ return common(exec.Command("hg", args...))
}
func Add(path string, files ...string) *exec.Cmd {
--- a/ssh/server.go Tue Jul 23 16:52:18 2019 -0500
+++ b/ssh/server.go Sun Sep 08 04:55:34 2019 -0500
@@ -124,7 +124,7 @@
log.Infof("%s requested command %q", serverConn.RemoteAddr(), rawCmd)
- cmd, err := commands.Find(rawCmd, s.reposPath)
+ cmd, err := commands.Find(rawCmd, s.reposPath, s.a)
if err != nil {
log.Warnf("failed to find command for %q, %v", rawCmd, err)
req.Reply(false, nil)
@@ -142,7 +142,7 @@
continue
}
- if err := cmd.Run(conn, serverConn, req); err != nil {
+ if err := cmd.Run(conn, serverConn, username, req); err != nil {
log.Warnf("%s command %q failed: %v", serverConn.RemoteAddr(), rawCmd, err)
req.Reply(false, nil)
}