Unverified Commit a738af78 authored by daz's avatar daz
Browse files

Provide cache-encryption-key as action input

This makes it easier for users to enable config-cache saving in their workflow.

Config-cache data will only be saved/restored when the key is provided,
and the key is exported as `GRADLE_ENCRYPTION_KEY` for use in subsequent steps.
parent ae24bf66
Loading
Loading
Loading
Loading
+7
Changes for action.yml: 7 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -40,6 +40,13 @@ inputs:
    required: false
    default: false

  cache-encryption-key:
    description: |
      A base64 encoded AES key used to encrypt the configuration-cache data. The key is exported as 'GRADLE_ENCRYPTION_KEY' for later steps. 
      A suitable key can be generated with `openssl rand -base64 16`.
      Configuration-cache data will not be saved/restored without an encryption key being provided.
    required: false

  gradle-home-cache-includes:
    description: Paths within Gradle User Home to cache.
    required: false
+34 −4
Changes for src/cache-extract-entries.ts: 34 added lines, 4 removed lines.
Original line number Diff line number Diff line
import path from 'path'
import fs from 'fs'
import crypto from 'crypto'
import * as core from '@actions/core'
import * as glob from '@actions/glob'

@@ -351,14 +352,43 @@ export class ConfigurationCacheEntryExtractor extends AbstractEntryExtractor {
     * entry is not reusable.
     */
    async restore(listener: CacheListener): Promise<void> {
        if (listener.fullyRestored) {
            return super.restore(listener)
        if (!listener.fullyRestored) {
            core.info('Not restoring configuration-cache state, as Gradle User Home was not fully restored')
            for (const cacheEntry of this.loadExtractedCacheEntries()) {
                listener.entry(cacheEntry.pattern).markNotRestored('Gradle User Home not fully restored')
            }
            return
        }

        core.info('Not restoring configuration-cache state, as Gradle User Home was not fully restored')
        if (!params.getCacheEncryptionKey()) {
            core.info('Not restoring configuration-cache state, as no encryption key was provided')
            for (const cacheEntry of this.loadExtractedCacheEntries()) {
            listener.entry(cacheEntry.pattern).markRequested('NOT_RESTORED')
                listener.entry(cacheEntry.pattern).markNotRestored('No encryption key provided')
            }
            return
        }

        const encryptionKey = this.getAESEncryptionKey()
        core.exportVariable('GRADLE_ENCRYPTION_KEY', encryptionKey)
        return await super.restore(listener)
    }

    async extract(listener: CacheListener): Promise<void> {
        if (!params.getCacheEncryptionKey()) {
            core.info('Not saving configuration-cache state, as no encryption key was provided')
            for (const cacheEntry of this.getExtractedCacheEntryDefinitions()) {
                listener.entry(cacheEntry.pattern).markNotSaved('No encryption key provided')
            }
            return
        }

        await super.extract(listener)
    }

    private getAESEncryptionKey(): string | undefined {
        const secret = params.getCacheEncryptionKey()
        const key = crypto.pbkdf2Sync(secret, '', 1000, 16, 'sha256')
        return key.toString('base64')
    }

    /**
+4
Changes for src/input-params.ts: 4 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -29,6 +29,10 @@ export function isCacheCleanupEnabled(): boolean {
    return getBooleanInput('gradle-home-cache-cleanup')
}

export function getCacheEncryptionKey(): string {
    return core.getInput('cache-encryption-key')
}

export function getCacheIncludes(): string[] {
    return core.getMultilineInput('gradle-home-cache-includes')
}