commands.py 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167
  1. import datetime
  2. import random
  3. import string
  4. import click
  5. from flask import current_app
  6. from libs.password import password_pattern, valid_password, hash_password
  7. from libs.helper import email as email_validate
  8. from extensions.ext_database import db
  9. from libs.rsa import generate_key_pair
  10. from models.account import InvitationCode, Tenant
  11. from models.model import Account
  12. import secrets
  13. import base64
  14. from models.provider import Provider
  15. @click.command('reset-password', help='Reset the account password.')
  16. @click.option('--email', prompt=True, help='The email address of the account whose password you need to reset')
  17. @click.option('--new-password', prompt=True, help='the new password.')
  18. @click.option('--password-confirm', prompt=True, help='the new password confirm.')
  19. def reset_password(email, new_password, password_confirm):
  20. if str(new_password).strip() != str(password_confirm).strip():
  21. click.echo(click.style('sorry. The two passwords do not match.', fg='red'))
  22. return
  23. account = db.session.query(Account). \
  24. filter(Account.email == email). \
  25. one_or_none()
  26. if not account:
  27. click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
  28. return
  29. try:
  30. valid_password(new_password)
  31. except:
  32. click.echo(
  33. click.style('sorry. The passwords must match {} '.format(password_pattern), fg='red'))
  34. return
  35. # generate password salt
  36. salt = secrets.token_bytes(16)
  37. base64_salt = base64.b64encode(salt).decode()
  38. # encrypt password with salt
  39. password_hashed = hash_password(new_password, salt)
  40. base64_password_hashed = base64.b64encode(password_hashed).decode()
  41. account.password = base64_password_hashed
  42. account.password_salt = base64_salt
  43. db.session.commit()
  44. click.echo(click.style('Congratulations!, password has been reset.', fg='green'))
  45. @click.command('reset-email', help='Reset the account email.')
  46. @click.option('--email', prompt=True, help='The old email address of the account whose email you need to reset')
  47. @click.option('--new-email', prompt=True, help='the new email.')
  48. @click.option('--email-confirm', prompt=True, help='the new email confirm.')
  49. def reset_email(email, new_email, email_confirm):
  50. if str(new_email).strip() != str(email_confirm).strip():
  51. click.echo(click.style('Sorry, new email and confirm email do not match.', fg='red'))
  52. return
  53. account = db.session.query(Account). \
  54. filter(Account.email == email). \
  55. one_or_none()
  56. if not account:
  57. click.echo(click.style('sorry. the account: [{}] not exist .'.format(email), fg='red'))
  58. return
  59. try:
  60. email_validate(new_email)
  61. except:
  62. click.echo(
  63. click.style('sorry. {} is not a valid email. '.format(email), fg='red'))
  64. return
  65. account.email = new_email
  66. db.session.commit()
  67. click.echo(click.style('Congratulations!, email has been reset.', fg='green'))
  68. @click.command('reset-encrypt-key-pair', help='Reset the asymmetric key pair of workspace for encrypt LLM credentials. '
  69. 'After the reset, all LLM credentials will become invalid, '
  70. 'requiring re-entry.'
  71. 'Only support SELF_HOSTED mode.')
  72. @click.confirmation_option(prompt=click.style('Are you sure you want to reset encrypt key pair?'
  73. ' this operation cannot be rolled back!', fg='red'))
  74. def reset_encrypt_key_pair():
  75. if current_app.config['EDITION'] != 'SELF_HOSTED':
  76. click.echo(click.style('Sorry, only support SELF_HOSTED mode.', fg='red'))
  77. return
  78. tenant = db.session.query(Tenant).first()
  79. if not tenant:
  80. click.echo(click.style('Sorry, no workspace found. Please enter /install to initialize.', fg='red'))
  81. return
  82. tenant.encrypt_public_key = generate_key_pair(tenant.id)
  83. db.session.query(Provider).filter(Provider.provider_type == 'custom').delete()
  84. db.session.commit()
  85. click.echo(click.style('Congratulations! '
  86. 'the asymmetric key pair of workspace {} has been reset.'.format(tenant.id), fg='green'))
  87. @click.command('generate-invitation-codes', help='Generate invitation codes.')
  88. @click.option('--batch', help='The batch of invitation codes.')
  89. @click.option('--count', prompt=True, help='Invitation codes count.')
  90. def generate_invitation_codes(batch, count):
  91. if not batch:
  92. now = datetime.datetime.now()
  93. batch = now.strftime('%Y%m%d%H%M%S')
  94. if not count or int(count) <= 0:
  95. click.echo(click.style('sorry. the count must be greater than 0.', fg='red'))
  96. return
  97. count = int(count)
  98. click.echo('Start generate {} invitation codes for batch {}.'.format(count, batch))
  99. codes = ''
  100. for i in range(count):
  101. code = generate_invitation_code()
  102. invitation_code = InvitationCode(
  103. code=code,
  104. batch=batch
  105. )
  106. db.session.add(invitation_code)
  107. click.echo(code)
  108. codes += code + "\n"
  109. db.session.commit()
  110. filename = 'storage/invitation-codes-{}.txt'.format(batch)
  111. with open(filename, 'w') as f:
  112. f.write(codes)
  113. click.echo(click.style(
  114. 'Congratulations! Generated {} invitation codes for batch {} and saved to the file \'{}\''.format(count, batch,
  115. filename),
  116. fg='green'))
  117. def generate_invitation_code():
  118. code = generate_upper_string()
  119. while db.session.query(InvitationCode).filter(InvitationCode.code == code).count() > 0:
  120. code = generate_upper_string()
  121. return code
  122. def generate_upper_string():
  123. letters_digits = string.ascii_uppercase + string.digits
  124. result = ""
  125. for i in range(8):
  126. result += random.choice(letters_digits)
  127. return result
  128. def register_commands(app):
  129. app.cli.add_command(reset_password)
  130. app.cli.add_command(reset_email)
  131. app.cli.add_command(generate_invitation_codes)
  132. app.cli.add_command(reset_encrypt_key_pair)