day13---堡垒机
1.业务需求
- 兼顾业务安全目标与用户体验,堡垒机部署后,不应使用户访问业务系统的访问变的复杂,否则工作将很难推进,因为没人喜欢改变现状,尤其是改变后生活变得更艰难
- 保证堡垒机稳定安全运行, 没有100%的把握,不要上线任何新系统,即使有100%把握,也要做好最坏的打算,想好故障预案
2.功能需求
- 所有的用户操作日志要保留在数据库中
- 每个用户登录堡垒机后,只需要选择具体要访问的设置,就连接上了,不需要再输入目标机器的访问密码
- 允许用户对不同的目标设备有不同的访问权限,例:
- 对10.0.2.34 有mysql 用户的权限
- 对192.168.3.22 有root用户的权限
- 对172.33.24.55 没任何权限
- 分组管理,即可以对设置进行分组,允许用户访问某组机器,但对组里的不同机器依然有不同的访问权限
3.设计表结构
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)
'''
代码地址:
https://github.com/triaquae/py3_training/tree/master/%E5%A0%A1%E5%9E%92%E6%9C%BA
day13---堡垒机的更多相关文章
- Python之路,Day13 - 堡垒机
项目实战:运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功能只是堡垒 ...
- Day13 SQLAlchemy连表操作和堡垒机
一.数据库操作 1.创建表.插入数据和一对多查询 #!/usr/bin/env python # -*- coding: utf-8 -*- # Author: wanghuafeng from sq ...
- Python之路第一课Day9--随堂笔记之一(堡垒机实例以及数据库操作)未完待续....
一.堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: ...
- Python之路:堡垒机实例
堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块机遇SSH用于连接远程服务器并执行相关操作 SSHClient 用于连接远程服务器并执行基本命令 基于用户名密码连接: 1 ...
- python远程连接paramiko 模块和堡垒机实现
paramiko使用 paramiko模块是基于python实现了SSH2远程安全连接,支持认证和密钥方式,可以实现远程连接.命令执行.文件传输.中间SSH代理功能 安装 pip install pa ...
- python学习笔记-(十三)堡垒机
1.课前准备: 本次学习堡垒机相关知识:之前,需要安装Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作. 前提: python3.5程序安装到默认路径下并已添加pa ...
- Python开发【第九章】:堡垒机实例
一.堡垒机前戏 开发堡垒机之前,先来学习Python的paramiko模块,该模块基于SSH用于连接远程服务器并执行相关操作 模块安装 C:\Program Files\Python 3.5\Scri ...
- 利用paramiko模块实现堡垒机+审计功能
paramiko模块是一个远程连接服务器,全真模拟ssh2协议的python模块,借助paramiko源码包中的demos目录下:demo.py和interactive.py两个模块实现简单的堡垒机+ ...
- Python之路-python(堡垒机)
运维堡垒机开发 前景介绍 到目前为止,很多公司对堡垒机依然不太感冒,其实是没有充分认识到堡垒机在IT管理中的重要作用的,很多人觉得,堡垒机就是跳板机,其实这个认识是不全面的,跳板功能只是堡垒机所具备的 ...
- python之实现基于paramiko和mysql数据库的堡垒机
一.堡垒机结构 堡垒机执行流程: 管理员为用户在服务器上创建账号(将公钥放置服务器,或者使用用户名密码) 用户登陆堡垒机,输入堡垒机用户名密码,现实当前用户管理的服务器列表 用户选择服务器,并自动登陆 ...
随机推荐
- 【转】+【举例】ArcGIS中的坐标系统定义与投影转换
背景知识: UTM (Universal Transverse Mercator)坐标系是由美国军方在1947提出的.虽然我们仍然将其看作与"高斯-克吕格"相似的坐标系统,但实际上 ...
- 音痴又音痴的LT
中文题不解释.比赛时想着操作一次sort一次,然而T到死.后来才知道C++内置容器vector这么强大. 内置函数upperbound(查找数组的首地址, 查找数组的尾地址, 待查找元素)为logn复 ...
- ubuntu14.04 安装系统
p { margin-bottom: 0.1in; line-height: 120% } code.cjk { font-family: "Droid Sans Fallback" ...
- windows10, 安装wamp无法启动服务的问题
今天在另一台电脑上安装了wamp, 就是这个玩意 结果怎么也启动不起来, 上网上查了一下, 原因是有些windows10的系统上有安装IIS10, 这个也不知道是啥东西, 占用了80端口, 所以启动不 ...
- (转)selenuim-webdriver注解之@FindBy、@FindBys、@FindAll的区别
selenium-webdriver中获取页面元素的方式有很多,使用注解获取页面元素是其中一种途径, 方式有3种:@FindBy.@FindBys.@FindAll.下文对3中类型的区别和使用场景进行 ...
- Ant: Class not found: javac1.8
今天用ant,在选择build.xml,run as ant build后出错Ant: Class not found: javac1.8 分析问题:是否是eclipse中的ant版本和java的版本 ...
- ng-repeat && ng-options的故事
ng-repeat && ng-options的故事 1. <select class="input-small" ng-model="newH ...
- ITextSharp导出PDF表格和图片(C#)
文章主要介绍使用ITextSharp导出PDF表格和图片的简单操作说明,以下为ITextSharp.dll下载链接 分享链接:http://pan.baidu.com/s/1nuc6glj 密码:3g ...
- MVC的自定义动作过滤器(一)
感谢好朋友wolfy在园子里的很多有价值的文章,方便了很多朋友,向榜样学习,开始自己的总结之旅:) 遇到问题: 1.http://q.cnblogs.com/q/67382/#a_150210 //添 ...
- ESM335x Linux输出脉冲计数
1.综述 ESM335X具有4路PWM输出,其中PWM1和PWM2除了可以用于产生标准的PWM信号,现已支持输出脉冲计数功能,可以在应用程序中设置脉冲个 数,当输出脉冲个数达到指定值时,驱动程序自 ...