~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/s3/acls.py

  • Committer: Duncan McGreggor
  • Date: 2009-11-22 02:20:42 UTC
  • mto: (44.3.2 484858-s3-scripts)
  • mto: This revision was merged to the branch mainline in revision 52.
  • Revision ID: duncan@canonical.com-20091122022042-4zi231hxni1z53xd
* Updated the LICENSE file with copyright information.
* Updated the README with license information.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
from txaws.util import XML
2
 
 
3
 
 
4
 
PERMISSIONS = ("FULL_CONTROL",
5
 
               "WRITE",
6
 
               "WRITE_ACP",
7
 
               "READ",
8
 
               "READ_ACP")
9
 
 
10
 
 
11
 
class XMLMixin(object):
12
 
 
13
 
    def to_xml(self):
14
 
        return "".join(self._to_xml())
15
 
 
16
 
 
17
 
class AccessControlPolicy(XMLMixin):
18
 
 
19
 
    def __init__(self, owner=None, access_control_list=()):
20
 
        self.owner = owner
21
 
        self.access_control_list = access_control_list
22
 
 
23
 
    def _to_xml(self, buffer=None):
24
 
        if buffer is None:
25
 
            buffer = []
26
 
        buffer.append("<AccessControlPolicy>\n")
27
 
        if self.owner:
28
 
            self.owner._to_xml(buffer=buffer, indent=1)
29
 
        buffer.append("  <AccessControlList>\n")
30
 
        for grant in self.access_control_list:
31
 
            grant._to_xml(buffer=buffer, indent=2)
32
 
        buffer.append("  </AccessControlList>\n"
33
 
                   "</AccessControlPolicy>")
34
 
        return buffer
35
 
 
36
 
    @classmethod
37
 
    def from_xml(cls, xml_bytes):
38
 
        root = XML(xml_bytes)
39
 
        owner_node = root.find("Owner")
40
 
        owner = Owner(owner_node.findtext("ID"),
41
 
                      owner_node.findtext("DisplayName"))
42
 
        acl_node = root.find("AccessControlList")
43
 
        acl = []
44
 
        for grant_node in acl_node.findall("Grant"):
45
 
            grantee_node = grant_node.find("Grantee")
46
 
            grantee = Grantee(grantee_node.findtext("ID"),
47
 
                              grantee_node.findtext("DisplayName"))
48
 
            permission = grant_node.findtext("Permission")
49
 
            acl.append(Grant(grantee, permission))
50
 
        return cls(owner, acl)
51
 
 
52
 
 
53
 
class Grant(XMLMixin):
54
 
 
55
 
    def __init__(self, grantee, permission=None):
56
 
        self.grantee = grantee
57
 
        self.permission = permission
58
 
 
59
 
    def _set_permission(self, perm):
60
 
        if perm not in PERMISSIONS:
61
 
            raise ValueError("Invalid permission '%s'. Must be one of %s" %
62
 
                             (perm, ",".join(PERMISSIONS)))
63
 
        self._permission = perm
64
 
 
65
 
    def _get_permission(self):
66
 
        return self._permission
67
 
 
68
 
    permission = property(_get_permission, _set_permission)
69
 
 
70
 
    def _to_xml(self, buffer=None, indent=0):
71
 
        if buffer is None:
72
 
            buffer = []
73
 
        ws = " " * (indent * 2)
74
 
        buffer.append(ws + "<Grant>\n")
75
 
        if self.grantee:
76
 
            self.grantee._to_xml(buffer, indent + 1)
77
 
        if self.permission:
78
 
            buffer.append("%s  <Permission>%s</Permission>\n" % (
79
 
                          ws, self.permission))
80
 
        buffer.append(ws + "</Grant>\n")
81
 
        return buffer
82
 
 
83
 
 
84
 
class Owner(XMLMixin):
85
 
 
86
 
    def __init__(self, id, display_name):
87
 
        self.id = id
88
 
        self.display_name = display_name
89
 
 
90
 
    def _to_xml(self, buffer=None, indent=0):
91
 
        if buffer is None:
92
 
            buffer = []
93
 
        ws = " " * (indent * 2)
94
 
        buffer.append("%s<Owner>\n"
95
 
                      "%s  <ID>%s</ID>\n"
96
 
                      "%s  <DisplayName>%s</DisplayName>\n"
97
 
                      "%s</Owner>\n" % (ws, ws, self.id, ws, self.display_name,
98
 
                                        ws))
99
 
        return buffer
100
 
 
101
 
 
102
 
class Grantee(XMLMixin):
103
 
 
104
 
    def __init__(self, id="", display_name="", email_address="", uri=""):
105
 
        if id or display_name:
106
 
            msg = "Both 'id' and 'display_name' must be provided."
107
 
            if not (id and display_name):
108
 
                raise ValueError(msg)
109
 
        self.id = id
110
 
        self.display_name = display_name
111
 
        self.email_address = email_address
112
 
        self.uri = uri
113
 
 
114
 
    def _to_xml(self, buffer=None, indent=0):
115
 
        if buffer is None:
116
 
            buffer = []
117
 
        ws = " " * (indent * 2)
118
 
        if self.id and self.display_name:
119
 
            xsi_type = "CanonicalUser"
120
 
            value = ("%s  <ID>%s</ID>\n"
121
 
                     "%s  <DisplayName>%s</DisplayName>\n" % (
122
 
                        ws, self.id, ws, self.display_name))
123
 
        elif self.email_address:
124
 
            xsi_type = "AmazonCustomerByEmail"
125
 
            value = "%s  <EmailAddress>%s</EmailAddress>\n" % (
126
 
                ws, self.email_address)
127
 
        elif self.uri:
128
 
            xsi_type = "Group"
129
 
            value = "%s  <URI>%s</URI>\n" % (ws, self.uri)
130
 
        buffer.append("%s<Grantee "
131
 
                      'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
132
 
                      ' xsi:type="%s">\n'
133
 
                      "%s%s</Grantee>\n" % (ws, xsi_type, value, ws))
134
 
        return buffer