2.3 KiB
Managing Secrets
Introduction
In many workflows, accessing sensitive data such as API tokens or credentials is essential. To handle this securely, you can use secrets management. This tutorial demonstrates how to manage secrets using the CLI and implement them in a simple Node.js workload.
Creating Secrets with the CLI
To create a new secret, use the mini-loader CLI as follows:
mini-loader secrets set <id>
For example, to store a GitHub personal access token, you would use:
mini-loader secrets set githubtoken
Upon execution, you'll be prompted to enter your access token.
Implementing Secrets in Your Workload
Next, let's create a Node.js script (github.js) that uses this token to fetch your GitHub username and saves it as an artifact.
-
Create
github.jsFile:import { secrets, artifacts } from '@morten-olsen/mini-loader'; import { Octokit } from '@octokit/rest'; // Retrieve the secret const accessToken = secrets.get('githubtoken'); // Main async function to fetch and save GitHub username async function run() { const octokit = new Octokit({ auth: accessToken }); const user = await octokit.users.getAuthenticated(); await artifacts.create('user', JSON.stringify(user.data.login)); } // Execute the function run().catch(console.error);This script initializes the Octokit client with the access token, fetches the authenticated user's data, and then saves the username as an artifact.
-
Run the Script:
Execute your script with
mini-loader:mini-loader loads push github.js -r -ai
Managing Local Secrets
If you're running the script locally, you can manage secrets either by using a .secrets file or setting an environment variable.
-
Using a
.secretsFile:Create a file named
.secretsand add your token:githubtoken=<your-token> -
Using Environment Variables:
Prefix your environment variable with
ML_S_and run the script:ML_S_githubtoken=<your-token> mini-loader local run github.js -ai
Conclusion
By following these steps, you can securely manage and use secrets within your workloads, enhancing the security and integrity of your applications.