Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

If vault stores already exist, don't wipe them out #60

Merged
merged 2 commits into from
May 17, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 26 additions & 19 deletions initialize_vault_store.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import json
import os
from authx.auth import get_service_store_secret, set_service_store_secret, add_provider_to_opa, add_program_to_opa
from authx.auth import get_service_store_secret, set_service_store_secret, add_provider_to_opa, add_program_to_opa, list_programs_in_opa
import sys

## Initializes Vault's opa service store with the information for our IDP and the data in site_roles.json, paths.json, programs.json
Expand All @@ -11,34 +11,41 @@
with open('/app/bearer.txt') as f:
try:
token = f.read().strip()
response, status_code = set_service_store_secret("opa", key="data", value=json.dumps({"keys":[]}))
response = add_provider_to_opa(token, os.getenv("KEYCLOAK_REALM_URL"))
results.append(response)
except Exception as e:
print(str(e))
raise Exception(f"failed to save idp keys: {str(e)} {status_code}")
sys.exit(1)

with open('/app/defaults/paths.json') as f:
data = f.read()
response, status_code = set_service_store_secret("opa", key="paths", value=data)
if status_code != 200:
sys.exit(3)
results.append(response)
response, status_code = get_service_store_secret("opa", key="paths")
if status_code != 200:
with open('/app/defaults/paths.json') as f:
data = f.read()
response, status_code = set_service_store_secret("opa", key="paths", value=data)
if status_code != 200:
raise Exception(f"failed to save paths: {str(e)} {status_code}")
results.append(response)

with open('/app/defaults/site_roles.json') as f:
data = f.read()
response, status_code = set_service_store_secret("opa", key="site_roles", value=data)
if status_code != 200:
sys.exit(2)
results.append(response)
response, status_code = get_service_store_secret("opa", key="site_roles")
if status_code != 200:
with open('/app/defaults/site_roles.json') as f:
data = f.read()
response, status_code = set_service_store_secret("opa", key="site_roles", value=data)
if status_code != 200:
raise Exception(f"failed to save site roles: {str(e)} {status_code}")
results.append(response)

current_programs, status_code = list_programs_in_opa()
if status_code != 200:
current_programs = []
with open('/app/defaults/programs.json') as f:
programs = json.load(f)
for program in programs:
response, status_code = add_program_to_opa(programs[program])
if status_code != 200:
sys.exit(2)
results.append(response)
if programs[program] not in current_programs:
response, status_code = add_program_to_opa(programs[program])
if status_code != 200:
raise Exception(f"failed to save program authz: {str(e)} {status_code}")
results.append(response)
except Exception as e:
print(str(e))
sys.exit(4)
Expand Down