python学习笔记_week13

一、前景介绍

到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在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账户,但由于他们用的堡垒账户都是自己独有的,因此依然可以通过堡垒机控制每个运维人员访问不同的机器。

(二)审计管理

审计管理其实很简单,就是把用户的所有操作都纪录下来,以备日后的审计或者事故后的追责。在纪录用户操作的过程中有一个问题要注意,就是这个纪录对于操作用户来讲是不可见的,什么意思?就是指,无论用户愿不愿意,他的操作都会被纪录下来,并且,他自己如果不想操作被纪录下来,或想删除已纪录的内容,这些都是他做不到的,这就要求操作日志对用户来讲是不可见和不可访问的,通过堡垒机就可以很好的实现。

python学习笔记_week13

二、堡垒机架构

堡垒机的主要作用权限控制和用户行为审计,堡垒机就像一个城堡的大门,城堡里的所有建筑就是你不同的业务系统 , 每个想进入城堡的人都必须经过城堡大门并经过大门守卫的授权,每个进入城堡的人必须且只能严格按守卫的分配进入指定的建筑,且每个建筑物还有自己的权限访问控制,不同级别的人可以到建筑物里不同楼层的访问级别也是不一样的。还有就是,每个进入城堡的人的所有行为和足迹都会被严格的监控和纪录下来,一旦发生犯罪事件,城堡管理人员就可以通过这些监控纪录来追踪责任人。

python学习笔记_week13

堡垒要想成功完全记到他的作用,只靠堡垒机本身是不够的, 还需要一系列安全上对用户进行限制的配合,堡垒机部署上后,同时要确保你的网络达到以下条件:

  • 所有人包括运维、开发等任何需要访问业务系统的人员,只能通过堡垒机访问业务系统
    • 回收所有对业务系统的访问权限,做到除了堡垒机管理人员,没有人知道业务系统任何机器的登录密码
    • 网络上限制所有人员只能通过堡垒机的跳转才能访问业务系统
  • 确保除了堡垒机管理员之外,所有其它人对堡垒机本身无任何操作权限,只有一个登录跳转功能
  • 确保用户的操作纪录不能被用户自己以任何方式获取到并篡改 

python学习笔记_week13

python学习笔记_week13

三、堡垒机功能实现需求

业务需求:

  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. 分组管理,即可以对设置进行分组,允许用户访问某组机器,但对组里的不同机器依然有不同的访问权限 

 设计表结构

python学习笔记_week13

python学习笔记_week13

ssh公钥登录过程

使用密码登录,每次都必须输入密码,非常麻烦。好在SSH还提供了公钥登录,可以省去输入密码的步骤。

所谓"公钥登录",原理很简单,就是用户将自己的公钥储存在远程主机上。登录的时候,远程主机会向用户发送一段随机字符串,用户用自己的私钥加密后,再发回来。远程主机用事先储存的公钥进行解密,如果成功,就证明用户是可信的,直接允许登录shell,不再要求密码。

这种方法要求用户必须提供自己的公钥。如果没有现成的,可以直接用ssh-keygen生成一个:

  $ ssh-keygen

运行上面的命令以后,系统会出现一系列提示,可以一路回车。其中有一个问题是,要不要对私钥设置口令(passphrase),如果担心私钥的安全,这里可以设置一个。

运行结束以后,在$HOME/.ssh/目录下,会新生成两个文件:id_rsa.pub和id_rsa。前者是你的公钥,后者是你的私钥。

这时再输入下面的命令,将公钥传送到远程主机host上面:

  $ ssh-copy-id user@host

好了,从此你再登录,就不需要输入密码了。

python学习笔记_week13

python学习笔记_week13

python学习笔记_week13

s_it.py

 import os,sys
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print(BASE_DIR)
sys.path.append(BASE_DIR)
if __name__ == '__main__':
from modules.actions import excute_from_command_line
excute_from_command_line(sys.argv)

action_registers.py

 from modules import views
actions = {
'start_session': views.start_session,
# 'stop': views.stop_server,
'syncdb': views.syncdb,
'create_users': views.create_users,
'create_groups': views.create_groups,
'create_hosts': views.create_hosts,
'create_bindhosts': views.create_bindhosts,
'create_remoteusers': views.create_remoteusers,
}

settiongs.py

 Conn_Params = "mysql+pymysql://root:123456@192.168.211.129/JBSdb?charset=utf8"

models.py

 from sqlalchemy import Table, Column, Integer,String,Enum,DATE, ForeignKey,UniqueConstraint #Enum枚举
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import ChoiceType,PasswordType #PasswordType 可以md5,但不好使
from sqlalchemy import create_engine
# from sqlalchemy.orm import sessionmaker
Base = declarative_base() #基类
user_m2m_bindhost = Table('user_m2m_bindhost', Base.metadata,
Column('userprofile_id',Integer,ForeignKey('user_profile.id')),
Column('bindhost_id',Integer,ForeignKey('bind_host.id')),
)
bindhost_m2m_hostgroup = Table('bindhost_m2m_hostgroup', Base.metadata,
Column('bindhost_id',Integer,ForeignKey('bind_host.id')),
Column('hostgroup_id',Integer,ForeignKey('host_group.id')),
)
host_m2m_remoteuser = Table('host_m2m_remoteuser', Base.metadata,
Column('host_id',Integer,ForeignKey('host.id')),
Column('remoteuser_id',Integer,ForeignKey('remote_user.id')),
)
user_m2m_hostgroup = Table('userprofile_m2m_hostgroup', Base.metadata,
Column('userprofile_id',Integer,ForeignKey('user_profile.id')),
Column('hostgroup_id',Integer,ForeignKey('host_group.id')),
)
class Host(Base):
__tablename__="host"
id=Column(Integer,primary_key=True)
hostname=Column(String(64),unique=True)
ip=Column(String(64),unique=True)
port=Column(Integer,default=22)
remote_users=relationship("RemoteUser",secondary=host_m2m_remoteuser,backref="hosts")
def __repr__(self):
return self.hostname
class HostGroup(Base):
__tablename__ = "host_group"
id = Column(Integer, primary_key=True)
name = Column(String(64), unique=True)
bind_hosts=relationship("BindHost",secondary="bindhost_m2m_hostgroup",backref="host_groups")
def __repr__(self):
return self.name
class RemoteUser(Base):
__tablename__ = "remote_user"
__table_args__ = (UniqueConstraint('auth_type', 'username', 'password', name='_user_passwd_uc'),)#联合唯一
AuthTypes = [
('ssh-password', 'SSH/Password'),#第一个是真正存到数据库的,第二个是显示给我们看的
('ssh-key', 'SSH/KEY'),
]
id = Column(Integer, primary_key=True)
auth_type = Column(ChoiceType(AuthTypes))
username = Column(String(32), nullable=False)
password=Column(String(128)) #没有md5
def __repr__(self):
return self.username
class BindHost(Base):
'''
192.168.1.11 web bj_group
192.168.1.11 mysql sh_group
'''
__tablename__ = "bind_host"
__table_args__ = (UniqueConstraint('host_id', 'remoteuser_id', name='host_remoteuser_uc'),)
id = Column(Integer, primary_key=True)
host_id = Column(Integer, ForeignKey('host.id'))
#group_id = Column(Integer, ForeignKey('group.id'))
remoteuser_id = Column(Integer, ForeignKey('remote_user.id'))
host=relationship("Host",backref="binf_hosts")
#host_group=relationship("HostGroup",backref="bind_hosts")
remote_user=relationship("RemoteUser",backref="binf_hosts")
def __repr__(self):
return "<%s -- %s >"%(self.host.ip,
self.remote_user.username,
)
class UserProfile(Base):
__tablename__ = "user_profile"
id = Column(Integer, primary_key=True)
username = Column(String(32), unique=True,nullable=False)
password = Column(String(128))
bind_hosts=relationship("BindHost",secondary="user_m2m_bindhost",backref="user_profiles")
host_groups=relationship("HostGroup",secondary="userprofile_m2m_hostgroup",backref="user_profiles")
def __repr__(self):
return self.username
# class AuditLog(Base):
# pass

models_v2.py

 from sqlalchemy import Table, Column, Integer,String,Enum,DATE, ForeignKey,UniqueConstraint #Enum枚举
from sqlalchemy.orm import relationship
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy_utils import ChoiceType,PasswordType #PasswordType 可以md5,但不好使
# from sqlalchemy import create_engine
# from sqlalchemy.orm import sessionmaker
Base = declarative_base() #基类
user_m2m_bindhost = Table('user_m2m_bindhost', Base.metadata,
Column('userprofile_id',Integer,ForeignKey('user_profile.id')),
Column('bindhost_id',Integer,ForeignKey('bind_host.id')),
)
host_m2m_remoteuser = Table('host_m2m_remoteuser', Base.metadata,
Column('host_id',Integer,ForeignKey('host.id')),
Column('remoteuser_id',Integer,ForeignKey('remote_user.id')),
)
class Host(Base):
__tablename__="host"
id=Column(Integer,primary_key=True)
hostname=Column(String(64),unique=True)
ip=Column(String(64),unique=True)
port=Column(Integer,default=22)
remote_users=relationship("RemoteUser",sencondary=host_m2m_remoteuser,backref="hosts")
def __repr__(self):
return self.hostname
class HostGroup(Base):
__tablename__ = "host_group"
id = Column(Integer, primary_key=True)
name = Column(String(64), unique=True)
def __repr__(self):
return self.name
class RemoteUser(Base):
__tablename__ = "romote_user"
__table_args__ = (UniqueConstraint('auth_type', 'username', 'password', name='_user_passwd_uc'),)#联合唯一
AuthTypes = [
('ssh-passwd', 'SSH/Password'),#第一个是真正存到数据库的,第二个是显示给我们看的
('ssh-key', 'SSH/KEY'),
]
id = Column(Integer, primary_key=True)
auth_type = Column(ChoiceType(AuthTypes))
username = Column(String(32), unique=True)
password=Column(String(128)) #没有md5
def __repr__(self):
return self.username
class BindHost(Base):
'''
192.168.1.11 web bj_group
192.168.1.11 mysql sh_group
'''
__tablename__ = "bind_host"
__table_args__ = (UniqueConstraint('host_id', 'group_id', 'remoteuser_id', name='host_group_remoteuser_uc'),)
id = Column(Integer, primary_key=True)
host_id = Column(Integer, ForeignKey('host.id'))
group_id = Column(Integer, ForeignKey('group.id'))
remoteuser_id = Column(Integer, ForeignKey('remote_user.id'))
host=relationship("Host",backref="binf_hosts")
host_group=relationship("HostGroup",backref="binf_hosts")
remote_user=relationship("RemoteUser",backref="binf_hosts")
def __repr__(self):
return "<%s -- %s -- %s>"%(self.host.ip,
self.remote_user.username,
self.host_group.name)
class UserProfile(Base):
__tablename__ = "user_profile"
id = Column(Integer, primary_key=True)
username = Column(String(32), unique=True)
password = Column(String(128))
bind_hosts=relationship("BindHost",secondary="user_m2m_bindhost",backref="user_profiles")
def __repr__(self):
return self.username
class AuditLog(Base):
pass

actions.py

 from conf import settings
from conf import action_registers
from modules import utils
def help_msg():
'''
print help msgs
:return:
'''
print("\033[31;1mAvailable commands:\033[0m")
for key in action_registers.actions:
print("\t",key)
def excute_from_command_line(argvs):
if len(argvs) < 2:
help_msg()
exit()
if argvs[1] not in action_registers.actions:
utils.print_err("Command [%s] does not exist!" % argvs[1], quit=True)
action_registers.actions[argvs[1]](argvs[1:])

common_filters.py

 from models import models
from modules.db_conn import engine,session
from modules.utils import print_err
def bind_hosts_filter(vals):
print('**>',vals.get('bind_hosts') )
bind_hosts = session.query(models.BindHost).filter(models.Host.hostname.in_(vals.get('bind_hosts'))).all()
if not bind_hosts:
print_err("none of [%s] exist in bind_host table." % vals.get('bind_hosts'),quit=True)
return bind_hosts
def user_profiles_filter(vals):
user_profiles = session.query(models.UserProfile).filter(models.UserProfile.username.in_(vals.get('user_profiles'))
).all()
if not user_profiles:
print_err("none of [%s] exist in user_profile table." % vals.get('user_profiles'),quit=True)
return user_profiles

db_conn.py

 from sqlalchemy import create_engine,Table
from sqlalchemy.orm import sessionmaker
from conf import settings
engine = create_engine(settings.Conn_Params)
#engine = create_engine(settings.DB_CONN,echo=True)
SessionCls = sessionmaker(bind=engine) #创建与数据库的会话session class ,注意,这里返回给session的是个class,不是实例
session = SessionCls()

interactive.py

 import socket
import sys
from paramiko.py3compat import u
from models import models
import datetime
# windows does not have termios...
try:
import termios
import tty
has_termios = True
except ImportError:
has_termios = False
def interactive_shell(chan,user_obj,bind_host_obj,cmd_caches,log_recording):
if has_termios:
posix_shell(chan,user_obj,bind_host_obj,cmd_caches,log_recording)
else:
windows_shell(chan)
def posix_shell(chan,user_obj,bind_host_obj,cmd_caches,log_recording):
import select
oldtty = termios.tcgetattr(sys.stdin)
try:
tty.setraw(sys.stdin.fileno())
tty.setcbreak(sys.stdin.fileno())
chan.settimeout(0.0)
cmd = ''
tab_key = False
while True:
r, w, e = select.select([chan, sys.stdin], [], [])
if chan in r:
try:
x = u(chan.recv(1024))
if tab_key:
if x not in ('\x07' , '\r\n'):
#print('tab:',x)
cmd += x
tab_key = False
if len(x) == 0:
sys.stdout.write('\r\n*** EOF\r\n')
break
sys.stdout.write(x)
sys.stdout.flush()
except socket.timeout:
pass
if sys.stdin in r:
x = sys.stdin.read(1)
if '\r' != x:
cmd +=x
else:
print('cmd->:',cmd)
log_item = models.AuditLog(user_id=user_obj.id,
bind_host_id=bind_host_obj.id,
action_type='cmd',
cmd=cmd ,
date=datetime.datetime.now()
)
cmd_caches.append(log_item)
cmd = ''
if len(cmd_caches)>=10:
log_recording(user_obj,bind_host_obj,cmd_caches)
cmd_caches = []
if '\t' == x:
tab_key = True
if len(x) == 0:
break
chan.send(x)
finally:
termios.tcsetattr(sys.stdin, termios.TCSADRAIN, oldtty)
# thanks to Mike Looijmans for this code
def windows_shell(chan):
import threading
sys.stdout.write("Line-buffered terminal emulation. Press F6 or ^Z to send EOF.\r\n\r\n")
def writeall(sock):
while True:
data = sock.recv(256)
if not data:
sys.stdout.write('\r\n*** EOF ***\r\n\r\n')
sys.stdout.flush()
break
sys.stdout.write(data)
sys.stdout.flush()
writer = threading.Thread(target=writeall, args=(chan,))
writer.start()
try:
while True:
d = sys.stdin.read(1)
if not d:
break
chan.send(d)
except EOFError:
# user hit ^Z or F6
pass

ssh_login.py

 import base64
import getpass
import os
import socket
import sys
import traceback
from paramiko.py3compat import input
from models import models
import datetime
import paramiko
try:
import interactive
except ImportError:
from . import interactive
def ssh_login(user_obj,bind_host_obj,mysql_engine,log_recording):
# now, connect and use paramiko Client to negotiate SSH2 across the connection
try:
client = paramiko.SSHClient()
client.load_system_host_keys()
client.set_missing_host_key_policy(paramiko.WarningPolicy())
print('*** Connecting...')
#client.connect(hostname, port, username, password)
client.connect(bind_host_obj.host.ip_addr,
bind_host_obj.host.port,
bind_host_obj.remoteuser.username,
bind_host_obj.remoteuser.password,
timeout=30) cmd_caches = []
chan = client.invoke_shell()
print(repr(client.get_transport()))
print('*** Here we go!\n')
cmd_caches.append(models.AuditLog(user_id=user_obj.id,
bind_host_id=bind_host_obj.id,
action_type='login',
date=datetime.datetime.now()
))
log_recording(user_obj,bind_host_obj,cmd_caches)
interactive.interactive_shell(chan,user_obj,bind_host_obj,cmd_caches,log_recording)
chan.close()
client.close()
except Exception as e:
print('*** Caught exception: %s: %s' % (e.__class__, e))
traceback.print_exc()
try:
client.close()
except:
pass
sys.exit(1)

utils.py

 import yaml
try:
from yaml import CLoader as Loader, CDumper as Dumper
except ImportError:
from yaml import Loader, Dumper
def print_err(msg,quit=False):
output = "\033[31;1mError: %s\033[0m" % msg
if quit:
exit(output)
else:
print(output)
def yaml_parser(yml_filename):
'''
load yaml file and return
:param yml_filename:
:return:
'''
#yml_filename = "%s/%s.yml" % (settings.StateFileBaseDir,yml_filename)
try:
yaml_file = open(yml_filename,'r')
data = yaml.load(yaml_file)
return data
except Exception as e:
print_err(e)

views.py

 from models import models
from conf import settings
from modules.utils import print_err,yaml_parser
from modules.db_conn import engine,session
from modules import common_filters
from modules import ssh_login
def auth():
'''
do the user login authentication
:return:
'''
count = 0
while count <3:
username = input("\033[32;1mUsername:\033[0m").strip()
if len(username) ==0:continue
password = input("\033[32;1mPassword:\033[0m").strip()
if len(password) ==0:continue
user_obj = session.query(models.UserProfile).filter(models.UserProfile.username==username,
models.UserProfile.password==password).first()
if user_obj:
return user_obj
else:
print("wrong username or password, you have %s more chances." %(3-count-1))
count +=1
else:
print_err("too many attempts.")
def welcome_msg(user):
WELCOME_MSG = '''\033[32;1m
------------- Welcome [%s] login JBSdb -------------
\033[0m'''% user.username
print(WELCOME_MSG)
def log_recording(user_obj,bind_host_obj,logs):
'''
flush user operations on remote host into DB
:param user_obj:
:param bind_host_obj:
:param logs: list format [logItem1,logItem2,...]
:return:
'''
print("\033[41;1m--logs:\033[0m",logs)
session.add_all(logs)
session.commit()
def start_session(argvs):
print('going to start sesssion ')
user = auth()
if user:
welcome_msg(user)
print(user.bind_hosts)
print(user.host_groups)
exit_flag = False
while not exit_flag:
if user.bind_hosts:
print('\033[32;1mz.\tungroupped hosts (%s)\033[0m' %len(user.bind_hosts) )
for index,group in enumerate(user.host_groups):
print('\033[32;1m%s.\t%s (%s)\033[0m' %(index,group.name, len(group.bind_hosts)) ) choice = input("[%s]:" % user.username).strip()
if len(choice) == 0:continue
if choice == 'z':
print("------ Group: ungroupped hosts ------" )
for index,bind_host in enumerate(user.bind_hosts):
print(" %s.\t%s@%s(%s)"%(index,
bind_host.remote_user.username,
bind_host.host.hostname,
bind_host.host.ip,
))
print("----------- END -----------" )
elif choice.isdigit():
choice = int(choice)
if choice < len(user.host_groups):
print("------ Group: %s ------" % user.host_groups[choice].name )
for index,bind_host in enumerate(user.host_groups[choice].bind_hosts):
print(" %s.\t%s@%s(%s)"%(index,
bind_host.remote_user.username,
bind_host.host.hostname,
bind_host.host.ip,
))
print("----------- END -----------" )
#host selection
while not exit_flag:
user_option = input("[(b)back, (q)quit, select host to login]:").strip()
if len(user_option)==0:continue
if user_option == 'b':break
if user_option == 'q':
exit_flag=True
if user_option.isdigit():
user_option = int(user_option)
if user_option < len(user.host_groups[choice].bind_hosts) :
print('host:',user.host_groups[choice].bind_hosts[user_option])
print('audit log:',user.host_groups[choice].bind_hosts[user_option].audit_logs)
ssh_login.ssh_login(user,
user.host_groups[choice].bind_hosts[user_option],
session,
log_recording)
else:
print("no this option..")
def stop_server(argvs):
pass
def create_users(argvs):
'''
create little_finger access user
:param argvs:
:return:
'''
if '-f' in argvs:
user_file = argvs[argvs.index("-f") +1 ]
else:
print_err("invalid usage, should be:\ncreateusers -f <the new users file>",quit=True)
source = yaml_parser(user_file)
if source:
for key,val in source.items():
print(key,val)
obj = models.UserProfile(username=key,password=val.get('password'))
# if val.get('groups'):
# groups = session.query(models.Group).filter(models.Group.name.in_(val.get('groups'))).all()
# if not groups:
# print_err("none of [%s] exist in group table." % val.get('groups'),quit=True)
# obj.groups = groups
# if val.get('bind_hosts'):
# bind_hosts = common_filters.bind_hosts_filter(val)
# obj.bind_hosts = bind_hosts
# #print(obj)
session.add(obj)
session.commit()
def create_groups(argvs):
'''
create groups
:param argvs:
:return:
'''
if '-f' in argvs:
group_file = argvs[argvs.index("-f") +1 ]
else:
print_err("invalid usage, should be:\ncreategroups -f <the new groups file>",quit=True)
source = yaml_parser(group_file)
if source:
for key,val in source.items():
print(key,val)
obj = models.HostGroup(name=key)
# if val.get('bind_hosts'):
# bind_hosts = common_filters.bind_hosts_filter(val)
# obj.bind_hosts = bind_hosts
#
# if val.get('user_profiles'):
# user_profiles = common_filters.user_profiles_filter(val)
# obj.user_profiles = user_profiles
session.add(obj)
session.commit()
def create_hosts(argvs):
'''
create hosts
:param argvs:
:return:
'''
if '-f' in argvs:
hosts_file = argvs[argvs.index("-f") +1 ]
else:
print_err("invalid usage, should be:\ncreate_hosts -f <the new hosts file>",quit=True)
source = yaml_parser(hosts_file)
if source:
print(source)
for key,val in source.items():
print(key,val)
obj = models.Host(hostname=key,ip=val.get('ip'), port=val.get('port') or 22)
session.add(obj)
session.commit()
def create_bindhosts(argvs):
'''
create bind hosts
:param argvs:
:return:
'''
if '-f' in argvs:
bindhosts_file = argvs[argvs.index("-f") +1 ]
else:
print_err("invalid usage, should be:\ncreate_hosts -f <the new bindhosts file>",quit=True)
source = yaml_parser(bindhosts_file)
if source:
for key,val in source.items():
#print(key,val)
host_obj = session.query(models.Host).filter(models.Host.hostname==val.get('hostname')).first()
assert host_obj
for item in val['remote_users']:
print(item )
assert item.get('auth_type') #assert --必须存在,不存在报错
if item.get('auth_type') == 'ssh-password':
remoteuser_obj = session.query(models.RemoteUser).filter(
models.RemoteUser.username==item.get('username'),
models.RemoteUser.password==item.get('password')
).first()
else:
remoteuser_obj = session.query(models.RemoteUser).filter(
models.RemoteUser.username==item.get('username'),
models.RemoteUser.auth_type==item.get('auth_type'),
).first()
if not remoteuser_obj:
print_err("RemoteUser obj %s does not exist." % item,quit=True )
bindhost_obj = models.BindHost(host_id=host_obj.id,remoteuser_id=remoteuser_obj.id)
session.add(bindhost_obj)
#for groups this host binds to
if source[key].get('groups'):
group_objs = session.query(models.HostGroup).filter(models.HostGroup.name.in_(source[key].get('groups') )).all()
assert group_objs
print('groups:', group_objs)
bindhost_obj.host_groups = group_objs
#for user_profiles this host binds to
if source[key].get('user_profiles'):
userprofile_objs = session.query(models.UserProfile).filter(models.UserProfile.username.in_(
source[key].get('user_profiles')
)).all()
assert userprofile_objs
print("userprofiles:",userprofile_objs)
bindhost_obj.user_profiles = userprofile_objs
#print(bindhost_obj)
session.commit()
def create_remoteusers(argvs):
'''
create remoteusers
:param argvs:
:return:
'''
if '-f' in argvs:
remoteusers_file = argvs[argvs.index("-f") +1 ]
else:
print_err("invalid usage, should be:\ncreate_remoteusers -f <the new remoteusers file>",quit=True)
source = yaml_parser(remoteusers_file)
if source:
for key,val in source.items():
print(key,val)
obj = models.RemoteUser(username=val.get('username'),auth_type=val.get('auth_type'),password=val.get('password'))
session.add(obj)
session.commit()
def syncdb(argvs):
print("Syncing DB....")
engine = models.create_engine(settings.Conn_Params,
echo=True)
models.Base.metadata.create_all(engine) #创建所有表结构

new_bindhosts.yml

 bind1:
hostname: ubuntu test
remote_users:
- user1:
username: root
auth_type: ssh-key
#password: 123
- user2:
username: jyh3
auth_type: ssh-password
password: jyh123
groups:
- bj_group
user_profiles:
- jyh
- jack
bind2:
hostname: server2
remote_users:
- user1:
username: root
auth_type: ssh-password
password: abc123
groups:
- bj_group
- sh_group
user_profiles:
- rain

new_groups.yml

 bj_group:
#bind_hosts:
# - h1
# - h2
user_profiles:
- jyh
sh_group:
user_profiles:
- jack
- jyh
- rain

new_hosts.yml

 ubuntu test:
ip: 192.168.2.243
port: 22
server1:
ip: 192.168.2.100
port: 30000
server2:
ip: 10.4.4.22

new_remoteusers.yml

 user0:
auth_type: ssh-password
username: root
password: abc123
user1:
auth_type: ssh-password
username: root
password: jyh123456
user2:
auth_type: ssh-key
username: root
#password: abc!23
user3:
auth_type: ssh-password
username: jyh3
password: jyh123

new_users.yml

 jyh:
password: jyh123
# groups:
# - web_servers
# - db_servers
#bind_hosts:
# - h1
# - h2
# - h3
jack:
password: jack123

完整示例代码 https://github.com/triaquae/py3_training/tree/master/%E5%A0%A1%E5%9E%92%E6%9C%BA

重点是---设计过程,架构,表结构,交互

完善audit  如在action_registers.py中 "audit" : views.log_audit 。。。设计表结构---能执行,能记录,能看

上一篇:MyBatis3-动态SQL语句


下一篇:Cython入门Demo(Linux)