Gateone初步--django堡垒机实现

部署

首先需要部署gateone,gateone是用tornado写的
可以直接使用docker,然后启动之后简单的配置就可以了。或者下载源码包,或者rpm包进行安装,这里就不说详细的安装过程了。
具体可以见github上的内容。
或者参考官方文档
http://liftoff.github.io/GateOne/About/installation.html

推荐使用docker的方式
https://github.com/liftoff/GateOne/tree/master/docker
在测试环境写完dockerfile之后,启动docker,注意开放的端口和持久化存储的目录。

我使用的是源码包安装的方式,因为要根据官方文档查看一些他的源码

基本配置

因为我要和堡垒机相结合,所以这里的认证方式需要使用api的方式。
首先进入到代码目录。

cd gateone
python run_gateone.py --new_api_key
# 生成一个api的key
cd conf.d
vim 10server.conf
# 配置要调用gateone的web应用地址及端口
"origins": ["localhost:10443", "127.0.0.1:10443","127.0.0.1:8088","172.16.13.80:8088"],
# 修改origins这一行,添加应用地址及端口
vim  20authentication.conf 
配置认证方式
"auth":"api",
cat  30api_keys.conf
// This file contains the key and secret pairs used by Gate One's API authentication method.
{
    "*": {
        "gateone": {
            "api_keys": {
                "ODdiN2QwZjI3OGUwNGQ4Njg2M2I5MTY3NTM1NTVjMWQyZ": "MTY5ZWVjYmU0YmFiNGYzNDliYjQxYWY2YTg2MjllNDc0N"
            }
        }
    }
}

# key1:secret1,key2:secret2,
# 左边的是key,右边的是secret 后面调用的时候要用到

基本web应用嵌入

在做一个堡垒机的项目,使用的是django,所以以django为例。

// host_list.html 
<a href="{% url 'host_connect' %}?host={{ post.host_ip }}&&user={{ post.host_user }}&&port={{ post.host_port }}" target="_blank" class="btn btn-xs btn-info">连接a>
// html 代码,展示的是主机列表。然后又连接按钮。

# view.py 
def host_connect(request):
    host_ip = request.GET.get('host',None)
    host_user = request.GET.get('user','root')
    host_port = request.GET.get('port',22)

    if host_port =="":host_port=22
    print host_port
    if not host_ip :
        return my_render('404.html',locals(),request)
    else:
        return my_render('gateone.html',locals(),request)

# 获取get请求的信息,然后返回gateone的页面


<script type="text/javascript" src="/static/js/gateone/gateone.js">script>
<div id="gateone_container" style="position: relative; width: 100%; height: 50em;margin: 0 auto">

    <div id="gateone">div>

div>

<script type="application/javascript" src="/static/js/jquery-2.1.1.js">script>




<script type="text/javascript">
        $(document).ready(function(){
            var ip = '{{ host_ip }}';
            var user = '{{ host_user }}';
            var port = {{ host_port }};
            var ssh_url = 'ssh://'+user+'@'+ip+':'+port;
            
            var request = $.ajax({
                url:'{% url "get_auth_obj" %}',   // api认证方式, 
                type:"GET",
                dataType:"json"
            });

            
            request.done(function(auth_info){
                console.log(auth_info.auth);
                var auth_message = auth_info.auth;
                var auth_url = auth_info.url;
                GateOne.init({
                    auth: auth_message,
                    url:auth_url,
                    theme:'solarized',
                    goDiv:'#gateone',
                    disableTermTransitions:'true',
                    autoConnectURL:ssh_url
                });

            });
            GateOne.Base.superSandbox("GateOne.SomePlugin", ["GateOne", "GateOne.Net",  "GateOne.Terminal.Input", "GateOne.Terminal"], function(window, undefined) {

                var location =  ip;
                GateOne.prefs.autoConnectURL=ssh_url;
                GateOne.prefs.fontSize="100%";
                GateOne.prefs.scrollback = 10000;  // scrollback buffer up to 10,000 lines
                GateOne.Terminal.loadFont("Source Code Pro", "150%");
{#                GateOne.locations; // Holds the state of all current known/open locations#}
                GateOne.Net.setLocation(location); 
                

            });


        }); 





    script>
# views.py   get_auth_obj
# 可参考官方文档的例子。
def create_signature(secret, *parts):
    import hmac, hashlib
    hash = hmac.new(secret, digestmod=hashlib.sha1)
    for part in parts:
        hash.update(str(part))
    return hash.hexdigest()

def get_auth_obj(request):
    import time, hmac, hashlib, json
    user = request.user.username
    # 安装gateone的服务器以及端口.
    gateone_server = 'https://172.16.13.80:10443'
    # 之前生成的api_key 和secret 
    secret = "MTY5ZWVjYmU0YmFiNGYzNDliYjQxYWY2YTg2MjllNDc0N"
    api_key = "ODdiN2QwZjI3OGUwNGQ4Njg2M2I5MTY3NTM1NTVjMWQyZ"

    authobj = {
        'api_key': api_key,
        'upn': "gateone",
        'timestamp': str(int(time.time() * 1000)),
        'signature_method': 'HMAC-SHA1',
        'api_version': '1.0'
    }
    my_hash = hmac.new(secret, digestmod=hashlib.sha1)
    my_hash.update(authobj['api_key'] + authobj['upn'] + authobj['timestamp'])

    authobj['signature'] = my_hash.hexdigest()
    auth_info_and_server = {"url": gateone_server, "auth": authobj}
    valid_json_auth_info = json.dumps(auth_info_and_server)
    #   print valid_json_auth_info
    return HttpResponse(valid_json_auth_info)

以上是gateone简单的使用,只实现了点击跳转连接,但还是得手动输入密码,
gateone还可以实现操作记录,记录的格式是golog,默认位置在
gateone/users/gateone/logs
gateone/gateone/applications/terminal/logviewer.py XX.golog
可以在终端上播放操作记录

当然在页面上也可以播放,在页面右侧的terminal application panel ,打开后有个logviewer,就可以看到了。

你可能感兴趣的:(python,django,gateone,webssh)