Commit d0b03178 authored by Carla Goncalves Ourofino's avatar Carla Goncalves Ourofino
Browse files

Merge branch 'new/soma-2553_token_header' into 'master'

SGA Posix trata token recebido nas chamadas REST

See merge request !37
parents 08de97b4 555cb374
#!/bin/sh #!/bin/sh
export SGA_NAME="chuva"
export SGA_BASE_URL="http://localhost:12345"
case "$1" in case "$1" in
job) job)
curl -v -H "Content-Type: application/json" -X POST -d '{ curl -v -H "Content-Type: application/json" -X POST -d '{
"cmd_string": "./hello.sh", "cmd_string": "bash -c \"echo user token: \\$CSBASE_USER_TOKEN \" && sleep 10 && echo bye",
"cmd_id": "gobo", "cmd_id": "job_123",
"parameters": {"foo": "bar" } "parameters": {
}' 'http://localhost:12345/v1/sga/chuva/job' "csbase_command_path": "/tmp",
"csbase_command_root_path":"/tmp",
"csbase_command_output_path":"/tmp",
"csbase_command_sandbox_paths.1":"/tmp/job_123",
"csbase_command_user_token":"USER_TOKEN",
}
}' $SGA_BASE_URL'/v1/sga/'$SGA_NAME'/job'
;; ;;
status) status)
...@@ -16,12 +25,13 @@ status) ...@@ -16,12 +25,13 @@ status)
then then
num=123 num=123
fi fi
curl -v 'http://localhost:12345/v1/sga/chuva/job/'$num'/status' curl -v $SGA_BASE_URL'/v1/sga/'$SGA_NAME'/job/'$num'/status'
;; ;;
path) path)
curl -v 'http://localhost:12345/v1/sga/chuva/path?name='$2 curl -v $SGA_BASE_URL'/v1/sga/'$SGA_NAME'/path?name='$2
;; ;;
esac esac
echo
...@@ -21,12 +21,19 @@ end ...@@ -21,12 +21,19 @@ end
-- Execute a new command. -- Execute a new command.
-- @param job The job object: job.data is a writable table for driver data. -- @param job The job object: job.data is a writable table for driver data.
-- @param cmd_string The command string -- @param cmd_string The command string
-- @param user_token A string with a token to be exported as an envvar.
-- @return True if succeded or nil and an error message -- @return True if succeded or nil and an error message
function posix.execute_command(self, job, cmd_string) function posix.execute_command(self, job, cmd_string, user_token)
self.active_commands = self.active_commands + 1 self.active_commands = self.active_commands + 1
filemonitor.init(job) filemonitor.init(job)
local job_cmd = cmd_string
-- Export the user token as an environment variable to the command string
if user_token then
job_cmd = "CSBASE_USER_TOKEN="..user_token.." "..job_cmd
end
for _, sandbox_path in ipairs(job.sandboxes) do for _, sandbox_path in ipairs(job.sandboxes) do
local ok, err = lfs.mkdir(sandbox_path) local ok, err = lfs.mkdir(sandbox_path)
if not ok then if not ok then
...@@ -65,7 +72,7 @@ function posix.execute_command(self, job, cmd_string) ...@@ -65,7 +72,7 @@ function posix.execute_command(self, job, cmd_string)
end end
local start_time = os.time() local start_time = os.time()
os.execute(cmd_string) os.execute(job_cmd)
local walltime_s = os.difftime(os.time(), start_time) local walltime_s = os.difftime(os.time(), start_time)
ok, err = util.write_file(done_file(self, job.jid), walltime_s) ok, err = util.write_file(done_file(self, job.jid), walltime_s)
if not ok then if not ok then
......
...@@ -220,10 +220,17 @@ function server.new(config, logger, client, driver) ...@@ -220,10 +220,17 @@ function server.new(config, logger, client, driver)
if sga_name ~= config.sga_name then if sga_name ~= config.sga_name then
return fail_sga_name(sga_name) return fail_sga_name(sga_name)
end end
-- FIXME Should we get the token from the request header? We could not get header info from API RestServer.
local user_token_key = "csbase_command_user_token"
local user_token = job_request.parameters[user_token_key]
job_request.parameters[user_token_key] = nil
self.logger:info("Received job: "..job_request.cmd_string.." "..job_request.cmd_id.." "..dkjson.encode(job_request.parameters)) self.logger:info("Received job: "..job_request.cmd_string.." "..job_request.cmd_id.." "..dkjson.encode(job_request.parameters))
local job = self.joblist:new_job(job_request.cmd_id, job_request.cmd_string, job_request.parameters) local job = self.joblist:new_job(job_request.cmd_id, job_request.cmd_string, job_request.parameters)
local pok, ok, err = pcall(self.driver.execute_command, self.driver, job, job_request.cmd_string)
local pok, ok, err = pcall(self.driver.execute_command, self.driver, job, job_request.cmd_string, user_token)
self.joblist:accept_job(job, pok and ok) self.joblist:accept_job(job, pok and ok)
if not pok then if not pok then
fail(ok, 500) fail(ok, 500)
......
...@@ -11,4 +11,4 @@ fi ...@@ -11,4 +11,4 @@ fi
eval $(luarocks path --bin) eval $(luarocks path --bin)
sgad ${confgfile} >> "${logfile}" 2>&1 sgad ${confgfile} 2>&1 | tee -a "${logfile}"
\ No newline at end of file
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment