~tribaal/txaws/xss-hardening

« back to all changes in this revision

Viewing changes to txaws/ec2/client.py

Merged 921421-completemultipart [r=oubiwann][f=921421]

This branch adds Complete Multipart method to s3 client.

Show diffs side-by-side

added added

removed removed

Lines of Context:
48
48
    def run_instances(self, image_id, min_count, max_count,
49
49
        security_groups=None, key_name=None, instance_type=None,
50
50
        user_data=None, availability_zone=None, kernel_id=None,
51
 
        ramdisk_id=None, subnet_id=None, security_group_ids=None):
 
51
        ramdisk_id=None):
52
52
        """Run new instances.
53
53
 
54
54
        TODO: blockDeviceMapping, monitoring, subnetId
57
57
                  "MaxCount": str(max_count)}
58
58
        if key_name is not None:
59
59
            params["KeyName"] = key_name
60
 
        if subnet_id is not None:
61
 
            params["SubnetId"] = subnet_id
62
 
            if security_group_ids is not None:
63
 
                for i, id in enumerate(security_group_ids):
64
 
                    params["SecurityGroupId.%d" % (i + 1)] = id
65
 
            else:
66
 
                msg = "You must specify the security_group_ids with the subnet_id"
67
 
                raise ValueError(msg)
68
 
        elif security_groups is not None:
 
60
        if security_groups is not None:
69
61
            for i, name in enumerate(security_groups):
70
62
                params["SecurityGroup.%d" % (i + 1)] = name
71
 
        else:
72
 
            msg = ("You must specify either the subnet_id and "
73
 
                   "security_group_ids or security_groups")
74
 
            raise ValueError(msg)
75
63
        if user_data is not None:
76
64
            params["UserData"] = b64encode(user_data)
77
65
        if instance_type is not None:
122
110
        d = query.submit()
123
111
        return d.addCallback(self.parser.describe_security_groups)
124
112
 
125
 
    def create_security_group(self, name, description, vpc_id=None):
 
113
    def create_security_group(self, name, description):
126
114
        """Create security group.
127
115
 
128
116
        @param name: Name of the new security group.
129
117
        @param description: Description of the new security group.
130
 
        @param vpc_id: ID of the VPC to which the security group will belong.
131
118
        @return: A C{Deferred} that will fire with a truth value for the
132
119
            success of the operation.
133
120
        """
134
121
        parameters = {"GroupName":  name, "GroupDescription": description}
135
 
        if vpc_id:
136
 
            parameters["VpcId"] = vpc_id
137
122
        query = self.query_factory(
138
123
            action="CreateSecurityGroup", creds=self.creds,
139
124
            endpoint=self.endpoint, other_params=parameters)
140
125
        d = query.submit()
141
 
        return d.addCallback(self.parser.create_security_group)
 
126
        return d.addCallback(self.parser.truth_return)
142
127
 
143
 
    def delete_security_group(self, name=None, id=None):
 
128
    def delete_security_group(self, name):
144
129
        """
145
 
        @param name: Name of the security group.
146
 
        @param id: Id of the security group.
 
130
        @param name: Name of the new security group.
147
131
        @return: A C{Deferred} that will fire with a truth value for the
148
132
            success of the operation.
149
133
        """
150
 
        if name:
151
 
            parameter = {"GroupName":  name}
152
 
        elif id:
153
 
            parameter = {"GroupId": id}
154
 
        else:
155
 
            raise ValueError("You must provide either the security group name or id")
 
134
        parameter = {"GroupName":  name}
156
135
        query = self.query_factory(
157
136
            action="DeleteSecurityGroup", creds=self.creds,
158
137
            endpoint=self.endpoint, other_params=parameter)
160
139
        return d.addCallback(self.parser.truth_return)
161
140
 
162
141
    def authorize_security_group(
163
 
        self, group_name=None, group_id=None, source_group_name="", source_group_owner_id="",
 
142
        self, group_name, source_group_name="", source_group_owner_id="",
164
143
        ip_protocol="", from_port="", to_port="", cidr_ip=""):
165
144
        """
166
145
        There are two ways to use C{authorize_security_group}:
171
150
 
172
151
        @param group_name: The group you will be modifying with a new
173
152
            authorization.
174
 
        @param group_id: The id of the group you will be modifying with
175
 
            a new authorization.
176
153
 
177
154
        Optionally, the following parameters:
178
155
        @param source_group_name: Name of security group to authorize access to
211
188
            msg = ("You must specify either both group parameters or "
212
189
                   "all the ip parameters.")
213
190
            raise ValueError(msg)
214
 
        if group_id:
215
 
            parameters["GroupId"] = group_id
216
 
        elif group_name:
217
 
            parameters["GroupName"] = group_name
218
 
        else:
219
 
            raise ValueError("You must specify either the group name of the group id.")
 
191
        parameters["GroupName"] = group_name
220
192
        query = self.query_factory(
221
193
            action="AuthorizeSecurityGroupIngress", creds=self.creds,
222
194
            endpoint=self.endpoint, other_params=parameters)
252
224
        return d
253
225
 
254
226
    def revoke_security_group(
255
 
        self, group_name=None, group_id=None, source_group_name="", source_group_owner_id="",
 
227
        self, group_name, source_group_name="", source_group_owner_id="",
256
228
        ip_protocol="", from_port="", to_port="", cidr_ip=""):
257
229
        """
258
230
        There are two ways to use C{revoke_security_group}:
301
273
            msg = ("You must specify either both group parameters or "
302
274
                   "all the ip parameters.")
303
275
            raise ValueError(msg)
304
 
        if group_id:
305
 
            parameters["GroupId"] = group_id
306
 
        elif group_name:
307
 
            parameters["GroupName"] = group_name
308
 
        else:
309
 
            raise ValueError("You must specify either the group name of the group id.")
 
276
        parameters["GroupName"] = group_name
310
277
        query = self.query_factory(
311
278
            action="RevokeSecurityGroupIngress", creds=self.creds,
312
279
            endpoint=self.endpoint, other_params=parameters)
580
547
              ipAddress, stateReason, architecture, rootDeviceName,
581
548
              blockDeviceMapping, instanceLifecycle, spotInstanceRequestId.
582
549
        """
583
 
        for group_data in instance_data.find("groupSet"):
584
 
            group_id = group_data.findtext("groupId")
585
 
            group_name = group_data.findtext("groupName")
586
 
            reservation.groups.append((group_id, group_name))
587
550
        instance_id = instance_data.findtext("instanceId")
588
551
        instance_state = instance_data.find(
589
552
            "instanceState").findtext("name")
636
599
        results = []
637
600
        # May be a more elegant way to do this:
638
601
        for reservation_data in root.find("reservationSet"):
 
602
            # Get the security group information.
 
603
            groups = []
 
604
            for group_data in reservation_data.find("groupSet"):
 
605
                group_id = group_data.findtext("groupId")
 
606
                groups.append(group_id)
639
607
            # Create a reservation object with the parsed data.
640
608
            reservation = model.Reservation(
641
609
                reservation_id=reservation_data.findtext("reservationId"),
642
 
                owner_id=reservation_data.findtext("ownerId"))
 
610
                owner_id=reservation_data.findtext("ownerId"),
 
611
                groups=groups)
643
612
            # Get the list of instances.
644
613
            instances = self.instances_set(
645
614
                reservation_data, reservation)
701
670
        root = XML(xml_bytes)
702
671
        result = []
703
672
        for group_info in root.findall("securityGroupInfo/item"):
704
 
            id = group_info.findtext("groupId")
705
673
            name = group_info.findtext("groupName")
706
674
            description = group_info.findtext("groupDescription")
707
675
            owner_id = group_info.findtext("ownerId")
741
709
                              for user_id, group_name in allowed_groups]
742
710
 
743
711
            security_group = model.SecurityGroup(
744
 
                id, name, description, owner_id=owner_id,
 
712
                name, description, owner_id=owner_id,
745
713
                groups=allowed_groups, ips=allowed_ips)
746
714
            result.append(security_group)
747
715
        return result
748
716
 
749
 
    def create_security_group(self, xml_bytes):
750
 
        root = XML(xml_bytes)
751
 
        return root.findtext("groupId")
752
 
 
753
717
    def truth_return(self, xml_bytes):
754
718
        """Parse the XML for a truth value.
755
719