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下載

  下載地址,選擇合適的版本和壓縮文件 http://ftp.gnu.org/gnu/bison/,這里下載的是bison-2.5.1.tar.xz這個版本的包。

解壓

  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的安裝 

  源碼下載

  下載源代碼 git clone https://github.com/mysql-inception/inception.git

   

   編譯&&安裝

  進入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是一個功能非常強大的軟件(服務),本文管中窺豹,僅粗略做了一下嘗試,僅供參考,更多請參考http://mysql-inception.github.io/inception-document/

 

最后參考官方文檔

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

 

參考 

  http://mysql-inception.github.io/inception-document/
  http://www.ywnds.com/?p=9423
  http://blog.51cto.com/amnesiasun/1947605

  

 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM