-
Notifications
You must be signed in to change notification settings - Fork 318
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
Do not throw on wrong password input #2237
Open
gztensor
wants to merge
1
commit into
rao
Choose a base branch
from
fix/do-not-throw-on-wrong-password
base: rao
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -301,57 +301,69 @@ def decrypt_keyfile_data( | |
if coldkey_name is not None and password is None: | ||
password = get_coldkey_password_from_environment(coldkey_name) | ||
|
||
try: | ||
password = ( | ||
getpass.getpass("Enter password to unlock key: ") | ||
if password is None | ||
else password | ||
) | ||
console = bittensor.__console__ | ||
with console.status(":key: Decrypting key..."): | ||
# NaCl SecretBox decrypt. | ||
if keyfile_data_is_encrypted_nacl(keyfile_data): | ||
password = bytes(password, "utf-8") | ||
kdf = pwhash.argon2i.kdf | ||
key = kdf( | ||
secret.SecretBox.KEY_SIZE, | ||
password, | ||
NACL_SALT, | ||
opslimit=pwhash.argon2i.OPSLIMIT_SENSITIVE, | ||
memlimit=pwhash.argon2i.MEMLIMIT_SENSITIVE, | ||
) | ||
box = secret.SecretBox(key) | ||
decrypted_keyfile_data = box.decrypt(keyfile_data[len("$NACL") :]) | ||
# Ansible decrypt. | ||
elif keyfile_data_is_encrypted_ansible(keyfile_data): | ||
vault = Vault(password) | ||
try: | ||
finished_password_input = False | ||
while not finished_password_input: | ||
try: | ||
password_input = ( | ||
getpass.getpass("Enter password to unlock key: ") | ||
if password is None | ||
else password | ||
) | ||
console = bittensor.__console__ | ||
with console.status(":key: Decrypting key..."): | ||
# NaCl SecretBox decrypt. | ||
if keyfile_data_is_encrypted_nacl(keyfile_data): | ||
password = bytes(password_input, "utf-8") | ||
kdf = pwhash.argon2i.kdf | ||
key = kdf( | ||
secret.SecretBox.KEY_SIZE, | ||
password, | ||
NACL_SALT, | ||
opslimit=pwhash.argon2i.OPSLIMIT_SENSITIVE, | ||
memlimit=pwhash.argon2i.MEMLIMIT_SENSITIVE, | ||
) | ||
box = secret.SecretBox(key) | ||
decrypted_keyfile_data = box.decrypt(keyfile_data[len("$NACL") :]) | ||
finished_password_input = True | ||
|
||
# Ansible decrypt. | ||
elif keyfile_data_is_encrypted_ansible(keyfile_data): | ||
vault = Vault(password) | ||
decrypted_keyfile_data = vault.load(keyfile_data) | ||
except AnsibleVaultError: | ||
raise bittensor.KeyFileError("Invalid password") | ||
# Legacy decrypt. | ||
elif keyfile_data_is_encrypted_legacy(keyfile_data): | ||
__SALT = ( | ||
b"Iguesscyborgslikemyselfhaveatendencytobeparanoidaboutourorigins" | ||
) | ||
kdf = PBKDF2HMAC( | ||
algorithm=hashes.SHA256(), | ||
salt=__SALT, | ||
length=32, | ||
iterations=10000000, | ||
backend=default_backend(), | ||
) | ||
key = base64.urlsafe_b64encode(kdf.derive(password.encode())) | ||
cipher_suite = Fernet(key) | ||
decrypted_keyfile_data = cipher_suite.decrypt(keyfile_data) | ||
# Unknown. | ||
else: | ||
raise bittensor.KeyFileError( | ||
"keyfile data: {} is corrupt".format(keyfile_data) | ||
) | ||
finished_password_input = True | ||
|
||
# Legacy decrypt. | ||
elif keyfile_data_is_encrypted_legacy(keyfile_data): | ||
__SALT = ( | ||
b"Iguesscyborgslikemyselfhaveatendencytobeparanoidaboutourorigins" | ||
) | ||
kdf = PBKDF2HMAC( | ||
algorithm=hashes.SHA256(), | ||
salt=__SALT, | ||
length=32, | ||
iterations=10000000, | ||
backend=default_backend(), | ||
) | ||
key = base64.urlsafe_b64encode(kdf.derive(password.encode())) | ||
cipher_suite = Fernet(key) | ||
decrypted_keyfile_data = cipher_suite.decrypt(keyfile_data) | ||
finished_password_input = True | ||
# Unknown. | ||
else: | ||
raise bittensor.KeyFileError( | ||
"keyfile data: {} is corrupt".format(keyfile_data) | ||
) | ||
|
||
except (InvalidSignature, InvalidKey, InvalidToken): | ||
raise bittensor.KeyFileError("Invalid password") | ||
except (InvalidSignature, InvalidKey, InvalidToken, AnsibleVaultError): | ||
print("Wrong password, try again"); | ||
password = None | ||
|
||
except Exception as e: | ||
if "Decryption failed. Ciphertext failed verification" in str(e): | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Remove this semicolon |
||
print("Wrong password, try again"); | ||
password = None | ||
else: | ||
raise | ||
|
||
if not isinstance(decrypted_keyfile_data, bytes): | ||
decrypted_keyfile_data = json.dumps(decrypted_keyfile_data).encode() | ||
|
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Remove this semicolon