Azure / azure-functions-core-tools

Command line tools for Azure Functions
MIT License
1.33k stars 437 forks source link

Creating Python HTTP template generates wrong value for --authlevel 'anonymous' #3779

Open yonahcitron opened 3 months ago

yonahcitron commented 3 months ago

Version

4.0.5907

Description

Problem

Create a new python function app, and add an http trigger template passing --authlevel anonymous or --authlevel ANONYMOUS.

This template generates the incorrect auth_level constant in the decorator: @app.route(route="MyHttpFunction", auth_level=func.AuthLevel.Anonymous)

It should be upper-case: @app.route(route="MyHttpFunction", auth_level=func.AuthLevel.ANONYMOUS)

When you run the buggy template, it will give you: [2024-07-30T13:23:33.803Z] Exception: AttributeError: Anonymous

NOTE

This bug only occurs when passing the parameters explicitly.

If you run it without the flag, i.e.: func new --template "HTTP trigger" --name MyHttpFunction

It will prompt you:

Select a number for Auth Level:
1. FUNCTION
2. ANONYMOUS
3. ADMIN

And the resulting generated template will have the correct constant value.

Steps to reproduce

Code to Execute

mkdir test_function && cd test_function

func init . --python

func new --template "HTTP trigger" --name MyHttpFunction --authlevel anonymous

func start

Exception Stack Trace


[2024-07-30T13:23:33.803Z] Worker failed to index functions
[2024-07-30T13:23:33.803Z] Result: Failure
[2024-07-30T13:23:33.803Z] Exception: AttributeError: Anonymous
[2024-07-30T13:23:33.803Z] Stack:   File "/opt/homebrew/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.10/OSX/Arm64/azure_functions_worker/dispatcher.py", line 413, in _handle__functions_metadata_request
[2024-07-30T13:23:33.803Z]     self.load_function_metadata(
[2024-07-30T13:23:33.803Z]   File "/opt/homebrew/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.10/OSX/Arm64/azure_functions_worker/dispatcher.py", line 393, in load_function_metadata
[2024-07-30T13:23:33.803Z]     self.index_functions(function_path, function_app_directory)) \
[2024-07-30T13:23:33.803Z]   File "/opt/homebrew/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.10/OSX/Arm64/azure_functions_worker/dispatcher.py", line 765, in index_functions
[2024-07-30T13:23:33.803Z]     indexed_functions = loader.index_function_app(function_path)
[2024-07-30T13:23:33.803Z]   File "/opt/homebrew/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.10/OSX/Arm64/azure_functions_worker/utils/wrappers.py", line 44, in call
[2024-07-30T13:23:33.803Z]     return func(*args, **kwargs)
[2024-07-30T13:23:33.803Z]   File "/opt/homebrew/lib/node_modules/azure-functions-core-tools/bin/workers/python/3.10/OSX/Arm64/azure_functions_worker/loader.py", line 238, in index_function_app
[2024-07-30T13:23:33.803Z]     imported_module = importlib.import_module(module_name)
[2024-07-30T13:23:33.804Z]   File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/importlib/__init__.py", line 126, in import_module
[2024-07-30T13:23:33.804Z]     return _bootstrap._gcd_import(name[level:], package, level)
[2024-07-30T13:23:33.804Z]   File "<frozen importlib._bootstrap>", line 1050, in _gcd_import
[2024-07-30T13:23:33.804Z]   File "<frozen importlib._bootstrap>", line 1027, in _find_and_load
[2024-07-30T13:23:33.804Z]   File "<frozen importlib._bootstrap>", line 1006, in _find_and_load_unlocked
[2024-07-30T13:23:33.804Z]   File "<frozen importlib._bootstrap>", line 688, in _load_unlocked
[2024-07-30T13:23:33.804Z]   File "<frozen importlib._bootstrap_external>", line 883, in exec_module
[2024-07-30T13:23:33.804Z]   File "<frozen importlib._bootstrap>", line 241, in _call_with_frames_removed
[2024-07-30T13:23:33.804Z]   File "/Users/yonah.citron/Downloads/test_function/function_app.py", line 8, in <module>
[2024-07-30T13:23:33.804Z]     @app.route(route="MyHttpFunction", auth_level=func.AuthLevel.Anonymous)
[2024-07-30T13:23:33.804Z]   File "/Library/Frameworks/Python.framework/Versions/3.10/lib/python3.10/enum.py", line 437, in __getattr__
[2024-07-30T13:23:33.804Z]     raise AttributeError(name) from None
[2024-07-30T13:23:33.804Z] .
[2024-07-30T13:23:34.287Z] No job functions found. Try making your job classes and methods public. If you're using binding extensions (e.g. Azure Storage, ServiceBus, Timers, etc.) make sure you've called the registration method for the extension(s) in your startup code (e.g. builder.AddAzureStorage(), builder.AddServiceBus(), builder.AddTimers(), etc.).
kshyju commented 3 months ago

Tagging @shreyabatra4 to route the issue to the appropriate team.

aaglietti-itsrizzoli commented 1 month ago

Same here @4.0.6280, lost 2h today during a lesson with students.

FYI: official docs shows WRONG command https://learn.microsoft.com/en-us/azure/azure-functions/create-first-function-cli-python?tabs=linux%2Cbash%2Cazure-cli%2Cbrowser#create-a-local-function

Image

julicjung commented 3 weeks ago

Same here, lost a couple of hours and figured out by comparing painfully and step by step. Please do also fix the docs.

yonahcitron commented 2 weeks ago

Created a tiny PR that should fix this, here.

Incidentally @aaglietti-itsrizzoli , the lower-case 'anonymous' arg given the documentation should work fine. The user's string isn't passed as a literal, it's first put into an enum