1. Encrypting sensitive information with GitLab project variable protection

    TypeScript

    When working with GitLab and Pulumi, one common task is to manage GitLab project-level CI/CD variables, especially sensitive ones like secret keys, passwords, and tokens. These variables should be protected and only exposed to protected branches or tags, which is what GitLab's variable protection is for.

    Pulumi doesn't have a dedicated GitLab provider, but you can use the GitLab's API through Pulumi's generic pulumi.Command resource to create or update project variables with the desired protection. This approach involves making a curl request to the GitLab API within a Pulumi program to set up protected environment variables.

    Below, I'll write a TypeScript program that demonstrates how to create or update a GitLab project variable and mark it as protected. For this program to work, you must have a GitLab personal access token with the appropriate permissions to manage project-level variables.

    First, the program will outline the steps needed to achieve this:

    1. Define a project ID and personal access token as constants. (Note: Never hardcode sensitive information like access tokens in your code. For this example, we'll use placeholders, but in a real-world scenario, you should use Pulumi's Config to securely manage sensitive values.)

    2. Use the @pulumi/command package to issue a curl command to the GitLab API, passing the project ID, variable key, value, and protection status.

    3. Handle the output of the command and any errors that might occur.

    Here's a detailed example of how to do this in Pulumi with TypeScript:

    import * as pulumi from '@pulumi/pulumi'; import * as command from '@pulumi/command'; // Define your GitLab project ID and the personal access token. // Replace these placeholders with your actual GitLab project ID and access token. // Store the token securely, for example in Pulumi's config system or a secret store, not in plaintext! const projectId = 'your-gitlab-project-id'; const token = 'your-gitlab-access-token'; const variableKey = 'YOUR_VARIABLE_KEY'; const variableValue = 'YOUR_SENSITIVE_VALUE'; // Sensitive values should also be stored securely. // Create the GitLab project variable with protection. const createOrUpdateProtectedVariable = new command.local.Command('protect-variable', { create: `curl --request POST --header "PRIVATE-TOKEN: ${token}" --header "Content-Type: application/json" --data '{"key": "${variableKey}", "value": "${variableValue}", "protected": true}' "https://gitlab.com/api/v4/projects/${projectId}/variables"`, update: `curl --request PUT --header "PRIVATE-TOKEN: ${token}" --header "Content-Type: application/json" --data '{"value": "${variableValue}", "protected": true}' "https://gitlab.com/api/v4/projects/${projectId}/variables/${variableKey}"`, // Deletes the variable when the Pulumi stack is destroyed. // If you want to keep the variable, comment out or remove the 'delete' line. delete: `curl --request DELETE --header "PRIVATE-TOKEN: ${token}" "https://gitlab.com/api/v4/projects/${projectId}/variables/${variableKey}"`, }); // Export the result of the command export const variableProtectionResult = createOrUpdateProtectedVariable.stdout;

    In this program:

    • You need to replace 'your-gitlab-project-id' and 'your-gitlab-access-token' with your actual project ID and personal access token from GitLab.

    Remember that managing secrets and sensitive data requires careful handling. Pulumi's Config might be used to securely manage such sensitive values. This is just a foundational example to illustrate the process. Always ensure you have appropriate error handling in place and validate responses from the API to ensure the operation succeeded as expected.

    Remember that running Pulumi with this program will execute the command to add or update your variable, so you should be sure that your token has the correct permissions and that you are updating the desired project and variable.