# -*- coding: utf-8 -*-## License: AGPLv3# This file is part of eduMFA. eduMFA is a fork of privacyIDEA which was forked from LinOTP.# Copyright (c) 2024 eduMFA Project-Team# Previous authors by privacyIDEA project:## 2015 Cornelius Kölbel <cornelius@privacyidea.org>## This code is free software; you can redistribute it and/or# modify it under the terms of the GNU AFFERO GENERAL PUBLIC LICENSE# License as published by the Free Software Foundation; either# version 3 of the License, or any later version.## This code is distributed in the hope that it will be useful,# but WITHOUT ANY WARRANTY; without even the implied warranty of# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the# GNU AFFERO GENERAL PUBLIC LICENSE for more details.## You should have received a copy of the GNU Affero General Public# License along with this program. If not, see <http://www.gnu.org/licenses/>.#__doc__="""This contains the Base Class for Machine Resolvers. MachinesResolvers are used to tie a Machine Object to a token and an application. Todo so a Machine Resolver can translate between a FQDN, Hostname, IP and themachine ID.This file is tested in tests/test_lib_machines.py"""importnetaddrclassMachine:""" The Machine object is returned by the resolver for a given machine_id. It contains data like the hostname, the ip address and additional information like expiry or decommission... """def__init__(self,resolver_name,machine_id,hostname=None,ip=None):self.id=machine_idself.resolver_name=resolver_nameself.hostname=hostnameifisinstance(ip,str):self.ip=netaddr.IPAddress(ip)else:self.ip=ipdefhas_hostname(self,hostname):""" Checks if the machine has the given hostname. A machine might have more than one hostname. The hostname is then provided as a list :param hostname: The hostname searched for :type hostname: basestring :return: True or false """iftype(self.hostname)==list:returnhostnameinself.hostnameelifisinstance(self.hostname,str):returnhostname.lower()==self.hostname.lower()defhas_ip(self,ip):""" Checks if the machine has the given IP. A machine might have more than one IP Address. The ip is then provided as a list :param ip: The IP address to search for :type ip: Netaddr IPAddress :return: True or false """# convert to IPAddressifisinstance(ip,str):ip=netaddr.IPAddress(ip)iftype(self.ip)==list:returnipinself.ipeliftype(self.ip)==netaddr.IPAddress:returnip==self.ipdefget_dict(self):""" Convert the object attributes to a dict :return: dict of attributes """ip=self.ipiftype(self.ip)==list:ip=["{0!s}".format(i)foriinip]eliftype(self.ip)==netaddr.IPAddress:ip="{0!s}".format(ip)d={"hostname":self.hostname,"ip":ip,"resolver_name":self.resolver_name,"id":self.id}returndclassMachineResolverError(Exception):pass
[docs]classBaseMachineResolver:type="base"def__init__(self,name,config=None):""" :param name: The identifying name of the resolver :param config: :return: """self.name=nameifconfig:self.load_config(config)@classmethoddefget_type(cls):returncls.type
[docs]defget_machines(self,machine_id=None,hostname=None,ip=None,any=None,substring=False):""" Return a list of all machine objects in this resolver :param substring: If set to true, it will also match search_hostnames, that only are a subnet of the machines hostname. :type substring: bool :param any: a substring that matches EITHER hostname, machineid or ip :type any: basestring :return: list of machine objects """return[]
[docs]defget_machine_id(self,hostname=None,ip=None):""" Returns the machine id for a given hostname or IP address. If hostname and ip is given, the resolver should also check that the hostname matches the IP. If it can check this and hostname and IP do not match, then an Exception must be raised. :param hostname: The hostname of the machine :type hostname: basestring :param ip: IP address of the machine :type ip: netaddr :return: The machine ID, which depends on the resolver :rtype: basestring """return""
[docs]defload_config(self,config):""" This loads the configuration dictionary, which contains the necessary information for the machine resolver to find and connect to the machine store. :param config: The configuration dictionary to run the machine resolver :type config: dict :return: None """returnNone
[docs]@staticmethoddefget_config_description():""" Returns a description what config values are expected and allowed. :return: dict """return{}
[docs]@staticmethoddeftestconnection(params):""" This method can test if the passed parameters would create a working machine resolver. :param params: :return: tupple of success and description :rtype: (bool, string) """returnFalse,"Not Implemented"