fix(api commands): cmdb-password-data-migrate ()

This commit is contained in:
pycook 2023-11-02 20:43:39 +08:00 committed by GitHub
parent 5e5ec3f887
commit 2c70cb49e1
2 changed files with 8 additions and 2 deletions
cmdb-api/api
commands
lib/secrets

View File

@ -329,7 +329,6 @@ def valid_address(address):
} }
KeyManage.print_response(response) KeyManage.print_response(response)
return False return False
return True return True
@ -444,6 +443,7 @@ def cmdb_password_data_migrate():
value_table = CIIndexValueText if attr.is_index else CIValueText value_table = CIIndexValueText if attr.is_index else CIValueText
failed = False
for i in value_table.get_by(attr_id=attr.id, to_dict=False): for i in value_table.get_by(attr_id=attr.id, to_dict=False):
if current_app.config.get("SECRETS_ENGINE", 'inner') == 'inner': if current_app.config.get("SECRETS_ENGINE", 'inner') == 'inner':
_, status = InnerCrypt().decrypt(i.value) _, status = InnerCrypt().decrypt(i.value)
@ -454,6 +454,7 @@ def cmdb_password_data_migrate():
if status: if status:
CIValueText.create(ci_id=i.ci_id, attr_id=attr.id, value=encrypt_value) CIValueText.create(ci_id=i.ci_id, attr_id=attr.id, value=encrypt_value)
else: else:
failed = True
continue continue
elif current_app.config.get("SECRETS_ENGINE") == 'vault': elif current_app.config.get("SECRETS_ENGINE") == 'vault':
if i.value == '******': if i.value == '******':
@ -464,8 +465,12 @@ def cmdb_password_data_migrate():
vault.update("/{}/{}".format(i.ci_id, i.attr_id), dict(v=i.value)) vault.update("/{}/{}".format(i.ci_id, i.attr_id), dict(v=i.value))
except Exception as e: except Exception as e:
print('save password to vault failed: {}'.format(e)) print('save password to vault failed: {}'.format(e))
failed = True
continue continue
else: else:
continue continue
i.delete() i.delete()
if not failed and attr.is_index:
attr.update(is_index=False)

View File

@ -65,7 +65,8 @@ class KeyManage:
self.backend = Backend(backend) self.backend = Backend(backend)
def init_app(self, app, backend=None): def init_app(self, app, backend=None):
if sys.argv[0].endswith("gunicorn") or (len(sys.argv) > 1 and sys.argv[1] == "run"): if (sys.argv[0].endswith("gunicorn") or
(len(sys.argv) > 1 and sys.argv[1] in ("run", "cmdb-password-data-migrate"))):
self.trigger = app.config.get("INNER_TRIGGER_TOKEN") self.trigger = app.config.get("INNER_TRIGGER_TOKEN")
if not self.trigger: if not self.trigger:
return return