Token获取方式从文件改为环境变量 (#5)

token获取方式改为环境变量
This commit is contained in:
zema1 2017-05-30 13:14:49 +08:00 committed by 李扬
parent de58bb33f2
commit cbcc9cb313
7 changed files with 13 additions and 17 deletions

View File

@ -5,10 +5,9 @@ services:
before_install:
- sudo mkdir -p /data/log
- pip install requests
- echo "token" > token.txt
script:
- docker pull qduoj/judge_server
- cp docker-compose.example.yml docker-compose.yml
- docker-compose up -d
- docker ps -a
- python tests/tests.py
- python tests/tests.py

View File

@ -18,9 +18,9 @@ services:
- $PWD/tests/test_case:/test_case
- /data/log:/log
- $PWD/server:/code:ro
- $PWD/token.txt:/token.txt
environment:
- service_discovery_url=https://virusdefender.net/service.php
- service_url=http://1.2.3.4:12358
- token=YOUR_TOKEN_HERE
ports:
- "0.0.0.0:12358:8080"

View File

@ -20,5 +20,3 @@ COMPILER_GROUP_GID = grp.getgrnam("compiler").gr_gid
TEST_CASE_DIR = "/test_case"
SPJ_SRC_DIR = "/spj"
SPJ_EXE_DIR = "/spj"
TOKEN_FILE_PATH = "/token.txt"

View File

@ -11,14 +11,14 @@ from utils import server_info, logger, token
class JudgeService(object):
def __init__(self):
# this container's ip and port, if these are not set, web server will think it's a linked container
self.service_url = os.environ.get("service_url")
# exists if docker link oj_web_server:oj_web_server
self.service_discovery_host = os.environ.get("OJ_WEB_SERVER_PORT_8080_TCP_ADDR")
self.service_discovery_port = os.environ.get("OJ_WEB_SERVER_PORT_8080_TCP_PORT")
self.service_discovery_url = os.environ.get("service_discovery_url", "")
# this container's ip and port, if these are not set, web server will think it's a linked container
self.service_url = os.environ.get("service_url")
if not self.service_discovery_url:
if not (self.service_discovery_host and self.service_discovery_port):
raise JudgeServiceError("service discovery host or port not found")

View File

@ -5,8 +5,8 @@ import psutil
import socket
import logging
import hashlib
import os
from config import TOKEN_FILE_PATH
from exception import JudgeClientError
@ -28,11 +28,11 @@ def server_info():
def get_token():
try:
with open(TOKEN_FILE_PATH, "r") as f:
return f.read().strip()
except IOError:
raise JudgeClientError("token.txt not found")
token = os.environ.get("token")
if token:
return token
else:
raise JudgeClientError("ENV token not found")
token = hashlib.sha256(get_token()).hexdigest()

View File

@ -24,7 +24,7 @@ class JudgeServerClientForTokenHeaderTest(JudgeServerClient):
class JudgeServerTest(unittest.TestCase):
def setUp(self):
self.token = "token"
self.token = "YOUR_TOKEN_HERE"
self.server_base_url = "http://127.0.0.1:12358"
self.client = JudgeServerClient(token=self.token, server_base_url=self.server_base_url)
@ -44,4 +44,4 @@ class JudgeServerTest(unittest.TestCase):
self.assertEqual(data["err"], "TokenVerificationFailed")
if __name__ == '__main__':
unittest.main()
unittest.main()

View File

@ -1 +0,0 @@
PLEASE_USE_YOUR_OWN_TOKEN