~ubuntu-branches/ubuntu/natty/euca2ools/natty-201106142030

« back to all changes in this revision

Viewing changes to .pc/euca-revoke-exit-on-usage.patch/bin/euca-revoke

  • Committer: Scott Moser
  • Date: 2010-11-17 21:23:16 UTC
  • Revision ID: smoser@ubuntu.com-20101117212316-is94e55u3jv46y9l
move to quilt 3.0 source format, re-apply all relevant delta

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
#!/usr/bin/python
 
2
# -*- coding: utf-8 -*-
 
3
 
 
4
# Software License Agreement (BSD License)
 
5
#
 
6
# Copyright (c) 2009, Eucalyptus Systems, Inc.
 
7
# All rights reserved.
 
8
#
 
9
# Redistribution and use of this software in source and binary forms, with or
 
10
# without modification, are permitted provided that the following conditions
 
11
# are met:
 
12
#
 
13
#   Redistributions of source code must retain the above
 
14
#   copyright notice, this list of conditions and the
 
15
#   following disclaimer.
 
16
#
 
17
#   Redistributions in binary form must reproduce the above
 
18
#   copyright notice, this list of conditions and the
 
19
#   following disclaimer in the documentation and/or other
 
20
#   materials provided with the distribution.
 
21
#
 
22
# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 
23
# AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 
24
# IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 
25
# ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 
26
# LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 
27
# CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 
28
# SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 
29
# INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 
30
# CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 
31
# ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 
32
# POSSIBILITY OF SUCH DAMAGE.
 
33
#
 
34
# Author: Neil Soman neil@eucalyptus.com
 
35
 
 
36
import getopt
 
37
import sys
 
38
import os
 
39
from euca2ools import Euca2ool, AddressValidationError, \
 
40
    ProtocolValidationError, Util, ConnectionFailed
 
41
 
 
42
usage_string = \
 
43
    """
 
44
Revoke a rule for a security group.
 
45
 
 
46
euca-revoke [-P | --protocol protocol] [-p | --port-range port_range] 
 
47
[-t | --icmp-type-code type:code] [-o | --source-group source_group]
 
48
[-u | --source-group-user source_group_user] [-s | --source-subnet source_subnet]
 
49
[-h, --help] [--version] [--debug] group_name 
 
50
 
 
51
REQUIRED PARAMETERS
 
52
 
 
53
group_name                      Name of the group to add the rule to.
 
54
 
 
55
OPTIONAL PARAMETERS
 
56
 
 
57
-P, --protocol                  Protocol ("tcp" "udp" or "icmp").
 
58
 
 
59
-p, --port-range                Range of ports for the rule (specified as "from-to").
 
60
 
 
61
-t, --icmp-type-code            ICMP type and code specified as "type:code"     
 
62
 
 
63
-o, --source-group              Group from which traffic is authorized by the rule.
 
64
 
 
65
-u, --source-group-user         User ID for the source group.
 
66
 
 
67
-s, --source-subnet             The source subnet for the rule.
 
68
 
 
69
"""
 
70
 
 
71
 
 
72
def usage(status=1):
 
73
    print usage_string
 
74
    Util().usage(compat=True)
 
75
 
 
76
 
 
77
def version():
 
78
    print Util().version()
 
79
    sys.exit(status)
 
80
 
 
81
 
 
82
def main():
 
83
    euca = None
 
84
    try:
 
85
        euca = Euca2ool('P:p:o:u:s:t:', [
 
86
            'protocol=',
 
87
            'port-range=',
 
88
            'source-group=',
 
89
            'source-group-user=',
 
90
            'source-subnet=',
 
91
            'icmp-type-code=',
 
92
            ], compat=True)
 
93
    except Exception, e:
 
94
        print e
 
95
        usage()
 
96
 
 
97
    group_name = None
 
98
    protocol = 'tcp'
 
99
    from_port = None
 
100
    to_port = None
 
101
    source_group_name = None
 
102
    source_group_owner_id = None
 
103
    cidr_ip = None
 
104
 
 
105
    for (name, value) in euca.opts:
 
106
        if name in ('-P', '--protocol'):
 
107
            protocol = value
 
108
        elif name in ('-p', '--port-range'):
 
109
            ports = value.split('-')
 
110
            if len(ports) > 1:
 
111
                from_port = int(ports[0])
 
112
                to_port = int(ports[1])
 
113
            else:
 
114
                from_port = to_port = int(ports[0])
 
115
        elif name in ('-o', '--source-group'):
 
116
            source_group_name = value
 
117
            protocol = None
 
118
        elif name in ('-u', '--source-group-user'):
 
119
            source_group_owner_id = value
 
120
        elif name in ('-s', '--source-subnet'):
 
121
            cidr_ip = value
 
122
        elif name in ('-t', '--icmp-type-code'):
 
123
            code_parts = value.split(':')
 
124
            if len(code_parts) > 1:
 
125
                try:
 
126
                    from_port = int(code_parts[0])
 
127
                    to_port = int(code_parts[1])
 
128
                except ValueError:
 
129
                    print 'port must be an integer.'
 
130
                    sys.exit(1)
 
131
        elif name in ('-h', '--help'):
 
132
            usage(0)
 
133
        elif name == '--version':
 
134
            version()
 
135
 
 
136
    if source_group_name:
 
137
        from_port = None
 
138
        to_port = None
 
139
        protocol = None
 
140
 
 
141
    for arg in euca.args:
 
142
        group_name = arg
 
143
        break
 
144
 
 
145
    if group_name:
 
146
        if cidr_ip:
 
147
            try:
 
148
                euca.validate_address(cidr_ip)
 
149
            except AddressValidationError:
 
150
                print 'Invalid address', cidr_ip
 
151
                sys.exit(1)
 
152
        if protocol:
 
153
            try:
 
154
                euca.validate_protocol(protocol)
 
155
            except ProtocolValidationError:
 
156
                print 'Invalid protocol', protocol
 
157
                sys.exit(1)
 
158
 
 
159
        try:
 
160
            euca_conn = euca.make_connection()
 
161
        except ConnectionFailed, e:
 
162
            print e.message
 
163
            sys.exit(1)
 
164
        try:
 
165
            return_code = euca_conn.revoke_security_group(
 
166
                group_name=group_name,
 
167
                src_security_group_name=source_group_name,
 
168
                src_security_group_owner_id=source_group_owner_id,
 
169
                ip_protocol=protocol,
 
170
                from_port=from_port,
 
171
                to_port=to_port,
 
172
                cidr_ip=cidr_ip,
 
173
                )
 
174
        except Exception, ex:
 
175
            euca.display_error_and_exit('%s' % ex)
 
176
 
 
177
        if return_code:
 
178
            print 'GROUP\t%s' % group_name
 
179
            permission_string = 'PERMISSION\t%s\tALLOWS' % group_name
 
180
            if protocol:
 
181
                permission_string += '\t%s' % protocol
 
182
            if from_port:
 
183
                permission_string += '\t%s' % from_port
 
184
            if to_port:
 
185
                permission_string += '\t%s' % to_port
 
186
            if source_group_owner_id:
 
187
                permission_string += '\tUSER\t%s' \
 
188
                    % source_group_owner_id
 
189
            if source_group_name:
 
190
                permission_string += '\tGRPNAME\t%s' % source_group_name
 
191
            if cidr_ip:
 
192
                permission_string += '\tFROM\tCIDR\t%s' % cidr_ip
 
193
            print permission_string
 
194
    else:
 
195
 
 
196
        print 'group_name must be specified'
 
197
        usage()
 
198
 
 
199
 
 
200
if __name__ == '__main__':
 
201
    main()
 
202