Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 4 additions & 1 deletion .travis.yml
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ install:

# Used by the certdb tests
services:
- mysql
- postgresql
before_install:
# CFSSL consists of multiple Go packages, which refer to each other by
Expand Down Expand Up @@ -47,6 +48,8 @@ before_script:
- if [[ $(uname -s) == 'Linux' ]]; then
psql -c 'create database certdb_development;' -U postgres;
goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/pg up;
mysql -e 'create database certdb_development;' -u root;
goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/mysql up;
fi
script:
- ./test.sh
Expand All @@ -64,7 +67,7 @@ env:
- secure: "OmaaZ3jhU9VQ/0SYpenUJEfnmKy/MwExkefFRpDbkRSu/hTQpxxALAZV5WEHo7gxLRMRI0pytLo7w+lAd2FlX1CNcyY62MUicta/8P2twsxp+lR3v1bJ7dwk6qsDbO7Nvv3BKPCDQCHUkggbAEJaHEQGdLk4ursNEB1aGimuCEc="
- GO15VENDOREXPERIMENT=1
matrix:
- BUILD_TAGS="postgresql"
- BUILD_TAGS="postgresql mysql"
matrix:
include:
- os: osx
Expand Down
29 changes: 21 additions & 8 deletions certdb/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -16,21 +16,26 @@ A database is required for the following:

This directory stores [goose](https://bitbucket.org/liamstask/goose/) db migration scripts for various DB backends.
Currently supported:
- SQLite in sqlite
- MySQL in mysql
- PostgreSQL in pg
- SQLite in sqlite

### Get goose

go get bitbucket.org/liamstask/goose/cmd/goose

### Use goose to start and terminate a SQLite DB
To start a SQLite DB using goose:
### Use goose to start and terminate a MySQL DB
To start a MySQL using goose:

goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/sqlite up'
goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/mysql up

To tear down a SQLite DB using goose
To tear down a MySQL DB using goose

goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/sqlite down
goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/mysql down

Note: the administration of MySQL DB is not included. We assume
the databases being connected to are already created and access control
is properly handled.

### Use goose to start and terminate a PostgreSQL DB
To start a PostgreSQL using goose:
Expand All @@ -43,7 +48,16 @@ To tear down a PostgreSQL DB using goose

Note: the administration of PostgreSQL DB is not included. We assume
the databases being connected to are already created and access control
are properly handled.
is properly handled.

### Use goose to start and terminate a SQLite DB
To start a SQLite DB using goose:

goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/sqlite up

To tear down a SQLite DB using goose

goose -path $GOPATH/src/github.com/cloudflare/cfssl/certdb/sqlite down

## CFSSL Configuration

Expand All @@ -55,4 +69,3 @@ JSON dictionary:
or

{"driver":"postgres","data_source":"postgres://user:password@host/db"}

15 changes: 15 additions & 0 deletions certdb/mysql/dbconf.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
development:
driver: mysql
open: root@tcp(localhost:3306)/certdb_development?parseTime=true

test:
driver: mysql
open: root@tcp(localhost:3306)/certdb_test?parseTime=true

staging:
driver: mysql
open: root@tcp(localhost:3306)/certdb_staging?parseTime=true

production:
driver: mysql
open: root@tcp(localhost:3306)/certdb_production?parseTime=true
28 changes: 28 additions & 0 deletions certdb/mysql/migrations/001_CreateCertificates.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,28 @@
-- +goose Up
-- SQL in section 'Up' is executed when this migration is applied

CREATE TABLE certificates (
serial_number varbinary(20) NOT NULL,
authority_key_identifier varbinary(128) NOT NULL,
ca_label varbinary(128),
status varbinary(128) NOT NULL,
reason int,
expiry timestamp DEFAULT '0000-00-00 00:00:00',
revoked_at timestamp DEFAULT '0000-00-00 00:00:00',
pem varbinary(4096) NOT NULL,
PRIMARY KEY(serial_number, authority_key_identifier)
);

CREATE TABLE ocsp_responses (
serial_number varbinary(20) NOT NULL,
authority_key_identifier varbinary(128) NOT NULL,
body varbinary(4096) NOT NULL,
expiry timestamp DEFAULT '0000-00-00 00:00:00',
PRIMARY KEY(serial_number, authority_key_identifier)
);

-- +goose Down
-- SQL section 'Down' is executed when this migration is rolled back

DROP TABLE certificates;
DROP TABLE ocsp_responses;
18 changes: 18 additions & 0 deletions certdb/sql/sql_mysql_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
// +build mysql

package sql

import (
"testing"

"github.com/cloudflare/cfssl/certdb/testdb"
)

func TestMySQL(t *testing.T) {
db := testdb.MySQLDB()
ta := TestAccessor{
Accessor: NewAccessor(db),
DB: db,
}
testEverything(ta, t)
}
44 changes: 38 additions & 6 deletions certdb/testdb/testdb.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,20 @@ package testdb

import (
"os"
"strings"

_ "github.com/go-sql-driver/mysql" // register mysql driver
"github.com/jmoiron/sqlx"
_ "github.com/lib/pq" // register postgresql driver
_ "github.com/mattn/go-sqlite3" // register sqlite3 driver
)

const (
mysqlTruncateTables = `
TRUNCATE certificates;
TRUNCATE ocsp_responses;
`

pgTruncateTables = `
CREATE OR REPLACE FUNCTION truncate_tables() RETURNS void AS $$
DECLARE
Expand All @@ -33,6 +40,24 @@ DELETE FROM ocsp_responses;
`
)

// MySQLDB returns a MySQL db instance for certdb testing.
func MySQLDB() *sqlx.DB {
connStr := "root@tcp(localhost:3306)/certdb_development?parseTime=true"

if dbURL := os.Getenv("DATABASE_URL"); dbURL != "" {
connStr = dbURL
}

db, err := sqlx.Open("mysql", connStr)
if err != nil {
panic(err)
}

Truncate(db)

return db
}

// PostgreSQLDB returns a PostgreSQL db instance for certdb testing.
func PostgreSQLDB() *sqlx.DB {
connStr := "dbname=certdb_development sslmode=disable"
Expand Down Expand Up @@ -63,19 +88,26 @@ func SQLiteDB(dbpath string) *sqlx.DB {
return db
}

// Truncate truncates teh DB
// Truncate truncates the DB
func Truncate(db *sqlx.DB) {
var sql string
var sql []string
switch db.DriverName() {
case "mysql":
sql = strings.Split(mysqlTruncateTables, "\n")
case "postgres":
sql = pgTruncateTables
sql = []string{pgTruncateTables}
case "sqlite3":
sql = sqliteTruncateTables
sql = []string{sqliteTruncateTables}
default:
panic("Unknown driver")
}

if _, err := db.Exec(sql); err != nil {
panic(err)
for _, expr := range sql {
if len(strings.TrimSpace(expr)) == 0 {
continue
}
if _, err := db.Exec(expr); err != nil {
panic(err)
}
}
}
5 changes: 3 additions & 2 deletions cmd/cfssl/cfssl.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,8 +43,9 @@ import (
"github.com/cloudflare/cfssl/cli/sign"
"github.com/cloudflare/cfssl/cli/version"

_ "github.com/lib/pq" // import to support Postgres
_ "github.com/mattn/go-sqlite3" // import to support SQLite3
_ "github.com/go-sql-driver/mysql" // import to support MySQL
_ "github.com/lib/pq" // import to support Postgres
_ "github.com/mattn/go-sqlite3" // import to support SQLite3
)

// main defines the cfssl usage and registers all defined commands and flags.
Expand Down
3 changes: 2 additions & 1 deletion cmd/multirootca/ca.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,8 @@ import (
"github.com/cloudflare/cfssl/signer/local"
"github.com/cloudflare/cfssl/whitelist"

_ "github.com/lib/pq" // import to support Postgres
_ "github.com/go-sql-driver/mysql" // import to support MySQL
_ "github.com/lib/pq" // import to support Postgres
)

func parseSigner(root *config.Root) (signer.Signer, error) {
Expand Down
Loading