1. 程式人生 > >Inception服務的安裝以及使用Python 3 實現MySQL的審計

Inception服務的安裝以及使用Python 3 實現MySQL的審計

Inception服務的安裝以及使用Python實現MySQL的審計

Bison是Inception服務所依賴的包之一,但是某些Linux版本已安裝的Bison,或者是通過yum安裝的Bison,通常是Bison 3.0+版本的.
對於Inception程式來說,其Bison版本是過高的,會導致Inception在編譯的過程出錯,按照官方的建議,最好需要Bison 2.5這個版本。
因此需要手動安裝Bison 2.5這個版本的依賴包。

Bison的安裝

Bison下載

解壓

  xz -d bison-2.5.1.tar.xz
  tar -xvf bison-2.5.1.tar

      

  安裝

  進入解壓後的bison路徑中,cd bison-2.5.1,按照正常的原始碼包依次編譯安裝即可。
  ./configure
  make && make install

  

  完成安裝,檢查Bison的版本,bison  -V(記得之前要設定環境變數的,這裡安裝完之後直接就識別Bison命令了)。

  

  最後安裝其其他的相關依賴包
    yum install gcc gcc-c++ cmake openssl-devel ncurses-devel MySQL-python git –y

Inception的安裝 

  原始碼下載

   

   編譯&&安裝

  進入Inception目錄之後執行:bash inception_build.sh debug [Xcode]
  經過十幾分鍾漫長的編譯安裝完成之後,確認安裝成功。
  如果是第一次安裝失敗,可能是缺少依賴的包或者是依賴的包的版本不對,可以根據具體的錯誤進行處理,重新安裝需要刪除安裝失敗生成的debug目錄,否則無法繼續安裝。

  

  新增一個最基本的Inception配置檔案

  

  啟動Inception服務

    /usr/local/inception/debug/mysql/bin/Inception --defaults-file=/etc/inc.cnf &

  

  從本地連線至Inception服務

  

     至此,Inception服務已完全安裝成功。

 使用Python(Python3)實現MySQL的審計作業

  Inception在驗證MySQL指令碼的有效性的時候,有自己的特定的格式,因此要將帶驗證(執行)的sql指令碼組裝成Inception服務指定的格式。

--Inception開始標誌
/*--user=username;--password=*****;--host=***.***.***.***;--enable-check;--port=3306;*/  
inception_magic_start;  

--中間開始是sql語句
use mysql;  
CREATE TABLE adaptive_office(id int);  

--Inception結束標誌
inception_magic_commit;

  網上大多數環境使用Python做Inception的審計,應該使用的是Python2,如果換做是Python3,發現根本無法正常連線至Inception服務。
  正常連線的時候,一直報錯:“invalid literal for int() with base 10: 'Inception2'”
  原因應該是connections.py在連線到Inception服務引數解析型別轉換的時候有一點問題。
  參考:http://blog.51cto.com/amnesiasun/1947605,修改Python的connections.py檔案中的_request_authentication方法
  這樣才能使用Python3正常連線至Inception服務。

如下是一個使用Python做MySQL指令碼審計的一個demo

#!/usr/bin/python
# -\*-coding: utf-8-\*-
import pymysql
import sys, os
import time

# 執行還是校驗
operation = '--enable-check'
# operation = '--enable-execute;--enable-ignore-warnings;--enable-force'

# 釋出目標伺服器
connstr_target = {'host': '***.***.***.***', 'port': 3306, 'user': 'root', 'password': '***', 'db': 'inception_testdb', 'charset': 'utf8mb4'}
# inception server
connstr_inception = {'host': '***.***.***.***', 'port': 6669, 'user': 'root', 'password': '', 'db': '',  'charset': 'utf8mb4'}



# inception格式要求
prefix_format = "/*--user={};--password={};--host={};{};--port={};*/ ".format(connstr_target['user'],
                                                                                      connstr_target['password'],
                                                                                      connstr_target['host'],
                                                                                      operation,
                                                                                      connstr_target['port']) \
                + '\n' \
                + "inception_magic_start;"
suffix_format = "inception_magic_commit;"


#待執行的sql語句
sql_demo1 = ''' use inception_testdb; 
            alter table test_inception 
            add column remark varchar(200);'''


try:
    # 將待執行的sql語句組合成inception識別的格式
    sql_demo1_with_format = prefix_format + "\n" + sql_demo1 + "\n" + suffix_format

    print(sql_demo1_with_format)

    # 連線至inception 伺服器
    conn_inception = pymysql.connect(host=connstr_inception['host'],
                                      port=connstr_inception['port'],
                                      user=connstr_inception['user'],
                                      password=connstr_inception['password'],
                                      charset=connstr_inception['charset'])


    cur = conn_inception.cursor()

    cur.execute(sql_demo1_with_format)
    result = cur.fetchall()
    num_fields = len(cur.description)
    field_names = [i[0] for i in cur.description]
    print(field_names)
    #打印出來Inception對MySQL語句的審計結果
    for row in result:
        print(row[0], "|", row[1], "|", row[2], "|", row[3], "|", row[4], "|", row[5], "|", row[6], "|",  row[7], "|",
              row[8], "|", row[9], "|", row[10])

    cur.close()
    conn_inception.close()

except  Exception as err:
    print(err)
finally:
    print('****************')

errlevel=0表示語句正常,1的話表示有警告資訊,比如欄位沒有預設值,沒有comment等,2的話表示嚴重的錯誤,比如往表中增加一個已經存在的欄位等等。
比如如下的審計結果中有一個errlevel=2的結果,原因是表中已經存在了remark欄位,再增加一個同名的欄位,肯定是通不過的,因此顯示“Column 'remark' have existed.”這個錯誤,
除了上述錯誤,也會顯示執行alter table test_inception add column remark varchar(200);這個語句產生的其他型別的錯誤(或者警告)。


需要了解的就是,一些潛在的不是嚴重錯誤級別的問題,其警告型別是可以配置化的,
比如欄位沒有comment,可以在Inception服務一級配置為沒有comment不顯示警告,亦或是欄位沒有預設值,Inception也會給予一個警告,這些非嚴重錯誤,都可以根據情況進行配置(是否給出告警)
當然,這裡僅僅是一個demo,對於Inception審計出來的結果,可以根據具體要求做的更加視覺化一些。

['ID', 'stage', 'errlevel', 'stagestatus', 'errormessage', 'SQL', 'Affected_rows', 'sequence', 'backup_dbname', 'execute_time', 'sqlsha1']
1 | CHECKED | 0 | Audit completed | None | use inception_testdb | 0 | '0_0_0' | None | 0 | 
2 | CHECKED | 2 | Audit completed | Column 'remark' have existed.
Column 'remark' in table 'test_inception' have no comments.
Column 'remark' in table 'test_inception' is not allowed to been nullable.
Set Default value for column 'remark' in table 'test_inception' | alter table test_inception 
            add column remark varchar(200) | 1 | '0_0_1' | 116_196_94_250_8000_inception_testdb | 0 | 

  對於Inception審計結果,也不是完全合理的,比如mysql中建立索引的語句,支援兩種語法,alter table的方式和create index的方式。
  早期的mysql版本都是通過alter table的語法增加索引的,後面的mysql支援了create index的語法,但是Inception對於create index的語句也是給予一個嚴重級別的告警的。
  另外對於DML的語句支援也有限,比如insert語句,如果insert語句插入一條與現在表中存在主鍵衝突的值,Inception也是檢測不出來的,
  Inception更多的是檢測語法這個層面的錯誤。 

其他

最後參考官方文件

不得不說,Inception還是國內比較牛逼的審計(執行,回滾等)MySQL管理的利器之一了,最起碼開源了,是騾子是馬拉已經來出來遛了,認可程度還是比較高的,沒有相當的實力,是攔不下這個瓷器活的。
能正常使用Python連線至Inception實現最基本的SQL審計之後,就可以嘗試適合自己的審計方式,以及發掘Inception更多的功能了。
最好的當然是官方的參考文件了:http://mysql-inception.github.io/inception-document/

參考