Example #1
0
def check_schema_versions(db, strict=False):
    modules = {
        'ad': 'Cerebrum.modules.ADObject',
        'ad_email': 'Cerebrum.modules.no.uit.ad_email',
        'apikeys': 'Cerebrum.modules.apikeys',
        'auditlog': 'Cerebrum.modules.audit',
        'bofhd_requests': 'Cerebrum.modules.bofhd_requests.request',
        'changelog': 'Cerebrum.modules.ChangeLog',
        'disk_quota': 'Cerebrum.modules.disk_quota',
        'dns': 'Cerebrum.modules.dns',
        'email': 'Cerebrum.modules.Email',
        'entity_expire': 'Cerebrum.modules.entity_expire',
        'entity_trait': 'Cerebrum.modules.EntityTrait',
        'eventlog': 'Cerebrum.modules.EventLog',
        'events': 'Cerebrum.modules.event_publisher',
        'hostpolicy': 'Cerebrum.modules.hostpolicy',
        'legacy_users': 'Cerebrum.modules.legacy_users',
        'note': 'Cerebrum.modules.Note',
        'password_history': 'Cerebrum.modules.pwcheck.history',
        'posixuser': '******',
        'stedkode': 'Cerebrum.modules.no.Stedkode',
        'stillingskoder': 'Cerebrum.modules.no.stillingskoder',
        'consent': 'Cerebrum.modules.consent.Consent',
        'employment': 'Cerebrum.modules.no.PersonEmployment',
        'gpg': 'Cerebrum.modules.gpg',
    }
    meta = Metainfo.Metainfo(db)
    for name, value in meta.list():
        if isinstance(value, tuple):
            print("WARNING: The version number of module {modulename} is "
                  "saved as a tuple.".format(modulename=name))
            value = "%d.%d.%d" % value
        if name == Metainfo.SCHEMA_VERSION_KEY:
            if not Cerebrum.__version__ == value:
                print("WARNING: cerebrum version %s does not"
                      " match schema version %s" %
                      (Cerebrum.__version__, value))
                if strict:
                    exit(1)
        elif name.startswith('sqlmodule_'):
            name = name[len('sqlmodule_'):]
            if name not in modules:
                # print "WARNING: unknown module %s" % name
                # if strict: exit(1)
                continue
            try:
                module = dyn_import(modules[name])
                version = module.__version__
            except Exception as e:
                print("ERROR: can't find version of module %s: %s" % (name, e))
                continue
            if not version == value:
                print("WARNING: module %s version %s does"
                      " not match schema version %s" % (name, version, value))
                if strict:
                    exit(1)
        else:
            print("ERROR: unknown metainfo %s: %s" % (name, value))
            if strict:
                exit(1)
Example #2
0
 def datasource(self):
     mod, name = self._config.datasource.datasource_class.split('/')
     mod = dyn_import(mod)
     cls = getattr(mod, name)
     return cls(db=self.db,
                config=self._config.datasource,
                logger=self.logger)
Example #3
0
 def get(comp):
     # Mostly cut 'n paste from Factory
     components = {'Settings': 'CLASS_SETTINGS',
                   'PreParser': 'CLASS_PREPARSER',
                   'Analyzer': 'CLASS_ANALYZER',
                   'Processor': 'CLASS_PROCESSOR',
                   'EntityIterator': 'CLASS_ENTITYITERATOR',
                   'PropertiesParser': 'CLASS_PROPERTIESPARSER',
                   'PersonParser': 'CLASS_PERSONPARSER',
                   'OrgParser': 'CLASS_ORGPARSER',
                   'OUParser': 'CLASS_OUPARSER',
                   'GroupParser': 'CLASS_GROUPPARSER',
                   'RelationParser': 'CLASS_RELATIONPARSER',
                   'Object2Cerebrum': 'CLASS_OBJ2CEREBRUM'}
     
     try:
         conf_var = components[comp]
     except KeyError:
         raise ValueError, "Unknown component %r" % comp
     import_spec = getattr(abcconf, conf_var)
     if not isinstance(import_spec, (tuple, list)):
         raise ValueError, \
               "Invalid import spec for component %s: %r" % (comp,
                                                             import_spec)
     bases = []
     for c in import_spec:
         (mod_name, class_name) = c.split("/", 1)
         mod = dyn_import(mod_name)
         cls = getattr(mod, class_name)
         # The cereconf.CLASS_* tuples control which classes
         # are used to construct a Factory product class.
         # Order inside such a tuple is significant for the
         # product class's method resolution order.
         #
         # A likely misconfiguration is to list a class A as
         # class_tuple[N], and a subclass of A as
         # class_tuple[N+x], as that would mean the subclass
         # won't override any of A's methods.
         #
         # The following code should ensure that this form of
         # misconfiguration won't be used.
         for override in bases:
             if issubclass(cls, override):
                 raise RuntimeError, \
                       ("Class %r should appear earlier in"
                        " abcconf.%s, as it's a subclass of"
                        " class %r." % (cls, conf_var, override))
         bases.append(cls)
     if len(bases) == 1:
         comp_class = bases[0]
     else:
         # Dynamically construct a new class that inherits from
         # all the specified classes.  The name of the created
         # class is the same as the component name with a
         # prefix of "_dynamic_"; the prefix is there to reduce
         # the probability of `auto_super` name collision
         # problems.
         comp_class = type('_dynamic_' + comp, tuple(bases), {})
     return comp_class
Example #4
0
def import_class(cls):
    """This function dynamically loads a class.

    :type cls: str
    :param cls: The class path, i.e. 'Cerebrum.modules.cheese/Stilton'
    """
    module, classname = cls.split('/', 1)
    mod = dyn_import(module)
    return getattr(mod, classname)
Example #5
0
def import_class(cls):
    """This function dynamically loads a class.

    :type cls: str
    :param cls: The class path, i.e. 'Cerebrum.modules.cheese/Stilton'
    """
    module, classname = cls.split('/', 1)
    mod = dyn_import(module)
    return getattr(mod, classname)
Example #6
0
def check_schema_versions(db, strict=False):
    modules = {
        'ad': 'Cerebrum.modules.ADObject',
        'changelog': 'Cerebrum.modules.ChangeLog',
        'dns': 'Cerebrum.modules.dns',
        'email': 'Cerebrum.modules.Email',
        'entity_trait': 'Cerebrum.modules.EntityTrait',
        'eventlog': 'Cerebrum.modules.EventLog',
        'events': 'Cerebrum.modules.event_publisher',
        'hostpolicy': 'Cerebrum.modules.hostpolicy',
        'note': 'Cerebrum.modules.Note',
        'password_history': 'Cerebrum.modules.pwcheck.history',
        'posixuser': '******',
        'stedkode': 'Cerebrum.modules.no.Stedkode',
        'consent': 'Cerebrum.modules.consent.Consent',
        'employment': 'Cerebrum.modules.no.PersonEmployment',
        'virtual_group': 'Cerebrum.modules.virtualgroup',
        'virtual_group_ou': 'Cerebrum.modules.virtualgroup.OUGroup',
        'gpg': 'Cerebrum.modules.gpg',
    }
    meta = Metainfo.Metainfo(db)
    for name, value in meta.list():
        if name == Metainfo.SCHEMA_VERSION_KEY:
            if not Cerebrum._version == value:
                print("WARNING: cerebrum version %s does not"
                      " match schema version %s" % (
                          "%d.%d.%d" % Cerebrum._version,
                          "%d.%d.%d" % value))
                if strict:
                    exit(1)
        elif name.startswith('sqlmodule_'):
            name = name[len('sqlmodule_'):]
            if name not in modules:
                # print "WARNING: unknown module %s" % name
                # if strict: exit(1)
                continue
            try:
                module = dyn_import(modules[name])
                version = module.__version__
            except Exception, e:
                print "ERROR: can't find version of module %s: %s" % (
                    name, e)
                continue
            if not version == value:
                print("WARNING: module %s version %s does"
                      " not match schema version %s" %
                      (name, version, value))
                if strict:
                    exit(1)
        else:
            print "ERROR: unknown metainfo %s: %s" % (
                name, value)
            if strict:
                exit(1)
Example #7
0
def check_schema_versions(db, strict=False):
    modules = {
        'ad': 'Cerebrum.modules.ADObject',
        'changelog': 'Cerebrum.modules.ChangeLog',
        'dns': 'Cerebrum.modules.dns',
        'email': 'Cerebrum.modules.Email',
        'entity_trait': 'Cerebrum.modules.EntityTrait',
        'eventlog': 'Cerebrum.modules.EventLog',
        'event-publisher': 'Cerebrum.modules.event_publisher',
        'hostpolicy': 'Cerebrum.modules.hostpolicy',
        'note': 'Cerebrum.modules.Note',
        'password_history': 'Cerebrum.modules.pwcheck.history',
        'posixuser': '******',
        'stedkode': 'Cerebrum.modules.no.Stedkode',
        'consent': 'Cerebrum.modules.consent.Consent',
        'employment': 'Cerebrum.modules.no.PersonEmployment',
        'virtual_group': 'Cerebrum.modules.virtualgroup',
        'virtual_group_ou': 'Cerebrum.modules.virtualgroup.OUGroup',
        'gpg': 'Cerebrum.modules.gpg',
    }
    meta = Metainfo.Metainfo(db)
    for name, value in meta.list():
        if name == Metainfo.SCHEMA_VERSION_KEY:
            if not Cerebrum._version == value:
                print(
                    "WARNING: cerebrum version %s does not"
                    " match schema version %s" %
                    ("%d.%d.%d" % Cerebrum._version, "%d.%d.%d" % value))
                if strict:
                    exit(1)
        elif name.startswith('sqlmodule_'):
            name = name[len('sqlmodule_'):]
            if name not in modules:
                # print "WARNING: unknown module %s" % name
                # if strict: exit(1)
                continue
            try:
                module = dyn_import(modules[name])
                version = module.__version__
            except Exception, e:
                print "ERROR: can't find version of module %s: %s" % (name, e)
                continue
            if not version == value:
                print(
                    "WARNING: module %s version %s does"
                    " not match schema version %s" % (name, version, value))
                if strict:
                    exit(1)
        else:
            print "ERROR: unknown metainfo %s: %s" % (name, value)
            if strict:
                exit(1)
Example #8
0
 def get(comp):
     # Mostly cut 'n paste from Factory
     components = {'BatchRunner': 'CLASS_BATCH',
                   'Handler': 'CLASS_HANDLER'}
     
     try:
         conf_var = components[comp]
     except KeyError:
         raise ValueError("Unknown component %r".format(comp))
     import_spec = getattr(procconf, conf_var)
     if not isinstance(import_spec, (tuple, list)):
         raise ValueError("Invalid import spec for component {}: {!r}"
                          .format(comp, import_spec))
     bases = []
     for c in import_spec:
         (mod_name, class_name) = c.split("/", 1)
         mod = dyn_import(mod_name)
         cls = getattr(mod, class_name)
         # The cereconf.CLASS_* tuples control which classes
         # are used to construct a Factory product class.
         # Order inside such a tuple is significant for the
         # product class's method resolution order.
         #
         # A likely misconfiguration is to list a class A as
         # class_tuple[N], and a subclass of A as
         # class_tuple[N+x], as that would mean the subclass
         # won't override any of A's methods.
         #
         # The following code should ensure that this form of
         # misconfiguration won't be used.
         for override in bases:
             if issubclass(cls, override):
                 raise RuntimeError(
                     "Class {!r} should appear earlier in procconf.{}, "
                     "as it's a subclass of class {!r}."
                     .format(cls, conf_var, override)
                 )
         bases.append(cls)
     if len(bases) == 1:
         comp_class = bases[0]
     else:
         # Dynamically construct a new class that inherits from
         # all the specified classes.  The name of the created
         # class is the same as the component name with a
         # prefix of "_dynamic_"; the prefix is there to reduce
         # the probability of `auto_super` name collision
         # problems.
         comp_class = type('_dynamic_' + comp, tuple(bases), {})
     return comp_class
Example #9
0
    def get(comp):
        # Mostly cut 'n paste from Factory
        components = {'BatchRunner': 'CLASS_BATCH', 'Handler': 'CLASS_HANDLER'}

        try:
            conf_var = components[comp]
        except KeyError:
            raise ValueError("Unknown component %r".format(comp))
        import_spec = getattr(procconf, conf_var)
        if not isinstance(import_spec, (tuple, list)):
            raise ValueError(
                "Invalid import spec for component {}: {!r}".format(
                    comp, import_spec))
        bases = []
        for c in import_spec:
            (mod_name, class_name) = c.split("/", 1)
            mod = dyn_import(mod_name)
            cls = getattr(mod, class_name)
            # The cereconf.CLASS_* tuples control which classes
            # are used to construct a Factory product class.
            # Order inside such a tuple is significant for the
            # product class's method resolution order.
            #
            # A likely misconfiguration is to list a class A as
            # class_tuple[N], and a subclass of A as
            # class_tuple[N+x], as that would mean the subclass
            # won't override any of A's methods.
            #
            # The following code should ensure that this form of
            # misconfiguration won't be used.
            for override in bases:
                if issubclass(cls, override):
                    raise RuntimeError(
                        "Class {!r} should appear earlier in procconf.{}, "
                        "as it's a subclass of class {!r}.".format(
                            cls, conf_var, override))
            bases.append(cls)
        if len(bases) == 1:
            comp_class = bases[0]
        else:
            # Dynamically construct a new class that inherits from
            # all the specified classes.  The name of the created
            # class is the same as the component name with a
            # prefix of "_dynamic_"; the prefix is there to reduce
            # the probability of `auto_super` name collision
            # problems.
            comp_class = type('_dynamic_' + comp, tuple(bases), {})
        return comp_class
Example #10
0
            port = int(val)
        elif opt in ('--unencrypted',):
            use_encryption = False
        elif opt in ('--instance',):
            instance = val
        elif opt in ('--interface',):
            interface = val
        elif opt in ('-h', '--help'):
            usage()
        else:
            print "Unknown argument: %s" % opt
            usage(1)

    # Get the service tier class and give it to the server
    module, classname = instance.split('/', 1)
    mod = dyn_import(module)
    cls = getattr(mod, classname)
    GroupService.cere_class = cls
    # TBD: Should Cerebrum tier be started once per session instead? Takes
    # more memory, but are there benefits we need, e.g. language control?

    private_key_file  = None
    certificate_file  = None
    client_ca         = None
    fingerprints      = None

    services = [auth.PasswordAuthenticationService, GroupService]
    if interface:
        SoapListener.TwistedSoapStarter.interface = interface

    if use_encryption:
Example #11
0
    def get(comp):
        # Mostly cut 'n paste from Factory
        components = {
            'Settings': 'CLASS_SETTINGS',
            'PreParser': 'CLASS_PREPARSER',
            'Analyzer': 'CLASS_ANALYZER',
            'Processor': 'CLASS_PROCESSOR',
            'EntityIterator': 'CLASS_ENTITYITERATOR',
            'PropertiesParser': 'CLASS_PROPERTIESPARSER',
            'PersonParser': 'CLASS_PERSONPARSER',
            'OrgParser': 'CLASS_ORGPARSER',
            'OUParser': 'CLASS_OUPARSER',
            'GroupParser': 'CLASS_GROUPPARSER',
            'RelationParser': 'CLASS_RELATIONPARSER',
            'Object2Cerebrum': 'CLASS_OBJ2CEREBRUM'
        }

        try:
            conf_var = components[comp]
        except KeyError:
            raise ValueError, "Unknown component %r" % comp
        import_spec = getattr(abcconf, conf_var)
        if not isinstance(import_spec, (tuple, list)):
            raise ValueError, \
                  "Invalid import spec for component %s: %r" % (comp,
                                                                import_spec)
        bases = []
        for c in import_spec:
            (mod_name, class_name) = c.split("/", 1)
            mod = dyn_import(mod_name)
            cls = getattr(mod, class_name)
            # The cereconf.CLASS_* tuples control which classes
            # are used to construct a Factory product class.
            # Order inside such a tuple is significant for the
            # product class's method resolution order.
            #
            # A likely misconfiguration is to list a class A as
            # class_tuple[N], and a subclass of A as
            # class_tuple[N+x], as that would mean the subclass
            # won't override any of A's methods.
            #
            # The following code should ensure that this form of
            # misconfiguration won't be used.
            for override in bases:
                if issubclass(cls, override):
                    raise RuntimeError, \
                          ("Class %r should appear earlier in"
                           " abcconf.%s, as it's a subclass of"
                           " class %r." % (cls, conf_var, override))
            bases.append(cls)
        if len(bases) == 1:
            comp_class = bases[0]
        else:
            # Dynamically construct a new class that inherits from
            # all the specified classes.  The name of the created
            # class is the same as the component name with a
            # prefix of "_dynamic_"; the prefix is there to reduce
            # the probability of `auto_super` name collision
            # problems.
            comp_class = type('_dynamic_' + comp, tuple(bases), {})
        return comp_class
Example #12
0
        ])
    except getopt.GetoptError, e:
        print e
        usage(1)

    # Scan for the service name before parsing args.
    # TODO: Make this pretty
    service = None
    for opt, val in opts:
        if opt in ('--service-name', ):
            service = val

    # Load configuration before parsing args, since we want to preserve
    # overrides.
    try:
        cisconf = dyn_import('cisconf.%s' % service)
    except ImportError, e:
        if e == 'No module named None':
            print('Error: must specify service-name')
            usage(2)
        else:
            print(
                'Error: service-name configuration \'cisconf.%s\' not found' %
                service)
            usage(3)

    use_encryption = True
    port = getattr(cisconf, 'PORT', 0)
    logfilename = getattr(cisconf, 'LOG_FILE', None)
    instance = getattr(cisconf, 'CEREBRUM_CLASS', None)
    interface = getattr(cisconf, 'INTERFACE', None)
Example #13
0
                                    'service-name=', 'dry-run'])
    except getopt.GetoptError, e:
        print e
        usage(1)

    # Scan for the service name before parsing args.
    # TODO: Make this pretty
    service = None
    for opt, val in opts:
        if opt in ('--service-name',):
            service = val

    # Load configuration before parsing args, since we want to preserve
    # overrides.
    try:
        cisconf = dyn_import('cisconf.%s' % service)
    except ImportError, e:
        if e == 'No module named None':
            print('Error: must specify service-name')
            usage(2)
        else:
            print('Error: service-name configuration \'cisconf.%s\' not found'
                  % service)
            usage(3)

    use_encryption = True
    port = getattr(cisconf, 'PORT', 0)
    logfilename = getattr(cisconf, 'LOG_FILE', None)
    instance = getattr(cisconf, 'CEREBRUM_CLASS', None)
    interface = getattr(cisconf, 'INTERFACE', None)
    log_prefix = getattr(cisconf, 'LOG_PREFIX', None)