13. Add a configuration variable¶
C-PAT is configured only through environment variables. A new setting is a new variable, read in one place and documented in one place. These steps add one; Backend guide explains how configuration flows.
Name it. Application settings are
CPAT_<GROUP>_<NAME>in upper snake case, for exampleCPAT_API_RATE_LIMIT. Settings for an integration use that integration’s prefix,STIGMAN_orTENABLE_. Never add a configuration file or a deployment-specific code path instead.Read it in ``api/utils/config.js``. Add the value to the group it belongs to (
settings,client,stigman,tenable,docs,http,database,swaggerUi,oauth,ai,primeng, orlog), with a default and any parsing next to it. Follow the existing lines, such as:port: process.env.CPAT_API_PORT || 8086,
Parse numbers and booleans here, once, so that the rest of the code never touches
process.env. A boolean isprocess.env.CPAT_X === 'true'; a number isNumber.parseInt(process.env.CPAT_X) || <default>.Add it to the template.
api/example_env.txtis the file deployers copy. Add the variable under its group comment (#http config,#database config, and so on) with the default as its value, quoted like its neighbours, and a comment line above it if the meaning is not obvious from the name.Expose it to the client only if the client needs it. Most variables are server-side only. When the client must read it:
Add a field to the object built by
getClientEnv()inapi/bootstrap/client.js. String values go through the file’sjsString()helper so that quotes and<are escaped.Add the same field, with a development value, to
client/src/development.example.html, so that a fresh checkout’s dev index page has it.Add the field to the
CPAT.Envstub inclient/src/test-setup.ts, so that every spec sees it.Read it in the client as
CPAT.Env.<field>at field initialization, with a??default if an olderdevelopment.htmlmight lack it. Never read it fromenvironment.ts.
See Frontend guide for the shape of
CPAT.Env.Mask it if it is a secret. Startup logs the environment and the configuration object.
serializeEnvironment()inapi/utils/logger.jsmasks onlyCPAT_DB_PASSWORD, andconfig.database.toJSON()inapi/utils/config.jsmasks only the database password in the configuration record. A new secret must be added to both masks, or it is written to the log in clear text on every start. Check the('bootstrapUtils', 'configuration')record after you start the API.Document it. Add a row to
docs/source/install/envvars.csv, whose columns areVariable,Description, andAffects(the component the variable changes, such asAPIorClient). The row appears in Environment Variables automatically. If the variable belongs to an integration, describe it on Integrations Configuration as well, and if it needs explanation beyond a sentence, on the Setup page that covers its subject.Verify. Start the API with the variable set. The
('bootstrapUtils', 'starting bootstrap')record lists everyCPAT_*andNODE_*variable the process sees, and the followingconfigurationrecord shows the parsed value in its group. Confirm the value, and confirm that a secret shows as masked. Then start the API with the variable unset and confirm that the default applies.