Gateone初步--django堡壘機實現
阿新 • • 發佈:2019-01-26
部署
首先需要部署gateone,gateone是用tornado寫的
可以直接使用docker,然後啟動之後簡單的配置就可以了。或者下載原始碼包,或者rpm包進行安裝,這裡就不說詳細的安裝過程了。
具體可以見github上的內容。
或者參考官方文件
http://liftoff.github.io/GateOne/About/installation.html
我使用的是原始碼包安裝的方式,因為要根據官方文件檢視一些他的原始碼
基本配置
因為我要和堡壘機相結合,所以這裡的認證方式需要使用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的頁面
<!-- gateone.html -->
<!-- body的內容-->
<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>
<!-- 需要用gateone.js 和jquery.js。-->
<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"
});
<!--根據認證資訊去連線websocket -->
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,就可以看到了。