Stop using global state to store configuration options

Aberta
#159 0 comentários 0 reações 0 responsáveis Ver no GitHub

Ninguém assumiu esta issue ainda.

  • #309 de @rudi193-cmd — fechado sem integrar

Avaliação

Dificuldade
5/5
Tempo estimado
Mais de uma semana
Facilidade para iniciantes
25/100
Tipo de issue
Refatoração
Clareza
Precisa de esclarecimento
Status de atividade
Estagnada
Stack de tecnologia
django, python
Domínio
backend, security

Direção de pesquisa

Comece lendo codejail.jail_code.COMMANDS e configure; em seguida, inspecione o middleware de codejail.django_integration que preenche a configuração. Decida e implemente um design de configuração não global, com configuração repetida independente da ordem dos testes unitários e comportamento consistente entre processos Python.

Escrita pelo modelo de indexação a partir do texto da issue.

Descrição

code health

Codejail has a global variable at codejail.jail_code.COMMANDS. It's a dictionary that maps command names (like "python") to safe shell commands. An example value is:

{'python': {'cmdline_start': ['/edx/app/edxapp/venvs/edxapp-sandbox/bin/python', '-E', '-B'], 'user': 'sandbox'}}

The dictionary is populated by calling codejail.jail_code.configure, which is generally done via the codejail.django_integration middleware.

This all works fine, but the fact that COMMANDS is a global variable is not idiomatic Python. More concretely, it's a pain in the neck for unit tests: the order and timing with which unit tests are run will affect whether .configure has been called or not. This could also cause problems across Python processes in production, leading to situations where codejail is configured in one process but effectively disabled in another process without anyone knowing. Not good.

This stateful system also forces us to use a middleware in order to configure codejail in Django--that's another layer where something could go wrong.

I recommend replacing COMMANDS with either:

  • a stateless function named load_commands(), which is safe to call over and over again, which does something like this:
     try:
        from django.conf import settings
     except ImportError, AttributeError
         settings = {}
      if settings.get('CODE_JAIL'):
          return _load_command_from_django_settings(settings)
      else:
          # .... load alternative/fallback configuration options, if we're not using Django
    
  • putting the entire Codejail interface behind a class, so clients would need to pass the settings into it, like this:
    from django.conf import settings
    codejail = CodeJail(some_setting=settings.CODEJAIL[...], etc..)
    codejail.safe_exec(...)
    
Linguagem predominante
Python
Estrelas
477
Forks
83
Merge médio
1d 8h
PRs com merge (30d)
6

Preparar o ambiente

Primeiros passos

  1. Leia a issue inteira e depois o guia de contribuição do projeto.
  2. Comente na issue dizendo que vai assumir — evita que duas pessoas façam o mesmo trabalho.
  3. Faça um fork do repositório e trabalhe em uma branch.
  4. Abra um pull request que referencie o número da issue.

Mais de openedx/codejail

Todas as issues de openedx/codejail

Issues semelhantes

Mais issues de Python

Receba novas issues na sua caixa de entrada

Um resumo curto de issues do GitHub para quem está começando.