mirror of
https://github.com/puppetlabs/vmpooler.git
synced 2026-01-26 10:08:40 -05:00
Validate data payload before operating on it
This commit is contained in:
parent
99a18c781a
commit
d48d487de0
1 changed files with 29 additions and 14 deletions
|
|
@ -494,6 +494,8 @@ module Vmpooler
|
||||||
put "#{api_prefix}/vm/:hostname/?" do
|
put "#{api_prefix}/vm/:hostname/?" do
|
||||||
content_type :json
|
content_type :json
|
||||||
|
|
||||||
|
failure = false
|
||||||
|
|
||||||
result = {}
|
result = {}
|
||||||
|
|
||||||
status 404
|
status 404
|
||||||
|
|
@ -504,29 +506,42 @@ module Vmpooler
|
||||||
if $redis.exists('vmpooler__vm__' + params[:hostname])
|
if $redis.exists('vmpooler__vm__' + params[:hostname])
|
||||||
jdata = JSON.parse(request.body.read)
|
jdata = JSON.parse(request.body.read)
|
||||||
|
|
||||||
|
# Validate data payload
|
||||||
|
jdata.each do |param, arg|
|
||||||
|
case param
|
||||||
|
when 'lifetime'
|
||||||
|
unless arg.to_i > 0
|
||||||
|
failure = true
|
||||||
|
end
|
||||||
|
when 'tags'
|
||||||
|
unless arg.is_a?(Hash)
|
||||||
|
failure = true
|
||||||
|
end
|
||||||
|
else
|
||||||
|
failure = true
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
|
if failure
|
||||||
|
status 400
|
||||||
|
else
|
||||||
jdata.each do |param, arg|
|
jdata.each do |param, arg|
|
||||||
case param
|
case param
|
||||||
when 'lifetime'
|
when 'lifetime'
|
||||||
arg = arg.to_i
|
arg = arg.to_i
|
||||||
|
|
||||||
if arg > 0
|
|
||||||
$redis.hset('vmpooler__vm__' + params[:hostname], param, arg)
|
$redis.hset('vmpooler__vm__' + params[:hostname], param, arg)
|
||||||
|
|
||||||
status 200
|
|
||||||
result['ok'] = true
|
|
||||||
end
|
|
||||||
when 'tags'
|
when 'tags'
|
||||||
if arg.is_a?(Hash)
|
|
||||||
arg.keys.each do |tag|
|
arg.keys.each do |tag|
|
||||||
$redis.hset('vmpooler__vm__' + params[:hostname], 'tag:' + tag, arg[tag])
|
$redis.hset('vmpooler__vm__' + params[:hostname], 'tag:' + tag, arg[tag])
|
||||||
end
|
end
|
||||||
|
end
|
||||||
|
end
|
||||||
|
|
||||||
status 200
|
status 200
|
||||||
result['ok'] = true
|
result['ok'] = true
|
||||||
end
|
end
|
||||||
end
|
end
|
||||||
end
|
|
||||||
end
|
|
||||||
|
|
||||||
JSON.pretty_generate(result)
|
JSON.pretty_generate(result)
|
||||||
end
|
end
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue