~soren/nova/iptables-security-groups

« back to all changes in this revision

Viewing changes to vendor/boto/boto/s3/acl.py

  • Committer: Jesse Andrews
  • Date: 2010-05-28 06:05:26 UTC
  • Revision ID: git-v1:bf6e6e718cdc7488e2da87b21e258ccc065fe499
initial commit

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
# Copyright (c) 2006,2007 Mitch Garnaat http://garnaat.org/
 
2
#
 
3
# Permission is hereby granted, free of charge, to any person obtaining a
 
4
# copy of this software and associated documentation files (the
 
5
# "Software"), to deal in the Software without restriction, including
 
6
# without limitation the rights to use, copy, modify, merge, publish, dis-
 
7
# tribute, sublicense, and/or sell copies of the Software, and to permit
 
8
# persons to whom the Software is furnished to do so, subject to the fol-
 
9
# lowing conditions:
 
10
#
 
11
# The above copyright notice and this permission notice shall be included
 
12
# in all copies or substantial portions of the Software.
 
13
#
 
14
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
 
15
# OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABIL-
 
16
# ITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT
 
17
# SHALL THE AUTHOR BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, 
 
18
# WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 
19
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
 
20
# IN THE SOFTWARE.
 
21
 
 
22
from boto.s3.user import User
 
23
 
 
24
 
 
25
CannedACLStrings = ['private', 'public-read',
 
26
                    'public-read-write', 'authenticated-read']
 
27
 
 
28
 
 
29
class Policy:
 
30
 
 
31
    def __init__(self, parent=None):
 
32
        self.parent = parent
 
33
        self.acl = None
 
34
 
 
35
    def __repr__(self):
 
36
        grants = []
 
37
        for g in self.acl.grants:
 
38
            if g.id == self.owner.id:
 
39
                grants.append("%s (owner) = %s" % (g.display_name, g.permission))
 
40
            else:
 
41
                if g.type == 'CanonicalUser':
 
42
                    u = g.display_name
 
43
                elif g.type == 'Group':
 
44
                    u = g.uri
 
45
                else:
 
46
                    u = g.email
 
47
                grants.append("%s = %s" % (u, g.permission))
 
48
        return "<Policy: %s>" % ", ".join(grants)
 
49
 
 
50
    def startElement(self, name, attrs, connection):
 
51
        if name == 'Owner':
 
52
            self.owner = User(self)
 
53
            return self.owner
 
54
        elif name == 'AccessControlList':
 
55
            self.acl = ACL(self)
 
56
            return self.acl
 
57
        else:
 
58
            return None
 
59
 
 
60
    def endElement(self, name, value, connection):
 
61
        if name == 'Owner':
 
62
            pass
 
63
        elif name == 'AccessControlList':
 
64
            pass
 
65
        else:
 
66
            setattr(self, name, value)
 
67
 
 
68
    def to_xml(self):
 
69
        s = '<AccessControlPolicy>'
 
70
        s += self.owner.to_xml()
 
71
        s += self.acl.to_xml()
 
72
        s += '</AccessControlPolicy>'
 
73
        return s
 
74
 
 
75
class ACL:
 
76
 
 
77
    def __init__(self, policy=None):
 
78
        self.policy = policy
 
79
        self.grants = []
 
80
 
 
81
    def add_grant(self, grant):
 
82
        self.grants.append(grant)
 
83
 
 
84
    def add_email_grant(self, permission, email_address):
 
85
        grant = Grant(permission=permission, type='AmazonCustomerByEmail',
 
86
                      email_address=email_address)
 
87
        self.grants.append(grant)
 
88
 
 
89
    def add_user_grant(self, permission, user_id):
 
90
        grant = Grant(permission=permission, type='CanonicalUser', id=user_id)
 
91
        self.grants.append(grant)
 
92
 
 
93
    def startElement(self, name, attrs, connection):
 
94
        if name == 'Grant':
 
95
            self.grants.append(Grant(self))
 
96
            return self.grants[-1]
 
97
        else:
 
98
            return None
 
99
 
 
100
    def endElement(self, name, value, connection):
 
101
        if name == 'Grant':
 
102
            pass
 
103
        else:
 
104
            setattr(self, name, value)
 
105
 
 
106
    def to_xml(self):
 
107
        s = '<AccessControlList>'
 
108
        for grant in self.grants:
 
109
            s += grant.to_xml()
 
110
        s += '</AccessControlList>'
 
111
        return s
 
112
        
 
113
class Grant:
 
114
 
 
115
    NameSpace = 'xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"'
 
116
 
 
117
    def __init__(self, permission=None, type=None, id=None,
 
118
                 display_name=None, uri=None, email_address=None):
 
119
        self.permission = permission
 
120
        self.id = id
 
121
        self.display_name = display_name
 
122
        self.uri = uri
 
123
        self.email_address = email_address
 
124
        self.type = type
 
125
 
 
126
    def startElement(self, name, attrs, connection):
 
127
        if name == 'Grantee':
 
128
            self.type = attrs['xsi:type']
 
129
        return None
 
130
 
 
131
    def endElement(self, name, value, connection):
 
132
        if name == 'ID':
 
133
            self.id = value
 
134
        elif name == 'DisplayName':
 
135
            self.display_name = value
 
136
        elif name == 'URI':
 
137
            self.uri = value
 
138
        elif name == 'EmailAddress':
 
139
            self.email_address = value
 
140
        elif name == 'Grantee':
 
141
            pass
 
142
        elif name == 'Permission':
 
143
            self.permission = value
 
144
        else:
 
145
            setattr(self, name, value)
 
146
 
 
147
    def to_xml(self):
 
148
        s = '<Grant>'
 
149
        s += '<Grantee %s xsi:type="%s">' % (self.NameSpace, self.type)
 
150
        if self.type == 'CanonicalUser':
 
151
            s += '<ID>%s</ID>' % self.id
 
152
            s += '<DisplayName>%s</DisplayName>' % self.display_name
 
153
        elif self.type == 'Group':
 
154
            s += '<URI>%s</URI>' % self.uri
 
155
        else:
 
156
            s += '<EmailAddress>%s</EmailAddress>' % self.email_address
 
157
        s += '</Grantee>'
 
158
        s += '<Permission>%s</Permission>' % self.permission
 
159
        s += '</Grant>'
 
160
        return s
 
161
        
 
162