Skip to content

Instantly share code, notes, and snippets.

@alexesDev
Last active January 15, 2021 11:43
Show Gist options
  • Save alexesDev/207fced5b5615f0d69481ec20430d403 to your computer and use it in GitHub Desktop.
Save alexesDev/207fced5b5615f0d69481ec20430d403 to your computer and use it in GitHub Desktop.
Download yandex certificates by ids
#!/usr/bin/python
import urllib.request
import json
import time
def get(url, headers={}):
req = urllib.request.Request(url, headers=headers)
with urllib.request.urlopen(req) as f:
return json.loads(f.read().decode('utf-8'))
def get_token():
url = 'http://169.254.169.254/computeMetadata/v1/instance/service-accounts/default/token'
data = get(url, {'Metadata-Flavor': 'Google'})
return data['access_token']
def get_cert(token, id):
url = 'https://data.certificate-manager.api.cloud.yandex.net/certificate-manager/v1/certificates/%s:getContent' % id
return get(url, {'Authorization': 'Bearer %s' % token})
def write_content(filename, content):
with open(filename, 'w') as f:
f.write(content)
def download_cert(data):
write_content('%s.cert' % data['certificateId'], '\n'.join(data['certificateChain']))
write_content('%s.key' % data['certificateId'], data['privateKey'])
if __name__ == '__main__':
cert_ids = ['xxx', 'xxx']
token = get_token()
while True:
for id in cert_ids:
print('Download %s' % id)
download_cert(get_cert(token, id))
print('Sleep')
time.sleep(24 * 60 * 60)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment