Creating a plugin
Last updated
Last updated
Plugins enable Cortex users to pull data into their Cortex instance from any source, expose custom metrics, and customize the UI to match other internal tools.
A plugin is a React application that is executed in Cortex. It uses a plugin proxy to send requests from the plugin to a third party.
Users with the Edit plugins
permission can create a plugin.
If you do not have access to the Workflows feature in Cortex, please contact your Customer Success Manager for assistance.
In Cortex, navigate to Workflows.
In the upper right corner, click Create Workflow.
Click Blank Workflow.
Add a Scaffolder step to your Workflow.
Configure the name, slug, and description for the Scaffolder step.
For the Scaffolder template dropdown, select Cortex Plugin.
Choose whether to Create a new repo or Open a pull request against an existing repo. Through the Scaffolder, Cortex can automatically set up a React + TypeScript repository for you with the basic setup for your plugin to make it easy to get started.
At the bottom of the Scaffolder side panel, click Save.
At the top of the page, click Save Workflow.
For detailed configuration instructions, see the Workflows documentation.
At the top of the page, click Run.
After saving your Workflow in the previous section, you need to run the Workflow and configure the plugin. The fields here differ depending on whether you chose to create a new repo or open a pull request.
At the top of the Workflow page, click Run.
Configure the plugin details:
Project name: Enter a name for the plugin.
License: Enter the license for your plugin.
Additional fields:
If you chose to create a new repo:
Repo org: Select an organization from the dropdown.
Repo name: Enter the name of the repo that will be created to hold this service.
Branch: Enter the name of the target branch that will be used as the default for the service.
Advanced configuration: Optionally, you can add encrypted GitHub secrets for other users in your organization to use during the service creation flow.
If you chose to open a pull request:
Repo: Enter the name of the repo the pull request will be opened against.
Branch to create: Enter the name of the branch where the generated code will be pushed.
Subdirectory: Enter the name of the subdirectory where the generated service will live.
At the bottom of the side panel, click Submit.
After you create the plugin from the Scaffolder template, you will be redirected to a status page for the plugin creation.
On the plugin status page after creating your plugin, click the button to open your repository.
Clone the repository locally.
Install dependencies via yarn
.
You can also use npm
commands if preferred.
Run yarn build
to generate a single HTML file representing your plugin (output at ./dist/ui.html
).
If you want to make any changes to the plugin, modify the code before running or re-running the build
command.
You will need this file in the next steps.
Go to the Plugins page in Cortex, and in the upper right corner, click Register plugin.
Configure the plugin:
Name: Enter a display name for the plugin.
Identifier: Enter a unique identifier for the plugin.
Description: Enter a description of the plugin to help others understand its purpose.
Minimum user role: Select the minimum user role required to be able to see this plugin. You can choose default or custom roles.
Note that you will not be able to delete a custom role if it is associated with a plugin.
Associated proxy: If you have configured a proxy, select the proxy to use for proxy fetches from this plugin.
Context: Select the context for your proxy:
Global: Select this option if you want to configure the plugin to appear in your main nav or as a tab on the dev homepage.
Specific entity types: Select this option if you want to plugin to display in the sidebar of entity pages.
Both options: Select this option to have the ability to display the plugin in the main nav, on the dev homepage, and on entity pages.
In the "Code" section, upload the HTML file you generated from running yarn build
in the previous steps.
Dev mode: Toggle on to enable dev mode. When in dev mode, the preview for your HTML file uses code running at http://localhost:9000/ui.html. To run your plugin code from this location, use yarn dev
or npm run dev
from your Scaffolded plugin.
In the preview, iterate on any changes you might want to make to the plugin.
Click Save plugin.
To access authenticated external APIs, you can configure a plugin proxy to add request headers to requests matching a URL prefix. See Creating a plugin proxy for more information.
Users with the Edit plugins
permission can edit plugins.
Optionally, you can preview your changes before implementing them:
While viewing the plugins list in Cortex, locate the plugin you want to share.
In the row containing the plugin, click the 3 dots icon, then click Edit plugin.
Next to the "Code" section, enable the toggle next to Dev mode.
The preview will use code running at http://localhost:9000.
From your scaffolded plugin in command line, run yarn dev
.
View the preview on the Plugin editor page in Cortex.
In your text editor, open the cloned repo for your plugin.
After making changes and saving, run yarn build
.
Optionally, run yarn dev
to preview your changes in Cortex.
In the "Plugin code" section of the plugin editing page in Cortex, click Edit next to the file name. Select your newly-updated file.
At the bottom of the page, click Save plugin.
While viewing the plugins list in Cortex, locate the plugin you want to share.
In the row containing the plugin, click the 3 dots icon.
The easiest way to access the plugin context is via the usePluginContext()
hook.
If you need to access the plugin context outside of a React component, you can use the CortexApi
class directly. The CortexAPI class exposed from @cortexapps/plugin-core
provides a method for accessing the context your plugin is running in, getContext()
.
You can access Cortex APIs using @cortexapps/plugin-core
’s CortexAPI
.
See the Cortex API docs for available API calls.
It is also possible to access non-Cortex APIs from your plugin. Because plugins are run in an iframe, typical fetch
requests often get blocked by the browser's enforcement of CORS. However, when using the Cortex-provided template, the browser fetch is shimmed to call CortexApi.proxyFetch
, a method for using Cortex as a proxy to make the request. For this reason, you should be able to use fetch()
as you typically would in a web application.
If your browser fetch is not getting shimmed properly, make sure that your @cortexapps/plugin-core
is up to date and you're using wrapping your app with ``. See the cookiecutter template for an example.
We add the following headers to each request made by Cortex:
x-cortex-timestamp
(current timestamp in millis, used to prevent replay attacks)
x-cortex-signature
x-cortex-signature-256
These headers can be used to verify that the request is valid and originated from Cortex.
Note x-cortex-signature
uses the SHA1 algorithm and exists for backward compatibility. SHA1 has been cracked and this signature should be considered deprecated. It is highly recommended to use x-cortex-signature-256
, which uses the SHA256 algorithm.
To calculate the signature (an RFC2104 HMAC):
Create a string with the value "$timestamp.$requestBody"
if the request body is non-null OR "$timestamp
" if the request body is null.
Calculate an HMAC using the SHA256 algorithm. Use the Secret you provided to Cortex as the key and the string from Step 1 as the payload.
Verify that the x-cortex-signature-256 matches the HMAC calculated in Step 2.
Cortex UI components are available for import from @cortexapps/plugin-core
as of v1.1.0. You can view a Storybook of the components to see what's available, what props they take, and how they look.
Click Share. A link is copied to your clipboard. You can share it with anyone who has access to view plugins in your Cortex workspace.