Adding a backend
This page is for contributors who want Hush! to store secrets in another service. A new backend takes three steps: implement the backend interface, register the type so projects can choose it in hush.config.json, and pass the shared backend tests.
The commands' logic doesn't need to change: commands talk to a backend only through the interface. A few user texts in src/HushCommand.ts still name AWS (the warning of pull, the argument help of grant and revoke); check whether they fit your backend.
A lint rule keeps commands away from cloud code: files in src/commands/ may not import the packages and paths listed under no-restricted-imports in eslint.config.js (today @aws-sdk/*, @azure/*, the AWS backend folder and its requests). Add your service's SDK and your backend's folder to that list.
1. Implement the backend interface
Create a class in its own folder, for example src/backends/example/ExampleBackend.ts, that implements SecretBackend from src/backends/SecretBackend.ts. The AWS backend in src/backends/aws/ is a complete example.
Things every backend must get right:
- Names and values pass through unchanged. Names arrive with the
hush-prefix already attached. Values are the serialized text of a Hush! secret; a backend stores and returns them as they are and never reads into them. - Errors are
BackendErrors fromsrc/backends/BackendError.ts. Translate the service's errors into one of the kindsnotFound,accessDenied,notSignedIn,invalidName,tooLarge,softDeleted,unknownFormatorother. Commands decide only by the kind, never by the service's own error names.src/backends/aws/toBackendError.tsshows how. - An error message never contains the value of an entry. The message is shown to the user as it is, and secrets hold customer credentials.
setupProblem()runs before a command uses the backend, so put checks like "is a profile or login there?" in it, not in the constructor. Hush! creates every backend of the project setting on each command, even ones the command doesn't use, andhush --helpmust work without any login.
2. Register the type
Projects choose a backend by its type in hush.config.json. Register yours in src/backends/backendTypes.ts:
export type ExampleBackendSettings = {
type: "example-vault";
vaultName: string;
};
export type BackendSettings = AwsBackendSettings | ExampleBackendSettings;
export const BACKEND_TYPES: Record<BackendType, BackendTypeDefinition> = {
"aws-secrets-manager": {
required: [],
optional: ["region"],
// checkValues and store as today; they narrow settings the same way
create: (settings) =>
new AwsBackend({ region: (settings as AwsBackendSettings).region }),
},
"example-vault": {
required: ["vaultName"],
optional: [],
store: (settings) =>
`Example vault ${(settings as ExampleBackendSettings).vaultName}`,
create: (settings) =>
new ExampleBackend({
vaultName: (settings as ExampleBackendSettings).vaultName,
}),
},
};
- With more than one settings type,
settingsis the union of all of them in every function of an entry (create,storeandcheckValues), so each of them narrows it to its own type, the AWS entry included (it does so already); otherwiseyarn buildfails. requiredandoptionallist every field besidestypethat a backend of this type may have. Hush! refuses any other field, so no credentials can end up in the file: let the backend find them the way its service's tools do (a profile, an environment variable, a login), never as a field.- The project setting checks that every listed field is a non-empty string. A field of another kind (a number, a list) needs a change to that check in
checkBackendinsrc/config/ProjectConfig.ts, which today refuses every field that isn't a string. storenames the store a backend points to, e.g.AWS eu-central-1. Two backends of the same type with the samestoreare refused, so give it everything that tells two stores apart and fill in the defaults of optional fields.checkValuesis optional and checks the values beyond "non-empty string", like the shape of the AWS region. It returns the field and the problem; the problem must not repeat the value.- Add the new type and its fields to the project setting page.
3. Pass the shared backend tests
test/unit/backends/contract.test.ts holds the tests every backend has to pass: reading, writing, new versions, listing by prefix, deleting with and without force, the error kinds for missing and deleted entries, and that grantAccess and revokeAccess come as a pair. They don't try granting or revoking, so write your own tests for those. Add your backend to the backends list at the top of the file:
The unit tests must not call the real service: mock its client, as test/support/fakeSecretsManager.ts does for AWS. Use only made-up values in tests and examples.
Then run the checks:
Tests against the real service belong in test/integration and don't run with yarn test.