Esempio n. 1
0
    def __init__(self, start, end, collectorName=None, timeout=None, association=None, changed=None):
        """
        Create a new part of relationships between two nodes.
        Sematics: "start is part of end"

        """
        Edge.__init__(self, start, end, collectorName=collectorName, timeout=timeout, association=association, changed=changed)
Esempio n. 2
0
    def __init__(self, chain, action, protocol, srcnet=None, destnet=None, srcports=None, destports=None, inInterface=None, outInterface=None, collectorName=None, timeout=None):
        Node.__init__(self, collectorName=collectorName, timeout=timeout)
        self.chain = chain
        self.action = action
        self.protocol = protocol
        self.srcnet = srcnet
        self.destnet = destnet
        self.srcports = srcports
        self.destports = destports

        if outInterface:
            Edge(self, outInterface, collectorName, timeout, association="outInterface", changed=self, name="outInterface")

        if inInterface:
            Edge(self, inInterface, collectorName, timeout, association="inInterface", changed=self, name="inInterface")
Esempio n. 3
0
    def __init__(self, id, location=None, collectorName=None, timeout=None):
        Node.__init__(self, collectorName=collectorName, timeout=timeout)
        self.__id = id
        self.__configNames = set()

        if location is not None:
            Edge(self, location)
Esempio n. 4
0
    def setNetwork(self, newNetwork, collectorName=None, timeout=None):
        """
        Set the network of this interface.

        :param newNetwork: New network
        :type newNetwork: insalata.model.Layer2Network.Layer2Network

        :param collectorName: Name of the collector module setting the network
        :type collectorName: str

        :param timeout: Timeout the collector uses
        :type timeout: int
        """
        if not newNetwork:
            return
        if newNetwork != self.getNetwork():
            for edge in self.getEdges():  #Delete old edges
                if isinstance(edge.getOther(self), Layer2Network):
                    edge.delete(association="network", changed=self)
            Edge(self,
                 newNetwork,
                 collectorName=collectorName,
                 timeout=timeout,
                 association="network",
                 changed=self)
            self.networkId = newNetwork.getID()

        else:
            for edge in [
                    e for e in self.getEdges()
                    if e.getOther(self) == newNetwork
            ]:
                edge.verify(collectorName, timeout)

        self.verify(collectorName, timeout)
Esempio n. 5
0
    def setHost(self, newHost, collectorName=None, timeout=None):
        """
        Set the host containing this interface.

        :param newHost: Host containing the interface
        :type newHost: insalata.model.Host.Host

        :param collectorName: Name of the collector module setting the host
        :type collectorName: str

        :param timeout: Timeout the collector uses
        :type timeout: int
        """
        if not newHost:
            return
        if newHost not in self.getAllNeighbors(Host):
            for edge in self.getEdges():
                if isinstance(edge.getOther(self), Host):
                    edge.delete(association="host", changed=self)
            Edge(self,
                 newHost,
                 collectorName=collectorName,
                 timeout=timeout,
                 association="host",
                 changed=self)
        else:
            for edge in [
                    e for e in self.getEdges() if e.getOther(self) == newHost
            ]:
                edge.verify(collectorName, timeout)
        self.verify(collectorName, timeout)
Esempio n. 6
0
    def setLocation(self, newLocation, collectorName=None, timeout=None):
        """
        Set the location this host is build on.

        :param location: New location a collector found
        :type location: insalata.model.Location.Location

        :param collectorName: Name of the collector module setting the location
        :type collectorName: str

        :param timeout: Timeout the collector uses
        :type timeout: int
        """
        if newLocation and newLocation != self.getLocation():
            for edge in self.getEdges():
                if isinstance(edge.getOther(self), Location):
                    edge.delete()
            Edge(self,
                 newLocation,
                 collectorName=collectorName,
                 timeout=timeout,
                 association="location",
                 changed=self)
        else:
            for edge in [
                    e for e in self.getEdges()
                    if e.getOther(self) == newLocation
            ]:
                edge.verify(collectorName, timeout)
        self.verify(collectorName, timeout)
Esempio n. 7
0
    def setLocation(self, newLocation, collectorName=None, timeout=None):
        """
        Set the location containing the layer 2 network.

        :param newLocation: The location object containing the network
        :type newLocation: insalata.model.Location.Location

        :param collectorName: Name of the collector module setting the location
        :type collectorName: str

        :param timeout: Timeout the collector uses
        :type timeout: int
        """
        if newLocation not in self.getAllNeighbors(Location):
            for edge in self.getEdges():
                if isinstance(edge.getOther(self), Location):
                    edge.delete(association="location", changed=self)
            Edge(self,
                 newLocation,
                 collectorName=collectorName,
                 timeout=timeout,
                 association="location",
                 changed=self)

        else:
            for edge in [
                    e for e in self.getEdges()
                    if e.getOther(self) == newLocation
            ]:
                edge.verify(collectorName, timeout)
        self.verify(collectorName, timeout)
Esempio n. 8
0
    def getOrCreateLayer3Address(self,
                                 address,
                                 collectorName,
                                 timeout,
                                 netmask=None,
                                 gateway=None):
        """
        Get the Layer3Address with the given address or create it if it does not exist.

        :param address: Address of this Layer3Address
        :type address: str

        :param collectorName: Name of the collector requesting this address
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param netmask: Netmask of this address
        :type netmask: str

        :param gateway: Gateway of this addess
        :type gateway: str
        """
        self.__locks[Layer3Address].acquire()
        addresses = [
            a for a in self.getAllNeighbors(Layer3Address)
            if a.getID() == address
        ]

        if len(addresses) > 0:
            addressEl = list(addresses)[0]
            addressEl.setGateway(gateway, collectorName, timeout)
            addressEl.setNetmask(netmask, collectorName, timeout)
            addressEl.verify(collectorName, timeout)
        else:
            addressEl = Layer3Address(address,
                                      netmask=netmask,
                                      gateway=gateway,
                                      collectorName=collectorName,
                                      timeout=timeout)
            Edge(self, addressEl)

            addressEl.getOnChangeEvent().add(self.objectChanged)
            addressEl.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {
                "address": address,
                "gateway": gateway,
                "netmask": netmask
            }
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Layer3Address",
                "values": valuesDict
            })

        self.__locks[Layer3Address].release()
        return addressEl
Esempio n. 9
0
    def getOrCreateService(self, port, protocol, collectorName, timeout, type,
                           address):
        """
        Get the Service with the given port on the given address or create it if it does not exist.

        :param port: Port of this service
        :type port: int

        :param protocol: Protocol this service is using
        :type protocol: str

        :param collectorName: Name of the collector requesting this service
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param type: Type/Name of the service
        :type netmask: str

        :param address: Address this service is deployed on
        :type gateway: insalata.model.Layer3Address.Layer3Address
        """
        self.__locks[Service].acquire()
        services = [
            s for s in address.getAllNeighbors(Service)
            if s.getPort() == port and s.getProtocol() == protocol
        ]

        if len(services) > 0:
            service = list(services)[0]
            service.setType(type, collectorName, timeout)
            service.verify(collectorName, timeout)
        else:
            service = Service(port,
                              protocol,
                              type,
                              collectorName=collectorName,
                              timeout=timeout,
                              address=address)
            Edge(self, service)

            service.getOnChangeEvent().add(self.objectChanged)
            service.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {
                "port": port,
                "protocol": protocol,
                "type": type,
                "address": address.getID()
            }
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Service",
                "values": valuesDict
            })

        self.__locks[Service].release()
        return service
Esempio n. 10
0
    def getOrCreateHost(self,
                        id,
                        collectorName,
                        timeout,
                        location=None,
                        template=None):
        """
        Get the host with the given id or create a new one.

        :param id: Identifier of the host
        :type id: str

        :param collectorName: Name of the collector requesting this host
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param location: Location containing this host
        :type location: insalata.model.Location.Location

        :param template: Template the host is build on
        :type template: str
        """
        self.__locks[Host].acquire()
        hosts = [
            h for h in self.getAllNeighbors(Host) if h.getGlobalID() == id
        ]

        if len(hosts) > 0:
            host = list(hosts)[0]
            host.setLocation(location, collectorName, timeout)
            host.setTemplate(template, collectorName, timeout)
            host.verify(collectorName, timeout)
        else:
            host = Host(id,
                        collectorName=collectorName,
                        timeout=timeout,
                        location=location,
                        template=template)
            Edge(self, host)

            host.getOnChangeEvent().add(partial(self.objectChanged))
            host.getOnDeleteEvent().add(partial(self.objectDeleted))
            valuesDict = {"id": id}
            if location:
                valuesDict["location"] = location.getID()
            if template:
                valuesDict["template"] = template.getID()
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Host",
                "values": valuesDict
            })

        self.__locks[Host].release()
        return host
Esempio n. 11
0
    def getOrCreateFirewallRaw(self,
                               collectorName,
                               timeout,
                               host,
                               firewall,
                               data=None):
        """
        Get the rule with the given parametes on the host if existing or create a new one.

        :param collectorName: Name of the collector requesting this address
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param host: Host that shall contain the rule
        :type host: insalata.model.Host.Host

        :param chain: The name of the chain this rule belongs to.
        :type chain: str

        :param firewall: The productname of the firewall, e.g. "iptables"
        :type firewall: str

        :param data: Raw firewall data of the given firewall type
        :type data: str
        """
        self.__locks[FirewallRaw].acquire()
        raws = [
            r for r in host.getAllNeighbors(FirewallRaw)
            if r.getFirewall() == firewall
        ]

        if len(raws) == 0:
            raw = FirewallRaw(firewall, data, collectorName, timeout)
            Edge(self, raw)

            raw.getOnChangeEvent().add(self.objectChanged)
            raw.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {
                "host": host.getID(),
                "firewall": firewall,
                "data": data
            }
            self.getObjectNewEvent().trigger(self, {
                "objectType": "FirewallRaw",
                "values": valuesDict
            })

        else:
            raw = raws[0]
            raw.verify(collectorName, timeout)

        self.__locks[FirewallRaw].release()
        return raw
Esempio n. 12
0
    def __init__(self,
                 id,
                 allL2Networks=set(),
                 allL3Networks=set(),
                 locations=set(),
                 allHosts=set()):
        Node.__init__(self)
        self.id = id
        for host in allHosts:
            Edge(self, host)

        for network in allL2Networks:
            Edge(self, network)

        for network in allL3Networks:
            Edge(self, network)

        for location in locations:
            Edge(self, location)

        self.__locks = {
            Host: threading.Lock(),
            DhcpService: threading.Lock(),
            Disk: threading.Lock(),
            DnsService: threading.Lock(),
            Interface: threading.Lock(),
            Layer2Network: threading.Lock(),
            Layer3Address: threading.Lock(),
            Layer3Network: threading.Lock(),
            Location: threading.Lock(),
            Route: threading.Lock(),
            Service: threading.Lock(),
            FirewallRule: threading.Lock(),
            FirewallRaw: threading.Lock()
        }

        self.__objectChangedEvent = Event()
        self.__objectNewEvent = Event()
        self.__objectDeletedEvent = Event()

        self.__out = open("output.txt", "w")
Esempio n. 13
0
    def getOrCreateLayer2Network(self,
                                 id,
                                 collectorName,
                                 timeout,
                                 location=None):
        """
        Get the Layer2Network with the given id or create a new one.

        :param id: Identifier of the network
        :type id: str

        :param collectorName: Name of the collector requesting this network
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param location: The locatin containing this network
        :type location: insalata.model.Location.Location
        """
        self.__locks[Layer2Network].acquire()
        networks = [
            n for n in self.getAllNeighbors(Layer2Network)
            if n.getGlobalID() == id
        ]

        if len(networks) > 0:
            network = list(networks)[0]
            network.setLocation(location, collectorName, timeout)
            network.verify(collectorName, timeout)
        else:
            network = Layer2Network(id,
                                    collectorName=collectorName,
                                    timeout=timeout,
                                    location=location)
            Edge(self, network)

            network.getOnChangeEvent().add(partial(self.objectChanged))
            network.getOnDeleteEvent().add(partial(self.objectDeleted))
            valuesDict = {"id": id}
            if location:
                valuesDict["location"] = location.getID()
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Layer2Network",
                "values": valuesDict
            })

        self.__locks[Layer2Network].release()
        return network
Esempio n. 14
0
    def getOrCreateLayer3Network(self, id, collectorName, timeout, address,
                                 netmask):
        """
        Get the Layer3Network with the given identifier or create a new one if it is not existing.

        :param id: Identifier of the network
        :type id: str

        :param collectorName: Name of the collector requesting this network
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param address: Address of this network
        :type address: str

        :param netmask: Netmask of the network
        :type netmask: str
        """
        self.__locks[Layer3Network].acquire()
        networks = [
            n for n in self.getAllNeighbors(Layer3Network)
            if n.getGlobalID() == id
        ]

        if len(networks) > 0:
            network = list(networks)[0]
            network.setAddress(address, collectorName, timeout)
            network.setNetmask(netmask, collectorName, timeout)
            network.verify(collectorName, timeout)
        else:
            network = Layer3Network(id,
                                    address,
                                    netmask,
                                    collectorName=collectorName,
                                    timeout=timeout)
            Edge(self, network)

            network.getOnChangeEvent().add(self.objectChanged)
            network.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {"id": id, "address": address, "netmask": netmask}
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Layer3Network",
                "values": valuesDict
            })

        self.__locks[Layer3Network].release()
        return network
Esempio n. 15
0
    def getOrCreateDisk(self, name, collectorName, timeout, host, size=None):
        """
        Get the host disk with the given name if existing or create a new one.

        :param name: Name of the disk on the host
        :type name: str

        :param collectorName: Name of the collector requesting this address
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param host: Host that contains the disk
        :type host: insalata.model.Host.Host

        :param size: Size of the disk
        :type size: int
        """
        self.__locks[Disk].acquire()
        disk = [
            d for d in host.getAllNeighbors(Disk) if d.getGlobalID() == name
        ]

        if len(disk) > 0:
            disk = list(disk)[0]
            disk.setSize(size, collectorName, timeout)
            disk.verify(collectorName, timeout)
        else:
            disk = Disk(name,
                        size=None,
                        collectorName=collectorName,
                        timeout=timeout)
            Edge(self, disk)

            disk.getOnChangeEvent().add(self.objectChanged)
            disk.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {"name": name, "host": host.getID(), "size": size}
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Disk",
                "values": valuesDict
            })

        self.__locks[Disk].release()
        return disk
Esempio n. 16
0
 def setInterface(self, newInterface, collectorName=None, timeout=None):
     if not newInterface:
         return
     edges = [
         e for e in self.getEdges()
         if isinstance(e.getOther(self), Interface)
     ]
     if len(edges) > 0 and (edges[0].getOther(self) == newInterface):
         edges[0].verify(collectorName, timeout)
     else:
         for edge in edges:
             edge.delete(association="interface", changed=self)
         Edge(self,
              newInterface,
              collectorName=collectorName,
              timeout=timeout,
              association="interface",
              changed=self)
Esempio n. 17
0
    def getOrCreateInterface(self, mac, collectorName, timeout, network=None):
        """
        Get the interface with the given MAC address or create a new one if it is not existing

        :param mac: MAC address of the interface
        :type mac: str

        :param collectorName: Name of the collector requesting this interface
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param network: Network this interface is conntected to
        :type network: insalata.model.Layer2Network.Layer2Network
        """
        self.__locks[Interface].acquire()
        interfaces = [
            i for i in self.getAllNeighbors(Interface) if i.getMAC() == mac
        ]

        if len(interfaces) > 0:
            interface = list(interfaces)[0]
            interface.setNetwork(network, collectorName, timeout)
            interface.verify(collectorName, timeout)
        else:
            interface = Interface(mac,
                                  collectorName=collectorName,
                                  timeout=timeout,
                                  network=network)
            Edge(self, interface)

            interface.getOnChangeEvent().add(self.objectChanged)
            interface.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {"mac": mac}
            if network:
                valuesDict["network"] = network.getID()
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Interface",
                "values": valuesDict
            })

        self.__locks[Interface].release()
        return interface
Esempio n. 18
0
    def __init__(self, mac, network=None, collectorName=None, timeout=None):
        """
        Method creating new interface opject.

        :param mac: MAC-address of this interface. (Used as identifier)
        :type mac: str

        :param network: 'Network' object this interface is connected to
        :type network: insalata.model.Layer2Network
        """
        Node.__init__(self, collectorName=collectorName, timeout=timeout)
        self.__mac = mac
        self.networkId = None
        self.rate = None  # In kbps
        self.mtu = None

        if network:
            Edge(self, network, collectorName=collectorName, timeout=timeout)
            self.networkId = network.getID()
Esempio n. 19
0
    def __init__(self,
                 id,
                 location=None,
                 template=None,
                 collectorName=None,
                 timeout=None):
        """
        Create a new host object.

        Keyword arguments:
            id -- Name of this host.
            location -- Location of this host. In Xen environment: Xen Server.
            template -- Template used to clone this host.
            collectorName -- Scanner that verifies the new host.
            timeout -- Timeout to delete the new host without new verify
        """
        Node.__init__(self, collectorName=collectorName, timeout=timeout)

        self.__id = id
        self.cpus = None
        self.cpuSpeed = None
        self.memoryMax = None
        self.memoryMin = None
        self.powerState = None

        self.__configNames = set()
        self.__nameApplied = not (self.__id == template)

        if location:
            Edge(self,
                 location,
                 collectorName=collectorName,
                 timeout=timeout,
                 association="location",
                 changed=self)

        if template:
            PartOfEdge(template,
                       self,
                       collectorName=collectorName,
                       timeout=timeout,
                       association="template",
                       changed=self)
Esempio n. 20
0
    def setOutInterface(self, newInterface, collectorName=None, timeout=None):
        """
        Set the outInterface of this firewall rule.

        :param newInterface: New Interface
        :type interface: insalata.model.Interface.Interface

        :param collectorName: Name of the collector module setting the interface
        :type collectorName: str

        :param timeout: Timeout the collector uses
        :type timeout: int
        """
        if self.getOutInterface() != newInterface:
            for edge in [e for e in self.getEdges() if e.getName() and e.getName() == "outInterface"]:
                edge.delete(association = "outInterface", changed = self)
            Edge(self, newInterface, collectorName, timeout, association="outInterface", changed=self, name="outInterface")
        else:
            for edge in [e for e in self.getEdges() if e.getName()=="outInterface"]:
                edge.verify(collectorName, timeout)
        self.verify(collectorName, timeout)
Esempio n. 21
0
    def getOrCreateDnsService(self, collectorName, timeout, address):
        """
        Get the DnsService on the given address or create a new one if necessary.

        :param collectorName: Name of the collector requesting this service
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param address: Address this service is deployed on
        :type gateway: insalata.model.Layer3Address.Layer3Address
        """
        self.__locks[DnsService].acquire()
        services = [
            s for s in address.getAllNeighbors(DnsService)
            if s.getType() == "dns"
        ]

        if len(services) > 0:
            service = list(services)[0]
            service.setAddress(address, collectorName, timeout)
            service.verify(collectorName, timeout)
        else:
            service = DnsService(collectorName=collectorName,
                                 timeout=timeout,
                                 address=address)
            Edge(self, service)

            service.getOnChangeEvent().add(self.objectChanged)
            service.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {"address": address.getID()}
            self.getObjectNewEvent().trigger(self, {
                "objectType": "DnsService",
                "values": valuesDict
            })

        self.__locks[DnsService].release()
        return service
Esempio n. 22
0
    def getOrCreateLocation(self, id, collectorName, timeout):
        """
        Get the location with the given name if existing or create a new one.

        :param id: Name of the location
        :type id: str

        :param collectorName: Name of the collector requesting this address
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int
        """
        self.__locks[Location].acquire()
        location = [
            l for l in self.getAllNeighbors(Location)
            if l.getGlobalID().lower() == id.lower()
        ]

        if len(location) > 0:
            location = list(location)[0]
            location.verify(collectorName, timeout)
        else:
            location = Location(id,
                                collectorName=collectorName,
                                timeout=timeout)
            Edge(self, location)

            location.getOnChangeEvent().add(self.objectChanged)
            location.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {"id": id}
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Location",
                "values": valuesDict
            })

        self.__locks[Location].release()
        return location
Esempio n. 23
0
    def setNetwork(self, newNetwork, collectorName=None, timeout=None):
        """
        Set the new Layer3Network of this address.
        If other networks are connected, the edges will be deleted.
        If the set network is already the defined one, the edge will be verified by the scanner.

        :param newNetwork: New network of this address
        :type newNetwork: :class: `Layer3Network`

        :param collectorName: Name of the scanner that sets the network
        :type collectorName: str

        :param timeout: Timeout the scanner uses
        :type timeout: int
        """
        if not newNetwork:
            return
        if newNetwork != self.getNetwork():
            for edge in self.getEdges():  #Delete old edges
                if isinstance(edge.getOther(self), Layer3Network):
                    edge.delete(association="network", changed=self)
            Edge(self,
                 newNetwork,
                 collectorName=collectorName,
                 timeout=timeout,
                 association="network",
                 changed=self)
            self.networkId = newNetwork.getID()

        else:
            for edge in [
                    e for e in self.getEdges()
                    if e.getOther(self) == newNetwork
            ]:
                edge.verify(collectorName, timeout)
        self.verify(collectorName, timeout)
Esempio n. 24
0
    def getOrCreateRoute(self,
                         collectorName,
                         timeout,
                         host,
                         dest,
                         genmask,
                         gateway,
                         interface=None):
        """
        Get the route with the given parametes on the host if existing or create a new one.

        :param collectorName: Name of the collector requesting this address
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param host: Host that shall contain the route
        :type host: insalata.model.Host.Host

        :param dest: Destination address of this route
        :type dest: str

        :param genmask: Genmask used by this route
        :type genmask: str

        :param gateway: Gateway used by this route
        :type gateway: str

        :param interface: Interface this route uses for forwarding
        :type interface: insalata.model.Interface.Interface
        """
        self.__locks[Route].acquire()
        routes = [
            r for r in host.getAllNeighbors(Route) if r.getGateway() == gateway
            and r.getDestination() == dest and r.getGenmask() == genmask
        ]
        if len(routes) > 0:
            route = routes[0]
            route.setInterface(interface, collectorName, timeout)
            route.verify(collectorName, timeout)
        else:
            route = Route(dest,
                          genmask,
                          gateway,
                          interface=interface,
                          collectorName=collectorName,
                          timeout=timeout)
            Edge(self, route)

            route.getOnChangeEvent().add(self.objectChanged)
            route.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {
                "host": host.getID(),
                "dest": dest,
                "genmask": genmask,
                "gateway": gateway
            }
            if interface:
                valuesDict["interface"] = interface.getID()
            self.getObjectNewEvent().trigger(self, {
                "objectType": "Route",
                "values": valuesDict
            })

        self.__locks[Route].release()
        return route
Esempio n. 25
0
    def getOrCreateFirewallRule(self,
                                collectorName,
                                timeout,
                                host,
                                chain,
                                action,
                                protocol,
                                srcnet=None,
                                destnet=None,
                                srcports=None,
                                destports=None,
                                inInterface=None,
                                outInterface=None):
        """
        Get the rule with the given parametes on the host if existing or create a new one.

        :param collectorName: Name of the collector requesting this address
        :type collectorName: str

        :param timeout: Timeout of the collector
        :type timeout: int

        :param host: Host that shall contain the rule
        :type host: insalata.model.Host.Host

        :param chain: The name of the chain this rule belongs to.
        :type chain: str

        :param action: The action (ACCEPT or DROP) for this rule.
        :type action: str

        :param protocol: Protocol for this rule
        :type protocol: str

        :param srcnet: Source network of this rule
        :type srcnet: str

        :param destnet: Source network of this rule
        :type destnet: str

        :param srcports: Source ports this rule uses
        :type srcports: list

        :param destports: Destination ports this rule uses
        :type destports: list

        :param inInterface: Interface the packet came from
        :type inInterface: insalata.model.Interface.Interface

        :param outInterface: Interface the packet leafes from
        :type outInterface: insalata.model.Interface.Interface
        """
        self.__locks[FirewallRule].acquire()
        rules = [
            r for r in host.getAllNeighbors(FirewallRule)
            if r.getChain() == chain and r.getAction() == action
            and r.getProtocol() == protocol and r.getSrcNet() == srcnet
            and r.getDestNet() == destnet and r.getSrcPorts() == srcports and
            r.getDestPorts() == destports and r.getInInterface() == inInterface
            and r.getOutInterface() == outInterface
        ]
        if len(rules) > 0:
            rule = rules[0]
            rule.verify(collectorName, timeout)
        else:
            rule = FirewallRule(chain, action, protocol, srcnet, destnet,
                                srcports, destports, inInterface, outInterface,
                                collectorName, timeout)
            Edge(self, rule)

            rule.getOnChangeEvent().add(self.objectChanged)
            rule.getOnDeleteEvent().add(self.objectDeleted)
            valuesDict = {
                "host": host.getID(),
                "chain": chain,
                "action": action,
                "protocol": protocol
            }
            if srcnet:
                valuesDict["srcNetwork"] = srcnet
            if destnet:
                valuesDict["destNetwork"] = destnet
            if srcports and len(srcports) > 0:
                valuesDict["srcPorts"] = str(
                    srcports[0]) if len(srcports) == 1 else str(
                        srcports[0]) + ":" + str(srcports[-1])
            if srcports and len(destports) > 0:
                valuesDict["destPorts"] = str(
                    destports[0]) if len(destports) == 1 else str(
                        destports[0]) + ":" + str(destports[-1])
            if inInterface:
                valuesDict["inInterface"] = inInterface.getID()
            if outInterface:
                valuesDict["outInterface"] = outInterface.getID()
            self.getObjectNewEvent().trigger(self, {
                "objectType": "FirewallRule",
                "values": valuesDict
            })

        self.__locks[FirewallRule].release()
        return rule