-
Notifications
You must be signed in to change notification settings - Fork 30
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
introduce automated credential rotation for BDBA #1144
base: master
Are you sure you want to change the base?
Conversation
@TuanAnh17N Thank you for your contribution. |
758dcfc
to
c703ed7
Compare
credentials = cfg_element.credentials() | ||
headers = {'Authorization': f'Bearer {credentials.token()}'} | ||
|
||
response = requests.get( |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
damn... I think it would be better to add this to delivery-service's bdba.client
. However, this would introduce a cyclic dependency :-(
Could you:
- also add this into mentioned bdba-client
- add a reference to delivery-service-repository + a todo (to deduplicate)
OTOH, from python-package's pov, the dependency would not actually be cyclic.... sooo wdyt about declaring dependency against delivery-service's bdba-package?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
not too sure about introducing a dependency towards delivery-service's bdba-package ...
this would also contain recently introduced secret-mgmt.
in my opinion it is fine to keep this specific bdba implementation in parallel, as it is not to be expected that this will be expanded in future
|
||
|
||
def get_api_key_expiry( | ||
cfg_element: model.bdba.BDBAConfig |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
see comment below. Also, I think we should not build such functions around our cfg-model-classes (separation of concerns). It is okay to have a util-function. however, the logic for issueing requests against bdba should be handled by our bdba-client.
@@ -18,6 +19,9 @@ | |||
|
|||
logger = logging.getLogger(__name__) | |||
|
|||
MAX_RETRIES = 5 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I think we need more retries / longer delay (or (better) exponential backoff).
consider we sometimes see maintenance windows of a couple of hours for GHE. so we should make our code be able to bridge such a gap
also, instead of setting module-level-constants, and read from multiple places in (one) function, preferably set those as default values in function-parameters.
@@ -297,7 +301,31 @@ def rotate_config_element_and_persist_in_cfg_repo( | |||
git_helper.add_and_commit( | |||
message=commit_message, | |||
) | |||
git_helper.push('@', target_ref) | |||
for attempt in range(1, MAX_RETRIES + 1): |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I suggest to stick to using recursion (as we commonly do in other places in our codebase). like so:
def function_with_retry(retries=123):
try:
something()
except ExpectedException as e:
if retries > 0:
function_with_retry(retries=retries - 1)
logger.info(f'Retrying in {RETRY_DELAY} seconds...') | ||
|
||
# pull the latest changes and rebase | ||
latest_commit = git_helper.fetch_head(target_ref) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
chance for success will be greater if we do fetch + hard-reset + recreate changes
git_helper.rebase(latest_commit.hexsha) | ||
|
||
time.sleep(RETRY_DELAY) | ||
else: |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
invert (early-exit) to decrease indent, like so:
if <error-condition>:
raise ....
# regular code (w/o indentation) goes here
''' | ||
if cfg_element._type_name == 'bdba': | ||
logger.info(f'NEW BDBA API KEY: {secret_id.get('api_key')}') | ||
raise RuntimeError(f'Failed to push changes to GitHub after {MAX_RETRIES} attempts') |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
we might want to use a dedicated exception-type for that case "FailedToWritebackCredentials" or similar, as this might be reasonable to be handled w/ specific error-handling
|
||
import cfg_mgmt | ||
import model.bdba | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: two blanks (pep8)
Release note: