~alai/charms/trusty/contrail-configuration-inc-timeout/trunk

« back to all changes in this revision

Viewing changes to hooks/charmhelpers/contrib/network/ufw.py

  • Committer: Robert Ayres
  • Date: 2015-08-28 10:58:17 UTC
  • Revision ID: robert.ayres@canonical.com-20150828105817-c8ar12o1x0o35uxc
Sync charm helpers

Show diffs side-by-side

added added

removed removed

Lines of Context:
180
180
        return True
181
181
 
182
182
 
183
 
def modify_access(src, dst='any', port=None, proto=None, action='allow'):
 
183
def default_policy(policy='deny', direction='incoming'):
 
184
    """
 
185
    Changes the default policy for traffic `direction`
 
186
 
 
187
    :param policy: allow, deny or reject
 
188
    :param direction: traffic direction, possible values: incoming, outgoing,
 
189
                      routed
 
190
    """
 
191
    if policy not in ['allow', 'deny', 'reject']:
 
192
        raise UFWError(('Unknown policy %s, valid values: '
 
193
                        'allow, deny, reject') % policy)
 
194
 
 
195
    if direction not in ['incoming', 'outgoing', 'routed']:
 
196
        raise UFWError(('Unknown direction %s, valid values: '
 
197
                        'incoming, outgoing, routed') % direction)
 
198
 
 
199
    output = subprocess.check_output(['ufw', 'default', policy, direction],
 
200
                                     universal_newlines=True,
 
201
                                     env={'LANG': 'en_US',
 
202
                                          'PATH': os.environ['PATH']})
 
203
    hookenv.log(output, level='DEBUG')
 
204
 
 
205
    m = re.findall("^Default %s policy changed to '%s'\n" % (direction,
 
206
                                                             policy),
 
207
                   output, re.M)
 
208
    if len(m) == 0:
 
209
        hookenv.log("ufw couldn't change the default policy to %s for %s"
 
210
                    % (policy, direction), level='WARN')
 
211
        return False
 
212
    else:
 
213
        hookenv.log("ufw default policy for %s changed to %s"
 
214
                    % (direction, policy), level='INFO')
 
215
        return True
 
216
 
 
217
 
 
218
def modify_access(src, dst='any', port=None, proto=None, action='allow',
 
219
                  index=None):
184
220
    """
185
221
    Grant access to an address or subnet
186
222
 
192
228
    :param port: destiny port
193
229
    :param proto: protocol (tcp or udp)
194
230
    :param action: `allow` or `delete`
 
231
    :param index: if different from None the rule is inserted at the given
 
232
                  `index`.
195
233
    """
196
234
    if not is_enabled():
197
235
        hookenv.log('ufw is disabled, skipping modify_access()', level='WARN')
199
237
 
200
238
    if action == 'delete':
201
239
        cmd = ['ufw', 'delete', 'allow']
 
240
    elif index is not None:
 
241
        cmd = ['ufw', 'insert', str(index), action]
202
242
    else:
203
243
        cmd = ['ufw', action]
204
244
 
227
267
                    level='ERROR')
228
268
 
229
269
 
230
 
def grant_access(src, dst='any', port=None, proto=None):
 
270
def grant_access(src, dst='any', port=None, proto=None, index=None):
231
271
    """
232
272
    Grant access to an address or subnet
233
273
 
238
278
                field has to be set.
239
279
    :param port: destiny port
240
280
    :param proto: protocol (tcp or udp)
 
281
    :param index: if different from None the rule is inserted at the given
 
282
                  `index`.
241
283
    """
242
 
    return modify_access(src, dst=dst, port=port, proto=proto, action='allow')
 
284
    return modify_access(src, dst=dst, port=port, proto=proto, action='allow',
 
285
                         index=index)
243
286
 
244
287
 
245
288
def revoke_access(src, dst='any', port=None, proto=None):