Skip to content

Commit

Permalink
user: Add mysql userdb implementation.
Browse files Browse the repository at this point in the history
  • Loading branch information
amass01 authored Jun 17, 2021
1 parent 431a94b commit 0b1982a
Show file tree
Hide file tree
Showing 18 changed files with 2,478 additions and 161 deletions.
5 changes: 4 additions & 1 deletion politeiad/backendv2/tstorebe/store/mysql/mysql.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (
"github.com/decred/politeia/politeiad/backendv2/tstorebe/store"
"github.com/decred/politeia/util"

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

Expand Down Expand Up @@ -278,7 +279,9 @@ func (s *mysql) Close() {
s.db.Close()
}

func New(appDir, host, user, password, dbname string) (*mysql, error) {
// New connects to a mysql instance using the given connection params,
// and returns pointer to the created mysql struct.
func New(host, user, password, dbname string) (*mysql, error) {
// The password is required to derive the encryption key
if password == "" {
return nil, fmt.Errorf("password not provided")
Expand Down
4 changes: 2 additions & 2 deletions politeiad/backendv2/tstorebe/tstore/tstore.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ const (
// store to a leveldb instance.
DBTypeLevelDB = "leveldb"

// DBTypeLevelDB is a config option that sets the backing key-value
// DBTypeMySQL is a config option that sets the backing key-value
// store to a MySQL instance.
DBTypeMySQL = "mysql"

Expand Down Expand Up @@ -227,7 +227,7 @@ func New(appDir, dataDir string, anp *chaincfg.Params, tlogHost, tlogPass, dbTyp
case DBTypeMySQL:
// Example db name: testnet3_unvetted_kv
dbName := fmt.Sprintf("%v_kv", anp.Name)
kvstore, err = mysql.New(appDir, dbHost, dbUser, dbPass, dbName)
kvstore, err = mysql.New(dbHost, dbUser, dbPass, dbName)
if err != nil {
return nil, err
}
Expand Down
35 changes: 35 additions & 0 deletions politeiawww/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,41 @@ can be skipped if you already completed it while setting up politeiad.
$ go install -v ./...
```

### Setup MySQL (optional)

This repo includes a script to setup a MySQL user database, it creates the
needed databases, the politeiawww user and assigns user privileges. Password
authentication is used for all database connections.

**Note:** This is an optional step. By default, politeiawww will use a LevelDB
instance that does not require any additional setup.

The setup script assumes MySQL is running on localhost:3306 and the users will
be accessing the databse from localhost. See the setup script comments for more
complex setups.

Run the following commands. You will need to replace rootpass with the existing
password of your root user. The politeiawwwpass is the password that will be
set for the politeiawww user when it's created.

```
$ cd $GOPATH/src/github.com/decred/politeia/scripts/userdb
$ env \
MYSQL_ROOT_PASSWORD=rootpass \
MYSQL_POLITEIAWWW_PASSWORD=politeiawwwpass \
./mysqlsetup.sh
```

You will need to use the `--userdb=mysql` flag when starting politeiawww or
add `userdb=mysql` to the `politeiawww.config` file that is setup in the steps
below.

Also, an encryption key is required when using a MySQL database, use
`politeiawww_dbutil` cmd tool to create one:
```
politeiawww_dbutil -createkey
```

### Setup and run politeiawww

[politeiad](https://github.com/decred/politeia/tree/master/politeiad#politeiad)
Expand Down
73 changes: 42 additions & 31 deletions politeiawww/cmd/politeiawww_dbutil/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -17,14 +17,16 @@ You can specify the following options:
Use LevelDB
-cockroachdb
Use CockroachDB
-mysql
Use MySQL

Application options
-testnet
Use testnet database
-datadir string
politeiawww data directory
(default osDataDir/politeiawww/data)
-host string
-cockroachdbhost string
CockroachDB ip:port
(default localhost:26257)
-rootcert string
Expand All @@ -37,54 +39,59 @@ You can specify the following options:
File containing the CockroachDB SSL client cert key
(default ~/.cockroachdb/certs/clients/politeiawww/client.politeiawww.key)
-encryptionkey string
File containing the CockroachDB encryption key
File containing the CockroachDB/MySQL encryption key
(default osDataDir/politeiawww/sbox.key)
-password string
MySQL database password.
-mysqlhost string
MySQL ip:port
(default localhost:3306)

Commands
-addcredits
Add proposal credits to a user's account
Required DB flag : -leveldb or -cockroachdb
Required DB flag : -leveldb, -cockroachdb or -mysql
LevelDB args : <email> <quantity>
CockroachDB args : <username> <quantity>
-setadmin
Set the admin flag for a user
Required DB flag : -leveldb or -cockroachdb
Required DB flag : -leveldb, -cockroachdb or -mysql
LevelDB args : <email> <true/false>
CockroachDB args : <username> <true/false>
-setemail
Set a user's email to the provided email address
Required DB flag : -cockroachdb
Required DB flag : -cockroachdb or -mysql
CockroachDB args : <username> <email>
-stubusers
Create user stubs for the public keys in a politeia repo
Required DB flag : -leveldb or -cockroachdb
Required DB flag : -leveldb, -cockroachdb or -mysql
LevelDB args : <importDir>
CockroachDB args : <importDir>
-dump
Dump the entire database or the contents of a specific user
Required DB flag : -leveldb
LevelDB args : <email>
LevelDB args : <username>
-createkey
Create a new encryption key that can be used to encrypt data at rest
Required DB flag : None
Args : <destination (optional)>
(default osDataDir/politeiawww/sbox.key)
-migrate
Migrate a LevelDB user database to CockroachDB
Migrate from one user database to another
Required DB flag : None
Args : None
-verifyidentities
Verify a user's identities do not violate any politeia rules. Invalid
identities are fixed.
Required DB flag : -cockroachdb
Args : <username>

Args : <fromDB> <toDB>
Valid DBs are mysql, cockroachdb, leveldb
-verifyidentities
Verify a user's identities do not violate any politeia rules. Invalid
identities are fixed.
Required DB flag : -cockroachdb or -mysql
Args : <username>
-resettotp
Reset a user's totp settings in case they are locked out and
confirm identity.
Required DB flag : -leveldb or -cockroachdb
LevelDB args : <email>
CockroachDB args : <username>
Reset a user's totp settings in case they are locked out and
confirm identity.
Required DB flag : -leveldb, -cockroachdb or -mysql
LevelDB args : <email>
CockroachDB args : <username>

### Examples

Expand All @@ -96,13 +103,17 @@ Testnet example:

$ politeiawww_dbutil -testnet -cockroachdb -setadmin username true

### Migrate from LevelDB to CockroachDB
### Migrate user database

The `-migrate` command allows you to migrate from one database type to another.

**Notes:**
- CockroachDB & MySQL encrypt data at rest so if you migrating from levelDB
you will first need to create an encryption key using the `-createkey` command.

The `-migrate` command allows you to migrate a LevelDB instance to CockroachDB.
CockroachDB encrypts data at rest so you will first need to create an
encryption key using the `-createkey` command. The flags `-datadir`, `-host`,
`-rootcert`, `-clientcert`, `-clientkey`, and `-encryptionkey` only need to be
set if they deviate from the defaults.
- The flags `-datadir`, `-cockroachdbhost`, `-rootcert`, `-clientcert`,
`-clientkey`, `-encryptionkey` and `mysqlhost` only need to be set if they
deviate from the defaults.

Create an encryption key.

Expand All @@ -111,18 +122,18 @@ Create an encryption key.

Migrate the user database.

$ politeiawww_dbutil -migrate
LevelDB : ~/.politeiawww/data/mainnet/users
CockroachDB : localhost:26257 mainnet
Migrating records from LevelDB to CockroachDB...
$ politeiawww_dbutil -testnet -password grrr -migrate cockroachdb mysql
CockroachDB : localhost:26257 testnet3
MySQL : localhost:3306 testnet3
Migrating records from cockroachdb to mysql...
Users migrated : 6
Paywall index : 5
Done!

Update your politeiawww.conf file. The location of the encryption key may
differ depending on your operating system.

userdb=cockroachdb
userdb=mysql
encryptionkey=~/.politeiawww/sbox.key

### Stubbing Users
Expand Down
Loading

0 comments on commit 0b1982a

Please sign in to comment.