summaryrefslogtreecommitdiffstats
path: root/server.lua
blob: c7988529b0c649a6393b16c0c1418a44741c02ef (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
--[[
Copyright (c) 2012-2014 Kaarle Ritvanen
See LICENSE file for license details
--]]

local aconf = require('aconf')
local mbin = aconf.model.binary
local mnode = aconf.model.node
local isinstance = aconf.object.isinstance

local json = require('cjson')
local posix = require('posix')
local stringy = require('stringy')


math.randomseed(os.time())

local save_req = os.execute('[ $(stat -f -c "%T" /) = tmpfs ]')

-- TODO shared storage for sessions
local sessions = {}


return function(env)
	  for sid, session in pairs(sessions) do
	     if session.expires < os.time() then sessions[sid] = nil end
	  end

	  local method = env.REQUEST_METHOD
	  local path = env.PATH_INFO

	  local function wrap(code, headers, res, encode)
	     if not headers then headers = {} end

	     if res then
		local ctype
		if encode then
		   ctype = 'application/json'
		   res = json.encode(res)
		else ctype = 'text/plain' end
		headers['Content-Type'] = ctype
	     else res = '' end

	     return code, headers, coroutine.wrap(
		function()
		   coroutine.yield(res)
		   posix.close(uwsgi.connection_fd())
		   aconf.commit()
		end
	     )
	  end

	  if path == '/' then
	     if method ~= 'GET' then return wrap(405) end
	     return wrap(301, {['Location']='/browser/'})
	  end

	  local data
	  local length = env.CONTENT_LENGTH and tonumber(
	     env.CONTENT_LENGTH
	  ) or 0
	  if length > 0 then
	     local success
	     success, data = pcall(
		json.decode,
		env.input:read(length)
	     )
	     if not success then
		return wrap(400, nil, 'Request not in JSON format')
	     end
	  end

	  local session, user, txn_id
	  function reset_session_expiry() session.expires = os.time() + 600 end

	  local sid = tonumber(env.HTTP_X_ACONF_AUTH_TOKEN)
	  if sid then
	     session = sessions[sid]
	     if not session then return wrap(401) end
	     reset_session_expiry()
	     user = session.user
	     txn_id = tonumber(env.HTTP_X_ACONF_TRANSACTION_ID)
	  end

	  local parent_txn
	  if txn_id then
	     parent_txn = session.txns[txn_id]
	     if not parent_txn then
		return wrap(400, nil, 'Invalid transaction ID')
	     end
	  end

	  local function new_txn(defer_validation)
	     return aconf.start_txn{
		allow_commit_defer=true,
		defer_validation=defer_validation,
		parent=parent_txn
	     }
	  end

	  local txn = new_txn(true)

	  local function fetch_user(name)
	     user = name and txn:fetch('/auth/users')[name]
	  end
	  if user then
	     fetch_user(user)
	     if not user then return wrap(401) end
	  end

	  if path == '/login' then
	     if method == 'POST' then
		if not data.username or not data.password then
		   return wrap(401)
		end
		fetch_user(data.username)
		if user and user:check_password(data.password) then
		   local sid
		   repeat
		      sid = math.floor(math.random() * 2^32)
		   until not sessions[sid]

		   session = {user=data.username, last_txn_id=0, txns={}}
		   reset_session_expiry()
		   sessions[sid] = session
		   return wrap(
		      204,
		      {
			 ['X-AConf-Auth-Token']=sid,
			 ['X-AConf-Save-Required']=save_req and 1 or 0
		      }
		   )
		end
		return wrap(401)
	     end

	     if not user then return wrap(401) end

	     if method == 'DELETE' then
		sessions[sid] = nil
		return wrap(204)
	     end

	     return wrap(405)
	  end

	  if not user then return wrap(401) end

	  if path == '/save' then
	     if not save_req then return wrap(404) end
	     if method ~= 'POST' then return wrap(405) end
	     if os.execute('lbu commit') then wrap(204) end
	     return wrap(500, nil, 'lbu commit failed')
	  end

	  local success, code, hdr, res = aconf.call(
	     function()
		if stringy.startswith(path, '/meta/') then
		   if method ~= 'GET' then return 405 end
		   return 200, nil, txn:meta(path:sub(6, -1))
		end

		if stringy.startswith(path, '/config/') then
		   path = path:sub(8, -1)
		   
		   local parent, name, res
		   if path ~= '/' then
		      parent = txn:fetch(aconf.path.parent(path))
		      name = aconf.path.name(path)
		   end
		   
		   if method == 'GET' then
		      local obj = txn:fetch(path)

		      if type(obj) == 'function' then return 404 end

		      if isinstance(obj, mnode.TreeNode) then
			 if not mnode.has_permission(obj, user, 'read') then
			    return 403
			 end

			 local node = {}
			 for k, v in mnode.pairs(obj) do
			    local readable = true
			    
			    if isinstance(v, mnode.TreeNode) then
			       readable = mnode.has_permission(
				  v,
				  user,
				  'read'
			       )
			       v = mnode.path(v)

			    elseif isinstance(v, mbin.Data) then v = v.path end
			    
			    if readable then node[k] = v end
			 end
			 res = {data=node, meta=mnode.meta(obj)}
		   
		      elseif mnode.has_permission(parent, user, 'read') then
			 if isinstance(obj, mbin.Data) then
			    obj = obj:encode()
			 end
			 res = {data=obj, meta=mnode.mmeta(parent, name)}
		      else return 403 end

		      return 200, nil, res
		   end

		   if method == 'POST' then
		      local obj = txn:fetch(path)

		      if isinstance(obj, mnode.List) then
			 if not mnode.has_permission(obj, user, 'create') then
			    return 403
			 end

			 local index
			 if not isinstance(obj, mnode.Set) then
			    index = data.index
			    data = data.data
			 end
			 mnode.insert(obj, data, index)

		      elseif type(obj) == 'function' then
			 if not mnode.has_permission(parent, user, name) then
			    return 403
			 end
			 res = obj(data)

		      else return 405 end

		   else
		      local obj = parent[name]
		      if obj ~= nil and not isinstance(obj, mnode.TreeNode) then
			 obj = parent
		      end

		      if method == 'DELETE' then
			 if obj == nil then return 404 end
			 if not mnode.has_permission(obj, user, 'delete') then
			    return 403
			 end
			 parent[name] = nil

		      elseif method == 'PUT' then
			 if isinstance(parent, mnode.Set) then return 405 end
			 local permission = 'modify'
			 if obj == nil then
			    obj = parent
			    permission = 'create'
			 end
			 if not mnode.has_permission(obj, user, permission) then
			    return 403
			 end
			 parent[name] = data
			 
		      else return 405 end
		   end
		   
		   txn:commit()
		   return res == nil and 205 or 200, nil, res
		end

		if path == '/transaction' then
		   if ({DELETE=true, PUT=true})[method] then
		      if not txn_id then return 405 end
		      if method == 'PUT' then parent_txn:commit() end
		      session.txns[txn_id] = nil
		      return 204
		   end

		   if method ~= 'POST' then return 405 end

		   session.last_txn_id = session.last_txn_id + 1
		   local txn_id = session.last_txn_id
		   session.txns[txn_id] = new_txn()
		   return 204, {['X-AConf-Transaction-ID']=txn_id}
		end

		return 404
	     end
	  )

	  if success then return wrap(code, hdr, res, true) end

	  if code.conflict then return wrap(409, nil, code.conflict, true) end
	  
	  return wrap(422, nil, code, true)
       end