Before Upload Worker Code Sample

JFrog Platform Administration Documentation

Content Type
Administration / Platform

The following section provides a sample code for an Before Upload worker.

Note

Only JFrog users with JFrog Advanced Security (JAS) can block upload of artifacts by before upload. For users without JAS, a warning will be logged.

export default async (
  context: PlatformContext,
  data: BeforeUploadRequest
): Promise<BeforeUploadResponse> => {
  let status: UploadStatus = UploadStatus.UPLOAD_UNSPECIFIED;

  try {
    // The in-browser HTTP client facilitates making calls to the JFrog REST APIs
    //To call an external endpoint, use 'await context.clients.axios.get("https://foo.com")'
    const res = await context.clients.platformHttp.get(
      "/artifactory/api/v1/system/readiness"
    );

    // You should reach this part if the HTTP request status is successful (HTTP Status 399 or lower)
    if (res.status === 200) {
      status = UploadStatus.UPLOAD_PROCEED;
      console.log("Artifactory ping success");
    } else {
      status = UploadStatus.UPLOAD_WARN;
      console.warn(
        `Request was successful but returned status other than 200. Status code : ${res.status}`
      );
    }
  } catch (error) {
    // The platformHttp client throws PlatformHttpClientError if the HTTP request status is 400 or higher
    status = UploadStatus.UPLOAD_STOP;
    console.error(
      `Request failed with status code ${
        error.status || "<none>"
      } caused by : ${error.message}`
    );
  }

  return {
    status,
    message: "Overwritten by worker-service if an error occurs.",
    modifiedRepoPath: data.metadata.repoPath,
  };
};

Input Parameters

context

Provides baseUrl, token, and clients to communicate with the JFrog Platform (for more information, see PlatformContext).

data

The request with upload details sent by Artifactory.

{
  "metadata": {  // Object containing metadata information about the artifact
    "repoPath": {  // Repository path information for the artifact
      "key": "local-repo",  // Unique key identifier for the repository
      "path": "folder/subfoder/my-file",  // Current path to the specific artifact within the repository
      "id": "local-repo:folder/subfoder/my-file",  // Unique identifier combining the repository key and path
      "isRoot": false,  // Indicates if the path is a root directory (false means it is nested)
      "isFolder": false  // Indicates if the path is a folder (false means it is a file)
    },
    "contentLength": 100,  // Length of the content in bytes
    "lastModified": 0,  // Timestamp of the last modification (0 indicates it has not been modified)
    "trustServerChecksums": false,  // Indicates whether server checksums should be trusted for validation
    "servletContextUrl": "servlet.com",  // URL for accessing the servlet context
    "skipJarIndexing": false,  // Indicates whether to skip indexing for JAR files (false means indexing will occur)
    "disableRedirect": false,  // Indicates whether HTTP redirects should be disabled
    "repoType": 1  // Numeric identifier representing the type of repository (e.g., local = 1)
  },
  "headers": {  // Object containing HTTP headers associated with the request
    "key": {  // Example of a custom header
      "key": "bla",  // Name of the custom header
      "value": "bla"  // Value of the custom header
    }
  },
  "userContext": {  // Object containing information about the user making the request
    "id": "jffe@00xxxxxxxxxxxxxxxxxxxxxxxx/users/bob",  // Unique identifier for the user
    "isToken": true,  // Indicates if the user is authenticated using a token (true means they are)
    "realm": "realm"  // Realm for user authentication context
  },
  "artifactProperties": {  // Object containing additional properties associated with the artifact
    "anyProperty": {  // Example of a custom property name
      "value": [  // Array of values for the property
        "anything"  // Example value for the property
      ]
    }
  }
}

Response

{
 "message": "proceed", // Message to print to the log, in case of an error, it will be printed as a warning
 "status": "proceed" // The instruction of how to proceed
}
  • message and status : These are mandatory fields.

Possible Statuses

  • UploadStatus.UPLOAD_PROCEED - The worker allows to proceed with the upload.

  • UploadStatus.UPLOAD_STOP - The worker forbids upload. Upload will be aborted.

  • UploadStatus.UPLOAD_WARN - The worker allows to proceed with the upload. A warning log with the provided message will be recorded in Artifactory.