-
Notifications
You must be signed in to change notification settings - Fork 49
LCORE-598: Add authorization e2e tests #636
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
Open
omertuc
wants to merge
1
commit into
lightspeed-core:main
Choose a base branch
from
omertuc:authe2e
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Some comments aren't visible on the classic Files Changed page.
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -188,3 +188,6 @@ dev/ | |
|
|
||
| # Database files | ||
| *.sqlite | ||
|
|
||
| # A file sometimes leftover by the e2e tests | ||
| lightspeed-stack.yaml.backup | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,52 @@ | ||
| name: Lightspeed Core Service (LCS) | ||
| service: | ||
| host: 0.0.0.0 | ||
| port: 8080 | ||
| auth_enabled: false | ||
| workers: 1 | ||
| color_log: true | ||
| access_log: true | ||
| llama_stack: | ||
| # Uses a remote llama-stack service | ||
| # The instance would have already been started with a llama-stack-run.yaml file | ||
| use_as_library_client: false | ||
| # Alternative for "as library use" | ||
| # use_as_library_client: true | ||
| # library_client_config_path: <path-to-llama-stack-run.yaml-file> | ||
| url: http://llama-stack:8321 | ||
| api_key: xyzzy | ||
| user_data_collection: | ||
| feedback_enabled: true | ||
| feedback_storage: "/tmp/data/feedback" | ||
| transcripts_enabled: true | ||
| transcripts_storage: "/tmp/data/transcripts" | ||
|
|
||
| authentication: | ||
| module: "jwk-token" | ||
| jwk_config: | ||
| url: "http://test-jwk-server:16161/jwk.json" | ||
| jwt_configuration: | ||
| user_id_claim: "user_id" | ||
| username_claim: "username" | ||
| role_rules: | ||
| - jsonpath: "$.roles[*]" | ||
| operator: "contains" | ||
| value: "admin" | ||
| roles: ["administrator"] | ||
| - jsonpath: "$.roles[*]" | ||
| operator: "contains" | ||
| value: "config" | ||
| roles: ["config"] | ||
| - jsonpath: "$.roles[*]" | ||
| operator: "contains" | ||
| value: "readonly" | ||
| roles: ["readonly"] | ||
|
|
||
| authorization: | ||
| access_rules: | ||
| - role: "administrator" | ||
| actions: ["admin"] | ||
| - role: "config" | ||
| actions: ["get_config", "info"] | ||
| - role: "readonly" | ||
| actions: ["info"] |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| # Ignore JWKs created for e2e tests | ||
| *.json |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| @JWKAuth | ||
| Feature: JWK authorization enforcement | ||
|
|
||
| Background: | ||
| Given The service is started locally | ||
| And REST API service hostname is localhost | ||
| And REST API service port is 8080 | ||
| And REST API service prefix is /v1 | ||
|
|
||
| Scenario: A user with the admin role can access the info endpoint | ||
| Given I have a valid JWT token with the admin role | ||
| When I access REST API endpoint "info" using HTTP GET method | ||
| Then The status code of the response is 200 | ||
|
|
||
| Scenario: A user with the admin role can access the config endpoint | ||
| Given I have a valid JWT token with the admin role | ||
| When I access REST API endpoint "config" using HTTP GET method | ||
| Then The status code of the response is 200 | ||
|
|
||
| Scenario: A user with the config role can access the config endpoint | ||
| Given I have a valid JWT token with the config role | ||
| When I access REST API endpoint "config" using HTTP GET method | ||
| Then The status code of the response is 200 | ||
|
|
||
| Scenario: A user with the config role can access the info endpoint | ||
| Given I have a valid JWT token with the config role | ||
| When I access REST API endpoint "info" using HTTP GET method | ||
| Then The status code of the response is 200 | ||
|
|
||
| Scenario: A user with the readonly role can access the info endpoint | ||
| Given I have a valid JWT token with the readonly role | ||
| When I access REST API endpoint "info" using HTTP GET method | ||
| Then The status code of the response is 200 | ||
|
|
||
| Scenario: A user with the readonly role can't access the config endpoint | ||
| Given I have a valid JWT token with the readonly role | ||
| When I access REST API endpoint "config" using HTTP GET method | ||
| Then The status code of the response is 403 | ||
| And The body of the response contains Insufficient permissions |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| """JWK auth steps - reusing unit test primitives.""" | ||
|
|
||
| from pathlib import Path | ||
| import sys | ||
|
|
||
| from behave import given # pyright: ignore[reportAttributeAccessIssue] | ||
| from behave.runner import Context | ||
|
|
||
|
|
||
| from authlib.jose import JsonWebToken | ||
|
|
||
| sys.path.append(str(Path(__file__).resolve().parents[4])) | ||
|
|
||
| # Import at runtime to avoid module load issues | ||
| from tests.unit.authentication.test_jwk_token import ( | ||
| create_token_header, | ||
| create_token_payload, | ||
| ) | ||
|
|
||
|
|
||
| @given("I have a valid JWT token with the {role} role") | ||
| def create_role_token(context: Context, role: str) -> None: | ||
| """Create token with role using the shared test key.""" | ||
| test_key = context.test_key | ||
|
|
||
| header = create_token_header(test_key["kid"]) | ||
| payload = create_token_payload() | ||
|
|
||
| # This works thanks to the definitions in lightspeed-stack-auth-jwk.yaml | ||
| payload["roles"] = [role] # Add role to existing payload | ||
|
|
||
| token = ( | ||
| JsonWebToken(algorithms=["RS256"]) | ||
| .encode(header, payload, test_key["private_key"]) | ||
| .decode() | ||
| ) | ||
|
|
||
| if not hasattr(context, "auth_headers"): | ||
| context.auth_headers = {} | ||
|
|
||
| context.auth_headers["Authorization"] = f"Bearer {token}" | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
🛠️ Refactor suggestion | 🟠 Major
Avoid
sys.path.appendfor imports.Manipulating
sys.pathat runtime is fragile and pollutes the global import state. Instead, structure your imports using relative imports or ensure the package is properly installed.Apply this diff to use a relative import:
If this approach causes import errors, verify that the test runner is invoked from the repository root or that the package structure includes proper
__init__.pyfiles.📝 Committable suggestion
🤖 Prompt for AI Agents