Skip to content

Instantly share code, notes, and snippets.

@pythoninthegrass
Created March 28, 2024 23:14
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pythoninthegrass/5a45589ad231b9e940a505f54125a50b to your computer and use it in GitHub Desktop.
Save pythoninthegrass/5a45589ad231b9e940a505f54125a50b to your computer and use it in GitHub Desktop.
Public facing version of an internal Infisical export script
BASE_URL=
ID=
SERVICE_TOKEN=
OUT_DIR=
#!/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()
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"
@pythoninthegrass
Copy link
Author

Quickstart

# create virtual environment
python -m venv .venv
source .venv/bin/activate

# install dependencies
python -m pip install -r requirements.txt

# fill out .env file
cp .env.example .env

# run the script
python main.py

# deactivate virtual environment
deactivate

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment