Skip to content

Conversation

yuryfirebolt
Copy link
Contributor

@yuryfirebolt yuryfirebolt commented Feb 23, 2022

On the first successful authentication, get the token and save it in app_config_dir token file. The token itself is encrypted using the username and password tuple. Hence, the token cannot be accessed, by someone who doesn’t know the username and the password.

During establishing the connection, we check, whether something is written in the app_config_dir token file. If yes, we decrypt it using the username and password. In case of successful decryption, we use this token for connection. If the connection failed, we use username, password as a fallback option.

For the encryption/decryption symmetric encryption Fernet is used with cryptography python library.

Copy link
Collaborator

@stepansergeevitch stepansergeevitch left a comment

Choose a reason for hiding this comment

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

Overall looks good

self._data_dir = user_data_dir(appname="firebolt")
os.makedirs(self._data_dir, exist_ok=True)

self._token_file = os.path.join(self._data_dir, "token.json")
Copy link
Collaborator

Choose a reason for hiding this comment

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

Seems like this implementation only supports caching token for a single set of credentials.
I would suggest changing token.json to something like hash(username + password).json, which would resolve this issue


return self.encrypter.decrypt(res["token"])

def cache_token(self, token: str) -> None:
Copy link
Collaborator

Choose a reason for hiding this comment

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

It could be useful to also store token expiration here. This way we can reduce the amount of requests by skipping expired tokens

)

@staticmethod
def generate_salt() -> str:
Copy link
Collaborator

Choose a reason for hiding this comment

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

nit: separate method

Comment on lines 29 to 32

- name: Type check with mypy
run: |
mypy src
Copy link
Contributor

Choose a reason for hiding this comment

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

I believe mypy is a part of pre-commit now

assert (
token
== TokenSecureStorage(
username="username", password="password"
Copy link
Contributor

Choose a reason for hiding this comment

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

nit: **settings like in others

Comment on lines 44 to 47
if exc.response.status_code == codes.UNAUTHORIZED:
# Do not raise an exception on UNAUTHORIZED
# It should be responsibility of authentication mechanism
return
Copy link
Contributor

Choose a reason for hiding this comment

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

What if the token is expired after auth and before command execution?

with:
extra_args: --all-files

- name: Type check with mypy
Copy link
Collaborator

Choose a reason for hiding this comment

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

Mypy checks are already performed inside pre-commit

Copy link
Contributor

@ptiurin ptiurin left a comment

Choose a reason for hiding this comment

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

LGTM

@sonarqubecloud
Copy link

sonarqubecloud bot commented Mar 4, 2022

Kudos, SonarCloud Quality Gate passed!    Quality Gate passed

Bug A 0 Bugs
Vulnerability A 0 Vulnerabilities
Security Hotspot A 0 Security Hotspots
Code Smell A 1 Code Smell

87.6% 87.6% Coverage
0.0% 0.0% Duplication

Comment on lines +5 to +8
@fixture(autouse=True)
def global_fake_fs() -> None:
with Patcher():
yield
Copy link
Contributor

Choose a reason for hiding this comment

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

To be honest, I preferred explicitly adding fs to the relevant tests. That way there's no hidden functionality, you know which tests are hitting a fake file system and which do not.
But up to you, I'm not too worried about it.

Copy link
Collaborator

Choose a reason for hiding this comment

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

This is required because now any authentication leaves token cache artifact file, which also causes tests to fail

Copy link
Collaborator

Choose a reason for hiding this comment

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

We would need to add fake fs to almost every integration test and to a lot of unit tests

@stepansergeevitch stepansergeevitch merged commit 258bcfa into main Mar 4, 2022
@stepansergeevitch stepansergeevitch deleted the FIR-11540-reuse-token branch March 4, 2022 09:57
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants