Skip to content

Add encoding option #294

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
May 31, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 12 additions & 1 deletion .github/workflows/integration.yml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ concurrency:

jobs:
integration:
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
permissions:
contents: 'read'
id-token: 'write'
Expand Down Expand Up @@ -48,3 +47,15 @@ jobs:

- name: 'outputs'
run: echo '${{ steps.secrets.outputs.token }}${{ steps.secrets.outputs.password }}'

- id: 'secrets-encoded'
name: 'secrets-encoded'
uses: './'
with:
encoding: 'hex'
secrets: |-
token:${{ vars.SECRET_NAME }}
password:${{ vars.SECRET_VERSION_NAME }}

- name: 'outputs-encoded'
run: echo '${{ steps.secrets-encoded.outputs.token }}${{ steps.secrets-encoded.outputs.password }}'
7 changes: 7 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -96,6 +96,13 @@ jobs:

- <a name="export_to_environment"></a><a href="#user-content-export_to_environment"><code>export_to_environment</code></a>: _(Optional)_ Make the fetched secrets additionally available as environment variables.

- <a name="encoding"></a><a href="#user-content-encoding"><code>encoding</code></a>: _(Optional, default: `utf8`)_ Encoding in which secrets will be exported into environment variables. For
secrets that cannot be represented in text, such as encryption key bytes,
choose an encoding that has a safe character set for environment variable
values like `base64` or `hex`. For more information about available
encoding types, please see the [Node.js Buffer and character
encodings](https://nodejs.org/docs/latest/api/buffer.html#buffers-and-character-encodings).


<!-- END_AUTOGEN_INPUTS -->

Expand Down
11 changes: 11 additions & 0 deletions action.yml
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,17 @@ inputs:
required: false
default: false

encoding:
description: |-
Encoding in which secrets will be exported into environment variables. For
secrets that cannot be represented in text, such as encryption key bytes,
choose an encoding that has a safe character set for environment variable
values like `base64` or `hex`. For more information about available
encoding types, please see the [Node.js Buffer and character
encodings](https://nodejs.org/docs/latest/api/buffer.html#buffers-and-character-encodings).
required: false
default: 'utf8'

outputs:
secrets:
description: |-
Expand Down
8 changes: 4 additions & 4 deletions dist/index.js

Large diffs are not rendered by default.

8 changes: 5 additions & 3 deletions src/client.ts
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

import { GoogleAuth } from 'google-auth-library';
import { errorMessage, fromBase64 } from '@google-github-actions/actions-utils';
import { errorMessage } from '@google-github-actions/actions-utils';
import { HttpClient } from '@actions/http-client';

// Do not listen to the linter - this can NOT be rewritten as an ES6 import statement.
Expand Down Expand Up @@ -77,7 +77,7 @@ export class Client {
* @param ref String of the full secret reference.
* @returns string secret contents.
*/
async accessSecret(ref: string): Promise<string> {
async accessSecret(ref: string, encoding: BufferEncoding): Promise<string> {
if (!ref) {
throw new Error(`Secret ref "${ref}" is empty!`);
}
Expand All @@ -101,7 +101,9 @@ export class Client {
throw new Error(`Secret "${ref}" returned no data!`);
}

return fromBase64(b64data);
let str = b64data.replace(/-/g, '+').replace(/_/g, '/');
while (str.length % 4) str += '=';
return Buffer.from(str, 'base64').toString(encoding);
} catch (err) {
const msg = errorMessage(err);
throw new Error(`Failed to access secret "${ref}": ${msg}`);
Expand Down
8 changes: 2 additions & 6 deletions src/main.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,14 +26,10 @@ import { parseSecretsRefs } from './reference';
*/
async function run(): Promise<void> {
try {
// Fetch the list of secrets provided by the user.
const secretsInput = getInput('secrets', { required: true });

// Get the minimum mask length.
const minMaskLength = parseInt(getInput('min_mask_length'));

// Get the setting for whether to export secrets as environment variables.
const exportEnvironment = parseBoolean(getInput('export_to_environment'));
const encoding = (getInput('encoding') || 'utf8') as BufferEncoding;

// Create an API client.
const client = new Client();
Expand All @@ -43,7 +39,7 @@ async function run(): Promise<void> {

// Access and export each secret.
for (const ref of secretsRefs) {
const value = await client.accessSecret(ref.selfLink());
const value = await client.accessSecret(ref.selfLink(), encoding);

// Split multiline secrets by line break and mask each line.
// Read more here: https://github.com/actions/runner/issues/161
Expand Down
Loading