2017-03-26 00:10:09 +00:00
|
|
|
import falcon
|
|
|
|
import logging
|
2017-03-26 16:58:29 +00:00
|
|
|
from ipaddress import ip_address
|
2017-03-26 00:10:09 +00:00
|
|
|
from datetime import datetime
|
|
|
|
from certidude import config, authority
|
|
|
|
from certidude.decorators import serialize
|
2017-04-13 20:30:28 +00:00
|
|
|
from xattr import getxattr, listxattr
|
2017-03-26 00:10:09 +00:00
|
|
|
|
2017-04-04 05:02:08 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
2017-03-26 00:10:09 +00:00
|
|
|
|
|
|
|
class AttributeResource(object):
|
|
|
|
@serialize
|
|
|
|
def on_get(self, req, resp, cn):
|
|
|
|
"""
|
|
|
|
Return extended attributes stored on the server.
|
|
|
|
This not only contains tags and lease information,
|
|
|
|
but might also contain some other sensitive information.
|
|
|
|
"""
|
2017-03-26 16:58:29 +00:00
|
|
|
try:
|
|
|
|
path, buf, cert = authority.get_signed(cn)
|
|
|
|
except IOError:
|
|
|
|
raise falcon.HTTPNotFound()
|
|
|
|
else:
|
|
|
|
attribs = dict()
|
|
|
|
for key in listxattr(path):
|
|
|
|
if not key.startswith("user."):
|
|
|
|
continue
|
|
|
|
value = getxattr(path, key)
|
|
|
|
current = attribs
|
|
|
|
if "." in key:
|
|
|
|
namespace, key = key.rsplit(".", 1)
|
|
|
|
for component in namespace.split("."):
|
|
|
|
if component not in current:
|
|
|
|
current[component] = dict()
|
|
|
|
current = current[component]
|
|
|
|
current[key] = value
|
2017-03-26 00:10:09 +00:00
|
|
|
|
2017-04-25 10:06:59 +00:00
|
|
|
try:
|
|
|
|
whitelist = ip_address(attribs.get("user").get("lease").get("address").decode("ascii"))
|
|
|
|
except AttributeError: # TODO: probably race condition
|
|
|
|
raise falcon.HTTPForbidden("Forbidden",
|
|
|
|
"Attributes only accessible to the machine")
|
2017-03-26 00:10:09 +00:00
|
|
|
|
2017-03-26 16:58:29 +00:00
|
|
|
if req.context.get("remote_addr") != whitelist:
|
|
|
|
logger.info("Attribute access denied from %s, expected %s for %s",
|
|
|
|
req.context.get("remote_addr"),
|
|
|
|
whitelist,
|
|
|
|
cn)
|
|
|
|
raise falcon.HTTPForbidden("Forbidden",
|
|
|
|
"Attributes only accessible to the machine")
|
2017-03-26 00:10:09 +00:00
|
|
|
|
2017-03-26 16:58:29 +00:00
|
|
|
return attribs
|