1
|
|
|
# Licensed to the StackStorm, Inc ('StackStorm') under one or more |
2
|
|
|
# contributor license agreements. See the NOTICE file distributed with |
3
|
|
|
# this work for additional information regarding copyright ownership. |
4
|
|
|
# The ASF licenses this file to You under the Apache License, Version 2.0 |
5
|
|
|
# (the "License"); you may not use this file except in compliance with |
6
|
|
|
# the License. You may obtain a copy of the License at |
7
|
|
|
# |
8
|
|
|
# http://www.apache.org/licenses/LICENSE-2.0 |
9
|
|
|
# |
10
|
|
|
# Unless required by applicable law or agreed to in writing, software |
11
|
|
|
# distributed under the License is distributed on an "AS IS" BASIS, |
12
|
|
|
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13
|
|
|
# See the License for the specific language governing permissions and |
14
|
|
|
# limitations under the License. |
15
|
|
|
|
16
|
|
|
import ipaddress |
17
|
|
|
|
18
|
|
|
from st2actions.runners.pythonrunner import Action |
19
|
|
|
|
20
|
|
|
|
21
|
|
|
class IsValidIpAction(Action): |
22
|
|
|
def run(self, ip_address, no_loopback=False, only_v4=False, only_v6=False): |
23
|
|
|
""" |
24
|
|
|
Is this a valid IP address? |
25
|
|
|
|
26
|
|
|
Args: |
27
|
|
|
ip_address: The IP address to validate. |
28
|
|
|
no_loopback: Raise an exception for Loopback addresses. |
29
|
|
|
only_v4: Raise an exception for IPv6 addresses. |
30
|
|
|
only_v6: Raise an exception for IPv4 addresses. |
31
|
|
|
|
32
|
|
|
Raises: |
33
|
|
|
ValueError: On invalid IP, loopback or when requesting only v4/v6 |
34
|
|
|
be considered valid. |
35
|
|
|
|
36
|
|
|
Returns: |
37
|
|
|
dict: With extra information about the IP address. |
38
|
|
|
""" |
39
|
|
|
|
40
|
|
|
# As ipaddress is a backport from Python 3.3+ it errors if the |
41
|
|
|
# ip address is a string and not unicode. |
42
|
|
|
ip_obj = ipaddress.ip_address(unicode(ip_address)) |
43
|
|
|
|
44
|
|
|
results = {'version': ip_obj.version, |
45
|
|
|
'is_private': ip_obj.is_private, |
46
|
|
|
'is_link_local': ip_obj.is_link_local, |
47
|
|
|
'is_unspecified': ip_obj.is_unspecified, |
48
|
|
|
'reverse_pointer': ip_obj.reverse_pointer, |
49
|
|
|
'is_multicast': ip_obj.is_multicast, |
50
|
|
|
'is_reserved': ip_obj.is_reserved, |
51
|
|
|
'is_loopback': ip_obj.is_loopback} |
52
|
|
|
|
53
|
|
|
if only_v6 and ip_obj.version == 4: |
54
|
|
|
raise ValueError("Valid IPv4 address, but IPv6 is required.") |
55
|
|
|
elif only_v4 and ip_obj.version == 6: |
56
|
|
|
raise ValueError("Valid IPv6 address, but IPv4 is required.") |
57
|
|
|
|
58
|
|
|
if no_loopback and ip_obj.is_loopback: |
59
|
|
|
raise ValueError("Address is a IPv{} loopback address".format( |
60
|
|
|
ip_obj.version)) |
61
|
|
|
|
62
|
|
|
return results |
63
|
|
|
|