Skip to content

chore(cloudrun): refactor to sample 'cloudrun_service_to_service_receive' and its test #13292

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

Conversation

eapl-gemugami
Copy link
Contributor

@eapl-gemugami eapl-gemugami commented Apr 7, 2025

Description

Fixes Internal: b/405225642

  • Add region tag as requested in b/405225642#comment6
  • Update dependencies to latest versions
  • Increase coverage of unit tests for anonymous requests and submitting invalid token
  • Remove noxfile_config.py as it was outdated for Python 3.8 and it isn't necessary anymore
  • Apply fixes for latest Style Guide
  • Add type hints
  • Rename variables to be consistent with their values
  • Replace ints for HTTP status with http.HTTPStatus enum
  • Rewrite comments for accuracy, as some were incomplete or misleading

Checklist

…Guide.

- Apply fixes for Style Guide
- Add type hints
- Rename variables to be consistent with their values
- Add HTTP error codes constants to avoid managing 'magic numbers'
- Rewrite comments for accuracy
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Hello @eapl-gemugami, I'm Gemini Code Assist1! I'm currently reviewing this pull request and will post my feedback shortly. In the meantime, here's a summary to help you and other reviewers quickly get up to speed!

Summary of Changes

This pull request focuses on refactoring the 'cloudrun_service_to_service_receive' sample and its associated test. The primary goals are to update dependencies, adhere to the latest style guide, improve code clarity with type hints and consistent naming, and enhance documentation. The noxfile configuration was removed as it was outdated. The changes touch both the application code (app.py, receive.py) and the test suite (receive_test.py), aiming for better maintainability and code quality.

Highlights

  • Dependency Updates: The pull request updates the dependencies in requirements.txt and requirements-test.txt to their latest versions, ensuring compatibility and access to the newest features and security patches. Specifically, pytest was updated from 8.2.0 to 8.3.5, requests from 2.31.0 to 2.32.3, Flask from 3.0.3 to 3.1.0, and Werkzeug from 3.0.3 to 3.1.3.
  • Code Style and Clarity: The code was refactored to align with the latest style guide, including the addition of type hints to improve readability and maintainability. Variable names were also adjusted for consistency. For example, a type hint was added to the main function in app.py on line 25, and to the receive_authorized_get_request function in receive.py on line 27.
  • Documentation Improvements: Comments within the code have been rewritten for accuracy and completeness, ensuring that the purpose and functionality of each section are clearly explained. The docstring for receive_authorized_get_request in receive.py was updated to provide more comprehensive information about the function's return values on lines 35-38.
  • Test Enhancements: The test suite was updated to include HTTP status code constants for better readability and maintainability. The test function test_auth in receive_test.py was renamed to test_authentication_on_cloud_run and updated to use these constants. The services fixture was also refactored and renamed to service.
  • Noxfile Configuration Removal: The noxfile_config.py file was removed because it was outdated for Python 3.8 and no longer necessary for the project's configuration.

Changelog

Click here to see the changelog
  • run/service-auth/app.py
    • Added a return type hint to the main function.
  • run/service-auth/noxfile_config.py
    • Removed the file as it was outdated and unnecessary.
  • run/service-auth/receive.py
    • Added a type hint to the receive_authorized_get_request function.
    • Rewrote the docstring for receive_authorized_get_request for accuracy and completeness.
    • Updated the code to use decoded_token instead of claims when verifying the token.
    • Added flask import.
  • run/service-auth/receive_test.py
    • Added HTTP status code constants for better readability.
    • Refactored the services fixture and renamed it to service.
    • Renamed the test_auth function to test_authentication_on_cloud_run.
    • Updated the test assertions to use the new HTTP status code constants.
    • Added a comment to indicate that the test raises a CalledProcessError exception for a non-zero exit code.
    • Added a comment to indicate that the test cleans up after running.
    • Updated the test to use the PROJECT_ID constant instead of reading the environment variable directly.
  • run/service-auth/requirements-test.txt
    • Updated the version of pytest from 8.2.0 to 8.3.5.
  • run/service-auth/requirements.txt
    • Updated the version of requests from 2.31.0 to 2.32.3.
    • Updated the version of Flask from 3.0.3 to 3.1.0.
    • Updated the version of Werkzeug from 3.0.3 to 3.1.3.
Using Gemini Code Assist

The full guide for Gemini Code Assist can be found on our documentation page, here are some quick tips.

Invoking Gemini

You can request assistance from Gemini at any point in your pull request via creating an issue comment (i.e. comment on the pull request page) using either /gemini <command> or @gemini-code-assist <command>. Below is a summary of the supported commands.

Feature Command Description
Code Review /gemini review Performs a code review for the current pull request in its current state.
Pull Request Summary /gemini summary Provides a summary of the current pull request in its current state.
Comment @gemini-code-assist Responds in comments when explicitly tagged, both in issue comments and review comments.
Help /gemini help Displays a list of available commands.

Customization

To customize Gemini Code Assist for GitHub experience, repository maintainers can create a configuration file and/or provide a custom code review style guide (such as PEP-8 for Python) by creating and adding files to a .gemini/ folder in the base of the repository. Detailed instructions can be found here.

Limitations & Feedback

Gemini Code Assist is currently in preview and may make mistakes. Please leave feedback on any instances where its feedback is incorrect or counter productive. You can react with 👍 and 👎 on @gemini-code-assist comments to provide feedback.

You can also get AI-powered code generation, chat, as well as code reviews directly in the IDE at no cost with the Gemini Code Assist IDE Extension.


A Cloud Run service,
Secured with token's embrace,
Greets users by name.

Footnotes

  1. Review the Privacy Notices, Generative AI Prohibited Use Policy, Terms of Service, and learn how to configure Gemini Code Assist in GitHub here. Gemini can make mistakes, so double check it and use code with caution.

@product-auto-label product-auto-label bot added samples Issues that are directly related to samples. api: run Issues related to the Cloud Run API. labels Apr 7, 2025
Copy link
Contributor

@gemini-code-assist gemini-code-assist bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Code Review

The pull request refactors the 'cloudrun_service_to_service_receive' sample and its test, updating dependencies, removing outdated configurations, applying style guide fixes, adding type hints, renaming variables, adding HTTP error code constants, and rewriting comments. Overall, the changes improve the code's readability, maintainability, and consistency.

Summary of Findings

  • Missing docstring: The service fixture in receive_test.py is missing a docstring, which should be added to explain its purpose.
  • Inconsistent naming: The services fixture was renamed to service, but the test function name test_authentication_on_cloud_run still refers to services in its argument type hint. This should be updated for consistency.

Merge Readiness

The pull request is in good shape and addresses the stated goals. However, addressing the missing docstring and inconsistent naming would further improve the code quality. I am unable to directly approve the pull request, and recommend that others review and approve this code before merging.

@eapl-gemugami eapl-gemugami added the snippet-bot:force-run Force snippet-bot runs its logic label Apr 7, 2025
@snippet-bot snippet-bot bot removed the snippet-bot:force-run Force snippet-bot runs its logic label Apr 7, 2025
Copy link

snippet-bot bot commented Apr 7, 2025

Here is the summary of changes.

You are about to add 1 region tag.

This comment is generated by snippet-bot.
If you find problems with this result, please file an issue at:
https://github.com/googleapis/repo-automation-bots/issues.
To update this comment, add snippet-bot:force-run label or use the checkbox below:

  • Refresh this comment

@eapl-gemugami eapl-gemugami marked this pull request as ready for review April 7, 2025 23:36
@eapl-gemugami eapl-gemugami requested review from a team as code owners April 7, 2025 23:36
@glasnt glasnt closed this in #13293 Apr 8, 2025
@glasnt glasnt reopened this Apr 8, 2025
@glasnt
Copy link
Contributor

glasnt commented Apr 8, 2025

(incorrectly marked this PR as fixed by another, please ignore)

Copy link
Contributor

@glasnt glasnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Initial feedback, mostly around working with 'magic' HTTP values and reducing duplicate code.

PR Review GoogleCloudPlatform#13292 (review)
- Replace HTTP status codes with IntEnum from http.HTTPStatus
- Replace hard coded token with a fixture for a fake token
- Remove duplicated code for the client, and moving it to a test fixture

Also
- Add HTTP codes to the app.py `/` endpoint
- Replace 'UTF-8' with 'utf-8' to follow the official documentation
@eapl-gemugami eapl-gemugami requested a review from glasnt April 8, 2025 17:46
@eapl-gemugami
Copy link
Contributor Author

@glasnt thanks for your review, great insights!
I have applied the requested changes in the latest commit

@eapl-gemugami
Copy link
Contributor Author

eapl-gemugami commented Apr 8, 2025

@glasnt I've just committed the approach of using Bearer i-am-not-a-real-token, and removing the fake JWT token.
I don't think that for this sample we need to cover a huge range of real issues found while validating a token, although I wanted to be a bit more explicit in the sample to cover samples-style-guide/#errors with a simple try/except in receive.py#L50-L54

app.py#L31-L37 (flask view outside the sample) was adjusted to support this case. Validating a Hello might be a little fragile for production, but I think it's enough for this Unit test.

Copy link
Contributor

@glasnt glasnt left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe this is now all correct

@glasnt glasnt merged commit 8b3d0f5 into GoogleCloudPlatform:main Apr 9, 2025
11 checks passed
@eapl-gemugami eapl-gemugami deleted the paradalicea/fix/cloudrun/service_to_service_receive/b-405225642 branch April 9, 2025 14:37
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
api: run Issues related to the Cloud Run API. samples Issues that are directly related to samples.
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants