grim/hgkeeper

ed523f1c967e
Parents 14af6e82af57
Children 713e642eb9bc
Add an admin repos command line argument
--- a/access/access.go Tue May 07 22:41:21 2019 -0500
+++ b/access/access.go Mon Jul 22 22:12:09 2019 -0500
@@ -34,7 +34,6 @@
var (
accessFile string
keysDir string
- reposDir string
)
type (
@@ -113,7 +112,7 @@
}
var perm Perm
perm.set(p)
- a.users[user] = make(map[string]Perm)
+ a.users[user] = map[string]Perm{}
a.users[user][pattern] = perm
}
}
@@ -248,13 +247,18 @@
// New initializes access strucuture, if any error is
// returned it will be I/O errors reading or parsing
// the access file.
-func New(reposPath string) (*Access, error) {
- reposDir = reposPath
- accessFile = filepath.Join(reposPath, "hgkeeper", AccessFile)
- keysDir = filepath.Join(reposPath, "hgkeeper", KeysDir)
- a := new(Access)
- a.users = make(map[string]map[string]Perm)
- a.keys = make(map[string]string)
+func New(reposPath, adminRepo string) (*Access, error) {
+ accessFile = filepath.Join(reposPath, adminRepo, AccessFile)
+ keysDir = filepath.Join(reposPath, adminRepo, KeysDir)
+
+ log.Errorf("accessFile: %q", accessFile)
+ log.Errorf("keysDir: %q", keysDir)
+
+ a := &Access{
+ users: map[string]map[string]Perm{},
+ keys: map[string]string{},
+ }
+
return a, a.Reset()
}
--- a/access/access_test.go Tue May 07 22:41:21 2019 -0500
+++ b/access/access_test.go Mon Jul 22 22:12:09 2019 -0500
@@ -1,77 +1,79 @@
package access
import (
+ "strings"
"testing"
- _ "github.com/stretchr/testify/assert"
+ "github.com/stretchr/testify/assert"
)
-var accessData = `
+// var accessData = `
+// global:
+// init:
+// - admins
+// read:
+// - public
+// groups:
+// admins:
+// - grim
+// pidgin:
+// - grim
+// pidgin-gsoc:
+// - student1
+// patterns:
+// hgkeeper:
+// read:
+// - admins
+// pidgin/gsoc/*:
+// write:
+// - pidgin
+// - pidgin-gsoc
+// pidgin/*:
+// write:
+// - pidgin
+// `
+
+// var keysData = []string{
+// "",
+// "",
+// }
+
+func TestAccessControlLoadSimple(t *testing.T) {
+ data := `
global:
init:
- - admins
+ - admin
read:
- - public
-groups:
- admins:
- - grim
- pidgin:
- - grim
- pidgin-gsoc:
- - student1
+ - everyone
patterns:
hgkeeper:
read:
- - admins
- pidgin/gsoc/*:
- write:
- - pidgin
- - pidgin-gsoc
- pidgin/*:
- write:
- - pidgin
+ - admin
`
-var keysData = []string{
- "",
- "",
-}
-
-// func TestAccessControlLoadSimple(t *testing.T) {
-// assert := assert.New(t)
-// data := `
-// global:
-// init:
-// - admin
-// read:
-// - everyone
-// patterns:
-// hgkeeper:
-// read:
-// - admin
-// `
+ ac, err := loadAccessControl(strings.NewReader(data))
+ assert.Nil(t, err)
-// ac, err := loadAccessControl(strings.NewReader(data))
-// assert.Nil(err)
-
-// assert.NotNil(ac)
-// assert.Equal(
-// acl{
-// Init: []string{"admin"},
-// Read: []string{"everyone"},
-// Write: nil,
-// },
-// ac.Global,
-// )
-// assert.Equal(
-// acl{
-// Init: nil,
-// Read: []string{"admin"},
-// Write: nil,
-// },
-// ac.Patterns["hgkeeper"],
-// )
-// }
+ assert.NotNil(t, ac)
+ assert.Equal(
+ t,
+ acl{
+ Init: []string{"admin"},
+ Read: []string{"everyone"},
+ Write: nil,
+ },
+ ac.Global,
+ )
+ assert.Equal(
+ t,
+ acl{
+ Init: nil,
+ Read: []string{"admin"},
+ Write: nil,
+ },
+ ac.Patterns["hgkeeper"],
+ )
+}
func TestIsPublic(t *testing.T) {
tests := []struct {
@@ -83,8 +85,6 @@
{"上市", false},
}
for _, v := range tests {
- if e := isPublic([]byte(v.u)); e != v.expect {
- t.Fatalf("isPublic(%s): expected: %v got: %v", v.u, v.expect, e)
- }
+ assert.Equal(t, v.expect, isPublic([]byte(v.u)))
}
}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/globals/globals.go Mon Jul 22 22:12:09 2019 -0500
@@ -0,0 +1,6 @@
+package globals
+
+type Globals struct {
+ ReposPath string `kong:"flag,name='repos-path',env='HGK_REPOS_PATH',default='repos',help='the directory where the repositories are stored'"`
+ AdminRepo string `kong:"flag,name='admin-repo',env='HGK_ADMIN_REPO',default='hgkeeper',help='the name of the admin repo to create/use'"`
+}
--- a/main.go Tue May 07 22:41:21 2019 -0500
+++ b/main.go Mon Jul 22 22:12:09 2019 -0500
@@ -6,14 +6,15 @@
"github.com/alecthomas/kong"
log "github.com/sirupsen/logrus"
+ "bitbucket.org/rw_grim/hgkeeper/globals"
"bitbucket.org/rw_grim/hgkeeper/serve"
"bitbucket.org/rw_grim/hgkeeper/setup"
)
type commands struct {
- ReposPath string `kong:"flag,name='repos-path',default='repos',help='the directory where the repository are stored'"`
- Serve serve.Command `kong:"cmd,help='run the ssh server'"`
- Setup setup.Command `kong:"cmd,help='inital setup for the server'"`
+ globals.Globals
+ Serve serve.Command `kong:"cmd,help='run the ssh server'"`
+ Setup setup.Command `kong:"cmd,help='inital setup for the server'"`
}
func init() {
@@ -34,6 +35,6 @@
func main() {
cmd := commands{}
ctx := kong.Parse(&cmd)
- err := ctx.Run(cmd.ReposPath)
+ err := ctx.Run(&cmd.Globals)
ctx.FatalIfErrorf(err)
}
--- a/serve/command.go Tue May 07 22:41:21 2019 -0500
+++ b/serve/command.go Mon Jul 22 22:12:09 2019 -0500
@@ -1,16 +1,17 @@
package serve
import (
+ "bitbucket.org/rw_grim/hgkeeper/globals"
"bitbucket.org/rw_grim/hgkeeper/ssh"
)
type Command struct {
- SSHAddr string `kong:"flag,name='ssh-listen-addr',short='l',help='what address to listen on',default=':22222'"`
- SSHHostKeysPath string `kong:"flag,name='ssh-host-keys-path',short='H',help='the path where host keys are kept',default='host-keys'"`
+ SSHAddr string `kong:"flag,name='ssh-listen-addr',env='HGK_SSH_LISTEN_ADDR',short='l',help='what address to listen on',default=':22222'"`
+ SSHHostKeysPath string `kong:"flag,name='ssh-host-keys-path',env='HGK_SSH_HOST_KEYS_PATH',short='H',help='the path where host keys are kept',default='host-keys'"`
}
-func (c *Command) Run(reposPath string) error {
- s, err := ssh.NewServer(c.SSHHostKeysPath, reposPath)
+func (c *Command) Run(g *globals.Globals) error {
+ s, err := ssh.NewServer(c.SSHHostKeysPath, g.ReposPath, g.AdminRepo)
if err != nil {
return err
}
--- a/setup/command.go Tue May 07 22:41:21 2019 -0500
+++ b/setup/command.go Mon Jul 22 22:12:09 2019 -0500
@@ -6,12 +6,13 @@
"os/exec"
"path/filepath"
+ log "github.com/sirupsen/logrus"
+
+ "bitbucket.org/rw_grim/hgkeeper/globals"
"bitbucket.org/rw_grim/hgkeeper/hg"
)
-type Command struct {
- AdminRepo string `kong:"flag,name='admin-repo',default='hgkeeper',help='the name of the admin repo to create'"`
-}
+type Command struct{}
var (
accessYmlFilename = "access.yml"
@@ -68,9 +69,12 @@
return err
}
-func (c *Command) createAdminRepo(reposPath string) error {
- path := filepath.Join(reposPath, c.AdminRepo)
+func (c *Command) createAdminRepo(reposPath, adminRepo string) error {
+ log.Errorf("reposPath: %q", reposPath)
+ log.Errorf("adminRepo: %q", adminRepo)
+ path := filepath.Join(reposPath, adminRepo)
+ log.Infof("creating %q", path)
// create the admin repo
if err := runCmd(hg.Init(path)); err != nil {
return err
@@ -107,8 +111,8 @@
return nil
}
-func (c *Command) Run(reposPath string) error {
- if err := c.createAdminRepo(reposPath); err != nil {
+func (c *Command) Run(g *globals.Globals) error {
+ if err := c.createAdminRepo(g.ReposPath, g.AdminRepo); err != nil {
// do clean up
return err
}
--- a/ssh/server.go Tue May 07 22:41:21 2019 -0500
+++ b/ssh/server.go Mon Jul 22 22:12:09 2019 -0500
@@ -17,7 +17,7 @@
listener net.Listener
}
-func NewServer(hostKeysPath, reposPath string) (*Server, error) {
+func NewServer(hostKeysPath, reposPath, adminRepo string) (*Server, error) {
s := &Server{
reposPath: reposPath,
}
@@ -30,7 +30,7 @@
if err = s.setHostKeysPath(hostKeysPath); err != nil {
return nil, err
}
- if s.a, err = access.New(s.reposPath); err != nil {
+ if s.a, err = access.New(s.reposPath, adminRepo); err != nil {
return nil, err
}
return s, nil
@@ -127,6 +127,7 @@
req.Reply(false, nil)
}
+ log.Warnf("running %#v\n", cmd)
if err := cmd.run(conn, serverConn, req); err != nil {
log.Warnf("%s command %q failed: %v", serverConn.RemoteAddr(), rawCmd, err)
req.Reply(false, nil)