Ejemplo n.º 1
0
    def read_config(self):
        config_file = os.path.join(context.conf_dir(), "fetcher.xml")
        try:
            c_elt = etree.parse(config_file).getroot()
        except:
            c_elt = etree.Element("config")

        return c_elt
Ejemplo n.º 2
0
    def _read_status(self):
        status_file = os.path.join(context.conf_dir(), "packageman.xml")
        try:
            s_elt = etree.parse(status_file).getroot()
        except:
            s_elt = etree.Element("worker")
            s_elt.set("id", self.name)

        return s_elt
Ejemplo n.º 3
0
 def _write_status(self):
     status_file = os.path.join(context.conf_dir(), "packageman.xml")
     with open(status_file, 'w') as s:
         s.write(etree.tostring(self.s_elt,
                                pretty_print=True).decode()
                )
Ejemplo n.º 4
0
 def write_config(self):
     config_file = os.path.join(context.conf_dir(), "fetcher.xml")
     with open(config_file, "w") as c:
         c.write(etree.tostring(self.config_elt, pretty_print=True).decode())
Ejemplo n.º 5
0
    def __init__(self, hierarchy_url, authen_type, obj_type,
                 credentials=None, service_id=None):
        '''Create a new WebClient

        hierarchy_url: URL on which to contact hierarchy

        authen_type: Type of authentication to use (cosign, cert, public)

        obj_type: Type of entity making the request (os_instance, person, ...)

        credentials (=None): A dictionary of credential information or
          a callable that will return such.

        service_id (=None): Used by the 'cert' method to look up
         certificate locations if they are not specified.

        '''

        self.hierarchy_url = hierarchy_url
        self.authen_type = authen_type
        self.obj_type = obj_type
        self.url = '{}/{}'.format(
            self.hierarchy_url,
            self.authen_type
            )
        self.encoding = 'utf-8'
        self.l = context.logger
        self.cookie_file = os.path.join(context.status_dir(), 'cookies.txt')
        self.cookie_jar = None
        handlers = []

        if self.authen_type == 'cosign':
            self.l.lmsg('building cosign handlers')
            self.cookie_jar = http.cookiejar.MozillaCookieJar(
                self.cookie_file
                )
            handlers.append(
                urllib_request.HTTPCookieProcessor(
                    self.cookie_jar
                    )
                )
            if (not hasattr(credentials, '__call__')) and (credentials is not None):
                values = credentials
                credentials = lambda x: values
            handlers.append(
                CosignHandler(
                    self.authen_elt.get('cosignLoginPage'),
                    self.cookie_jar,
                    CosignPasswordMgr(callback = credentials),
                    save_cookies = True
                    )
                )

        elif self.authen_type == 'cert':
            # Get the cert and key locations from credentials
            try:
                # See if credentials is callable
                cred = credentials()
            except TypeError:
                # It should be a dictionary
                if credentials is None:
                    cred = {}
                else:
                    cred = credentials

            keyfile = cred.get('key')
            if keyfile is None and service_id is not None:
                keyfile = os.path.join(context.conf_dir(),
                                       'services',
                                       service_id,
                                       'myself.key')

            certfile = cred.get('cert')
            if certfile is None and service_id is not None:
                certfile = os.path.join(context.conf_dir(),
                                        'services',
                                        service_id,
                                        'myself.crt')

            handlers.append(
                HTTPSClientAuthHandler(keyfile,certfile)
                )

        elif self.authen_type == 'debug':
            try:
                # See if credentials is callable
                cred = credentials()
            except TypeError:
                # It should be a dictionary
                cred = credentials
            if cred is None:
                # Still not set: raise exception
                raise ValueError('"name" not set for debug authentication')

            self.url = '{}/{}:{}'.format(self.url,
                                         self.obj_type,
                                         cred.get('name'))

        elif self.authen_type == 'public':
            # Nothing to be done - just need it to be in the list of
            # auth types.
            pass
        else:
            raise ValueError(
                'Invalid authentication type "{}"'.format(self.authen_type)
                )

        self.opener = urllib_request.build_opener(*handlers)