Skip to content

Commit 6d8a2fa

Browse files
committed
Add encoding option
1 parent 59cf22d commit 6d8a2fa

File tree

6 files changed

+43
-14
lines changed

6 files changed

+43
-14
lines changed

.github/workflows/integration.yml

+12-1
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,6 @@ concurrency:
1717

1818
jobs:
1919
integration:
20-
if: ${{ github.event_name == 'push' || github.repository == github.event.pull_request.head.repo.full_name && github.actor != 'dependabot[bot]' }}
2120
permissions:
2221
contents: 'read'
2322
id-token: 'write'
@@ -48,3 +47,15 @@ jobs:
4847
4948
- name: 'outputs'
5049
run: echo '${{ steps.secrets.outputs.token }}${{ steps.secrets.outputs.password }}'
50+
51+
- id: 'secrets-encoded'
52+
name: 'secrets-encoded'
53+
uses: './'
54+
with:
55+
encoding: 'hex'
56+
secrets: |-
57+
token:${{ vars.SECRET_NAME }}
58+
password:${{ vars.SECRET_VERSION_NAME }}
59+
60+
- name: 'outputs-encoded'
61+
run: echo '${{ steps.secrets-encoded.outputs.token }}${{ steps.secrets-encoded.outputs.password }}'

README.md

+7
Original file line numberDiff line numberDiff line change
@@ -96,6 +96,13 @@ jobs:
9696

9797
- <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.
9898

99+
- <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
100+
secrets that cannot be represented in text, such as encryption key bytes,
101+
choose an encoding that has a safe character set for environment variable
102+
values like `base64` or `hex`. For more information about available
103+
encoding types, please see the [Node.js Buffer and character
104+
encodings](https://nodejs.org/docs/latest/api/buffer.html#buffers-and-character-encodings).
105+
99106

100107
<!-- END_AUTOGEN_INPUTS -->
101108

action.yml

+11
Original file line numberDiff line numberDiff line change
@@ -65,6 +65,17 @@ inputs:
6565
required: false
6666
default: false
6767

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

dist/index.js

+4-4
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

src/client.ts

+5-3
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
*/
1616

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

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

104-
return fromBase64(b64data);
104+
let str = b64data.replace(/-/g, '+').replace(/_/g, '/');
105+
while (str.length % 4) str += '=';
106+
return Buffer.from(str, 'base64').toString(encoding);
105107
} catch (err) {
106108
const msg = errorMessage(err);
107109
throw new Error(`Failed to access secret "${ref}": ${msg}`);

src/main.ts

+4-6
Original file line numberDiff line numberDiff line change
@@ -20,20 +20,18 @@ import { errorMessage, parseBoolean } from '@google-github-actions/actions-utils
2020
import { Client } from './client';
2121
import { parseSecretsRefs } from './reference';
2222

23+
type X = keyof BufferEncoding;
24+
2325
/**
2426
* Executes the main action. It includes the main business logic and is the
2527
* primary entry point. It is documented inline.
2628
*/
2729
async function run(): Promise<void> {
2830
try {
29-
// Fetch the list of secrets provided by the user.
3031
const secretsInput = getInput('secrets', { required: true });
31-
32-
// Get the minimum mask length.
3332
const minMaskLength = parseInt(getInput('min_mask_length'));
34-
35-
// Get the setting for whether to export secrets as environment variables.
3633
const exportEnvironment = parseBoolean(getInput('export_to_environment'));
34+
const encoding = (getInput('encoding') || 'utf8') as BufferEncoding;
3735

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

4442
// Access and export each secret.
4543
for (const ref of secretsRefs) {
46-
const value = await client.accessSecret(ref.selfLink());
44+
const value = await client.accessSecret(ref.selfLink(), encoding);
4745

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

0 commit comments

Comments
 (0)