1. 程式人生 > >項目二:主機管理-00-借鑒

項目二:主機管理-00-借鑒

base 1.3 所有 數據庫 意思 重新 多公司 for rand

主機管理+堡壘機系統開發

本節內容

  1. 需求討論
  2. 構架設計
  3. 表結構設計
  4. 程序開發

1.需求討論

  1. 實現對用戶的權限管理,能訪問哪些機器,在被訪問的機器上有哪些權限
  2. 實現可以通過web頁面對指定主機列表 進行 批量發布命令、文件
  3. 實現對用戶操作進行紀錄

2.架構設計

技術分享圖片

3. 表結構設計

參考 http://www.cnblogs.com/alex3714/articles/5286889.html

前景介紹

到目前為止,很多公司對堡壘機依然不太感冒,其實是沒有充分認識到堡壘機在IT管理中的重要作用的,很多人覺得,堡壘機就是跳板機,其實這個認識是不全面的,跳板功能只是堡壘機所具備的功能屬性中的其中一項而已,下面我就給大家介紹一下堡壘機的重要性,以幫助大家參考自己公司的業務是否需要部署堡壘機。

堡壘機有以下兩個至關重要的功能:

權限管理

當你公司的服務器變的越來越多後,需要操作這些服務器的人就肯定不只是一個運維人員,同時也可能包括多個開發人員,那麽這麽多的人操作業務系統,如果權限分配不當就會存在很大的安全風險,舉幾個場景例子:

  1. 設想你們公司有300臺Linux服務器,A開發人員需要登錄其中5臺WEB服務器查看日誌或進行問題追蹤等事務,同時對另外10臺hadoop服務器有root權限,在有300臺服務器規模的網絡中,按常理來講你是已經使用了ldap權限統一認證的,你如何使這個開發人員只能以普通用戶的身份登錄5臺web服務器,並且同時允許他以管理員的身份登錄另外10臺hadoop服務器呢?並且同時他對其它剩下的200多臺服務器沒有訪問權限

  2. 目前據我了解,很多公司的運維團隊為了方面,整個運維團隊的運維人員還是共享同一套root密碼,這樣內部信任機制雖然使大家的工作方便了,但同時存在著極大的安全隱患,很多情況下,一個運維人員只需要管理固定數量的服務器,畢竟公司分為不同的業務線,不同的運維人員管理的業務線也不同,但如果共享一套root密碼,其實就等於無限放大了每個運維人員的權限,也就是說,如果某個運維人員想幹壞事的話,他可以在幾分鐘內把整個公司的業務停轉,甚至數據都給刪除掉。為了降低風險,於是有人想到,把不同業務線的root密碼改掉就ok了麽,也就是每個業務線的運維人員只知道自己的密碼,這當然是最簡單有效的方式,但問題是如果你同時用了ldap,這樣做又比較麻煩,即使你設置了root不通過ldap認證,那新問題就是,每次有運維人員離職,他所在的業務線的密碼都需要重新改一次。

其實上面的問題,我覺得可以很簡單的通過堡壘機來實現,收回所有人員的直接登錄服務器的權限,所有的登錄動作都通過堡壘機授權,運維人員或開發人員不知道遠程服務器的密碼,這些遠程機器的用戶信息都綁定在了堡壘機上,堡壘機用戶只能看到他能用什麽權限訪問哪些遠程服務器。

在回收了運維或開發人員直接登錄遠程服務器的權限後,其實就等於你們公司生產系統的所有認證過程都通過堡壘機來完成了,堡壘機等於成了你們生產系統的SSO(single sign on)模塊了。你只需要在堡壘機上添加幾條規則就能實現以下權限控制了:

  1. 允許A開發人員通過普通用戶登錄5臺web服務器,通過root權限登錄10臺hadoop服務器,但對其余的服務器無任務訪問權限

  2. 多個運維人員可以共享一個root賬戶,但是依然能分辨出分別是誰在哪些服務器上操作了哪些命令,因為堡壘機賬戶是每個人獨有的,也就是說雖然所有運維人員共享了一同一個遠程root賬戶,但由於他們用的堡壘賬戶都是自己獨有的,因此依然可以通過堡壘機控制每個運維人員訪問不同的機器。

審計管理

審計管理其實很簡單,就是把用戶的所有操作都紀錄下來,以備日後的審計或者事故後的追責。在紀錄用戶操作的過程中有一個問題要註意,就是這個紀錄對於操作用戶來講是不可見的,什麽意思?就是指,無論用戶願不願意,他的操作都會被紀錄下來,並且,他自己如果不想操作被紀錄下來,或想刪除已紀錄的內容,這些都是他做不到的,這就要求操作日誌對用戶來講是不可見和不可訪問的,通過堡壘機就可以很好的實現。

堡壘機架構

堡壘機的主要作用權限控制和用戶行為審計,堡壘機就像一個城堡的大門,城堡裏的所有建築就是你不同的業務系統 , 每個想進入城堡的人都必須經過城堡大門並經過大門守衛的授權,每個進入城堡的人必須且只能嚴格按守衛的分配進入指定的建築,且每個建築物還有自己的權限訪問控制,不同級別的人可以到建築物裏不同樓層的訪問級別也是不一樣的。還有就是,每個進入城堡的人的所有行為和足跡都會被嚴格的監控和紀錄下來,一旦發生犯罪事件,城堡管理人員就可以通過這些監控紀錄來追蹤責任人。

技術分享圖片

堡壘要想成功完全記到他的作用,只靠堡壘機本身是不夠的, 還需要一系列安全上對用戶進行限制的配合,堡壘機部署上後,同時要確保你的網絡達到以下條件:

  • 所有人包括運維、開發等任何需要訪問業務系統的人員,只能通過堡壘機訪問業務系統
    • 回收所有對業務系統的訪問權限,做到除了堡壘機管理人員,沒有人知道業務系統任何機器的登錄密碼
    • 網絡上限制所有人員只能通過堡壘機的跳轉才能訪問業務系統
  • 確保除了堡壘機管理員之外,所有其它人對堡壘機本身無任何操作權限,只有一個登錄跳轉功能
  • 確保用戶的操作紀錄不能被用戶自己以任何方式獲取到並篡改  

堡壘機功能實現需求

業務需求:

  1. 兼顧業務安全目標與用戶體驗,堡壘機部署後,不應使用戶訪問業務系統的訪問變的復雜,否則工作將很難推進,因為沒人喜歡改變現狀,尤其是改變後生活變得更艱難
  2. 保證堡壘機穩定安全運行, 沒有100%的把握,不要上線任何新系統,即使有100%把握,也要做好最壞的打算,想好故障預案

功能需求:

  1. 所有的用戶操作日誌要保留在數據庫中
  2. 每個用戶登錄堡壘機後,只需要選擇具體要訪問的設置,就連接上了,不需要再輸入目標機器的訪問密碼
  3. 允許用戶對不同的目標設備有不同的訪問權限,例:
    1. 對10.0.2.34 有mysql 用戶的權限
    2. 對192.168.3.22 有root用戶的權限
    3. 對172.33.24.55 沒任何權限
  4. 分組管理,即可以對設置進行分組,允許用戶訪問某組機器,但對組裏的不同機器依然有不同的訪問權限    

設計表結構:

技術分享圖片

技術分享圖片 技術分享圖片
#_*_coding:utf-8_*_
__author__ = ‘Alex Li‘
 
from sqlalchemy import create_engine,Table
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy import Column, Integer, String,ForeignKey,UniqueConstraint
from sqlalchemy.orm import relationship
from  sqlalchemy.orm import sessionmaker
from sqlalchemy import or_,and_
from sqlalchemy import func
from sqlalchemy_utils import ChoiceType,PasswordType
 
Base = declarative_base() #生成一個SqlORM 基類
 
 
engine = create_engine("mysql+mysqldb://root@localhost:3306/test",echo=False)
 
 
BindHost2Group = Table(‘bindhost_2_group‘,Base.metadata,
    Column(‘bindhost_id‘,ForeignKey(‘bind_host.id‘),primary_key=True),
    Column(‘group_id‘,ForeignKey(‘group.id‘),primary_key=True),
)
 
BindHost2UserProfile = Table(‘bindhost_2_userprofile‘,Base.metadata,
    Column(‘bindhost_id‘,ForeignKey(‘bind_host.id‘),primary_key=True),
    Column(‘uerprofile_id‘,ForeignKey(‘user_profile.id‘),primary_key=True),
)
 
Group2UserProfile = Table(‘group_2_userprofile‘,Base.metadata,
    Column(‘userprofile_id‘,ForeignKey(‘user_profile.id‘),primary_key=True),
    Column(‘group_id‘,ForeignKey(‘group.id‘),primary_key=True),
)
 
 
class UserProfile(Base):
    __tablename__ = ‘user_profile‘
    id = Column(Integer,primary_key=True,autoincrement=True)
    username = Column(String(32),unique=True,nullable=False)
    password = Column(String(128),unique=True,nullable=False)
    groups = relationship(‘Group‘,secondary=Group2UserProfile)
    bind_hosts = relationship(‘BindHost‘,secondary=BindHost2UserProfile)
 
    def __repr__(self):
        return "<UserProfile(id=‘%s‘,username=‘%s‘)>" % (self.id,self.username)
 
class RemoteUser(Base):
    __tablename__ = ‘remote_user‘
    AuthTypes = [
        (u‘ssh-passwd‘,u‘SSH/Password‘),
        (u‘ssh-key‘,u‘SSH/KEY‘),
    ]
    id = Column(Integer,primary_key=True,autoincrement=True)
    auth_type = Column(ChoiceType(AuthTypes))
    username = Column(String(64),nullable=False)
    password = Column(String(255))
 
    __table_args__ = (UniqueConstraint(‘auth_type‘, ‘username‘,‘password‘, name=‘_user_passwd_uc‘),)
 
    def __repr__(self):
        return "<RemoteUser(id=‘%s‘,auth_type=‘%s‘,user=‘%s‘)>" % (self.id,self.auth_type,self.username)
 
 
class Host(Base):
    __tablename__ = ‘host‘
    id = Column(Integer,primary_key=True,autoincrement=True)
    hostname = Column(String(64),unique=True,nullable=False)
    ip_addr = Column(String(128),unique=True,nullable=False)
    port = Column(Integer,default=22)
    bind_hosts = relationship("BindHost")
    def __repr__(self):
        return "<Host(id=‘%s‘,hostname=‘%s‘)>" % (self.id,self.hostname)
 
class Group(Base):
    __tablename__  = ‘group‘
    id = Column(Integer,primary_key=True,autoincrement=True)
    name = Column(String(64),nullable=False,unique=True)
    bind_hosts = relationship("BindHost",secondary=BindHost2Group, back_populates=‘groups‘ )
    user_profiles = relationship("UserProfile",secondary=Group2UserProfile )
 
    def __repr__(self):
        return "<HostGroup(id=‘%s‘,name=‘%s‘)>" % (self.id,self.name)
 
 
class BindHost(Base):
    ‘‘‘Bind host with different remote user,
       eg. 192.168.1.1 mysql passAbc123
       eg. 10.5.1.6    mysql pass532Dr!
       eg. 10.5.1.8    mysql pass532Dr!
       eg. 192.168.1.1 root
    ‘‘‘
    __tablename__ = ‘bind_host‘
    id = Column(Integer,primary_key=True,autoincrement=True)
    host_id = Column(Integer,ForeignKey(‘host.id‘))
    remoteuser_id = Column(Integer,ForeignKey(‘remote_user.id‘))
    host = relationship("Host")
    remoteuser = relationship("RemoteUser")
    groups = relationship("Group",secondary=BindHost2Group,back_populates=‘bind_hosts‘)
    user_profiles = relationship("UserProfile",secondary=BindHost2UserProfile)
 
    __table_args__ = (UniqueConstraint(‘host_id‘, ‘remoteuser_id‘, name=‘_bindhost_and_user_uc‘),)
 
    def __repr__(self):
        return "<BindHost(id=‘%s‘,name=‘%s‘,user=‘%s‘)>" % (self.id,
                                                           self.host.hostname,
                                                           self.remoteuser.username
                                                                      )
 
 
Base.metadata.create_all(engine) #創建所有表結構
 
if __name__ == ‘__main__‘:
    SessionCls = sessionmaker(bind=engine) #創建與數據庫的會話session class ,註意,這裏返回給session的是個class,不是實例
    session = SessionCls()
    #h1 = session.query(Host).filter(Host.hostname==‘ubuntu4‘).first()
    #hg1 = session.query(HostGroup).filter(HostGroup.name==‘t2‘).first()
 
    #h2 = Host(hostname=‘ubuntu4‘,ip_addr=‘192.168.1.21‘)
    #h3 = Host(hostname=‘ubuntu5‘,ip_addr=‘192.168.1.24‘,port=20000)
    #hg= HostGroup(name=‘TestServers3‘,host_id=h3.id)
    #hg2= HostGroup(name=‘TestServers2‘,host_id=h2.id)
    #hg3= HostGroup(name=‘TestServers3‘)
    #hg4= HostGroup(name=‘TestServers4‘)
    #session.add_all([hg3,hg4])
    #h2.host_groups = [HostGroup(name="t1"),HostGroup(name="t2")]
    #h3.host_groups = [HostGroup(name="t2")]
    #h1.host_groups.append(HostGroup(name="t3") )
    #print(h1.host_groups)
    #print("hg1:",hg1.host.hostname)
    #join_res = session.query(Host).join(Host.host_groups).filter(HostGroup.name==‘t1‘).group_by("Host").all()
    #print(‘join select:‘,join_res)
    #group_by_res = session.query(HostGroup, func.count(HostGroup.name )).group_by(HostGroup.name).all()
    #print("-------------group by res-----")
 
    ‘‘‘
    h1=Host(hostname=‘h1‘,ip_addr=‘1.1.1.1‘)
    h2=Host(hostname=‘h2‘,ip_addr=‘1.1.1.2‘)
    h3=Host(hostname=‘h3‘,ip_addr=‘1.1.1.3‘)
    r1=RemoteUser(auth_type=u‘ssh-passwd‘,username=‘alex‘,password=‘abc123‘)
    r2=RemoteUser(auth_type=u‘ssh-key‘,username=‘alex‘)
 
    g1 = Group(name=‘g1‘)
    g2 = Group(name=‘g2‘)
    g3 = Group(name=‘g3‘)
    session.add_all([h1,h2,h3,r1,r2])
    session.add_all([g1,g2,g3])
 
 
 
    b1 = BindHost(host_id=1,remoteuser_id=1)
    b2 = BindHost(host_id=1,remoteuser_id=2)
    b3 = BindHost(host_id=2,remoteuser_id=2)
    b4 = BindHost(host_id=3,remoteuser_id=2)
    session.add_all((b1,b2,b3,b4))
     
    all_groups = session.query(Group).filter().all() #first()
    all_bindhosts = session.query(BindHost).filter().all()
 
    #h1 = session.query(BindHost).filter(BindHost.host_id==1).first()
    #h1.groups.append(all_groups[1])
    #print("h1:",h1)
    #print("----------->",all_groups.name,all_groups.bind_hosts)
    u1 = session.query(UserProfile).filter(UserProfile.id==1).first()
    print(‘--user:‘,u1.bind_hosts)
    print(‘--user:‘,u1.groups[0].bind_hosts)
    #u1.groups = [all_groups[1] ]
    #u1.bind_hosts.append(all_bindhosts[1])
    #u1 = UserProfile(username=‘alex‘,password=‘123‘)
    #u2 = UserProfile(username=‘rain‘,password=‘abc!23‘)
    #session.add_all([u1,u2])
    #b1 = BindHost()
    session.commit()
    #print(h2.host_groups)
    ‘‘‘
技術分享圖片

ssh公鑰登錄過程

使用密碼登錄,每次都必須輸入密碼,非常麻煩。好在SSH還提供了公鑰登錄,可以省去輸入密碼的步驟。

所謂"公鑰登錄",原理很簡單,就是用戶將自己的公鑰儲存在遠程主機上。登錄的時候,遠程主機會向用戶發送一段隨機字符串,用戶用自己的私鑰加密後,再發回來。遠程主機用事先儲存的公鑰進行解密,如果成功,就證明用戶是可信的,直接允許登錄shell,不再要求密碼。

這種方法要求用戶必須提供自己的公鑰。如果沒有現成的,可以直接用ssh-keygen生成一個:

$ ssh-keygen

運行上面的命令以後,系統會出現一系列提示,可以一路回車。其中有一個問題是,要不要對私鑰設置口令(passphrase),如果擔心私鑰的安全,這裏可以設置一個。

運行結束以後,在$HOME/.ssh/目錄下,會新生成兩個文件:id_rsa.pub和id_rsa。前者是你的公鑰,後者是你的私鑰。

這時再輸入下面的命令,將公鑰傳送到遠程主機host上面:

$ ssh-copy-id user@host

好了,從此你再登錄,就不需要輸入密碼了。

需要調用strace命令:

yum -y install strace 安裝strace
技術分享圖片
[root@python-web2 ~]# strace
usage: strace [-CdffhiqrtttTvVxxy] [-I n] [-e expr]...
              [-a column] [-o file] [-s strsize] [-P path]...
              -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]
   or: strace -c[df] [-I n] [-e expr]... [-O overhead] [-S sortby]
              -p pid... / [-D] [-E var=val]... [-u username] PROG [ARGS]
-c -- count time, calls, and errors for each syscall and report summary #監測時間
-C -- like -c but also print regular output
-d -- enable debug output to stderr
-D -- run tracer process as a detached grandchild, not as parent
-f -- follow forks, -ff -- with output into separate files #進程會產生子進程
-i -- print instruction pointer at time of syscall
-q -- suppress messages about attaching, detaching, etc.
-r -- print relative timestamp, -t -- absolute timestamp, -tt -- with usecs
-T -- print time spent in each syscall
-v -- verbose mode: print unabbreviated argv, stat, termios, etc. args
-x -- print non-ascii strings in hex, -xx -- print all strings in hex
-y -- print paths associated with file descriptor arguments
-h -- print help message, -V -- print version
-a column -- alignment COLUMN for printing syscall results (default 40)
-b execve -- detach on this syscall
-e expr -- a qualifying expression: option=[!]all or option=[!]val1[,val2]...
   options: trace, abbrev, verbose, raw, signal, read, write
-I interruptible --
   1: no signals are blocked
   2: fatal signals are blocked while decoding syscall (default)
   3: fatal signals are always blocked (default if ‘-o FILE PROG‘)
   4: fatal signals and SIGTSTP (^Z) are always blocked
      (useful to make ‘strace -o FILE PROG‘ not stop on ^Z)
-o file -- send trace output to FILE instead of stderr
-O overhead -- set overhead for tracing syscalls to OVERHEAD usecs
-p pid -- trace process with process id PID, may be repeated #監測進程id
-s strsize -- limit length of print strings to STRSIZE chars (default 32)
-S sortby -- sort syscall counts by: time, calls, name, nothing (default time)
-u username -- run command as username handling setuid and/or setgid
-E var=val -- put var=val in the environment for command
-E var -- remove var from the environment for command
-P path -- trace accesses to path
技術分享圖片

技術分享圖片
進程id和進程號

[root@localhost ~]# ps -ef | grep ssh

UID PID PPID C STIME TTY TIME CMD
root 1036 1 0 00:37 ? 00:00:00 /usr/sbin/sshd
root 1050 1036 0 00:37 ? 00:00:00 sshd: root@pts/0
root 1222 1036 0 00:44 ? 00:00:00 sshd: root@pts/1
root 1240 1224 0 00:44 pts/1 00:00:00 grep --color=auto ssh

紅顏色的是進程號(PID),黑色的是父進程id(PPID)

0是根進程

技術分享圖片

strace監控子進程的活動狀況2004就是192.168.117.130下面的ssh的子進程,這個子進程連接著192.168.117.128( [root@python-web2 ~]# strace -fp 2004 ),當192.168.117.128輸入ifconfig命令,這個192.168.117.130會輸出一系列指令

技術分享圖片

技術分享圖片

這裏出現了select,聯想到之前學到的select,poll,epoll,select

ssh的底層就是用的select.

監控所有有活動的連接數,有活動的就返回,select的原理就是監控所有的文件句柄,有哪個文件句柄活動,select就會返回

這裏監控的是屏幕的輸入和輸出。

strace -fp 2004 -t -o ssh_log輸出到日誌

在centos7上運行CrazyEye堡壘機程序,遇到如下問題:

python版本的問題,python3.4運行不起來,必須安裝python3.6,註意Django的路徑和python的路徑。

運行server:

python3.6 manage.py runserver 0.0.0.0:9000

技術分享圖片

python3.6 manage.py createsuperuser

完整示例代碼

https://github.com/triaquae/py3_training/tree/master/%E5%A0%A1%E5%9E%92%E6%9C%BA

項目二:主機管理-00-借鑒