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

✨ Allow login with new API key #840

Merged
merged 15 commits into from
Sep 4, 2024
Merged

✨ Allow login with new API key #840

merged 15 commits into from
Sep 4, 2024

Conversation

Koncopd
Copy link
Member

@Koncopd Koncopd commented Sep 3, 2024

No description provided.

@falexwolf falexwolf changed the title ✨ Allow login with API token ✨ Allow login with API key Sep 3, 2024
Copy link

github-actions bot commented Sep 3, 2024

@github-actions github-actions bot temporarily deployed to pull request September 3, 2024 15:20 Inactive
Copy link

codecov bot commented Sep 3, 2024

Codecov Report

Attention: Patch coverage is 93.42105% with 5 lines in your changes missing coverage. Please review.

Project coverage is 83.41%. Comparing base (bc17873) to head (d164dd3).

Files with missing lines Patch % Lines
lamindb_setup/_setup_user.py 90.62% 3 Missing ⚠️
lamindb_setup/core/_hub_core.py 92.85% 2 Missing ⚠️
Additional details and impacted files
@@            Coverage Diff             @@
##             main     #840      +/-   ##
==========================================
+ Coverage   83.32%   83.41%   +0.09%     
==========================================
  Files          42       42              
  Lines        3250     3299      +49     
==========================================
+ Hits         2708     2752      +44     
- Misses        542      547       +5     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

@github-actions github-actions bot temporarily deployed to pull request September 3, 2024 15:37 Inactive
@falexwolf falexwolf marked this pull request as draft September 3, 2024 15:59
@falexwolf falexwolf changed the title ✨ Allow login with API key ✨ Allow login with new API key, deprecate existing API key Sep 3, 2024
@github-actions github-actions bot temporarily deployed to pull request September 3, 2024 16:24 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 3, 2024 18:08 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 3, 2024 18:28 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 4, 2024 08:49 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 4, 2024 09:13 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 4, 2024 09:39 Inactive
@Koncopd Koncopd marked this pull request as ready for review September 4, 2024 09:40
@Koncopd
Copy link
Member Author

Koncopd commented Sep 4, 2024

Ok, this adds
login(api_key="some_api_key")
Loads a user with API key and allows to renew an access token with the stored API key.

@Koncopd
Copy link
Member Author

Koncopd commented Sep 4, 2024

@falexwolf are you fine with this?

@falexwolf
Copy link
Member

This looks great except that I don't fully get the .env file management. 🤔

The main issue is that his will raise an error for everybody when it goes live. We have the same issue for the instance env file where we'll add the api_url.

I think it'd be worthwhile to no longer use pydantic to validate the env file but instead just open the text file and parse it so that doesn't destroy everybody's workflow.

@Koncopd
Copy link
Member Author

Koncopd commented Sep 4, 2024

I will try to fix this behavior here.

@falexwolf
Copy link
Member

We're already not using pydantic to write settings.

So, you should be able to create a read_settings function similar to the below and remove pydantic from the dependencies here.

def save_settings(
settings: Any,
settings_file: Path,
type_hints: dict[str, Any],
prefix: str,
):
with open(settings_file, "w") as f:
for store_key, type in type_hints.items():
if type == Optional[str]:
type = str
if type == Optional[bool]:
type = bool
if "__" not in store_key:
if store_key == "model_config":
continue
if store_key == "storage_root":
value = settings.storage.root_as_str
elif store_key == "storage_region":
value = settings.storage.region
else:
if store_key in {"db", "schema_str", "name_", "uuid", "id"}:
settings_key = f"_{store_key.rstrip('_')}"
else:
settings_key = store_key
value = getattr(settings, settings_key)
if value is None:
value = "null"
elif isinstance(value, UUID):
value = value.hex
else:
value = type(value)
f.write(f"{prefix}{store_key}={value}\n")

@@ -65,6 +65,7 @@ class UserSettingsStore(BaseSettings):
email: str
password: str
access_token: str
api_key: Optional[str] = None
Copy link
Member Author

Choose a reason for hiding this comment

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

@falexwolf this is actually enough to avoid the validation error.
Do you think the rewrite of settings load is still needed? It is not hard to do, especially via dotenv_values like here, i just think it is reasonable to offload loading and validation logic to pydantic-settings.

Copy link
Member

Choose a reason for hiding this comment

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

Ok! If users don't run into errors that's alright!

@github-actions github-actions bot temporarily deployed to pull request September 4, 2024 13:17 Inactive
@falexwolf falexwolf changed the title ✨ Allow login with new API key, deprecate existing API key ✨ Allow login with new API key Sep 4, 2024
@falexwolf
Copy link
Member

Another thing: This should be aware of an environement variable "LAMIN_API_KEY" which then allows to bypass things.

Exactly like wandb do it: https://docs.lamin.ai/wandb

@Koncopd
Copy link
Member Author

Koncopd commented Sep 4, 2024

I thought that "LAMIN_API_KEY" should be for the CLI. Should ln_setup.login() try to read the env variable also?

) -> None:
"""Log in user.

Args:
user: handle or email
key: API key
key: permanent API key
api_key: new API key
Copy link
Member Author

Choose a reason for hiding this comment

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

I would describe it like this. I don't want to describe the old key as "legacy" while the new approach is still tested.

Copy link
Member

Choose a reason for hiding this comment

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

Agreed! You can call the new key "Beta API key" and the old key "API key".

Copy link
Member

Choose a reason for hiding this comment

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

And at some point we switch it to "Legacy API key" and "API key".

@github-actions github-actions bot temporarily deployed to pull request September 4, 2024 15:55 Inactive
@falexwolf
Copy link
Member

I thought that "LAMIN_API_KEY" should be for the CLI. Should ln_setup.login() try to read the env variable also?

Yes. The CLI shouldn't add additional logic. If people call ln.setup.login() without args this should behave in the same way as lamin login.

@falexwolf
Copy link
Member

Well I agree that the CLI should be responsible for the logic around the input() prompt.

@Koncopd
Copy link
Member Author

Koncopd commented Sep 4, 2024

Yes, but CLI should still check if "LAMIN_API_KEY" is present to avoid calling input.

email, handle = user, None
if user is None and api_key is None:
if "LAMIN_API_KEY" in os.environ:
api_key = os.environ["LAMIN_API_KEY"]
Copy link
Member Author

Choose a reason for hiding this comment

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

@falexwolf added the logic for "LAMIN_API_KEY"

Copy link
Member

Choose a reason for hiding this comment

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

Great!

Copy link
Member Author

Choose a reason for hiding this comment

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

Ok, merging this then.

@github-actions github-actions bot temporarily deployed to pull request September 4, 2024 16:26 Inactive
@github-actions github-actions bot temporarily deployed to pull request September 4, 2024 17:17 Inactive
@Koncopd Koncopd merged commit 046f9e5 into main Sep 4, 2024
13 checks passed
@Koncopd Koncopd deleted the api_token branch September 4, 2024 17:20
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.

2 participants