Last active
January 17, 2020 09:37
-
-
Save s-fujimoto/0f9bf014a0f730575872 to your computer and use it in GitHub Desktop.
request to amazon es controled iam policy
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env python | |
from boto.connection import AWSAuthConnection | |
import requests | |
import json | |
import sys | |
def main(): | |
host = sys.argv[1] | |
region = sys.argv[2] | |
method = sys.argv[3] | |
path = sys.argv[4] | |
if len(sys.argv) > 5: | |
data = sys.argv[5] | |
else: | |
data = "" | |
credentials = get_credentials() | |
client = ESConnection( | |
region=region, | |
host=host, | |
aws_access_key_id=credentials["access_key"], | |
aws_secret_access_key=credentials["secret_key"], | |
security_token=credentials["token"], | |
is_secure=False) | |
print(client.request(method, path, data)) | |
def get_credentials(): | |
url = "http://169.254.169.254/latest/meta-data/iam/security-credentials/" | |
role_res = requests.get(url) | |
rolename = role_res.text | |
credential_res = requests.get(url + rolename) | |
credential = json.loads(credential_res.text) | |
return {"access_key": credential["AccessKeyId"], | |
"secret_key": credential["SecretAccessKey"], | |
"token": credential["Token"] | |
} | |
class ESConnection(AWSAuthConnection): | |
def __init__(self, region, **kwargs): | |
super(ESConnection, self).__init__(**kwargs) | |
self._set_auth_region_name(region) | |
self._set_auth_service_name("es") | |
def _required_auth_capability(self): | |
return ['hmac-v4'] | |
def request(self, method="GET", path="/", data=""): | |
resp = self.make_request(method=method, path=path, data=data) | |
return resp.read() | |
if __name__ == "__main__": | |
main() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment