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

introduce automated credential rotation for BDBA #1144

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

TuanAnh17N
Copy link
Contributor

Release note:

Introduce automated credential rotation for BDBA

@gardener-robot
Copy link

@TuanAnh17N Thank you for your contribution.

@gardener-robot gardener-robot added the size/m Size of pull request is medium (see gardener-robot robot/bots/size.py) label Feb 3, 2025
cfg_mgmt/util.py Dismissed Show dismissed Hide dismissed
credentials = cfg_element.credentials()
headers = {'Authorization': f'Bearer {credentials.token()}'}

response = requests.get(
Copy link
Member

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?

/cc @zkdev | @8R0WNI3

Copy link
Member

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
Copy link
Member

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.

cfg_mgmt/bdba.py Show resolved Hide resolved
cfg_mgmt/bdba.py Show resolved Hide resolved
cfg_mgmt/bdba.py Show resolved Hide resolved
@@ -18,6 +19,9 @@

logger = logging.getLogger(__name__)

MAX_RETRIES = 5
Copy link
Member

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):
Copy link
Member

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)
Copy link
Member

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:
Copy link
Member

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')
Copy link
Member

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

@gardener-robot gardener-robot added the needs/changes Needs (more) changes label Feb 3, 2025

import cfg_mgmt
import model.bdba

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: two blanks (pep8)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
needs/changes Needs (more) changes needs/review Needs review size/m Size of pull request is medium (see gardener-robot robot/bots/size.py)
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants