Skip to content

Commit 7f48ce7

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 7f48ce7

File tree

2 files changed

+27
-66
lines changed

2 files changed

+27
-66
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: 24 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,8 @@
1212
// See the License for the specific language governing permissions and
1313
// limitations under the License.
1414

15-
import { join as pathjoin } from 'path';
15+
import { join as pathjoin } from 'node:path';
16+
import { mkdir } from 'node:fs/promises';
1617

1718
import {
1819
exportVariable,
@@ -25,11 +26,10 @@ import {
2526
import {
2627
errorMessage,
2728
exactlyOneOf,
28-
isEmptyDir,
2929
isPinnedToHead,
30-
parseMultilineCSV,
3130
parseBoolean,
3231
parseDuration,
32+
parseMultilineCSV,
3333
pinnedToHeadWarning,
3434
} from '@google-github-actions/actions-utils';
3535

@@ -141,44 +141,29 @@ export async function run(logger: Logger) {
141141
if (createCredentialsFile) {
142142
logger.debug(`Creating credentials file`);
143143

144-
// Note: We explicitly and intentionally export to GITHUB_WORKSPACE
145-
// instead of RUNNER_TEMP, because RUNNER_TEMP is not shared with
146-
// Docker-based actions on the filesystem. Exporting to GITHUB_WORKSPACE
147-
// ensures that the exported credentials are automatically available to
148-
// Docker-based actions without user modification.
149-
//
150-
// This has the unintended side-effect of leaking credentials over time,
151-
// because GITHUB_WORKSPACE is not automatically cleaned up on self-hosted
152-
// runners. To mitigate this issue, this action defines a post step to
153-
// remove any created credentials.
154-
const githubWorkspace = process.env.GITHUB_WORKSPACE;
155-
if (!githubWorkspace) {
156-
throw new Error('$GITHUB_WORKSPACE is not set');
144+
// TODO: comment
145+
const runnerTempDir = process.env.RUNNER_TEMP;
146+
if (!runnerTempDir) {
147+
throw new Error('$RUNNER_TEMP is not set');
157148
}
158149

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-
);
176-
}
177-
178-
// Create credentials file.
179-
const outputFile = generateCredentialsFilename();
180-
const outputPath = pathjoin(githubWorkspace, outputFile);
181-
const credentialsPath = await client.createCredentialsFile(outputPath);
150+
// This is an undocumented path that is shared with Docker containers as a
151+
// volume and has path remapping.
152+
//
153+
// https://github.com/actions/runner/blob/0d24afa114c2ee4b6451e35f2ba2cb9b96955789/src/Runner.Worker/Handlers/ContainerActionHandler.cs#L193-L202
154+
const githubHomeDir = pathjoin(runnerTempDir, '_github_home');
155+
logger.debug(`Computed home directory: "${githubHomeDir}"`);
156+
157+
// Create the directory. Unlike $GITHUB_WORKSPACE, this directory may not
158+
// yet exist.
159+
await mkdir(githubHomeDir, { recursive: true });
160+
logger.debug(`Created home directory: "${githubHomeDir}"`);
161+
162+
// Generate an output file that is unique, but still coupled to the run
163+
// and run attempt.
164+
const credentialsPath = await client.createCredentialsFile(
165+
pathjoin(githubHomeDir, generateCredentialsFilename()),
166+
);
182167
logger.info(`Created credentials file at "${credentialsPath}"`);
183168

184169
// Output to be available to future steps.

0 commit comments

Comments
 (0)