Created
March 28, 2024 23:14
-
-
Save pythoninthegrass/5a45589ad231b9e940a505f54125a50b to your computer and use it in GitHub Desktop.
Public facing version of an internal Infisical export script
This file contains 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
BASE_URL= | |
ID= | |
SERVICE_TOKEN= | |
OUT_DIR= |
This file contains 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 | |
import requests | |
import json | |
from decouple import config | |
from pathlib import Path | |
# env vars | |
base_url = config("BASE_URL", default="https://app.infisical.com") | |
envs = [ | |
"dev", | |
"staging", | |
"prod", | |
] | |
id = config("ID") | |
svc_token = config("SERVICE_TOKEN") | |
out_dir = config("OUT_DIR", default="json") | |
# create output directory | |
Path(out_dir).mkdir(parents=True, exist_ok=True) | |
def get_secrets(url, headers): | |
"""Get secrets from the API and return the data as JSON.""" | |
response = requests.get(url, headers=headers) | |
data = response.json() | |
return data | |
def sort_by_key(data): | |
"""Sort the secrets by secretKey.""" | |
if isinstance(data, str): | |
data = json.loads(data) | |
return sorted(data['secrets'], key=lambda x: x['secretKey']) | |
def write_to_file(file, data): | |
"""Write the data to a file.""" | |
with open(file, "w") as f: | |
f.write(data) | |
def main(): | |
for env in envs: | |
url = f"{base_url}/api/v3/secrets/raw?environment={env}&workspaceId={id}" | |
headers = {"Authorization": f"Bearer {svc_token}"} | |
data = get_secrets(url, headers) | |
sorted_data = sort_by_key(data) | |
write_to_file(f"{out_dir}/{env}.json", json.dumps(sorted_data, indent=2)) | |
if __name__ == "__main__": | |
main() | |
This file contains 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
certifi==2024.2.2 ; python_version >= "3.11" and python_version < "3.13" | |
charset-normalizer==3.3.2 ; python_version >= "3.11" and python_version < "3.13" | |
idna==3.6 ; python_version >= "3.11" and python_version < "3.13" | |
python-decouple==3.8 ; python_version >= "3.11" and python_version < "3.13" | |
requests==2.31.0 ; python_version >= "3.11" and python_version < "3.13" | |
urllib3==2.2.1 ; python_version >= "3.11" and python_version < "3.13" |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Quickstart