Skip to content

Commit

Permalink
Merge pull request #99 from ohkinozomu/mysql-docker-compose
Browse files Browse the repository at this point in the history
Add Docker Compose for MySQL testing
  • Loading branch information
wind-c authored Jul 28, 2024
2 parents 5cb6e4c + d42bf46 commit 66f830c
Show file tree
Hide file tree
Showing 7 changed files with 75 additions and 2 deletions.
11 changes: 10 additions & 1 deletion .github/workflows/runtests.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -17,5 +17,14 @@ jobs:
- name: Build
run: go build ./...

- name: Docker compose up
run: |
docker compose up -d
until docker exec mysql mysqladmin ping -h "127.0.0.1" --silent; do
echo 'waiting for mysql...'
sleep 3
done
- name: Test
run: go test ./...
run: go test -v ./...
24 changes: 24 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,30 @@ if err != nil {
}
println("Password hash for MQTT client: ", hashed)
```
### MySQL

The schema required is as follows:
```sql
BEGIN;
CREATE TABLE auth (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
allow SMALLINT DEFAULT 1 NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NULL
);
CREATE TABLE acl (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
topic VARCHAR(255) NOT NULL,
access SMALLINT DEFAULT 3 NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NULL
);
CREATE INDEX acl_username_idx ON acl(username);
COMMIT;
```
### Access Control
#### Allow Hook
By default, Comqtt uses a DENY-ALL access control rule. To allow connections, this must overwritten using an Access Control hook. The simplest of these hooks is the `auth.AllowAll` hook, which provides ALLOW-ALL rules to all connections, subscriptions, and publishing. It's also the simplest hook to use:
Expand Down
12 changes: 12 additions & 0 deletions docker-compose.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
services:
mysql:
image: mysql:8.0
container_name: mysql
environment:
MYSQL_ROOT_PASSWORD: 12345678
MYSQL_DATABASE: comqtt
ports:
- "3306:3306"
command: --character-set-server=utf8 --collation-server=utf8_general_ci
volumes:
- ./plugin/auth/mysql/testdata/init.sql:/docker-entrypoint-initdb.d/init.sql
1 change: 1 addition & 0 deletions plugin/auth/crypto.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"crypto/sha512"
"encoding/base64"
"encoding/hex"

"golang.org/x/crypto/bcrypt"
)

Expand Down
2 changes: 1 addition & 1 deletion plugin/auth/mysql/mysql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
"github.com/wind-c/comqtt/v2/plugin"
)

const path = "./conf.yml"
const path = "./testdata/conf.yml"

var (
// Currently, the input is directed to /dev/null. If you need to
Expand Down
File renamed without changes.
27 changes: 27 additions & 0 deletions plugin/auth/mysql/testdata/init.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
BEGIN;

CREATE TABLE auth (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL UNIQUE,
password VARCHAR(255) NOT NULL,
allow SMALLINT DEFAULT 1 NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NULL
);

CREATE TABLE acl (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(255) NOT NULL,
topic VARCHAR(255) NOT NULL,
access SMALLINT DEFAULT 3 NOT NULL,
created TIMESTAMP DEFAULT CURRENT_TIMESTAMP,
updated TIMESTAMP NULL
);

CREATE INDEX acl_username_idx ON acl(username);

-- 123456
INSERT INTO auth (username, password, allow) VALUES ('zhangsan', '$2a$12$j8bs10UCRC5GUENPqZXLceACpN1l72wcDaN6F0j0rIbcHIZpt0Cbq', 1);
INSERT INTO acl (username, topic, access) VALUES ('zhangsan', 'topictest/1', 2);

COMMIT;

0 comments on commit 66f830c

Please sign in to comment.