Merge pull request #230 from FirmlyReality/network

Fix some bugs.
This commit is contained in:
Yujian Zhu 2017-05-08 17:58:04 +08:00 committed by GitHub
commit 3c971b3bc1
6 changed files with 32 additions and 38 deletions

View File

@ -170,3 +170,8 @@
# USER_PORT: user listen port
# default:9100
# USER_PORT=""
# AUTH_KEY: the key to request users server from master,
# or to request master from users server. Please set the
# same value on each machine. Please don't use the default value.
# AUTH_KEY="docklet"

View File

@ -66,5 +66,7 @@ def getenv(key):
return os.environ.get("USER_IP","0.0.0.0")
elif key =="USER_PORT":
return int(os.environ.get("USER_PORT",9100))
elif key =="AUTH_KEY":
return os.environ.get("AUTH_KEY","docklet")
else:
return os.environ[key]

View File

@ -68,30 +68,16 @@ def login_required(func):
return wrapper
def token_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
fspath = env.getenv('FS_PREFIX')
tokenfile = open(fspath+"/global/token", 'r')
token_1 = tokenfile.readline().strip()
token_2 = request.form.get("token",None)
if token_2 is None or token_1 != token_2:
return json.dumps({'success':'false','message':'Token is required!'})
else:
return func(*args, **kwargs)
return wrapper
def user_ip_required(func):
def auth_key_required(func):
@wraps(func)
def wrapper(*args,**kwargs):
global G_userip
ip = request.remote_addr
key_1 = env.getenv('AUTH_KEY')
key_2 = request.form.get("auth_key",None)
#logger.info(str(ip) + " " + str(G_userip))
if ip == '127.0.0.1' or ip == '0.0.0.0' or ip == G_userip:
if key_2 is not None and key_1 == key_2:
return func(*args, **kwargs)
else:
return json.dumps({'success':'false','message':'User node\'s ip is required!'})
return json.dumps({'success':'false','message': 'auth_key is required!'})
return wrapper
@ -294,7 +280,7 @@ def list_cluster(user, beans, form):
return json.dumps({'success':'false', 'action':'list cluster', 'message':clusterlist})
@app.route("/cluster/stopall/",methods=['POST'])
@token_required
@auth_key_required
def stopall_cluster():
global G_vclustermgr
user = request.form.get('username',None)
@ -520,7 +506,7 @@ def listphynodes_monitor(user, beans, form):
return json.dumps({'success':'true', 'monitor':res})
@app.route("/billing/beans/", methods=['POST'])
@token_required
@auth_key_required
def billing_beans():
form = request.form
res = post_to_user("/billing/beans/",data=form)

View File

@ -215,10 +215,8 @@ class Container_Collector(threading.Thread):
raise
# update users' tables in database
owner_name = get_owner(vnode_name)
fspath = env.getenv('FS_PREFIX')
tokenfile = open(fspath+"/global/token", 'r')
token = tokenfile.readline().strip()
data = {"owner_name":owner_name,"billing":billingval, "token":token}
auth_key = env.getenv('AUTH_KEY')
data = {"owner_name":owner_name,"billing":billingval, "auth_key":auth_key}
request_master("/billing/beans/",data)
return billingval

View File

@ -368,6 +368,9 @@ class VclusterMgr(object):
# so, check is necessary
self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True')
# set proxy
if not "proxy_server_ip" in info.keys():
info['proxy_server_ip'] = self.addr
write_clusterinfo(info,clustername,username)
try:
target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000"
if self.distributedgw == 'True':
@ -408,6 +411,9 @@ class VclusterMgr(object):
# need to check and recover gateway of this user
self.networkmgr.check_usergw(username, self.nodemgr,self.distributedgw=='True')
# recover proxy of cluster
if not "proxy_server_ip" in info.keys():
info['proxy_server_ip'] = self.addr
write_clusterinfo(info,clustername,username)
try:
target = 'http://'+info['containers'][0]['ip'].split('/')[0]+":10000"
if self.distributedgw == 'True':

View File

@ -56,17 +56,16 @@ def login_required(func):
return wrapper
def token_required(func):
def auth_key_required(func):
@wraps(func)
def wrapper(*args, **kwargs):
fspath = env.getenv('FS_PREFIX')
tokenfile = open(fspath+"/global/token", 'r')
token_1 = tokenfile.readline().strip()
token_2 = request.form.get("token",None)
if token_2 is None or token_1 != token_2:
return json.dumps({'success':'false','message':'Token is required!'})
else:
key_1 = env.getenv('AUTH_KEY')
key_2 = request.form.get("auth_key",None)
#logger.info(str(ip) + " " + str(G_userip))
if key_2 is not None and key_1 == key_2:
return func(*args, **kwargs)
else:
return json.dumps({'success':'false','message': 'auth_key is required!'})
return wrapper
@ -442,7 +441,7 @@ def query_self_notifications_infos(cur_user, user, form):
return json.dumps(result)
@app.route("/billing/beans/", methods=['POST'])
@token_required
@auth_key_required
def billing_beans():
logger.info("handle request: /billing/beans/")
form = request.form
@ -475,10 +474,8 @@ def billing_beans():
if owner.beans <= 0:
# stop all vcluster of the user if his beans are equal to or lower than 0.
logger.info("The beans of User(" + str(owner) + ") are less than or equal to zero, all his or her vclusters will be stopped.")
fspath = env.getenv('FS_PREFIX')
tokenfile = open(fspath+"/global/token", 'r')
token = tokenfile.readline().strip()
form = {'username':owner.username, 'token':token}
auth_key = env.getenv('AUTH_KEY')
form = {'username':owner.username, 'auth_key':auth_key}
request_master("/cluster/stopall/",form)
return json.dumps({'success':'true'})