-
Notifications
You must be signed in to change notification settings - Fork 135
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Snow 1825478 pat support #1022
Open
sfc-gh-pmotacki
wants to merge
9
commits into
master
Choose a base branch
from
SNOW-1825478-PAT-support
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Snow 1825478 pat support #1022
Changes from all commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
c1e02e4
SNOW-1883649 - wiremock integration
sfc-gh-pmotacki 3c45739
SNOW-1883649 - wiremock integration
sfc-gh-pmotacki 563d6c8
SNOW-1883649 - wiremock integration
sfc-gh-pmotacki e6fd54e
SNOW-1883649 - wiremock integration
sfc-gh-pmotacki c097139
SNOW-1926267: Fix promise rejecting for file upload errors
sfc-gh-pmotacki 5044a00
SNOW-1825478- Support for Oauth PAT
sfc-gh-pmotacki 56aa1c1
SNOW-1825478- Support for Oauth PAT
sfc-gh-pmotacki 8a48b1c
SNOW-1825478-PAT-support
sfc-gh-pmotacki 8cf7328
SNOW-1825478-PAT-support
sfc-gh-pmotacki File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
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,29 @@ | ||
const Util = require('../util'); | ||
/** | ||
* Creates an oauth PAT authenticator. | ||
* | ||
* @param {String} token | ||
* @param {String} password | ||
* | ||
* @returns {Object} | ||
* @constructor | ||
*/ | ||
function AuthOauthPAT(token, password) { | ||
/** | ||
* Update JSON body with token. | ||
* | ||
* @param {JSON} body | ||
* | ||
* @returns {null} | ||
*/ | ||
this.updateBody = function (body) { | ||
if (Util.exists(token)) { | ||
body['data']['TOKEN'] = token; | ||
} else if (Util.exists(password)) { | ||
body['data']['TOKEN'] = password; | ||
} | ||
}; | ||
|
||
this.authenticate = async function () {}; | ||
} | ||
module.exports = AuthOauthPAT; |
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
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
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,51 @@ | ||
const net = require('net'); | ||
const connParameters = require('../../authentication/connectionParameters'); | ||
const AuthTest = require('../../authentication/authTestsBaseClass'); | ||
const { runWireMockAsync, addWireMockMappingsFromFile, } = require('../../wiremockRunner'); | ||
const os = require('os'); | ||
const { getFreePort } = require('../../../lib/util'); | ||
|
||
if (os.platform !== 'win32') { | ||
describe('Oauth PAT authentication', function () { | ||
let port; | ||
let authTest; | ||
let wireMock; | ||
before(async () => { | ||
port = await getFreePort(); | ||
wireMock = await runWireMockAsync(port); | ||
}); | ||
beforeEach(async () => { | ||
authTest = new AuthTest(); | ||
}); | ||
afterEach(async () => { | ||
wireMock.scenarios.resetAllScenarios(); | ||
}); | ||
after(async () => { | ||
await wireMock.global.shutdown(); | ||
}); | ||
|
||
it('Successful flow scenario PAT as token', async function () { | ||
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/pat/successful_flow.json'); | ||
const connectionOption = { ...connParameters.oauthPATOnWiremock, token: 'MOCK_TOKEN', port: port }; | ||
authTest.createConnection(connectionOption); | ||
await authTest.connectAsync(); | ||
authTest.verifyNoErrorWasThrown(); | ||
}); | ||
|
||
it('Successful flow scenario PAT as password', async function () { | ||
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/pat/successful_flow.json'); | ||
const connectionOption = { ...connParameters.oauthPATOnWiremock, password: 'MOCK_TOKEN', port: port }; | ||
authTest.createConnection(connectionOption); | ||
await authTest.connectAsync(); | ||
authTest.verifyNoErrorWasThrown(); | ||
}); | ||
|
||
it('Invalid token', async function () { | ||
await addWireMockMappingsFromFile(wireMock, 'wiremock/mappings/pat/invalid_pat_token.json'); | ||
const connectionOption = { ...connParameters.oauthPATOnWiremock, token: 'INVALID_TOKEN', port: port }; | ||
authTest.createConnection(connectionOption); | ||
await authTest.connectAsync(); | ||
authTest.verifyErrorWasThrown('Programmatic access token is invalid.'); | ||
}); | ||
}); | ||
} |
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,43 @@ | ||
const assert = require('assert'); | ||
const fs = require('fs'); | ||
const net = require('net'); | ||
const axios = require('axios'); | ||
const { runWireMockAsync } = require('../../wiremockRunner'); | ||
const os = require('os'); | ||
|
||
async function getFreePort() { | ||
return new Promise(res => { | ||
const srv = net.createServer(); | ||
srv.listen(0, () => { | ||
const port = srv.address().port; | ||
srv.close(() => res(port)); | ||
}); | ||
}); | ||
} | ||
|
||
if (os.platform !== 'win32') { | ||
describe('Wiremock test', function () { | ||
let port, wireMock; | ||
before(async () => { | ||
port = await getFreePort(); | ||
wireMock = await runWireMockAsync(port); | ||
}); | ||
after(async () => { | ||
await wireMock.global.shutdown(); | ||
}); | ||
it('Run Wiremock instance, wait, verify connection and shutdown', async function () { | ||
assert.doesNotReject(async () => await wireMock.mappings.getAllMappings()); | ||
}); | ||
it('Add mappings', async function () { | ||
const requests = JSON.parse(fs.readFileSync('wiremock/mappings/testMapping.json', 'utf8')); | ||
for (const mapping of requests.mappings) { | ||
await wireMock.mappings.createMapping(mapping); | ||
} | ||
const mappings = await wireMock.mappings.getAllMappings(); | ||
assert.strictEqual(mappings.mappings.length, 2); | ||
const response = await axios.get(`http://localhost:${port}/test/authorize.html`); | ||
assert.strictEqual(response.status, 200); | ||
}); | ||
}); | ||
|
||
} |
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,58 @@ | ||
const WireMockRestClient = require('wiremock-rest-client').WireMockRestClient; | ||
const { exec } = require('child_process'); | ||
const Logger = require('../lib/logger'); | ||
const fs = require('fs'); | ||
|
||
|
||
async function runWireMockAsync(port) { | ||
let timeoutHandle; | ||
const waitingWireMockPromise = new Promise( (resolve, reject) => { | ||
try { | ||
exec(`npx wiremock --enable-browser-proxying --proxy-pass-through false --port ${port} `); | ||
const wireMock = new WireMockRestClient(`http://localhost:${port}`, { logLevel: 'debug' }); | ||
const readyWireMock = waitForWiremockStarted(wireMock); | ||
resolve(readyWireMock); | ||
} catch (err) { | ||
reject(err); | ||
} | ||
}); | ||
|
||
const timeout = new Promise((resolve, reject) => | ||
timeoutHandle = setTimeout( | ||
() => reject('Wiremock unavailable after 30s.'), | ||
30000)); | ||
return Promise.race([waitingWireMockPromise, timeout]) | ||
.then(result => { | ||
clearTimeout(timeoutHandle); | ||
return result; | ||
}); | ||
} | ||
|
||
async function waitForWiremockStarted(wireMock) { | ||
return fetch(wireMock.baseUri) | ||
.then(async (resp) => { | ||
if (resp.ok) { | ||
return Promise.resolve(wireMock); | ||
} else { | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
Logger.getInstance().info(`Retry connection to WireMock after wrong response status: ${resp.status}`); | ||
return await waitForWiremockStarted(wireMock); | ||
} | ||
}) | ||
.catch(async (err) => { | ||
await new Promise(resolve => setTimeout(resolve, 1000)); | ||
Logger.getInstance().info(`Retry connection to WireMock after error: ${err}`); | ||
return await waitForWiremockStarted(wireMock); | ||
}); | ||
} | ||
|
||
async function addWireMockMappingsFromFile(wireMock, filePath) { | ||
const requests = JSON.parse(fs.readFileSync(filePath, 'utf8')); | ||
for (const mapping of requests.mappings) { | ||
await wireMock.mappings.createMapping(mapping); | ||
} | ||
} | ||
|
||
exports.runWireMockAsync = runWireMockAsync; | ||
exports.addWireMockMappingsFromFile = addWireMockMappingsFromFile; | ||
|
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Maybe this should go in
testUtil.js
instead?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Good catch, I will try to move it.