Example #1
0
    def authenticate(self, timeout=20):
        """ Make authentication request to AWS STS service
            Timeout defaults to 20 seconds"""
        if self.validate_certs:
            conn = CertValidatingHTTPSConnection(self.host, self.port, timeout=timeout, **self.kwargs)
        else:
            conn = httplib.HTTPSConnection(self.host, self.port, timeout=timeout)

        headers = {"Content-type": "application/x-www-form-urlencoded"}
        try:
            conn.request('POST', '', self.package, headers)
            response = conn.getresponse()
            if response.status != 200:
                raise urllib2.HTTPError(url='', code=response.status, msg=response.reason, hdrs=None, fp=None)
            body = response.read()
            
            # parse AccessKeyId, SecretAccessKey and SessionToken
            creds = Credentials()
            h = BotoXmlHandler(creds, None)
            parseString(body, h)
            return creds
        except SSLError as err:
            if err.message != '':
                raise urllib2.URLError(err.message)
            else:
                raise urllib2.URLError(err[1])
        except socket.error as err:
            raise urllib2.URLError(err.message)
Example #2
0
    def _authenticate_(self, account, user, passwd, new_passwd=None, timeout=15, duration=3600):
        if user == 'admin' and duration > 3600:  # admin cannot have more than 1 hour duration
            duration = 3600
        # because of the variability, we need to keep this here, not in __init__
        auth_path = self.TEMPLATE.format(dur=duration)
        if not self.dns_enabled:
            auth_path = self.NON_DNS_QUERY_PATH + auth_path
        else:
            auth_path = '/' + auth_path
        host = self.host
        if self.dns_enabled:
            host = 'tokens.{0}'.format(host)
        if self.validate_certs:
            conn = CertValidatingHTTPSConnection(host, self.port, timeout=timeout, **self.kwargs)
        else:
            conn = HttpsConnectionFactory(self.port).https_connection_factory(host, timeout=timeout)

        if new_passwd:
            auth_string = u"{user}@{account};{pw}@{new_pw}".format(
                user=base64.b64encode(user),
                account=base64.b64encode(account),
                pw=base64.b64encode(passwd),
                new_pw=new_passwd
            )
        else:
            auth_string = u"{user}@{account}:{pw}".format(
                user=base64.b64encode(user),
                account=base64.b64encode(account),
                pw=passwd
            )
        encoded_auth = base64.b64encode(auth_string)
        headers = {'Authorization': "Basic %s" % encoded_auth}
        try:
            conn.request('GET', auth_path, '', headers)
            response = conn.getresponse()
            if response.status != 200:
                raise urllib2.HTTPError(url='', code=response.status, msg=response.reason, hdrs=None, fp=None)
            body = response.read()

            # parse AccessKeyId, SecretAccessKey and SessionToken
            creds = Credentials()
            h = BotoXmlHandler(creds, None)
            parseString(body, h)
            return creds
        except SSLError as err:
            if err.message != '':
                raise urllib2.URLError(str(err))
            else:
                raise urllib2.URLError(err[1])
        except socket.error as err:
            # when dns enabled, but path cloud, we get here with
            # err=gaierror(8, 'nodename nor servname provided, or not known')
            # when dns disabled, but path cloud, we get here with
            # err=gaierror(8, 'nodename nor servname provided, or not known')
            raise urllib2.URLError(str(err))
Example #3
0
def _parse_last_change_event(text: str) -> Mapping[str, Mapping[str, str]]:
    """
    Parse a LastChange event.

    :param text Text to parse.

    :return Dict per Instance, containing changed state variables with values.
    """
    content_handler = DlnaDmrEventContentHandler()
    error_handler = DlnaDmrEventErrorHandler()
    parseString(text.encode(), content_handler, error_handler)
    return content_handler.changes
Example #4
0
    def authenticate(self, timeout=20):
        """ Make authentication request to AWS STS service
            Timeout defaults to 20 seconds"""
        if self.validate_certs:
            conn = CertValidatingHTTPSConnection(self.host,
                                                 self.port,
                                                 timeout=timeout,
                                                 **self.kwargs)
        else:
            conn = httplib.HTTPSConnection(self.host,
                                           self.port,
                                           timeout=timeout)

        headers = {"Content-type": "application/x-www-form-urlencoded"}
        try:
            conn.request('POST', '', self.package, headers)
            response = conn.getresponse()
            if response.status != 200:
                raise urllib2.HTTPError(url='',
                                        code=response.status,
                                        msg=response.reason,
                                        hdrs=None,
                                        fp=None)
            body = response.read()

            # parse AccessKeyId, SecretAccessKey and SessionToken
            creds = Credentials()
            h = BotoXmlHandler(creds, None)
            parseString(body, h)
            return creds
        except SSLError as err:
            if err.message != '':
                raise urllib2.URLError(err.message)
            else:
                raise urllib2.URLError(err[1])
        except socket.error as err:
            raise urllib2.URLError(err.message)
Example #5
0
    def _authenticate_(self,
                       account,
                       user,
                       passwd,
                       new_passwd=None,
                       timeout=15,
                       duration=3600):
        auth_path = self.TEMPLATE.format(dur=duration)
        if not self.dns_enabled:
            auth_path = self.NON_DNS_QUERY_PATH + auth_path
        else:
            auth_path = '/' + auth_path
        host = self.host
        if self.dns_enabled:
            host = 'tokens.{0}'.format(host)
        if self.validate_certs:
            conn = CertValidatingHTTPSConnection(host,
                                                 self.port,
                                                 timeout=timeout,
                                                 **self.kwargs)
        else:
            conn = HttpsConnectionFactory(self.port).https_connection_factory(
                host, timeout=timeout)

        if new_passwd:
            auth_string = u"{user}@{account};{pw}@{new_pw}".format(
                user=base64.b64encode(user),
                account=base64.b64encode(account),
                pw=base64.b64encode(passwd),
                new_pw=new_passwd)
        else:
            auth_string = u"{user}@{account}:{pw}".format(
                user=base64.b64encode(user),
                account=base64.b64encode(account),
                pw=passwd)
        encoded_auth = base64.b64encode(auth_string)
        headers = {'Authorization': "Basic %s" % encoded_auth}
        try:
            conn.request('GET', auth_path, '', headers)
            response = conn.getresponse()
            if response.status != 200:
                raise urllib2.HTTPError(url='',
                                        code=response.status,
                                        msg=response.reason,
                                        hdrs=None,
                                        fp=None)
            body = response.read()

            # parse AccessKeyId, SecretAccessKey and SessionToken
            creds = Credentials()
            h = BotoXmlHandler(creds, None)
            parseString(body, h)
            return creds
        except SSLError as err:
            if err.message != '':
                raise urllib2.URLError(str(err))
            else:
                raise urllib2.URLError(err[1])
        except socket.error as err:
            # when dns enabled, but path cloud, we get here with
            # err=gaierror(8, 'nodename nor servname provided, or not known')
            # when dns disabled, but path cloud, we get here with
            # err=gaierror(8, 'nodename nor servname provided, or not known')
            raise urllib2.URLError(str(err))
Example #6
0
 def parse(self, string):
   self.reset()
   sax.parseString(string, self)