rqlite 相關(guān)操作說(shuō)明
項(xiàng)目總為實(shí)現(xiàn) sqlite 添加賬號(hào)和密碼,以滿足信息安全的需求。
鑒于對(duì) sqlite 實(shí)現(xiàn)添加賬號(hào)和密碼比較復(fù)雜和困難,項(xiàng)目中使用了分布式 rqlite ,故轉(zhuǎn)而研究 rqlite 如何添加賬號(hào)和密碼。
項(xiàng)目概況:
開(kāi)發(fā)語(yǔ)言:Python
存儲(chǔ): sqlite (rqlite)
整體流程:
- rqlite 介紹
- rqlite CLI 客戶端介紹
- rqlite Basic Auth
- pyrqlite 介紹
- sqlalchemy-rqlite 介紹
1. rqlite 使用介紹
作者博客
rqlite : 基于 SQLite 構(gòu)建的輕量級(jí)、分布式關(guān)系數(shù)據(jù)庫(kù), 使用Go 編程實(shí)現(xiàn),使用 Raft 算法來(lái)確保所有 SQLite 數(shù)據(jù)庫(kù)實(shí)例的一致性。
- 下載地址和方式
以 Linux版本為例, 解壓之后:存在: rqlite 和 rqlited 兩個(gè)文件
- 啟動(dòng)服務(wù)
./rqlited /pathdir/rqlitedata
# 在 /pathdir/rqlitedata 目錄下存放數(shù)據(jù)文件和其他一些配置文件
peers.json raft.db snapshots
- 啟動(dòng)多個(gè)節(jié)點(diǎn)
rqlited -http-addr localhost:4003 -raft-addr localhost:4004 -join http://localhost:4001 /pathdir/rqlitedata1
rqlited -http-addr localhost:4005 -raft-addr localhost:4006 -join http://localhost:4001 /pathdir/rqlitedata2
rqlite 支持Securing 操作
- File system security
- Network security
- HTTPS API
- Node-to-node encryption
- Basic Auth
2. rqlite CLI 命令行使用介紹
啟動(dòng)服務(wù)后可以使用命令行工具進(jìn)行數(shù)據(jù)的增刪改查:
./rqlite
127.0.0.1:4001>
# sqlite 的語(yǔ)法的增刪改查都支持
演示示例:
$ ./rqlite
127.0.0.1:4001> CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT)
0 row affected (0.000362 sec)
127.0.0.1:4001> .tables
+------+
| name |
+------+
| foo |
+------+
127.0.0.1:4001> .schema
+---------------------------------------------------------------+
| sql |
+---------------------------------------------------------------+
| CREATE TABLE foo (id INTEGER NOT NULL PRIMARY KEY, name TEXT) |
+---------------------------------------------------------------+
127.0.0.1:4001> INSERT INTO foo(name) VALUES("fiona")
1 row affected (0.000117 sec)
127.0.0.1:4001> SELECT * FROM foo
+----+-------+
| id | name |
+----+-------+
| 1 | fiona |
+----+-------+
127.0.0.1:4001> quit
bye~
命令行工具的參數(shù)說(shuō)明
./rqlite -h
Options:
-h, --help
display help information
-s, --scheme[=http]
protocol scheme (http or https)
-H, --host[=127.0.0.1]
rqlited host address
-p, --port[=4001]
rqlited host port
-P, --prefix[=/]
rqlited HTTP URL prefix
-i, --insecure[=false]
do not verify rqlited HTTPS certificate
除命令行之外,rqlite 支持 HTTP API
通過(guò) HTTP API 完成對(duì)數(shù)據(jù)庫(kù)的增刪改查。
演示示例:
# 創(chuàng)建數(shù)據(jù)庫(kù)
curl -X POST 'localhost:4001/db/execute?pretty&timings' -H "Content-Type: application/json" -d '[
"CREATE TABLE foo (id integer not null primary key, name text)"
]'
# 返回是個(gè) json 格式的數(shù)據(jù)
{
"results": [
{
"last_insert_id": 1,
"rows_affected": 1,
"time": 0.00886
}
],
"time": 0.0152
}
# 查詢數(shù)據(jù)
curl -G 'localhost:4001/db/query?pretty&timings' --data-urlencode 'q=SELECT * FROM foo'
# 返回結(jié)果
{
"results": [
{
"columns": [
"id",
"name"
],
"types": [
"integer",
"text"
],
"values": [
[
1,
"fiona"
]
],
"time": 0.0150043
}
],
"time": 0.0220043
}
# 也有很好的錯(cuò)誤處理機(jī)制,比如查詢一個(gè)不存在的數(shù)據(jù)庫(kù),或者語(yǔ)法錯(cuò)誤,都能很好的捕獲
curl -XPOST 'localhost:4001/db/execute?pretty&timings' -H "Content-Type: application/json" -d "[
\"INSERT INTO nonsense\"
]"
{
"results": [
{
"error": "near \"nonsense\": syntax error"
}
],
"time": 2.478862
}
# 還可顯示響應(yīng)信息
curl -v -G 'localhost:4003/db/query?pretty&timings' --data-urlencode 'q=SELECT * FROM foo'
# 響應(yīng)信息
* About to connect() to localhost port 4001 (#0)
* Trying ::1...
* Connection refused
* Trying 127.0.0.1...
* Connected to localhost (127.0.0.1) port 4001 (#0)
* Server auth using Basic with user 'bob'
> GET /db/query?pretty&timings&q=SELECT%20%2A%20FROM%20foo HTTP/1.1
> Authorization: Basic Ym9iOnNlY3JldDE=
> User-Agent: curl/7.29.0
> Host: localhost:4001
> Accept: */*
>
< HTTP/1.1 200 OK
< Content-Type: application/json; charset=utf-8
< X-Rqlite-Version: v4.0.0
< Date: Tue, 14 Nov 2017 08:07:27 GMT
< Content-Length: 276
<
{
"results": [
{
"columns": [
"id",
"name"
],
"types": [
"integer",
"text"
],
"time": 0.000116084
}
],
"time": 0.000741562
* Connection #0 to host localhost left intact
3. rqlite Basic Auth
其他安全策略暫時(shí)不研究,研究認(rèn)證方式。
具體做法是:
配置文件: config.json 規(guī)定相應(yīng)用戶的操作權(quán)限
啟動(dòng)服務(wù)傳入配置文件
命令行操作 -H 傳入?yún)?shù)
HTTP API 則也需要傳入用戶名和密碼
演示:config.json
1. 用戶名、密碼和操作權(quán)限設(shè)置
[
{
"username": "bob",
"password": "secret1",
"perms": ["all"]
},
{
"username": "mary",
"password": "secret2",
"perms": ["query", "status"]
}
]
# 配置了兩個(gè)用戶名和密碼,兩個(gè)用戶的操作權(quán)限不一致,mary 用戶只要query , status 權(quán)限
注: 用戶權(quán)限包括:
- all: user can perform all operations on a node.
- execute: user may access the execute endpoint.
- query: user may access the query endpoint.
- load: user may load an SQLite dump file into a node.
- backup: user may perform backups.
- status: user can retrieve status information from the node.
- join: user can join a cluster. In practice only a node joins a cluster.
- remove: user can remove a node from a cluster.
2. 傳入認(rèn)證參數(shù)啟動(dòng)節(jié)點(diǎn)服務(wù)
./rqlited -auth=/root/config.json -http-addr localhost:4001 -raft-addr localhost:4002 /root/data/rqlite
3. 若使用命令界面訪問(wèn)認(rèn)證的數(shù)據(jù)庫(kù)
bob,secret1 為配置文件中的用戶名和密碼
./rqlite -H bob:secret1@localhost
bob:secret1@localhost:4001>
# 正常使用 SQL 實(shí)現(xiàn)增刪改查,不添加-H 參數(shù),報(bào)錯(cuò),無(wú)法讀取數(shù)據(jù)庫(kù)數(shù)據(jù)
4. 若使用HTTP API 的方式訪問(wèn)認(rèn)證的數(shù)據(jù)庫(kù)
訪問(wèn) API 有所差異
curl -G 'https://bob:secret1@localhost:4001/db/query?pretty&timings' \
--data-urlencode 'q=SELECT * FROM foo'
# API 中帶入了用戶名和密碼
4. pyrqlite 使用介紹
假設(shè)啟動(dòng)了節(jié)點(diǎn)服務(wù),那如何實(shí)現(xiàn)編程實(shí)現(xiàn)對(duì)數(shù)據(jù)庫(kù)的增刪改查?
pyrqlite 和絕大多數(shù)數(shù)據(jù)庫(kù)API 的使用方法一致:
安裝
- 下載源碼
- python setup.py install
基本使用
import pyrqlite.dbapi2 as dbapi2
# Connect to the database
connection = dbapi2.connect(
host='localhost',
port=4001,
)
try:
with connection.cursor() as cursor:
cursor.execute('CREATE TABLE foo (id integer not null primary key, name text)')
cursor.executemany('INSERT INTO foo(name) VALUES(?)', seq_of_parameters=(('a',), ('b',)))
with connection.cursor() as cursor:
# Read a single record
sql = "SELECT `id`, `name` FROM `foo` WHERE `name`=?"
cursor.execute(sql, ('a',))
result = cursor.fetchone()
print(result)
finally:
connection.close()
# 結(jié)果
OrderedDict([('id', 1), ('name', 'a')])
若啟動(dòng)了一個(gè)配置認(rèn)證信息的節(jié)點(diǎn)如何操作pyrqlite
最新版本,已經(jīng)實(shí)現(xiàn)創(chuàng)建數(shù)據(jù)庫(kù)連接時(shí)配置 user 和 paasword
connection = dbapi2.connect(
host='localhost',
port=4001,
user='bob',
password='secret1'
)
5. sqlalchemy-rqlite 使用介紹
如果對(duì)直接編寫(xiě)SQL 語(yǔ)句容易出錯(cuò),作者還提供了ORM 方式。
安裝
- 下載源碼
- python setup.py install
基本使用
from sqlalchemy import create_engine
engine = create_engine('rqlite+pyrqlite://localhost:4001/', echo=True)
如果對(duì)已經(jīng)配置認(rèn)證信息的節(jié)點(diǎn)進(jìn)行連接
from sqlalchemy import create_engine
engine = create_engine('rqlite+pyrqlite://localhost:4001/?user=bob&password=secret1', echo=True)
(全文完)