-
Notifications
You must be signed in to change notification settings - Fork 7
User context manager #39
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
base: main
Are you sure you want to change the base?
Conversation
Signed-off-by: Esteban Aguililla Klein <esteban.aguililla.klein.pro@outlook.com>
Signed-off-by: Esteban Aguililla Klein <esteban.aguililla.klein.pro@outlook.com>
apaolillo
left a comment
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.
Thanks @EstAK . It is a valuable feature. However, my overall feeling is that this PR does too many changes at once. It should probably split the user manager from the context manager, and the scala example could also come later (you may do a smaller example to validate the changes). Also is this change breaking the API? We can further discuss this.
| tailored specifically for Docker environments. | ||
| """ | ||
|
|
||
| from __future__ import annotations |
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.
why?
| """ | ||
|
|
||
| def __init__( | ||
| self, partial_docker_builder: PartialDockerBuilder, on_behalf: str, return_to: str |
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.
| self, partial_docker_builder: PartialDockerBuilder, on_behalf: str, return_to: str | |
| self, partial_docker_builder: PartialDockerBuilder, on_behalf: str, return_to: str, |
and apply format (style.sh)
| """ | ||
| A class that implements a user context manager where within said context, all actions will be | ||
| done on behalf of a chosen user. | ||
| """ |
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.
can this be nested?
| self.partial_docker_builder.user(name=self.on_behalf) | ||
| return self.partial_docker_builder | ||
|
|
||
| def __exit__(self, exc_type, exc_value, traceback): |
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.
| def __exit__(self, exc_type, exc_value, traceback): | |
| def __exit__(self, exc_type, exc_value, traceback) -> None: |
|
|
||
| def manage_user(self, username: str) -> None: | ||
| """ | ||
| Manages the user `username` through the `UserManager` |
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.
I don't understand what does that mean.
| """ | ||
| if not name: | ||
| name = "${USER_NAME}" # fallback user | ||
| elif check and (name not in self.managed_users): |
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.
not sure why this check is necessary
| partial_docker_builder._build_commands.append(StrDockerBuildCommand(cmd)) | ||
|
|
||
| def as_user( | ||
| self, partial_docker_builder: PartialDockerBuilder, username: str |
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.
| self, partial_docker_builder: PartialDockerBuilder, username: str | |
| self, partial_docker_builder: PartialDockerBuilder, username: str, |
| return_to: str = self.current_user | ||
| self.current_user = username | ||
|
|
||
| return UserContext(partial_docker_builder, on_behalf=self.current_user, return_to=return_to) |
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.
| return UserContext(partial_docker_builder, on_behalf=self.current_user, return_to=return_to) | |
| return UserContext(partial_docker_builder, on_behalf=self.current_user, return_to=return_to,) |
| Initializes a PartialDockerBuilder with an empty list of build commands. | ||
| """ | ||
| self._build_commands: List[DockerBuildCommand] = [] | ||
| self.user_manager: UserManager | None = None |
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.
this means we maintain in the builder all the users that were created?
src/pythainer/builders/__init__.py
Outdated
| command = " && \\\n ".join(commands) | ||
| self.run(command=command) | ||
|
|
||
| def user(self, name: str = "") -> None: |
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.
I'd like to keep the feature as before as well. Sometimes you don't want to return you just want a one way USER change.
|
as discussed: we split into more incremental feature set. |
Features
UserManagerMotivation
Instead of trusting the user to correctly scope where a user should be enable : encapsulate it. No need to use constructs like anymore :
now, you can use
at the end of the context manager you are ensured to return to the user that was used before entering it :