saltstack的rest接口salt-api开发—基于flask-restful

以前官方给的salt-api在前期用着也还不错,后来发现貌似不支持timeout选项(也许我太弱,也没研究过它cherrypy的框架),搞来搞去实在没办法就拿着flask从新封装了一层webapi。
python


刚学的flask,写的很差勿喷(彻底是为了公司需求啊!!)nginx


代码都放在github上了,基本功能OK,只去掉了根据公司需求自定义的一些功能类。git

地址:https://github.com/lustlost/saltstack-api-Flaskgithub


简单的搞了一个token验证,经过rest传过去的。这里就讲下具体的认证方式:web


首先双方协商一个key,加上当先时间,到小时为止,而后hash出一个值做为最终的token,因此这个token是每小时都会变化的,双方协商好key就会自动生成。也能够精确到每十分之或者每分钟,具体看不一样需求了。flask


class Token():
    def __init__(self):
        self.now_time = time.strftime('%Y-%m-%d-%H',time.localtime(time.time()))
    def getToken(self,key):
        md5 = hashlib.md5()
        md5.update(self.now_time+key)
        return md5.hexdigest()
    def authToken(self,one_token,two_token):
        if one_token == two_token:
            return True
        else:
            return False



这是一个请求的demo,本身修改para_dict里的value就好了,能够根据需求在para_dict中能够加上timeout的值api

import commands,urllib
import time,sys
import hashlib
now_time = time.strftime('%Y-%m-%d-%H',time.localtime(time.time()))
key = 'haha'
md5 = hashlib.md5()
md5.update(now_time+key)
token = md5.hexdigest()
"""
para_dict={
    "tgt":"200-119-0.jh.qszg.uuzu.idc",
    "server_config":"true",
    "token":token
}
"""
para_dict={
    "tgt":"200-119-0.jh.qszg.uuzu.idc",
    "fun":"cmd.run",
    "args":"uptime",
    "expr_form":"list",
    "token":token
}
api_url = "http://10.0.5.201:5000/api"
                                                                                                                                                                      
post_para = urllib.urlencode(para_dict)
api_info=urllib.urlopen(api_url,post_para).read()
print "Now Token : %s" % token
print api_info


最后经过uwsgi和nginx整合,提供一下uwsgi.xmlapp

<uwsgi>
     <pythonpath>/root/test</pythonpath>
     <module>api</module>
     <callable>app</callable>
     <socket>/tmp/uwsgi.sock</socket>
     <master/>
     <processes>4</processes>
     <memory-report/>
</uwsgi>


看下效果:框架

wKiom1MUVjTwgKm-AACBybSdDKE584.jpg

相关文章
相关标签/搜索