Skip to content
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

fix: Use yt01 token generator environment for k6 tests running on yt01 #1391

Merged
merged 1 commit into from
Nov 5, 2024

Conversation

elsand
Copy link
Member

@elsand elsand commented Nov 5, 2024

Description

This updates the token generator to use the yt01 token generator when running against the yt01.

Related Issue(s)

  • N/A

Verification

  • Your code builds clean without any errors or warnings
  • Manual testing done (required)

Summary by CodeRabbit

  • New Features

    • Introduced a new environment variable for dynamic token generation based on the API environment.
    • Enhanced token generation functionality to use the new environment variable in URL construction.
  • Bug Fixes

    • Improved logging for token generation to provide clearer context about the environment used.
  • Documentation

    • Updated comments to reflect the changes in token generation logic and environment handling.

@elsand elsand requested a review from a team as a code owner November 5, 2024 13:36
Copy link
Contributor

coderabbitai bot commented Nov 5, 2024

📝 Walkthrough

Walkthrough

The changes introduce a new constant tokenGeneratorEnv in tests/k6/common/config.js, which is assigned based on the __ENV.API_ENVIRONMENT variable. If the environment is "yt01", it sets tokenGeneratorEnv to "yt01"; otherwise, it defaults to "tt02". This constant is then utilized in tests/k6/common/token.js to replace hardcoded environment values in token generation URLs, enhancing the flexibility of the token fetching mechanism while maintaining existing logic and error handling.

Changes

File Path Change Summary
tests/k6/common/config.js Added constant tokenGeneratorEnv based on __ENV.API_ENVIRONMENT.
tests/k6/common/token.js Imported tokenGeneratorEnv and updated URLs in getServiceOwnerTokenFromGenerator and getEnduserTokenFromGenerator to use this new variable. Logging messages updated to reflect tokenGeneratorEnv.

Possibly related PRs

Suggested reviewers

  • knuhau

Thank you for using CodeRabbit. We offer it for free to the OSS community and would appreciate your support in helping us grow. If you find it useful, would you consider giving us a shout-out on your favorite social media?

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Generate unit testing code for this file.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai generate unit testing code for this file.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@elsand elsand changed the title Use yt01 token generator environment for k6 tests running on yt01 fix: Use yt01 token generator environment for k6 tests running on yt01 Nov 5, 2024
Copy link

sonarqubecloud bot commented Nov 5, 2024

Copy link
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

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

Actionable comments posted: 1

🧹 Outside diff range and nitpick comments (2)
tests/k6/common/config.js (1)

44-44: Enhance comment clarity for token generator environment selection.

The implementation correctly handles the token generator environment selection. However, the comment could be more descriptive about the purpose and implications of this configuration.

Consider updating the comment to be more explicit:

-export const tokenGeneratorEnv = __ENV.API_ENVIRONMENT == "yt01" ? "yt01" : "tt02"; // yt01 is the only environment that has a separate token generator environment
+export const tokenGeneratorEnv = __ENV.API_ENVIRONMENT == "yt01" ? "yt01" : "tt02"; // Use yt01's dedicated token generator for yt01 environment, default to tt02 for all other environments
tests/k6/common/token.js (1)

60-61: Add validation for tokenGeneratorEnv value

Consider adding validation to ensure tokenGeneratorEnv contains a valid value before using it in URL construction. This would prevent potential issues with invalid environments.

+ const validEnvironments = ['tt02', 'yt01'];
+ if (!validEnvironments.includes(tokenGeneratorEnv)) {
+   throw new Error(`Invalid token generator environment: ${tokenGeneratorEnv}`);
+ }

Also applies to: 66-67

📜 Review details

Configuration used: .coderabbit.yaml
Review profile: CHILL

📥 Commits

Reviewing files that changed from the base of the PR and between c887542 and a726c6f.

📒 Files selected for processing (2)
  • tests/k6/common/config.js (1 hunks)
  • tests/k6/common/token.js (2 hunks)
🔇 Additional comments (3)
tests/k6/common/config.js (1)

44-44: Verify token generator environment usage across the codebase.

Let's ensure this change is consistently applied across all token-related functionality.

✅ Verification successful

Let me check the actual implementation and usage of this token generator environment configuration.


Token generator environment configuration is correctly implemented

The verification shows that:

  1. token.js correctly uses the imported tokenGeneratorEnv constant from config.js in both token generation functions (getServiceOwnerTokenFromGenerator and getEnduserTokenFromGenerator).
  2. The shell script generate_tokens.sh handles environment mapping separately for its specific use case (batch token generation) and doesn't need to use the tokenGeneratorEnv constant as it's not part of the k6 test execution context.
  3. No other files in the codebase require similar environment-specific token generator logic as the token generation is centralized in these two files.
🏁 Scripts executed

The following scripts were executed for the analysis:

Script:

#!/bin/bash
# Description: Check for any other files that might need to use tokenGeneratorEnv
# and verify the usage in token.js

# Check for any files containing token generator URLs or environment references
echo "Checking for files with token generator references:"
rg -l "token.*generator|maskinporten|idporten" tests/

# Check for specific environment strings that might need to use tokenGeneratorEnv
echo -e "\nChecking for hardcoded environment references:"
rg -l "tt02|yt01" tests/

# Look for potential token-related configuration
echo -e "\nChecking for other token-related configuration:"
rg -l "getToken|fetchToken|token.*config" tests/

Length of output: 897


Script:

#!/bin/bash
# Check the implementation in token.js
echo "Content of token.js:"
cat tests/k6/common/token.js

# Check the usage in generate_tokens.sh
echo -e "\nContent of generate_tokens.sh:"
cat tests/k6/tests/scripts/generate_tokens.sh

# Check for any direct usage of tt02/yt01 in these files
echo -e "\nChecking for direct environment references in token-related files:"
rg "tt02|yt01" tests/k6/common/token.js tests/k6/tests/scripts/generate_tokens.sh

Length of output: 6944

tests/k6/common/token.js (2)

4-4: LGTM: Clean import addition

The import of tokenGeneratorEnv is well-placed and follows proper naming conventions.


60-61: ⚠️ Potential issue

Security: Consider using HTTPS for token generator endpoint

While the changes to use dynamic environment configuration are good, the token generator endpoint is using HTTP. This could expose sensitive information during token generation.

Let's verify if HTTPS is supported:

Consider updating the URL to use HTTPS:

-  const url = `http://altinn-testtools-token-generator.azurewebsites.net/api/GetEnterpriseToken?env=${tokenGeneratorEnv}&scopes=${encodeURIComponent(fullTokenOptions.scopes)}&org=${fullTokenOptions.orgName}&orgNo=${fullTokenOptions.orgNo}&ttl=${tokenTtl}`;
+  const url = `https://altinn-testtools-token-generator.azurewebsites.net/api/GetEnterpriseToken?env=${tokenGeneratorEnv}&scopes=${encodeURIComponent(fullTokenOptions.scopes)}&org=${fullTokenOptions.orgName}&orgNo=${fullTokenOptions.orgNo}&ttl=${tokenTtl}`;

tests/k6/common/token.js Show resolved Hide resolved
@elsand elsand merged commit 393176c into main Nov 5, 2024
4 of 5 checks passed
@elsand elsand deleted the fix/tokengenerator-env-for-yt01 branch November 5, 2024 14:42
oskogstad pushed a commit that referenced this pull request Nov 5, 2024
🤖 I have created a release *beep* *boop*
---


##
[1.28.2](v1.28.1...v1.28.2)
(2024-11-05)


### Bug Fixes

* Use yt01 token generator environment for k6 tests running on yt01
([#1391](#1391))
([393176c](393176c))

---
This PR was generated with [Release
Please](https://github.com/googleapis/release-please). See
[documentation](https://github.com/googleapis/release-please#release-please).
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants