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

Integrated code lifecycle: Generate authentication tokens to authenticate against localVC #8664

Closed
wants to merge 12 commits into from

Conversation

SimonEntholzer
Copy link
Contributor

@SimonEntholzer SimonEntholzer commented May 25, 2024

Checklist

General

Server

  • Important: I implemented the changes with a very good performance and prevented too many (unnecessary) database calls.
  • I strictly followed the server coding and design guidelines.
  • I added multiple integration tests (Spring) related to the features (with a high test coverage).
  • I documented the Java code using JavaDoc style.

Changes affecting Programming Exercises

  • High priority: I tested all changes and their related features with all corresponding user types on a test server configured with the integrated lifecycle setup (LocalVC and LocalCI).
  • I tested all changes and their related features with all corresponding user types on a test server configured with Gitlab and Jenkins.

Motivation and Context

To allow users to use the LocalVC repositories with access tokens, these must be generated in the first place.

Description

Added functionality in the LocalVC setup, which re-generates access tokens, when they are expiring.

image

Steps for Testing

  1. Using the LocalVC & LocalCI setup
  2. Easiest way for testing locally: Change the cron job in VcsTokenRenewalService.java to run every minute, and then check in your database if all accounts have new Access tokens.
  3. Using one of the accounts, clone a repository from artemis over HTTP and the access token.

Testserver States

Note

These badges show the state of the test servers.
Green = Currently available, Red = Currently locked






Review Progress

Performance Review

  • I (as a reviewer) confirm that the client changes (in particular related to REST calls and UI responsiveness) are implemented with a very good performance
  • I (as a reviewer) confirm that the server changes (in particular related to database calls) are implemented with a very good performance

Code Review

  • Code Review 1
  • Code Review 2

Manual Tests

  • Test 1
  • Test 2

Exam Mode Test

  • Test 1
  • Test 2

Test Coverage

Screenshots

Summary by CodeRabbit

  • New Features

    • Introduced a new service for managing personal access tokens for local version control systems.
    • Added functionality to generate and renew VCS access tokens for users.
  • Improvements

    • Centralized configuration constants for better maintainability and readability.
    • Enhanced token management with new methods and logic for handling VCS access tokens.
  • Tests

    • Updated test cases to reflect changes in access token details.

@SimonEntholzer SimonEntholzer added tests server Pull requests that update Java code. (Added Automatically!) client Pull requests that update TypeScript code. (Added Automatically!) documentation labels May 25, 2024
@SimonEntholzer SimonEntholzer self-assigned this May 25, 2024
SimonEntholzer and others added 2 commits May 25, 2024 09:18
# Conflicts:
#	src/test/java/de/tum/in/www1/artemis/service/ParticipationServiceTest.java
@SimonEntholzer SimonEntholzer changed the title Integrated code lifecycle: Add authentication token to authenticate against localVC Integrated code lifecycleGenerate authentication tokens to authenticate against localVC Jun 3, 2024
@SimonEntholzer SimonEntholzer changed the title Integrated code lifecycleGenerate authentication tokens to authenticate against localVC Integrated code lifecycle: Generate authentication tokens to authenticate against localVC Jun 3, 2024
@SimonEntholzer SimonEntholzer changed the title Integrated code lifecycle: Generate authentication tokens to authenticate against localVC Integrated code lifecycle: Generate authentication tokens to authenticate against localVC Jun 3, 2024
@github-actions github-actions bot added the config-change Pull requests that change the config in a way that they require a deployment via Ansible. label Jun 9, 2024
@SimonEntholzer SimonEntholzer removed the client Pull requests that update TypeScript code. (Added Automatically!) label Jun 10, 2024
@SimonEntholzer SimonEntholzer marked this pull request as ready for review June 10, 2024 08:13
@SimonEntholzer SimonEntholzer requested a review from a team as a code owner June 10, 2024 08:13
Copy link

coderabbitai bot commented Jun 10, 2024

Walkthrough

The updates introduce new functionalities and enhance existing ones across multiple components. Key changes include adding a new constant for GitLab profile, updating user VCS access tokens, improving maintainability by centralizing profile constants, activating access token details for LocalVC, and creating a new service for managing LocalVC access tokens. Additionally, the UserCreationService now handles VCS access tokens for LocalVC setups, and relevant tests have been updated accordingly.

Changes

Files/Paths Change Summary
.idea/runConfigurations/Artemis__Server__LocalVC___LocalCI_.xml Modified Gradle tasks configuration.
src/main/java/de/tum/in/www1/artemis/config/Constants.java Added PROFILE_GITLAB constant.
src/main/java/de/tum/in/www1/artemis/repository/UserRepository.java Added updateUserVCAccessToken method.
src/main/java/de/tum/in/www1/artemis/service/connectors/gitlab/... Updated @Profile annotation to use PROFILE_GITLAB constant.
src/main/java/de/tum/in/www1/artemis/service/connectors/localvc/... Activated INFO_VERSION_CONTROL_ACCESS_TOKEN_DETAIL feature.
src/main/java/de/tum/in/www1/artemis/service/connectors/localvc/... Introduced LocalVCPersonalAccessTokenManagementService for managing LocalVC access tokens.
src/test/java/de/tum/in/www1/artemis/localvcci/... Modified test case assertion for versionControlAccessToken value to expect true.
src/main/java/de/tum/in/www1/artemis/service/connectors/vcs/... Added renewAllVcsAccessTokens method call in VcsTokenRenewalService constructor.
src/main/java/de/tum/in/www1/artemis/service/user/UserCreationService.java Enhanced to handle VCS access tokens for LocalVC setups, including new imports and constructor changes.

Sequence Diagram(s)

sequenceDiagram
    participant User as User
    participant UserCreationService as UserCreationService
    participant LocalVCPersonalAccessTokenManagementService as LocalVCPersonalAccessTokenManagementService
    participant UserRepository as UserRepository

    User ->> UserCreationService: createUser()
    alt Profile is LocalVC and VCS Access Token Enabled
        UserCreationService ->> LocalVCPersonalAccessTokenManagementService: createAccessToken(user, lifetime)
        LocalVCPersonalAccessTokenManagementService -->> UserCreationService: token, expiryDate
        UserCreationService ->> UserRepository: updateUserVCAccessToken(userId, token, expiryDate)
    end
    UserCreationService -->> User: User Created
Loading

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>.
    • 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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @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 as 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.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

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.

Copy link

@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: 6

Comment on lines +572 to +581
@Modifying
@Transactional // ok because of modifying query
@Query("""
UPDATE User user
SET user.vcsAccessToken = :vcsAccessToken,
user.vcsAccessTokenExpiryDate = :vcsAccessTokenExpiryDate
WHERE user.id = :userId
""")
void updateUserVCAccessToken(@Param("userId") long userId, @Param("vcsAccessToken") String vcsAccessToken,
@Param("vcsAccessTokenExpiryDate") ZonedDateTime vcsAccessTokenExpiryDate);
Copy link

Choose a reason for hiding this comment

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

Review transactional boundaries and isolation levels.

The @Transactional annotation is used, but the isolation level is not specified. Consider specifying an isolation level if concurrent modifications are expected.


Validate the security of token storage and renewal processes.

Given the sensitivity of access token management, ensure that the storage and handling of tokens are secure. Consider implementing additional security measures such as encryption at rest and regular audits of token usage and expiration.


Ensure proper handling of time zones in vcsAccessTokenExpiryDate.

The method updateUserVCAccessToken uses ZonedDateTime for the vcsAccessTokenExpiryDate parameter. Ensure that the application consistently handles time zones across different layers to prevent potential time zone discrepancies.

Comment on lines +28 to +32
@Value("${artemis.version-control.version-control-access-token:#{false}}")
private Boolean versionControlAccessToken;

@Value("${artemis.version-control.vc-access-token-max-lifetime-in-days:365}")
private int vcMaxLifetimeInDays;
Copy link

Choose a reason for hiding this comment

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

Review configuration property defaults and ensure they are documented.

The default values for versionControlAccessToken and vcMaxLifetimeInDays are embedded directly in the @Value annotations. It's a good practice to document these defaults either in the application properties file or in the class documentation to improve maintainability and clarity for other developers.

@Profile(PROFILE_LOCALVC)
public class LocalVCPersonalAccessTokenManagementService extends VcsTokenManagementService {

private static final Logger log = LoggerFactory.getLogger(UserResource.class);
Copy link

Choose a reason for hiding this comment

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

Logger class mismatch.

The logger is incorrectly associated with UserResource.class instead of LocalVCPersonalAccessTokenManagementService.class. This could lead to confusion when analyzing logs as they will incorrectly indicate they are originating from UserResource.

- private static final Logger log = LoggerFactory.getLogger(UserResource.class);
+ private static final Logger log = LoggerFactory.getLogger(LocalVCPersonalAccessTokenManagementService.class);
Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
private static final Logger log = LoggerFactory.getLogger(UserResource.class);
private static final Logger log = LoggerFactory.getLogger(LocalVCPersonalAccessTokenManagementService.class);

Comment on lines +288 to +291
/**
* The name of the Spring profile used for gitlab.
*/
public static final String PROFILE_GITLAB = "gitlab";
Copy link

Choose a reason for hiding this comment

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

Tip

Codebase Verification

The search results indicate that the string "gitlab" is still hard-coded in several places in the codebase. These instances should be replaced with the new constant PROFILE_GITLAB to maintain consistency.

  • Locations to update:
    • src/test/java/de/tum/in/www1/artemis/AbstractSpringIntegrationGitlabCIGitlabSamlTest.java
    • src/test/java/de/tum/in/www1/artemis/AbstractSpringIntegrationJenkinsGitlabTest.java
    • src/test/java/de/tum/in/www1/artemis/connector/GitlabRequestMockProvider.java
    • src/main/java/de/tum/in/www1/artemis/service/connectors/gitlab/GitLabPersonalAccessTokenManagementService.java
    • src/main/java/de/tum/in/www1/artemis/service/connectors/gitlab/GitLabService.java
    • src/main/java/de/tum/in/www1/artemis/service/connectors/gitlab/GitLabUserManagementService.java
    • src/main/java/de/tum/in/www1/artemis/service/connectors/gitlab/GitLabAuthorizationInterceptor.java

Please update these instances to use Constants.PROFILE_GITLAB instead of the hard-coded "gitlab".

Analysis chain

Ensure the new constant is appropriately used across the application.

The addition of PROFILE_GITLAB as a constant is a good practice for maintaining consistency and avoiding hard-coded values throughout the application. This change should be verified across all usages to ensure it replaces all previous hard-coded instances.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that the new constant `PROFILE_GITLAB` is used consistently across the application.

# Test: Search for the string "gitlab" that should now be replaced by the constant.
rg --type java '"gitlab"'

Length of output: 1149

Copy link

@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: 2

@@ -50,6 +50,7 @@ public VcsTokenRenewalService(@Value("${artemis.version-control.version-control-
this.versionControlAccessToken = versionControlAccessToken;
this.vcsTokenManagementService = vcsTokenManagementService;
this.userRepository = userRepository;
renewAllVcsAccessTokens();
Copy link

Choose a reason for hiding this comment

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

Consider moving the renewAllVcsAccessTokens() call out of the constructor to avoid potential performance issues and unexpected behavior during application startup.

@@ -1,12 +1,15 @@
package de.tum.in.www1.artemis.service.user;

import static de.tum.in.www1.artemis.config.Constants.PROFILE_CORE;
import static de.tum.in.www1.artemis.config.Constants.PROFILE_LOCALVC;
Copy link

Choose a reason for hiding this comment

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

Refactor the VCS token generation logic into a separate method to improve code readability and maintainability.

- if (versionControlAccessToken && profileService.isProfileActive(PROFILE_LOCALVC)) {
-     newUser.setVcsAccessToken(LocalVCPersonalAccessTokenManagementService.generateSecureToken());
-     newUser.setVcsAccessTokenExpiryDate(ZonedDateTime.now().plus(Duration.ofDays(vcMaxLifetimeInDays)));
- }
+ generateAndSetVcsAccessToken(newUser);

+ private void generateAndSetVcsAccessToken(User user) {
+     if (versionControlAccessToken && profileService.isProfileActive(PROFILE_LOCALVC)) {
+         user.setVcsAccessToken(LocalVCPersonalAccessTokenManagementService.generateSecureToken());
+         user.setVcsAccessTokenExpiryDate(ZonedDateTime.now().plus(Duration.ofDays(vcMaxLifetimeInDays)));
+     }
+ }

Also applies to: 10-10, 12-12, 38-40, 49-49, 66-71, 92-92, 101-101, 147-151, 208-212

Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

Suggested change
import static de.tum.in.www1.artemis.config.Constants.PROFILE_LOCALVC;
import static de.tum.in.www1.artemis.config.Constants.PROFILE_LOCALVC;
private void generateAndSetVcsAccessToken(User user) {
if (versionControlAccessToken && profileService.isProfileActive(PROFILE_LOCALVC)) {
user.setVcsAccessToken(LocalVCPersonalAccessTokenManagementService.generateSecureToken());
user.setVcsAccessTokenExpiryDate(ZonedDateTime.now().plus(Duration.ofDays(vcMaxLifetimeInDays)));
}
}

Copy link
Contributor

@JohannesStoehr JohannesStoehr left a comment

Choose a reason for hiding this comment

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

Can you also add a test for the new behavior?

@@ -43,7 +43,7 @@ public void contribute(Info.Builder builder) {
// TODO: only activate this when access tokens are available and make sure this does not lead to issues
// TODO: If activated, reflect this in LocalVCInfoContributorTest
// with the account.service.ts and its check if the access token is required
builder.withDetail(Constants.INFO_VERSION_CONTROL_ACCESS_TOKEN_DETAIL, false);
builder.withDetail(Constants.INFO_VERSION_CONTROL_ACCESS_TOKEN_DETAIL, true);
Copy link
Contributor

Choose a reason for hiding this comment

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

Is this related to the TODOs above? Can we remove them?

private final UserRepository userRepository;

@Value("${artemis.version-control.version-control-access-token:#{false}}")
private Boolean versionControlAccessToken;
Copy link
Contributor

Choose a reason for hiding this comment

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

The name does not sound like it should be of type boolean. What is it supposed to mean?

Copy link
Contributor

Choose a reason for hiding this comment

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

Can we also change it to the base type, since we have a default value?

public void renewAccessToken(User user, Duration newLifetime) {
if (versionControlAccessToken) {
if (user.getVcsAccessTokenExpiryDate() != null && user.getVcsAccessTokenExpiryDate().isBefore(ZonedDateTime.now())) {
// todo create new one here and
Copy link
Contributor

Choose a reason for hiding this comment

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

Seams like the comment got cut off

@dfuchss dfuchss self-requested a review June 17, 2024 11:38
Copy link
Contributor

@dfuchss dfuchss left a comment

Choose a reason for hiding this comment

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

Just some comments for the code :)

@@ -10,4 +10,4 @@
<option name="Gradle.BeforeRunTask" enabled="false" tasks="build" externalProjectPath="$PROJECT_DIR$" vmOptions="" scriptParameters="-x webapp -x test -x jacocoTestCoverageVerification -x spotlessCheck -x checkstyleMain -x checkstyleTest" />
</method>
</configuration>
</component>
</component>
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe revert this :)

private Boolean versionControlAccessToken;

@Value("${artemis.version-control.vc-access-token-max-lifetime-in-days:365}")
private int vcMaxLifetimeInDays;
Copy link
Contributor

Choose a reason for hiding this comment

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

Maybe rename the attribute. It's not vcmax time .. it's the token's lifetime (also as far as I understand the code. It's not the max lifetime; it's basically the lifetime :D )

@@ -56,6 +63,12 @@ public class UserCreationService {
@Value("${info.guided-tour.course-group-instructors:#{null}}")
private Optional<String> tutorialGroupInstructors;

@Value("${artemis.version-control.version-control-access-token:#{false}}")
private Boolean versionControlAccessToken;
Copy link
Contributor

Choose a reason for hiding this comment

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

You could use boolean ?

@@ -12,7 +14,7 @@
import de.tum.in.www1.artemis.config.Constants;

@Component
@Profile("gitlab")
@Profile(PROFILE_GITLAB)
Copy link
Contributor

Choose a reason for hiding this comment

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

How is this GitLab stuff related to the feature of this Pull Request?
In general, introducing constant here is not bad, but it seems out of scope here to me.

@SimonEntholzer
Copy link
Contributor Author

This approach is outdated, we'll use a different approach to solve this. #8929 deprecates this PR

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
config-change Pull requests that change the config in a way that they require a deployment via Ansible. documentation server Pull requests that update Java code. (Added Automatically!) tests
Projects
Archived in project
Development

Successfully merging this pull request may close these issues.

5 participants