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

Unable to import aiomysql while running as uid without account #587

Closed
Nothing4You opened this issue May 30, 2021 · 5 comments · Fixed by #687
Closed

Unable to import aiomysql while running as uid without account #587

Nothing4You opened this issue May 30, 2021 · 5 comments · Fixed by #687
Labels
Milestone

Comments

@Nothing4You
Copy link
Collaborator

Hi,

I'm trying to run a script inside a container.
I don't know the UID the container runs as at build time and I don't want to initialize the container as root, so I can't workaround by creating the user at runtime.
This means that at runtime the UID the script is running as does not have an entry in /etc/passwd.

This is however expected at least in the current master at the time of writing, not sure if there are other places where this is expected in the code:

DEFAULT_USER = getpass.getuser()

Currently when trying to import aiomysql the following exception is raised:

Traceback (most recent call last):
  File "demo.py", line 2, in <module>
    import aiomysql
  File "/usr/local/lib/python3.7/site-packages/aiomysql/__init__.py", line 32, in <module>
    from .connection import Connection, connect
  File "/usr/local/lib/python3.7/site-packages/aiomysql/connection.py", line 45, in <module>
    DEFAULT_USER = getpass.getuser()
  File "/usr/local/lib/python3.7/getpass.py", line 169, in getuser
    return pwd.getpwuid(os.getuid())[0]
KeyError: 'getpwuid(): uid not found: 12345'

To reproduce:

Dockerfile

FROM python:3.7-slim-buster

RUN pip3 install aiomysql

COPY demo.py /app/

WORKDIR /app

ENTRYPOINT ["/usr/bin/env", "python3", "demo.py"]

demo.py

print("Hello World from container")
import aiomysql
  1. build container DOCKER_BUILDKIT=1 docker build -t test-py .
  2. run container as uid 12345 docker run --rm -it --user 12345 test-py
  3. see KeyError
@Nothing4You Nothing4You changed the title Unable to import aiomysql while running as uid Unable to import aiomysql while running as uid without account May 30, 2021
chrisburr added a commit to chrisburr/aiomysql that referenced this issue Aug 4, 2021
@Nothing4You Nothing4You added this to the 1.0.0 milestone Jan 22, 2022
@Nothing4You
Copy link
Collaborator Author

@chrisburr do you want to create a PR from your commit?

@chrisburr
Copy link
Contributor

I'm surprised I hadn't already 😅.

Done in #687

Nothing4You added a commit that referenced this issue Jan 23, 2022
* Allow getpass.getuser() to fail

Fixes #587

* add changelog entry

Co-authored-by: Richard Schwab <gitrichardschwab-7a2qxq42kj@central-intelligence.agency>
@Miguelme
Copy link

Miguelme commented Feb 9, 2022

Hello, I'm running into the same issue for a deployment. How can we include this fix in our code? The latest release I see on pypi is from november

@Nothing4You
Copy link
Collaborator Author

Nothing4You commented Feb 9, 2022

Hi,

I recently took over maintaining aiomysql.
As of right now I can't deploy to pypi yet, so for now I can't push new releases.
I expect it to still be a few more weeks until I have built a deployment process for this and also resolved my access to pypi.

In my personal projects I worked around this by creating a container user in the image, though I do realize that this may not be possible in all scenarios, such as openshift I believe, where it gets randomized.

Other than that, until a new release is out, there's probably only the option to package it yourself and use a local package or vendoring this library.

@chrisburr
Copy link
Contributor

chrisburr commented Feb 9, 2022

On openshift I currently use this as a workaround:

# Workaround for https://github.com/aio-libs/aiomysql/issues/587
getuser = getpass.getuser
getpass.getuser = lambda: "unknown"
import aiomysql  # noqa

getpass.getuser = getuser

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants