Skip to content

Commit a6bb6fa

Browse files
authored
Merge pull request #591 from jfontan/feature/add-new-auth-interface
Use new auth interface
2 parents be58625 + 0c95def commit a6bb6fa

File tree

29 files changed

+947
-80
lines changed

29 files changed

+947
-80
lines changed

Gopkg.lock

Lines changed: 3 additions & 2 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Gopkg.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[[constraint]]
22
name = "gopkg.in/src-d/go-mysql-server.v0"
3-
revision = "a9eddbfbf43f3b79fb6012fec948354222a9f2da"
3+
revision = "33657588d2ed14ade95deed511444b3def865b2b"
44

55
[[constraint]]
66
name = "github.com/jessevdk/go-flags"

cmd/gitbase/command/server.go

Lines changed: 29 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package command
22

33
import (
4+
"fmt"
45
"net"
56
"os"
67
"path/filepath"
@@ -18,6 +19,7 @@ import (
1819
"github.com/uber/jaeger-client-go/config"
1920
"gopkg.in/src-d/go-git.v4/plumbing/cache"
2021
sqle "gopkg.in/src-d/go-mysql-server.v0"
22+
"gopkg.in/src-d/go-mysql-server.v0/auth"
2123
"gopkg.in/src-d/go-mysql-server.v0/server"
2224
"gopkg.in/src-d/go-mysql-server.v0/sql"
2325
"gopkg.in/src-d/go-mysql-server.v0/sql/analyzer"
@@ -36,8 +38,9 @@ const (
3638

3739
// Server represents the `server` command of gitbase cli tool.
3840
type Server struct {
39-
engine *sqle.Engine
40-
pool *gitbase.RepositoryPool
41+
engine *sqle.Engine
42+
pool *gitbase.RepositoryPool
43+
userAuth auth.Auth
4144

4245
Name string `long:"db" default:"gitbase" description:"Database name"`
4346
Version string // Version of the application.
@@ -47,13 +50,14 @@ type Server struct {
4750
Port int `short:"p" long:"port" default:"3306" description:"Port where the server is going to listen"`
4851
User string `short:"u" long:"user" default:"root" description:"User name used for connection"`
4952
Password string `short:"P" long:"password" default:"" description:"Password used for connection"`
53+
UserFile string `short:"U" long:"user-file" env:"GITBASE_USER_FILE" default:"" description:"JSON file with credentials list"`
5054
ConnTimeout int `short:"t" long:"timeout" env:"GITBASE_CONNECTION_TIMEOUT" description:"Timeout in seconds used for connections"`
5155
IndexDir string `short:"i" long:"index" default:"/var/lib/gitbase/index" description:"Directory where the gitbase indexes information will be persisted." env:"GITBASE_INDEX_DIR"`
5256
CacheSize cache.FileSize `long:"cache" default:"512" description:"Object cache size in megabytes" env:"GITBASE_CACHESIZE_MB"`
5357
Parallelism uint `long:"parallelism" description:"Maximum number of parallel threads per table. By default, it's the number of CPU cores. 0 means default, 1 means disabled."`
5458
DisableSquash bool `long:"no-squash" description:"Disables the table squashing."`
5559
TraceEnabled bool `long:"trace" env:"GITBASE_TRACE" description:"Enables jaeger tracing"`
56-
ReadOnly bool `short:"r" long:"readonly" description:"Only allow read queries. This disables creating and deleting indexes as well." env:"GITBASE_READONLY"`
60+
ReadOnly bool `short:"r" long:"readonly" description:"Only allow read queries. This disables creating and deleting indexes as well. Cannot be used with --user-file." env:"GITBASE_READONLY"`
5761
SkipGitErrors bool // SkipGitErrors disables failing when Git errors are found.
5862
DisableGit bool `long:"no-git" description:"disable the load of git standard repositories."`
5963
DisableSiva bool `long:"no-siva" description:"disable the load of siva files."`
@@ -69,16 +73,13 @@ func (l *jaegerLogrus) Error(s string) {
6973
}
7074

7175
func NewDatabaseEngine(
72-
readonly bool,
76+
userAuth auth.Auth,
7377
version string,
7478
parallelism int,
7579
squash bool,
7680
) *sqle.Engine {
7781
catalog := sql.NewCatalog()
78-
ab := analyzer.NewBuilder(catalog)
79-
if readonly {
80-
ab = ab.ReadOnly()
81-
}
82+
ab := analyzer.NewBuilder(catalog).WithAuth(userAuth)
8283

8384
if parallelism == 0 {
8485
parallelism = runtime.NumCPU()
@@ -107,6 +108,24 @@ func (c *Server) Execute(args []string) error {
107108
logrus.SetLevel(logrus.DebugLevel)
108109
}
109110

111+
var err error
112+
if c.UserFile != "" {
113+
if c.ReadOnly {
114+
return fmt.Errorf("cannot use both --user-file and --readonly")
115+
}
116+
117+
c.userAuth, err = auth.NewNativeFile(c.UserFile)
118+
if err != nil {
119+
return err
120+
}
121+
} else {
122+
permissions := auth.AllPermissions
123+
if c.ReadOnly {
124+
permissions = auth.ReadPerm
125+
}
126+
c.userAuth = auth.NewNativeSingle(c.User, c.Password, permissions)
127+
}
128+
110129
if err := c.buildDatabase(); err != nil {
111130
logrus.WithField("error", err).Fatal("unable to initialize database engine")
112131
return err
@@ -153,7 +172,7 @@ func (c *Server) Execute(args []string) error {
153172
server.Config{
154173
Protocol: "tcp",
155174
Address: hostString,
156-
Auth: auth,
175+
Auth: c.userAuth,
157176
Tracer: tracer,
158177
ConnReadTimeout: timeout,
159178
ConnWriteTimeout: timeout,
@@ -174,7 +193,7 @@ func (c *Server) Execute(args []string) error {
174193
func (c *Server) buildDatabase() error {
175194
if c.engine == nil {
176195
c.engine = NewDatabaseEngine(
177-
c.ReadOnly,
196+
c.userAuth,
178197
c.Version,
179198
int(c.Parallelism),
180199
!c.DisableSquash,

docs/using-gitbase/configuration.md

Lines changed: 42 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
| `GITBASE_UAST_CACHE_SIZE` | size of the cache for the `uast` and `uast_mode` UDFs. The size is the maximum number of elements kept in the cache, 10000 by default |
1616
| `GITBASE_CACHESIZE_MB` | size of the cache for git objects specified as MB |
1717
| `GITBASE_CONNECTION_TIMEOUT` | timeout in seconds used for client connections on write and reads. No timeout by default. |
18+
| `GITBASE_USER_FILE` | JSON file with user credentials |
1819

1920
### Jaeger tracing variables
2021

@@ -75,6 +76,7 @@ Help Options:
7576
-p, --port= Port where the server is going to listen (default: 3306)
7677
-u, --user= User name used for connection (default: root)
7778
-P, --password= Password used for connection
79+
-U, --user-file= JSON file with credentials list [$GITBASE_USER_FILE]
7880
-t, --timeout= Timeout in seconds used for connections [$GITBASE_CONNECTION_TIMEOUT]
7981
-i, --index= Directory where the gitbase indexes information will be persisted. (default:
8082
/var/lib/gitbase/index) [$GITBASE_INDEX_DIR]
@@ -83,10 +85,48 @@ Help Options:
8385
means default, 1 means disabled.
8486
--no-squash Disables the table squashing.
8587
--trace Enables jaeger tracing [$GITBASE_TRACE]
86-
-r, --readonly Only allow read queries. This disables creating and deleting indexes as well.
87-
[$GITBASE_READONLY]
88+
-r, --readonly Only allow read queries. This disables creating and deleting indexes as well. Cannot be used
89+
with --user-file. [$GITBASE_READONLY]
8890
--no-git disable the load of git standard repositories.
8991
--no-siva disable the load of siva files.
9092
-v Activates the verbose mode
9193
9294
```
95+
## User credentials
96+
97+
User credentials can be specified in the command line or using a user file. For single user this can be done with parameters `--user` and `--password`:
98+
99+
```
100+
gitbase server --user root --password r00tp4ssword! -d /my/repositories/path
101+
```
102+
103+
If you want to have more than one user or do not have the password in plain text you can use a user file with this format:
104+
105+
```json
106+
[
107+
{
108+
"name": "root",
109+
"password": "*2470C0C06DEE42FD1618BB99005ADCA2EC9D1E19",
110+
"permissions": ["read", "write"]
111+
},
112+
{
113+
"name": "user",
114+
"password": "plain_passw0rd!"
115+
}
116+
]
117+
```
118+
119+
You can either specify a plain text password or hashed. Hashed version uses the same format as MySQL 5.x passwords. You can generate the native password with this command, remember to prefix the hash with `*`:
120+
121+
```
122+
echo -n password | openssl sha1 -binary | openssl sha1 | tr '[:lower:]' '[:upper:]'
123+
```
124+
125+
There are two permissions you can set to users, `read` and `write`. `read` only allows to execute queries. `write` is needed to create and delete indexes or lock tables. If no permissions are set for a user the default permission is `read`.
126+
127+
Then you can specify which user file to use with parameter `--user-file`:
128+
129+
```
130+
gitbase server --user-file /path/to/user-file.json -d /my/repositories/path
131+
```
132+

docs/using-gitbase/functions.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -95,4 +95,4 @@ Also, if you want to retrieve values from a non common property, you can pass it
9595
9696
## Standard functions
9797

98-
You can check standard functions in [`go-mysql-server` documentation](https://github.com/src-d/go-mysql-server/tree/a9eddbfbf43f3b79fb6012fec948354222a9f2da#custom-functions).
98+
You can check standard functions in [`go-mysql-server` documentation](https://github.com/src-d/go-mysql-server/tree/33657588d2ed14ade95deed511444b3def865b2b#custom-functions).

docs/using-gitbase/indexes.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,4 +10,4 @@ Note that you can create an index either **on one or more columns** or **on a si
1010

1111
You can find some more examples in the [examples](./examples.md#create-an-index-for-columns-on-a-table) section.
1212

13-
See [go-mysql-server](https://github.com/src-d/go-mysql-server/tree/a9eddbfbf43f3b79fb6012fec948354222a9f2da#indexes) documentation for more details
13+
See [go-mysql-server](https://github.com/src-d/go-mysql-server/tree/33657588d2ed14ade95deed511444b3def865b2b#indexes) documentation for more details
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
## Supported clients
22

3-
To see the supported MySQL clients and examples about how to use them, take a look [here](https://github.com/src-d/go-mysql-server/blob/a9eddbfbf43f3b79fb6012fec948354222a9f2da/SUPPORTED_CLIENTS.md).
3+
To see the supported MySQL clients and examples about how to use them, take a look [here](https://github.com/src-d/go-mysql-server/blob/33657588d2ed14ade95deed511444b3def865b2b/SUPPORTED_CLIENTS.md).
Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,3 @@
11
## Supported syntax
22

3-
To see the SQL subset currently supported take a look at [this list](https://github.com/src-d/go-mysql-server/blob/a9eddbfbf43f3b79fb6012fec948354222a9f2da/SUPPORTED.md) from [src-d/go-mysql-server](https://github.com/src-d/go-mysql-server).
3+
To see the SQL subset currently supported take a look at [this list](https://github.com/src-d/go-mysql-server/blob/33657588d2ed14ade95deed511444b3def865b2b/SUPPORTED.md) from [src-d/go-mysql-server](https://github.com/src-d/go-mysql-server).

integration_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ import (
1818
fixtures "gopkg.in/src-d/go-git-fixtures.v3"
1919
"gopkg.in/src-d/go-git.v4/plumbing/cache"
2020
sqle "gopkg.in/src-d/go-mysql-server.v0"
21+
"gopkg.in/src-d/go-mysql-server.v0/auth"
2122
"gopkg.in/src-d/go-mysql-server.v0/sql"
2223
"gopkg.in/src-d/go-mysql-server.v0/sql/analyzer"
2324
"gopkg.in/src-d/go-mysql-server.v0/sql/expression"
@@ -872,7 +873,8 @@ func newSquashEngine() *sqle.Engine {
872873

873874
func newBaseEngine() *sqle.Engine {
874875
foo := gitbase.NewDatabase("foo")
875-
engine := command.NewDatabaseEngine(false, "test", 0, false)
876+
au := new(auth.None)
877+
engine := command.NewDatabaseEngine(au, "test", 0, false)
876878

877879
engine.AddDatabase(foo)
878880
engine.Catalog.RegisterFunctions(function.Functions)

vendor/gopkg.in/src-d/go-mysql-server.v0/_example/main.go

Lines changed: 2 additions & 7 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)