Skip to content

Commit e18504c

Browse files
committed
Switch to using /github/home for storing credentials
The GitHub Action currently puts generated credentials into $GITHUB_WORKSPACE (/github/workspace). Unfortunately this is also the working directory of the checkout, so it's too easy to accidentally bundle the generated credentials into Docker containers, binaries, or anything that uses `*` or `.` as a build context. In the past, we tried to move the exported credentials into RUNNER_TEMP or other directories, but it always introduced incompatibility with the various community workflows (Docker, self-hosted, etc.): - google-github-actions/setup-gcloud#148 - google-github-actions/setup-gcloud#149 - google-github-actions/setup-gcloud#405 - google-github-actions/setup-gcloud#412 While undocumented, it appears that `/github/home` is an understood path, AND that path is mounted into Docker containers. That means we can export credentials outside of the workspace and still have them available inside the Docker container without users taking manual actions. This comes at three major costs: 1. We have to write the file into two locations. This isn't ideal, but it's also not the end of the world. 2. We would be relying on an undocumented filepath which GitHub could change at any point in the future. Since this is not part of the publicly-documented API, GitHub is within their rights to change this without notice, potentially breaking everyone/everything. 3. Because of the previous point, there are no environment variables that export these paths. We have to dynamically compile them, and it's a bit messy.
1 parent 0920706 commit e18504c

File tree

2 files changed

+39
-50
lines changed

2 files changed

+39
-50
lines changed

README.md

Lines changed: 3 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -26,19 +26,6 @@ support](https://cloud.google.com/support).**
2626

2727
## Prerequisites
2828

29-
- Run the `actions/checkout@v4` step _before_ this action. Omitting the
30-
checkout step or putting it after `auth` will cause future steps to be
31-
unable to authenticate.
32-
33-
- To create binaries, containers, pull requests, or other releases, add the
34-
following to your `.gitignore`, `.dockerignore` and similar files to prevent
35-
accidentally committing credentials to your release artifact:
36-
37-
```text
38-
# Ignore generated credentials from google-github-actions/auth
39-
gha-creds-*.json
40-
```
41-
4229
- This action runs using Node 20. Use a [runner
4330
version](https://github.com/actions/virtual-environments) that supports this
4431
version of Node or newer.
@@ -237,20 +224,9 @@ regardless of the authentication mechanism.
237224
generate a credentials file which can be used for authentication via gcloud
238225
and Google Cloud SDKs in other steps in the workflow. The default is true.
239226

240-
The credentials file is exported into `$GITHUB_WORKSPACE`, which makes it
241-
available to all future steps and filesystems (including Docker-based GitHub
242-
Actions). The file is automatically removed at the end of the job via a post
243-
action. In order to use exported credentials, you **must** add the
244-
`actions/checkout` step before calling `auth`. This is due to how GitHub
245-
Actions creates `$GITHUB_WORKSPACE`:
246-
247-
```yaml
248-
jobs:
249-
job_id:
250-
steps:
251-
- uses: 'actions/checkout@v4' # Must come first!
252-
- uses: 'google-github-actions/auth@v2'
253-
```
227+
The credentials file is exported into the GitHub home directory
228+
(`/github/home` on Unix, `C:/TODO/TODO` on Windows) and a special directory
229+
in the temp directory that is shared with Docker-based actions.
254230

255231
- `export_environment_variables`: (Optional) If true, the action will export
256232
common environment variables which are known to be consumed by popular

src/main.ts

Lines changed: 36 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -25,12 +25,13 @@ import {
2525
import {
2626
errorMessage,
2727
exactlyOneOf,
28-
isEmptyDir,
2928
isPinnedToHead,
30-
parseMultilineCSV,
3129
parseBoolean,
3230
parseDuration,
31+
parseMultilineCSV,
3332
pinnedToHeadWarning,
33+
randomFilename,
34+
toPlatformPath,
3435
} from '@google-github-actions/actions-utils';
3536

3637
import {
@@ -156,31 +157,43 @@ export async function run(logger: Logger) {
156157
throw new Error('$GITHUB_WORKSPACE is not set');
157158
}
158159

159-
// There have been a number of issues where users have not used the
160-
// "actions/checkout" step before our action. Our action relies on the
161-
// creation of that directory; worse, if a user puts "actions/checkout"
162-
// after our action, it will delete the exported credential. This
163-
// following code does a small check to see if there are any files in the
164-
// directory. It emits a warning if there are no files, since there may be
165-
// legitimate use cases for authenticating without checking out the
166-
// repository.
167-
const githubWorkspaceIsEmpty = await isEmptyDir(githubWorkspace);
168-
if (githubWorkspaceIsEmpty) {
169-
logger.info(
170-
`⚠️ The "create_credentials_file" option is true, but the current ` +
171-
`GitHub workspace is empty. Did you forget to use ` +
172-
`"actions/checkout" before this step? If you do not intend to ` +
173-
`share authentication with future steps in this job, set ` +
174-
`"create_credentials_file" to false.`,
175-
);
160+
// Generate an output file that is unique, but still coupled to the run
161+
// and run attempt.
162+
const outputFileSuffix = [
163+
process.env.GITHUB_REPOSITORY_OWNER_ID,
164+
process.env.GITHUB_REPOSITORY_ID,
165+
process.env.GITHUB_RUN_ID,
166+
process.env.GITHUB_RUN_ATTEMPT,
167+
randomFilename(4),
168+
].join('-');
169+
const outputFile = `gha-creds-${outputFileSuffix}.json`;
170+
171+
// TODO: figure out how to do this path on Windows
172+
const hostHomeFolder = '/github/home';
173+
174+
console.log('\n\n');
175+
console.log('\nprocess.env:\n');
176+
console.log({ env: process.env });
177+
console.log('\n\n');
178+
179+
// /home/runner/work/_temp/_github_home/gha-creds-<random>.json
180+
const startingPoint = process.env.GITHUB_OUTPUT;
181+
if (!startingPoint) {
182+
throw new Error('$GITHUB_OUTPUT is not set');
176183
}
184+
const homeRunnerTemp = startingPoint.slice(0, startingPoint.indexOf('_runner_file_commands'));
185+
logger.debug(`Found home runner temp: "${homeRunnerTemp}"`);
177186

178-
// Create credentials file.
179-
const outputFile = generateCredentialsFilename();
180-
const outputPath = pathjoin(githubWorkspace, outputFile);
181-
const credentialsPath = await client.createCredentialsFile(outputPath);
187+
const dockerHomeFolder = toPlatformPath(`${homeRunnerTemp}/_github_home`);
188+
logger.debug(`Computed GitHub home Docker mount: "${dockerHomeFolder}"`);
189+
190+
// Create credentials files.
191+
const hostHomeOutputPath = pathjoin(hostHomeFolder, outputFile);
192+
const credentialsPath = await client.createCredentialsFile(hostHomeOutputPath);
182193
logger.info(`Created credentials file at "${credentialsPath}"`);
183194

195+
// TODO: copy to docker path
196+
184197
// Output to be available to future steps.
185198
setOutput('credentials_file_path', credentialsPath);
186199

0 commit comments

Comments
 (0)