Repository: Infisical/infisical
File: backend/src/services/offline-usage-report/offline-usage-report-service.ts (TypeScript)

Code before:

export type TOfflineUsageReportServiceFactory = ReturnType<typeof offlineUsageReportServiceFactory>;

export const offlineUsageReportServiceFactory = ({
  offlineUsageReportDAL,
  licenseService
}: TOfflineUsageReportServiceFactoryDep) => {
  const signReportContent = (content: string, licenseId: string): string => {
    const contentHash = crypto.createHash("sha256").update(content).digest("hex");
    const hmac = crypto.createHmac("sha256", licenseId);
    hmac.update(contentHash);
    return hmac.digest("hex");
  };

  const verifyReportContent = (content: string, signature: string, licenseId: string): boolean => {
    const expectedSignature = signReportContent(content, licenseId);
    return signature === expectedSignature;
  };

  const generateUsageReportCSV = async () => {
    const licenseKeyConfig = getLicenseKeyConfig();
    const hasOfflineLicense = licenseKeyConfig.isValid && licenseKeyConfig.type === "offline";

    if (!hasOfflineLicense) {
      throw new BadRequestError({
        message: "Offline usage reports are not enabled. An offline license must be configured in LICENSE_KEY."
      });
    }

    const customerId = licenseService.getCustomerId() as string;
    const licenseId = licenseService.getLicenseId();

    const [
      userMetrics,
      machineIdentityMetrics,
      projectMetrics,
      secretMetrics,
      secretSyncMetrics,
      dynamicSecretMetrics,
      secretRotationMetrics
    ] = await Promise.all([
      offlineUsageReportDAL.getUserMetrics(),
      offlineUsageReportDAL.getMachineIdentityMetrics(),
      offlineUsageReportDAL.getProjectMetrics(),
      offlineUsageReportDAL.getSecretMetrics(),
      offlineUsageReportDAL.getSecretSyncMetrics(),
      offlineUsageReportDAL.getDynamicSecretMetrics(),
      offlineUsageReportDAL.getSecretRotationMetrics()
    ]);

    const headers = [

Review comment (suggestion):
Maybe we can just say here `Offline usage reports are not enabled. Usage reports are only available for self-hosted offline instances`

Code after:
};

export type TOfflineUsageReportServiceFactory = ReturnType<typeof offlineUsageReportServiceFactory>;

export const offlineUsageReportServiceFactory = ({
  offlineUsageReportDAL,
  licenseService
}: TOfflineUsageReportServiceFactoryDep) => {
  const signReportContent = (content: string, licenseId: string): string => {
    const contentHash = crypto.createHash("sha256").update(content).digest("hex");
    const hmac = crypto.createHmac("sha256", licenseId);
    hmac.update(contentHash);
    return hmac.digest("hex");
  };

  const verifyReportContent = (content: string, signature: string, licenseId: string): boolean => {
    const expectedSignature = signReportContent(content, licenseId);
    return signature === expectedSignature;
  };

  const generateUsageReportCSV = async () => {
    const licenseKeyConfig = getLicenseKeyConfig();
    const hasOfflineLicense = licenseKeyConfig.isValid && licenseKeyConfig.type === LicenseType.Offline;

    if (!hasOfflineLicense) {
      throw new BadRequestError({
        message:
          "Offline usage reports are not enabled. Usage reports are only available for self-hosted offline instances"
      });
    }

    const customerId = licenseService.getCustomerId() as string;
    const licenseId = licenseService.getLicenseId();

    const [
      userMetrics,
      machineIdentityMetrics,
      projectMetrics,
      secretMetrics,
      secretSyncMetrics,
      dynamicSecretMetrics,
      secretRotationMetrics
    ] = await Promise.all([
      offlineUsageReportDAL.getUserMetrics(),
      offlineUsageReportDAL.getMachineIdentityMetrics(),
      offlineUsageReportDAL.getProjectMetrics(),
      offlineUsageReportDAL.getSecretMetrics(),
      offlineUsageReportDAL.getSecretSyncMetrics(),
      offlineUsageReportDAL.getDynamicSecretMetrics(),
      offlineUsageReportDAL.getSecretRotationMetrics()
    ]);