Elasticsearch: Sign aws request based on connection/host used from connectionpool

65 Views Asked by At

I am trying to sign the request based on connection/host used if es is hosted in aws. Not sure exactly where should I do it. But this is what I have come up with till now and request signing is not working now. Using elasticsearch 6.8.2 in aws

class MyESConnection(Urllib3HttpConnection):
def __init__(self, weight: int = 1, **kwargs):
    assert isinstance(weight, int) and weight >= 1 and weight <= 100
    port = kwargs.pop("port", 9200)
    scheme = kwargs.pop("scheme", "http")
    use_ssl = kwargs.pop("use_ssl", False)

    super(MyESConnection, self).__init__(
        port=port, scheme=scheme, use_ssl=use_ssl, **kwargs
    )
    self.weight = weight

def perform_request(
    self, method, url, params=None, body=None, timeout=None, ignore=(), headers=None
):
    self.headers
    hostname = self.hostname
    print(self.host)
    print(method)
    print(url)

    # see if host is aws service then use signed request
    if hostname.endswith("amazonaws.com"):
        service = "es"

        # hostname format in aws is vpc-test-bllnacmuxpeqs4b2x6ujbagnfi.us-west-2.es.amazonaws.com
        # get region name from host beause request should be signed based on host region
        region = hostname.split(".")[-4]
        print(region)

        credentials = boto3.Session().get_credentials()

        awsauth = AWS4Auth(
            credentials.access_key,
            credentials.secret_key,
            region,
            service,
            session_token=credentials.token,
        )
         

        #self.headers.update(urllib3.make_headers(basic_auth=awsauth))

    return super().perform_request(
        method,
        url,
        params=params,
        body=body,
        timeout=timeout,
        ignore=ignore,
        headers=headers,
    )

Not sure how should I sign the request based on connection used from connectionpool

0

There are 0 best solutions below