Repository: corona-warn-app/cwa-server
File: services/distribution/src/main/java/app/coronawarn/server/services/distribution/dgc/dsc/DigitalCovidValidationCertificateToProtobufMapping.java (Java)

Code before:
  private static final Logger LOGGER = LoggerFactory
      .getLogger(DigitalCovidValidationCertificateToProtobufMapping.class);

  public static final String DCC_VALIDATION_RULE_JSON_CLASSPATH = "dgc/dcc-validation-service-allowlist-rule.json";

  private final DistributionServiceConfig distributionServiceConfig;
  private final ResourceLoader resourceLoader;

  public DigitalCovidValidationCertificateToProtobufMapping(
      DistributionServiceConfig distributionServiceConfig, ResourceLoader resourceLoader) {
    this.distributionServiceConfig = distributionServiceConfig;
    this.resourceLoader = resourceLoader;
  }

  /**
   * Validates an object (JSON) based on a provided schema containing validation rules.
   * @param allowList - object to be validated
   * @throws JsonProcessingException - if object to be validated fails on JSON processing
   * @throws ValidationException - if the validation of the object based on validation schema fails.
   */
  public boolean validateSchema(AllowList allowList) {
    try (final InputStream in = resourceLoader.getResource(DCC_VALIDATION_RULE_JSON_CLASSPATH).getInputStream()) {
      validateJsonSchema(allowList, in);
      return true;
    } catch (ValidationException e) {
      LOGGER.error("Schema invalid", e);
      return false;
    } catch (IOException e) {
      LOGGER.error("Could not read resource " + DCC_VALIDATION_RULE_JSON_CLASSPATH, e);
      return false;
    }
  }

  /**
   * Validates an object (JSON) based on a provided schema containing validation rules.
   */
  public boolean validateCertificate() {
    String content = distributionServiceConfig.getDigitalGreenCertificate().getAllowListAsString();
    byte[] signature = distributionServiceConfig.getDigitalGreenCertificate().getAllowListSignature();

    try {
      PublicKey publicKey = getPublicKeyFromString(
          distributionServiceConfig.getDigitalGreenCertificate().getAllowListCertificate());
      ecdsaSignatureVerification(
          signature,
          publicKey,
          content.getBytes(StandardCharsets.UTF_8));
      return true;
    } catch (Exception e) {
      LOGGER.error("Failed to validate certificate", e);
      return false;

Review comment (bug):
```suggestion
      LOGGER.error("Json schema validation failed", e);
```

Code after:
  private static final Logger LOGGER = LoggerFactory
      .getLogger(DigitalCovidValidationCertificateToProtobufMapping.class);

  public static final String DCC_VALIDATION_RULE_JSON_CLASSPATH = "dgc/dcc-validation-service-allowlist-rule.json";

  private final DistributionServiceConfig distributionServiceConfig;
  private final ResourceLoader resourceLoader;

  public DigitalCovidValidationCertificateToProtobufMapping(
      DistributionServiceConfig distributionServiceConfig, ResourceLoader resourceLoader) {
    this.distributionServiceConfig = distributionServiceConfig;
    this.resourceLoader = resourceLoader;
  }

  /**
   * Validates an object (JSON) based on a provided schema containing validation rules.
   * @param allowList - object to be validated
   * @throws JsonProcessingException - if object to be validated fails on JSON processing
   * @throws ValidationException - if the validation of the object based on validation schema fails.
   */
  public boolean validateSchema(AllowList allowList) {
    try (final InputStream in = resourceLoader.getResource(DCC_VALIDATION_RULE_JSON_CLASSPATH).getInputStream()) {
      validateJsonSchema(allowList, in);
      return true;
    } catch (ValidationException e) {
      LOGGER.error("Json schema validation failed", e);
      return false;
    } catch (IOException e) {
      LOGGER.error("Could not read resource " + DCC_VALIDATION_RULE_JSON_CLASSPATH, e);
      return false;
    }
  }

  /**
   * Validates an object (JSON) based on a provided schema containing validation rules.
   */
  public boolean validateCertificate() {
    String content = distributionServiceConfig.getDigitalGreenCertificate().getAllowListAsString();
    byte[] signature = distributionServiceConfig.getDigitalGreenCertificate().getAllowListSignature();

    try {
      PublicKey publicKey = getPublicKeyFromString(
          distributionServiceConfig.getDigitalGreenCertificate().getAllowListCertificate());
      ecdsaSignatureVerification(
          signature,
          publicKey,
          content.getBytes(StandardCharsets.UTF_8));
      return true;
    } catch (Exception e) {
      LOGGER.error("Failed to validate certificate", e);
      return false;