Skip to content

Commit

Permalink
Merge pull request #500 from permitio/rk/log-api-policy-bundle-errors
Browse files Browse the repository at this point in the history
ApiPolicySource: Log errors in bundle init/update before retrying
  • Loading branch information
roekatz authored Sep 20, 2023
2 parents 8527bc9 + 7102de0 commit a7176dc
Showing 1 changed file with 37 additions and 25 deletions.
62 changes: 37 additions & 25 deletions packages/opal-common/opal_common/sources/api_policy_source.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,39 +76,51 @@ async def get_initial_policy_state_from_remote(self):
"""init remote data to local repo."""
async for attempt in AsyncRetrying(wait=wait_fixed(5)):
with attempt:
await self.fetch_policy_bundle_from_api_source(
self.remote_source_url, self.token
)
self.local_git = self.tar_to_git.create_local_git()
try:
await self.fetch_policy_bundle_from_api_source(
self.remote_source_url, self.token
)
self.local_git = self.tar_to_git.create_local_git()
except Exception:
logger.exception(
"Failed to load initial policy from remote API bundle server"
)
raise

async def api_update_policy(self) -> Tuple[bool, str, str]:
async for attempt in AsyncRetrying(wait=wait_fixed(5)):
with attempt:
(
tmp_bundle_path,
prev_version,
current_hash,
) = await self.fetch_policy_bundle_from_api_source(
self.remote_source_url, self.token
)
if tmp_bundle_path and prev_version and current_hash:
commit_msg = f"new version {current_hash}"
try:
(
self.local_git,
prev_commit,
new_commit,
) = self.tar_to_git.extract_bundle_to_local_git(
commit_msg=commit_msg
)
return (
True,
tmp_bundle_path,
prev_version,
current_hash,
prev_commit,
new_commit,
) = await self.fetch_policy_bundle_from_api_source(
self.remote_source_url, self.token
)
if tmp_bundle_path and prev_version and current_hash:
commit_msg = f"new version {current_hash}"
(
self.local_git,
prev_commit,
new_commit,
) = self.tar_to_git.extract_bundle_to_local_git(
commit_msg=commit_msg
)
return (
True,
prev_version,
current_hash,
prev_commit,
new_commit,
)
else:
return False, None, current_hash, None, None
except Exception as e:
logger.exception(
f"Failed to update policy from remote API bundle server"
)
else:
return False, None, current_hash, None, None
raise

def build_auth_headers(self, token=None, path=None):
# if it's a simple HTTP server with a bearer token
Expand Down

0 comments on commit a7176dc

Please sign in to comment.