forked from hashicorp/vault-action
-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'main' into wagnerm/add-error-message-output
* main: Add userpass auth and ldap auth support (hashicorp#440) chore(deps-dev): bump jest from 29.4.3 to 29.5.0 (hashicorp#438)
- Loading branch information
Showing
8 changed files
with
960 additions
and
668 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,134 @@ | ||
jest.mock('@actions/core'); | ||
jest.mock('@actions/core/lib/command'); | ||
const core = require('@actions/core'); | ||
|
||
const got = require('got'); | ||
const { when } = require('jest-when'); | ||
|
||
const { exportSecrets } = require('../../src/action'); | ||
|
||
const vaultUrl = `http://${process.env.VAULT_HOST || 'localhost'}:${process.env.VAULT_PORT || '8200'}`; | ||
const vaultToken = `${process.env.VAULT_TOKEN || 'testtoken'}` | ||
|
||
describe('authenticate with approle', () => { | ||
let roleId; | ||
let secretId; | ||
beforeAll(async () => { | ||
try { | ||
// Verify Connection | ||
await got(`${vaultUrl}/v1/secret/config`, { | ||
headers: { | ||
'X-Vault-Token': vaultToken, | ||
}, | ||
}); | ||
|
||
await got(`${vaultUrl}/v1/secret/data/approle-test`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken, | ||
}, | ||
json: { | ||
data: { | ||
secret: 'SUPERSECRET_WITH_APPROLE', | ||
}, | ||
}, | ||
}); | ||
|
||
// Enable approle | ||
try { | ||
await got(`${vaultUrl}/v1/sys/auth/approle`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
json: { | ||
type: 'approle' | ||
}, | ||
}); | ||
} catch (error) { | ||
const {response} = error; | ||
if (response.statusCode === 400 && response.body.includes("path is already in use")) { | ||
// Approle might already be enabled from previous test runs | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
// Create policies | ||
await got(`${vaultUrl}/v1/sys/policies/acl/test`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
json: { | ||
"name":"test", | ||
"policy":"path \"auth/approle/*\" {\n capabilities = [\"read\", \"list\"]\n}\npath \"auth/approle/role/my-role/role-id\"\n{\n capabilities = [\"create\", \"read\", \"update\", \"delete\", \"list\"]\n}\npath \"auth/approle/role/my-role/secret-id\"\n{\n capabilities = [\"create\", \"read\", \"update\", \"delete\", \"list\"]\n}\n\npath \"secret/data/*\" {\n capabilities = [\"list\"]\n}\npath \"secret/metadata/*\" {\n capabilities = [\"list\"]\n}\n\npath \"secret/data/approle-test\" {\n capabilities = [\"read\", \"list\"]\n}\npath \"secret/metadata/approle-test\" {\n capabilities = [\"read\", \"list\"]\n}\n" | ||
}, | ||
}); | ||
|
||
// Create approle | ||
await got(`${vaultUrl}/v1/auth/approle/role/my-role`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
json: { | ||
policies: 'test' | ||
}, | ||
}); | ||
|
||
// Get role-id | ||
const roldIdResponse = await got(`${vaultUrl}/v1/auth/approle/role/my-role/role-id`, { | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
responseType: 'json', | ||
}); | ||
roleId = roldIdResponse.body.data.role_id; | ||
|
||
// Get secret-id | ||
const secretIdResponse = await got(`${vaultUrl}/v1/auth/approle/role/my-role/secret-id`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
responseType: 'json', | ||
}); | ||
secretId = secretIdResponse.body.data.secret_id; | ||
} catch(err) { | ||
console.warn('Create approle', err.response.body); | ||
throw err; | ||
} | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
|
||
when(core.getInput) | ||
.calledWith('method', expect.anything()) | ||
.mockReturnValueOnce('approle'); | ||
when(core.getInput) | ||
.calledWith('roleId', expect.anything()) | ||
.mockReturnValueOnce(roleId); | ||
when(core.getInput) | ||
.calledWith('secretId', expect.anything()) | ||
.mockReturnValueOnce(secretId); | ||
when(core.getInput) | ||
.calledWith('url', expect.anything()) | ||
.mockReturnValueOnce(`${vaultUrl}`); | ||
}); | ||
|
||
function mockInput(secrets) { | ||
when(core.getInput) | ||
.calledWith('secrets', expect.anything()) | ||
.mockReturnValueOnce(secrets); | ||
} | ||
|
||
it('authenticate with approle', async() => { | ||
mockInput('secret/data/approle-test secret'); | ||
|
||
await exportSecrets(); | ||
|
||
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET_WITH_APPROLE'); | ||
}) | ||
}); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,116 @@ | ||
jest.mock('@actions/core'); | ||
jest.mock('@actions/core/lib/command'); | ||
const core = require('@actions/core'); | ||
|
||
const got = require('got'); | ||
const { when } = require('jest-when'); | ||
|
||
const { exportSecrets } = require('../../src/action'); | ||
|
||
const vaultUrl = `http://${process.env.VAULT_HOST || 'localhost'}:${process.env.VAULT_PORT || '8200'}`; | ||
const vaultToken = `${process.env.VAULT_TOKEN || 'testtoken'}` | ||
|
||
describe('authenticate with userpass', () => { | ||
const username = `testUsername`; | ||
const password = `testPassword`; | ||
beforeAll(async () => { | ||
try { | ||
// Verify Connection | ||
await got(`${vaultUrl}/v1/secret/config`, { | ||
headers: { | ||
'X-Vault-Token': vaultToken, | ||
}, | ||
}); | ||
|
||
await got(`${vaultUrl}/v1/secret/data/userpass-test`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken, | ||
}, | ||
json: { | ||
data: { | ||
secret: 'SUPERSECRET_WITH_USERPASS', | ||
}, | ||
}, | ||
}); | ||
|
||
// Enable userpass | ||
try { | ||
await got(`${vaultUrl}/v1/sys/auth/userpass`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
json: { | ||
type: 'userpass' | ||
}, | ||
}); | ||
} catch (error) { | ||
const {response} = error; | ||
if (response.statusCode === 400 && response.body.includes("path is already in use")) { | ||
// Userpass might already be enabled from previous test runs | ||
} else { | ||
throw error; | ||
} | ||
} | ||
|
||
// Create policies | ||
await got(`${vaultUrl}/v1/sys/policies/acl/userpass-test`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
json: { | ||
"name":"userpass-test", | ||
"policy":`path \"auth/userpass/*\" {\n capabilities = [\"read\", \"list\"]\n}\npath \"auth/userpass/users/${username}\"\n{\n capabilities = [\"create\", \"read\", \"update\", \"delete\", \"list\"]\n}\n\npath \"secret/data/*\" {\n capabilities = [\"list\"]\n}\npath \"secret/metadata/*\" {\n capabilities = [\"list\"]\n}\n\npath \"secret/data/userpass-test\" {\n capabilities = [\"read\", \"list\"]\n}\npath \"secret/metadata/userpass-test\" {\n capabilities = [\"read\", \"list\"]\n}\n` | ||
}, | ||
}); | ||
|
||
// Create user | ||
await got(`${vaultUrl}/v1/auth/userpass/users/${username}`, { | ||
method: 'POST', | ||
headers: { | ||
'X-Vault-Token': vaultToken | ||
}, | ||
json: { | ||
password: `${password}`, | ||
policies: 'userpass-test' | ||
}, | ||
}); | ||
} catch(err) { | ||
console.warn('Create user in userpass', err.response.body); | ||
throw err; | ||
} | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
|
||
when(core.getInput) | ||
.calledWith('method', expect.anything()) | ||
.mockReturnValueOnce('userpass'); | ||
when(core.getInput) | ||
.calledWith('username', expect.anything()) | ||
.mockReturnValueOnce(username); | ||
when(core.getInput) | ||
.calledWith('password', expect.anything()) | ||
.mockReturnValueOnce(password); | ||
when(core.getInput) | ||
.calledWith('url', expect.anything()) | ||
.mockReturnValueOnce(`${vaultUrl}`); | ||
}); | ||
|
||
function mockInput(secrets) { | ||
when(core.getInput) | ||
.calledWith('secrets', expect.anything()) | ||
.mockReturnValueOnce(secrets); | ||
} | ||
|
||
it('authenticate with userpass', async() => { | ||
mockInput('secret/data/userpass-test secret'); | ||
|
||
await exportSecrets(); | ||
|
||
expect(core.exportVariable).toBeCalledWith('SECRET', 'SUPERSECRET_WITH_USERPASS'); | ||
}) | ||
}); |
Oops, something went wrong.