OrganizationLinkerService.java

package edu.ucsb.cs156.frontiers.services;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import edu.ucsb.cs156.frontiers.entities.Course;
import edu.ucsb.cs156.frontiers.errors.InvalidInstallationTypeException;
import edu.ucsb.cs156.frontiers.errors.NoLinkedOrganizationException;
import edu.ucsb.cs156.frontiers.models.CourseWarning;
import java.security.NoSuchAlgorithmException;
import java.security.spec.InvalidKeySpecException;
import java.time.Duration;
import java.time.ZonedDateTime;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.boot.web.client.RestTemplateBuilder;
import org.springframework.data.auditing.DateTimeProvider;
import org.springframework.http.HttpEntity;
import org.springframework.http.HttpHeaders;
import org.springframework.http.HttpMethod;
import org.springframework.http.ResponseEntity;
import org.springframework.stereotype.Service;
import org.springframework.web.client.HttpClientErrorException;
import org.springframework.web.client.RestTemplate;

@Service
public class OrganizationLinkerService {
  /*
   * A RestTemplate built with no timeout blocks its calling thread forever on a hung external
   * call. That's a real incident lib-jobs' single-threaded jobsExecutor hit on another app
   * (proj-scaffold): a job stuck this way permanently wedged the executor, with no way to recover
   * short of restarting the app (see lib-jobs DESIGN.md 9 -- cooperative job cancellation only
   * helps a job that reaches another checkpoint, which a truly hung thread never will). Generous
   * but finite: long enough to never trip on legitimate slowness, short enough to guarantee a job
   * can't hang forever.
   */
  private static final Duration CONNECT_TIMEOUT = Duration.ofSeconds(10);
  private static final Duration READ_TIMEOUT = Duration.ofSeconds(60);

  private RestTemplate restTemplate;

  @Autowired JwtService jwtService;

  @Autowired ObjectMapper objectMapper;

  @Autowired DateTimeProvider provider;

  public OrganizationLinkerService(RestTemplateBuilder restTemplateBuilder) {
    restTemplate =
        restTemplateBuilder.connectTimeout(CONNECT_TIMEOUT).readTimeout(READ_TIMEOUT).build();
  }

  /**
   * Returns the URL for a redirect to install Frontiers
   *
   * @return URL to install Frontiers to an organization
   */
  public String getRedirectUrl()
      throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException {
    String token = jwtService.getJwt();
    HttpHeaders requestHeaders = new HttpHeaders();
    requestHeaders.add("Authorization", "Bearer " + token);
    requestHeaders.add("Accept", "application/vnd.github+json");
    requestHeaders.add("X-GitHub-Api-Version", "2022-11-28");
    String ENDPOINT = "https://api.github.com/app";
    HttpEntity<String> newEntity = new HttpEntity<>(requestHeaders);
    ResponseEntity<String> response =
        restTemplate.exchange(ENDPOINT, HttpMethod.GET, newEntity, String.class);

    JsonNode responseJson = objectMapper.readTree(response.getBody());

    String newUrl = responseJson.get("html_url").toString().replaceAll("\"", "");
    return newUrl;
  }

  /**
   * Provides the name of the organization attached to a particular installation ID
   *
   * @param installation_id ID of the app installation
   * @return name of the organization attached to the installation
   */
  public String getOrgName(String installation_id)
      throws NoSuchAlgorithmException, InvalidKeySpecException, JsonProcessingException {
    String token = jwtService.getJwt();
    String ENDPOINT = "https://api.github.com/app/installations/" + installation_id;

    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Bearer " + token);
    headers.add("Accept", "application/vnd.github+json");
    headers.add("X-GitHub-Api-Version", "2022-11-28");
    HttpEntity<String> entity = new HttpEntity<>(headers);
    ResponseEntity<String> response =
        restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, String.class);
    JsonNode responseJson = objectMapper.readTree(response.getBody());
    String type = responseJson.get("account").get("type").asText();
    if (!type.equals("Organization")) {
      throw new InvalidInstallationTypeException(type);
    }
    String orgName = responseJson.get("account").get("login").asText();
    return orgName;
  }

  public CourseWarning checkCourseWarnings(Course course)
      throws NoSuchAlgorithmException,
          InvalidKeySpecException,
          JsonProcessingException,
          NoLinkedOrganizationException {

    if (course.getOrgName() == null || course.getInstallationId() == null) {
      return new CourseWarning(false, false, false);
    }

    String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName();
    HttpHeaders headers = new HttpHeaders();
    String token = jwtService.getInstallationToken(course);
    headers.add("Authorization", "Bearer " + token);
    headers.add("Accept", "application/vnd.github+json");
    headers.add("X-GitHub-Api-Version", "2022-11-28");
    HttpEntity<String> entity = new HttpEntity<>(headers);
    ResponseEntity<JsonNode> response =
        restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, JsonNode.class);
    JsonNode responseJson = response.getBody();
    ZonedDateTime creationDate = ZonedDateTime.parse(responseJson.get("created_at").asText());
    ZonedDateTime now = ZonedDateTime.from(provider.getNow().get());
    boolean showOrganizationAgeWarning = creationDate.isAfter(now.minusMonths(1));
    String defaultRepositoryPermission = getDefaultRepositoryPermission(course);
    boolean showDefaultBasePermissions = !"none".equals(defaultRepositoryPermission);
    boolean showFreePlanWarning = FREE_PLAN_NAME.equals(getPlanName(responseJson));
    return new CourseWarning(
        showOrganizationAgeWarning, showDefaultBasePermissions, showFreePlanWarning);
  }

  /** The {@code plan.name} GitHub reports for an organization on its Free plan. */
  public static final String FREE_PLAN_NAME = "free";

  /**
   * Extracts the organization's plan name (e.g. {@code free}, {@code team}, {@code enterprise})
   * from a GET /orgs/{org} response. GitHub only includes the {@code plan} object when the GitHub
   * App has the organization "Plan" (read) permission; without it, the plan is unknown and the
   * empty string is returned, so no plan-based warning is shown.
   *
   * @param orgJson the response body of GET /orgs/{org}
   * @return the plan name, or the empty string if it is not present
   */
  public static String getPlanName(JsonNode orgJson) {
    return orgJson.path("plan").path("name").asText("");
  }

  /**
   * Retrieves the default repository permission for the GitHub organization linked to a course.
   *
   * @param course The course whose linked organization is queried.
   * @return The organization's default repository permission (e.g. {@code none}, {@code read}).
   */
  public String getDefaultRepositoryPermission(Course course)
      throws NoSuchAlgorithmException,
          InvalidKeySpecException,
          JsonProcessingException,
          NoLinkedOrganizationException {
    String ENDPOINT = "https://api.github.com/orgs/" + course.getOrgName();
    HttpHeaders headers = new HttpHeaders();
    String token = jwtService.getInstallationToken(course);
    headers.add("Authorization", "Bearer " + token);
    headers.add("Accept", "application/vnd.github+json");
    headers.add("X-GitHub-Api-Version", "2022-11-28");
    HttpEntity<String> entity = new HttpEntity<>(headers);
    ResponseEntity<JsonNode> response =
        restTemplate.exchange(ENDPOINT, HttpMethod.GET, entity, JsonNode.class);
    return response.getBody().get("default_repository_permission").asText();
  }

  /**
   * Removes the Frontiers installation from the linked GitHub org
   *
   * @param course The entity for the course about to be deleted
   */
  public void unenrollOrganization(Course course)
      throws NoSuchAlgorithmException, InvalidKeySpecException {
    if (course.getOrgName() == null || course.getInstallationId() == null) {
      return;
    }
    String token = jwtService.getJwt();
    String ENDPOINT = "https://api.github.com/app/installations/" + course.getInstallationId();
    HttpHeaders headers = new HttpHeaders();
    headers.add("Authorization", "Bearer " + token);
    headers.add("Accept", "application/vnd.github+json");
    headers.add("X-GitHub-Api-Version", "2022-11-28");
    HttpEntity<String> entity = new HttpEntity<>(headers);
    try {
      ResponseEntity<String> response =
          restTemplate.exchange(ENDPOINT, HttpMethod.DELETE, entity, String.class);
    } catch (HttpClientErrorException ignored) {

    }
  }
}