Commit d94db221 authored by Jared Petersen's avatar Jared Petersen
Browse files

Added support for GPG

parent 5c87b70f
Loading
Loading
Loading
Loading
+19
Changes for README.md: 19 added lines, 0 removed lines.
Original line number Diff line number Diff line
@@ -113,36 +113,55 @@ jobs:
        server-id: maven # Value of the distributionManagement/repository/id field of the pom.xml
        server-username: MAVEN_USERNAME # env variable for username in deploy
        server-password: MAVEN_CENTRAL_TOKEN # env variable for token in deploy
        gpg-private-key: ${{ secrets.MAVEN_GPG_PRIVATE_KEY }} # Value of the GPG private key to import
        gpg-passphrase: MAVEN_GPG_PASSPHRASE # env variable for GPG private key passphrase

    - name: Publish to Apache Maven Central
      run: mvn deploy
      env:
        MAVEN_USERNAME: maven_username123
        MAVEN_CENTRAL_TOKEN: ${{ secrets.MAVEN_CENTRAL_TOKEN }}
        MAVEN_GPG_PASSPHRASE: ${{ secrets.MAVEN_GPG_PASSPHRASE }}
```

The two `settings.xml` files created from the above example look like the following.

`settings.xml` file created for the first deploy to GitHub Packages
```xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
      <id>github</id>
      <username>${env.GITHUB_ACTOR}</username>
      <password>${env.GITHUB_TOKEN}</password>
    </server>
    <server>
      <id>gpg.passphrase</id>
      <passphrase>${env.GPG_PASSPHRASE}</passphrase>
    </server>
  </servers>
</settings>
```

`settings.xml` file created for the second deploy to Apache Maven Central
```xml
<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
      <id>maven</id>
      <username>${env.MAVEN_USERNAME}</username>
      <password>${env.MAVEN_CENTRAL_TOKEN}</password>
    </server>
    <server>
      <id>gpg.passphrase</id>
      <passphrase>${env.MAVEN_GPG_PASSPHRASE}</passphrase>
    </server>
  </servers>
</settings>
```

***NOTE: The `settings.xml` file is created in the Actions $HOME directory. If you have an existing `settings.xml` file at that location, it will be overwritten. See below for using the `settings-path` to change your `settings.xml` file location.***
+48 −42
Changes for __tests__/auth.test.ts: 48 added lines, 42 removed lines.
Original line number Diff line number Diff line
@@ -53,7 +53,7 @@ describe('auth tests', () => {
    await io.rmRF(altHome);
  }, 100000);

  it('creates settings.xml with username and password', async () => {
  it('creates settings.xml with minimal configuration', async () => {
    const id = 'packages';
    const username = 'UNAME';
    const password = 'TOKEN';
@@ -67,78 +67,84 @@ describe('auth tests', () => {
    );
  }, 100000);

  it('overwrites existing settings.xml files', async () => {
  it('creates settings.xml with additional configuration', async () => {
    const id = 'packages';
    const username = 'USERNAME';
    const password = 'PASSWORD';

    fs.mkdirSync(m2Dir, {recursive: true});
    fs.writeFileSync(settingsFile, 'FAKE FILE');
    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    const username = 'UNAME';
    const password = 'TOKEN';
    const gpgPassphrase = 'GPG';

    await auth.configAuthentication(id, username, password);
    await auth.configAuthentication(id, username, password, gpgPassphrase);

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(id, username, password)
      auth.generate(id, username, password, gpgPassphrase)
    );
  }, 100000);

  it('does not create settings.xml without required parameters', async () => {
    await auth.configAuthentication('FOO');
  it('overwrites existing settings.xml files', async () => {
    const id = 'packages';
    const username = 'USERNAME';
    const password = 'PASSWORD';

    fs.mkdirSync(m2Dir, {recursive: true});
    fs.writeFileSync(settingsFile, 'FAKE FILE');
    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate('FOO', auth.DEFAULT_USERNAME, auth.DEFAULT_PASSWORD)
    );

    await auth.configAuthentication(undefined, 'BAR', undefined);
    await auth.configAuthentication(id, username, password);

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(auth.DEFAULT_ID, 'BAR', auth.DEFAULT_PASSWORD)
      auth.generate(id, username, password)
    );
  }, 100000);

    await auth.configAuthentication(undefined, undefined, 'BAZ');

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(auth.DEFAULT_ID, auth.DEFAULT_USERNAME, 'BAZ')
    );
  it('generates valid settings.xml with minimal configuration', () => {
    const id = 'packages';
    const username = 'USER';
    const password = '&<>"\'\'"><&';

    await auth.configAuthentication();
    const expectedSettings = `<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
      <id>${id}</id>
      <username>\${env.${username}}</username>
      <password>\${env.&amp;&lt;&gt;"''"&gt;&lt;&amp;}</password>
    </server>
  </servers>
</settings>`;

    expect(fs.existsSync(m2Dir)).toBe(true);
    expect(fs.existsSync(settingsFile)).toBe(true);
    expect(fs.readFileSync(settingsFile, 'utf-8')).toEqual(
      auth.generate(
        auth.DEFAULT_ID,
        auth.DEFAULT_USERNAME,
        auth.DEFAULT_PASSWORD
      )
    );
  }, 100000);
    expect(auth.generate(id, username, password)).toEqual(expectedSettings);
  });

  it('escapes invalid XML inputs', () => {
  it('generates valid settings.xml with additional configuration', () => {
    const id = 'packages';
    const username = 'USER';
    const password = '&<>"\'\'"><&';
    const gpgPassphrase = 'PASSPHRASE';

    expect(auth.generate(id, username, password)).toEqual(`
  <settings>
    const expectedSettings = `<settings xmlns="http://maven.apache.org/SETTINGS/1.0.0"
  xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
  xsi:schemaLocation="http://maven.apache.org/SETTINGS/1.0.0 https://maven.apache.org/xsd/settings-1.0.0.xsd">
  <servers>
    <server>
      <id>${id}</id>
      <username>\${env.${username}}</username>
          <password>\${env.&amp;&lt;&gt;&quot;&apos;&apos;&quot;&gt;&lt;&amp;}</password>
      <password>\${env.&amp;&lt;&gt;"''"&gt;&lt;&amp;}</password>
    </server>
    <server>
      <id>gpg.passphrase</id>
      <passphrase>\${env.${gpgPassphrase}}</passphrase>
    </server>
  </servers>
  </settings>
  `);
</settings>`;

    expect(auth.generate(id, username, password, gpgPassphrase)).toEqual(
      expectedSettings
    );
  });
});
+56
Changes for __tests__/gpg.test.ts: 56 added lines, 0 removed lines.
Original line number Diff line number Diff line
import path = require('path');
import io = require('@actions/io');
import exec = require('@actions/exec');

jest.mock('@actions/exec', () => {
  return {
    exec: jest.fn()
  };
});

const tempDir = path.join(__dirname, 'runner', 'temp');
process.env['RUNNER_TEMP'] = tempDir;

import gpg = require('../src/gpg');

describe('gpg tests', () => {
  beforeEach(async () => {
    await io.mkdirP(tempDir);
  }, 300000);

  afterAll(async () => {
    try {
      await io.rmRF(tempDir);
    } catch {
      console.log('Failed to remove test directories');
    }
  }, 100000);

  describe('importKey', () => {
    it('attempts to import private key and returns null key id on failure', async () => {
      const privateKey = 'KEY CONTENTS';
      const keyId = await gpg.importKey(privateKey);

      expect(keyId).toBeNull();

      expect(exec.exec).toHaveBeenCalledWith(
        'gpg',
        expect.anything(),
        expect.anything()
      );
    });
  });

  describe('deleteKey', () => {
    it('deletes private key', async () => {
      const keyId = 'asdfhjkl';
      await gpg.deleteKey(keyId);

      expect(exec.exec).toHaveBeenCalledWith(
        'gpg',
        expect.anything(),
        expect.anything()
      );
    });
  });
});
+61
Changes for __tests__/util.test.ts: 61 added lines, 0 removed lines.
Original line number Diff line number Diff line
import path = require('path');

const env = process.env;

describe('util tests', () => {
  beforeEach(() => {
    const tempEnv = Object.assign({}, env);
    delete tempEnv.RUNNER_TEMP;
    delete tempEnv.USERPROFILE;
    process.env = tempEnv;
    Object.defineProperty(process, 'platform', {value: 'linux'});
  });

  describe('getTempDir', () => {
    it('gets temp dir using env', () => {
      process.env['RUNNER_TEMP'] = 'defaulttmp';
      const util = require('../src/util');

      const tempDir = util.getTempDir();

      expect(tempDir).toEqual(process.env['RUNNER_TEMP']);
    });

    it('gets temp dir for windows using userprofile', () => {
      Object.defineProperty(process, 'platform', {value: 'win32'});
      process.env['USERPROFILE'] = 'winusertmp';
      const util = require('../src/util');

      const tempDir = util.getTempDir();

      expect(tempDir).toEqual(
        path.join(process.env['USERPROFILE'], 'actions', 'temp')
      );
    });

    it('gets temp dir for windows using c drive', () => {
      Object.defineProperty(process, 'platform', {value: 'win32'});
      const util = require('../src/util');

      const tempDir = util.getTempDir();

      expect(tempDir).toEqual(path.join('C:\\', 'actions', 'temp'));
    });

    it('gets temp dir for mac', () => {
      Object.defineProperty(process, 'platform', {value: 'darwin'});
      const util = require('../src/util');

      const tempDir = util.getTempDir();

      expect(tempDir).toEqual(path.join('/Users', 'actions', 'temp'));
    });

    it('gets temp dir for linux', () => {
      const util = require('../src/util');
      const tempDir = util.getTempDir();

      expect(tempDir).toEqual(path.join('/home', 'actions', 'temp'));
    });
  });
});
+9 −1
Changes for action.yml: 9 added lines, 1 removed line.
Original line number Diff line number Diff line
@@ -36,6 +36,14 @@ inputs:
  settings-path:
    description: 'Path to where the settings.xml file will be written. Default is ~/.m2.'
    required: false
  gpg-private-key:
    description: 'GPG private key to import. Default is empty string.'
    required: false
  gpg-passphrase:
    description: 'Environment variable name for the GPG private key passphrase. Default is
       $GPG_PASSPHRASE.'
    required: false
runs:
  using: 'node12'
  main: 'dist/index.js'
  main: 'dist/setup/index.js'
  post: 'dist/cleanup/index.js'
Loading