Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

TLS for mysql #211

Merged
merged 6 commits into from
May 26, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 21 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,8 @@ I recommend checking out [Chronos](https://github.com/airbnb/chronos). This is d
Once you have installed Kala onto the machine you would like to use, you can follow the below steps to start using it.

To Run Kala:
```bash

```console
$ kala run
INFO[0000] Preparing cache
INFO[0000] Starting server on port :8000
Expand All @@ -48,30 +49,44 @@ INFO[0000] Preparing cache
INFO[0000] Starting server on port :2222
```

Kala uses BoltDB by default for the job database
use Redis by using the jobDB and jobDBAddress params:
Kala uses BoltDB by default for the job database by using `jobDB` and `boltpath` params:

```bash
kala run --jobDB=boltdb --boltpath=/path/to/dir
```

use Redis by using the `jobDB` and `jobDBAddress` params:

```bash
kala run --jobDB=redis --jobDBAddress=127.0.0.1:6379
```
use Consul by using the jobDB and jobDBAddress params:

use Consul by using the `jobDB` and `jobDBAddress` params:

```bash
kala run --jobDB=consul --jobDBAddress=127.0.0.1:8500
```

use Mongo by using the jobDB, jobDBAddress, jobDBUsername, and jobDBPassword params:
use Mongo by using the `jobDB`, `jobDBAddress`, `jobDBUsername`, and `jobDBPassword` params:

```bash
kala run --jobDB=mongo --jobDBAddress=server1.example.com,server2.example.com --jobDBUsername=admin --jobDBPassword=password
```

use Postgres by using the jobDB, jobDBAddress params:
use Postgres by using the `jobDB`, `jobDBAddress` params:

```bash
kala run --jobDB=postgres --jobDBAddress=server1.example.com/kala --jobDBUsername=admin --jobDBPassword=password
```

use MariaDB, MySQL by using the `jobDB`, `jobDBAddress`, `jobDBTlsCAPath`, `jobDBTlsCertPath`, `jobDBTlsKeyPath`, `jobDBTlsServerName` params:

```bash
kala run --jobDB=mariadb --jobDBAddress=(server1.example.com)/kala --jobDBUsername=admin --jobDBPassword=password

kala run --jobDB=mysql --jobDBAddress="tcp(server1.example.com:3306)/kala?tls=custom" --jobDBUsername=admin --jobDBPassword=password --jobDBTlsCAPath=/path/to/server-ca.pem --jobDBTlsCertPath=/path/to/client-cert.pem --jobDBTlsKeyPath=/path/to/client-key.pem --jobDBTlsServerName=server1.example.com
```

Kala runs on `127.0.0.1:8000` by default. You can easily test it out by curling the metrics path.

```bash
Expand Down
54 changes: 50 additions & 4 deletions cmd/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"fmt"
"strings"
"time"
"crypto/tls"
"crypto/x509"
"io/ioutil"

"github.com/ajvb/kala/api"
"github.com/ajvb/kala/job"
Expand Down Expand Up @@ -80,7 +83,7 @@ func init() {
cli.StringFlag{
Name: "jobDB",
Value: "boltdb",
Usage: "Implementation of job database, either 'boltdb', 'redis', 'mongo', 'consul', or 'postgres'.",
Usage: "Implementation of job database, either 'boltdb', 'redis', 'mongo', 'consul', 'postgres', 'mariadb', or 'mysql'.",
},
cli.StringFlag{
Name: "boltpath",
Expand All @@ -95,13 +98,33 @@ func init() {
cli.StringFlag{
Name: "jobDBUsername",
Value: "",
Usage: "Username for the job database, in 'username' format. Currently only needed for Mongo.",
Usage: "Username for the job database, in 'username' format.",
},
cli.StringFlag{
Name: "jobDBPassword",
Value: "",
Usage: "Password for the job database, in 'password' format.",
},
cli.StringFlag{
Name: "jobDBTlsCAPath",
Value: "",
Usage: "Path to tls server CA file for the job database.",
},
cli.StringFlag{
Name: "jobDBTlsCertPath",
Value: "",
Usage: "Path to tls client cert file for the job database.",
},
cli.StringFlag{
Name: "jobDBTlsKeyPath",
Value: "",
Usage: "Path to tls client key file for the job database.",
},
cli.StringFlag{
Name: "jobDBTlsServerName",
Value: "",
Usage: "Server name to verify cert for the job database.",
},
cli.BoolFlag{
Name: "verbose, v",
Usage: "Set for verbose logging.",
Expand Down Expand Up @@ -165,9 +188,32 @@ func init() {
case "postgres":
dsn := fmt.Sprintf("postgres://%s:%s@%s", c.String("jobDBUsername"), c.String("jobDBPassword"), c.String("jobDBAddress"))
db = postgres.New(dsn)
case "mysql":
case "mysql", "mariadb":
dsn := fmt.Sprintf("%s:%s@%s", c.String("jobDBUsername"), c.String("jobDBPassword"), c.String("jobDBAddress"))
db = mysql.New(dsn)
if c.String("jobDBTlsCAPath") != "" {
// https://godoc.org/github.com/go-sql-driver/mysql#RegisterTLSConfig
rootCertPool := x509.NewCertPool()
pem, err := ioutil.ReadFile(c.String("jobDBTlsCAPath"))
if err != nil {
log.Fatal(err)
}
if ok := rootCertPool.AppendCertsFromPEM(pem); !ok {
log.Fatal("Failed to append PEM.")
}
clientCert := make([]tls.Certificate, 0, 1)
certs, err := tls.LoadX509KeyPair(c.String("jobDBTlsCertPath"), c.String("jobDBTlsKeyPath"))
if err != nil {
log.Fatal(err)
}
clientCert = append(clientCert, certs)
db = mysql.New(dsn, &tls.Config{
RootCAs: rootCertPool,
Certificates: clientCert,
ServerName: c.String("jobDBTlsServerName"),
})
} else {
db = mysql.New(dsn, nil)
}
default:
log.Fatalf("Unknown Job DB implementation '%s'", c.String("jobDB"))
}
Expand Down
12 changes: 10 additions & 2 deletions job/storage/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,9 @@ import (
"database/sql"
"encoding/json"
"fmt"
"crypto/tls"

_ "github.com/go-sql-driver/mysql"
"github.com/go-sql-driver/mysql"

"github.com/jmoiron/sqlx"

Expand All @@ -23,7 +24,14 @@ type DB struct {
}

// New instantiates a new DB.
func New(dsn string) *DB {
func New(dsn string, tlsConfig *tls.Config) *DB {
if tlsConfig != nil {
log.Infof("Register TLS config")
err := mysql.RegisterTLSConfig("custom", tlsConfig)
if err != nil {
log.Fatal(err)
}
}
connection, err := sqlx.Open("mysql", dsn)
if err != nil {
log.Fatal(err)
Expand Down
4 changes: 2 additions & 2 deletions job/storage/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,7 +136,7 @@ func TestRealDb(t *testing.T) {
}
}

db := New(dsn)
db := New(dsn, nil)
cache := job.NewLockFreeJobCache(db)

genericMockJobOne := job.GetMockJobWithGenericSchedule(time.Now())
Expand Down Expand Up @@ -229,7 +229,7 @@ func TestPersistEpsilon(t *testing.T) {
}
}

db := New(dsn)
db := New(dsn, nil)
defer db.Close()

cache := job.NewMemoryJobCache(db)
Expand Down