Skip to content

Django management command with quoted argument #2139

Description

@ipmb

Context

I'm trying to run:

zappa manage production 'shell -c "from django.contrib.auth import get_user_model; get_user_model().objects.create_superuser(username=\"admin\", password=\"changeme\")"'

Expected Behavior

It will invoke a command that is equivalent to:

manage.py shell -c "from django.contrib.auth import get_user_model; get_user_model().objects.create_superuser(username=\"admin\", password=\"changeme\")"

Actual Behavior

[START] RequestId: edf0eeda-879e-41ca-8595-e51a0adaee17 Version: $LATEST
[ERROR] CommandError: Error: unrecognized arguments: django.contrib.auth import get_user_model; get_user_model().objects.create_superuser(username="admin", password="changeme")\'
Traceback (most recent call last):
  File "/tmp/project/sentry_sdk/integrations/aws_lambda.py", line 57, in sentry_handler
    reraise(*exc_info)
  File "/tmp/project/sentry_sdk/_compat.py", line 57, in reraise
    raise value
  File "/tmp/project/sentry_sdk/integrations/aws_lambda.py", line 48, in sentry_handler
    return handler(event, context, *args, **kwargs)
  File "/var/task/handler.py", line 609, in lambda_handler
    return LambdaHandler.lambda_handler(event, context)
  File "/var/task/handler.py", line 243, in lambda_handler
    return handler.handler(event, context)
  File "/var/task/handler.py", line 408, in handler
    management.call_command(*event['manage'].split(' '))
  File "/tmp/project/django/core/management/__init__.py", line 147, in call_command
    defaults = parser.parse_args(args=parse_args)
  File "/tmp/project/django/core/management/base.py", line 55, in parse_args
    return super().parse_args(args, namespace)
  File "/var/lang/lib/python3.7/argparse.py", line 1758, in parse_args
    self.error(msg % ' '.join(argv))
  File "/tmp/project/django/core/management/base.py", line 61, in error
    raise CommandError("Error: %s" % message)

Possible Fix

I suspect the issue is here:

management.call_command(*event['manage'].split(' '))

The code is simply splitting arguments by spaces where shlex.split might be more appropriate.

>>> manage = 'shell -c "from django.contrib.auth import get_user_model; get_user_model().objects.create_superuser(username=\\"admin\\", password=\\"changeme\\")"'
>>> manage.split(" ")
['shell', '-c', '"from', 'django.contrib.auth', 'import', 'get_user_model;', 'get_user_model().objects.create_superuser(username=\\"admin\\",', 'password=\\"changeme\\")"']
>>> import shlex
>>> shlex.split(manage)
['shell', '-c', 'from django.contrib.auth import get_user_model; get_user_model().objects.create_superuser(username="admin", password="changeme")']

Your Environment

  • Zappa version used: 0.51.0

  • Operating System and Python version: MacOS Python3.7

  • The output of pip freeze:

    appnope==0.1.0
    argcomplete==1.12.0
    asgiref==3.2.10
    backcall==0.2.0
    boto==2.39.0
    boto3==1.14.25
    botocore==1.17.25
    certifi==2020.6.20
    cffi==1.14.0
    cfn-flip==1.2.3
    chardet==3.0.4
    click==7.1.2
    cryptography==3.0
    decorator==4.4.2
    dj-database-url==0.5.0
    Django==3.0.8
    django-storages==1.9.1
    docutils==0.15.2
    durationpy==0.5
    filechunkio==1.6
    future==0.18.2
    futures==3.0.4
    goodconf==1.0.0
    hjson==3.0.1
    idna==2.10
    importlib-metadata==1.7.0
    ipykernel==5.3.3
    ipython==7.16.1
    ipython-genutils==0.2.0
    jedi==0.17.2
    jmespath==0.10.0
    jupyter-client==6.1.6
    jupyter-core==4.6.3
    kappa==0.6.0
    lxml==4.5.2
    nose==1.3.7
    ntlm-auth==1.5.0
    numpy==1.19.1
    pandas==1.0.5
    parso==0.7.0
    pexpect==4.8.0
    pickleshare==0.7.5
    pip-tools==5.2.1
    placebo==0.9.0
    prompt-toolkit==3.0.5
    psycopg2-binary==2.8.5
    ptyprocess==0.6.0
    pycparser==2.20
    Pygments==2.6.1
    python-dateutil==2.6.1
    python-slugify==4.0.1
    pytz==2020.1
    PyYAML==5.3.1
    pyzmq==19.0.1
    requests==2.24.0
    requests-ntlm==1.1.0
    requests-toolbelt==0.9.1
    ruamel.yaml==0.16.10
    ruamel.yaml.clib==0.2.0
    s3transfer==0.3.3
    sentry-sdk==0.16.1
    SharePlum==0.5.1
    six==1.15.0
    sqlparse==0.3.1
    text-unidecode==1.3
    toml==0.10.1
    tornado==6.0.4
    tqdm==4.48.0
    traitlets==4.3.3
    troposphere==2.6.2
    urllib3==1.25.9
    wcwidth==0.2.5
    Werkzeug==0.16.1
    whitenoise==5.1.0
    wsgi-request-logger==0.4.6
    xlrd==1.2.0
    zappa==0.51.0
    zipp==3.1.0
    
  • Link to your project (optional):

  • Your zappa_settings.json:

    {
        "production": {
            "aws_region": "us-west-2",
            "django_settings": "project.settings",
            "exclude": ["project.yml"],
            "project_name": "project",
            "runtime": "python3.7",
            "s3_bucket": "project",
            "manage_roles": false,
            "keep_warm": false,
            "role_arn": "arn:aws:iam::xxxx:role/service-role/project-lambda-service",
            "route53_enabled": false,
            "domain": "project.example.com",
            "async_resources": false,
            "apigateway_enabled": false,
            "lambda_description": "my project",
            "slim_handler": true,
            "memory_size": 256,
            "timeout_seconds": 300,
            "log_level": "INFO"
        }
    }
    ```

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions