forked from kubeflow/pipelines
-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
[UI] Multi user permission separation for artifact api (kubeflow#3522)
* [UI Server] Proxy /namespaces/:namespace/artifacts/get requests to namespace specific artifact services * [UI] Show artifacts by namespace * Fix minio artifact link tests * Fix DetailsTable tests * Fix OutputArtifactLoader.test * Change artifact proxy to use query param instead * Add integration tests for artifact proxy * Fix unit tests * Rename service name * Add comment * add more comments * Fix import * Refactored how to spy on internal methods from tests
- Loading branch information
Showing
18 changed files
with
530 additions
and
164 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,2 @@ | ||
./BUILD_DATE | ||
./COMMIT_HASH |
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
147 changes: 147 additions & 0 deletions
147
frontend/server/integration-tests/artifact-proxy.test.ts
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,147 @@ | ||
import { UIServer } from '../app'; | ||
import { commonSetup, buildQuery } from './test-helper'; | ||
import * as requests from 'supertest'; | ||
import { loadConfigs } from '../configs'; | ||
import * as minioHelper from '../minio-helper'; | ||
import { PassThrough } from 'stream'; | ||
import * as express from 'express'; | ||
import { Server } from 'http'; | ||
import * as artifactsHandler from '../handlers/artifacts'; | ||
|
||
beforeEach(() => { | ||
jest.spyOn(global.console, 'info').mockImplementation(); | ||
jest.spyOn(global.console, 'log').mockImplementation(); | ||
jest.spyOn(global.console, 'debug').mockImplementation(); | ||
}); | ||
|
||
const commonParams = { | ||
source: 'minio', | ||
bucket: 'ml-pipeline', | ||
key: 'hello.txt', | ||
}; | ||
|
||
describe('/artifacts/get namespaced proxy', () => { | ||
let app: UIServer; | ||
const { argv } = commonSetup(); | ||
|
||
afterEach(() => { | ||
if (app) { | ||
app.close(); | ||
} | ||
}); | ||
|
||
function setupMinioArtifactDeps({ content }: { content: string }) { | ||
const getObjectStreamSpy = jest.spyOn(minioHelper, 'getObjectStream'); | ||
const objStream = new PassThrough(); | ||
objStream.end(content); | ||
getObjectStreamSpy.mockImplementationOnce(() => Promise.resolve(objStream)); | ||
} | ||
|
||
let artifactServerInUserNamespace: Server; | ||
function setUpNamespacedArtifactService({ | ||
namespace = 'any-ns', | ||
port = 3002, | ||
}: { | ||
namespace?: string; | ||
port?: number; | ||
}) { | ||
const receivedUrls: string[] = []; | ||
const artifactService = express(); | ||
const response = `artifact service in ${namespace}`; | ||
artifactService.all('/*', (req, res) => { | ||
receivedUrls.push(req.url); | ||
res.status(200).send(response); | ||
}); | ||
artifactServerInUserNamespace = artifactService.listen(port); | ||
const getArtifactServiceGetterSpy = jest | ||
.spyOn(artifactsHandler, 'getArtifactServiceGetter') | ||
.mockImplementation(() => () => `http://localhost:${port}`); | ||
return { receivedUrls, getArtifactServiceGetterSpy, response }; | ||
} | ||
afterEach(() => { | ||
if (artifactServerInUserNamespace) { | ||
artifactServerInUserNamespace.close(); | ||
} | ||
}); | ||
|
||
it('is disabled by default', done => { | ||
setupMinioArtifactDeps({ content: 'text-data' }); | ||
const configs = loadConfigs(argv, {}); | ||
app = new UIServer(configs); | ||
requests(app.start()) | ||
.get( | ||
`/artifacts/get${buildQuery({ | ||
...commonParams, | ||
namespace: 'ns2', | ||
})}`, | ||
) | ||
.expect(200, 'text-data', done); | ||
}); | ||
|
||
it('proxies a request to namespaced artifact service', done => { | ||
const { receivedUrls, getArtifactServiceGetterSpy } = setUpNamespacedArtifactService({ | ||
namespace: 'ns2', | ||
}); | ||
const configs = loadConfigs(argv, { | ||
ARTIFACTS_SERVICE_PROXY_NAME: 'artifact-svc', | ||
ARTIFACTS_SERVICE_PROXY_PORT: '80', | ||
ARTIFACTS_SERVICE_PROXY_ENABLED: 'true', | ||
}); | ||
app = new UIServer(configs); | ||
requests(app.start()) | ||
.get( | ||
`/artifacts/get${buildQuery({ | ||
...commonParams, | ||
namespace: 'ns2', | ||
})}`, | ||
) | ||
.expect(200, 'artifact service in ns2', err => { | ||
expect(getArtifactServiceGetterSpy).toHaveBeenCalledWith({ | ||
serviceName: 'artifact-svc', | ||
servicePort: 80, | ||
enabled: true, | ||
}); | ||
expect(receivedUrls).toEqual( | ||
// url is the same, except namespace query is omitted | ||
['/artifacts/get?source=minio&bucket=ml-pipeline&key=hello.txt'], | ||
); | ||
done(err); | ||
}); | ||
}); | ||
|
||
it('does not proxy requests without namespace argument', done => { | ||
setupMinioArtifactDeps({ content: 'text-data2' }); | ||
const configs = loadConfigs(argv, { ARTIFACTS_SERVICE_PROXY_ENABLED: 'true' }); | ||
app = new UIServer(configs); | ||
requests(app.start()) | ||
.get( | ||
`/artifacts/get${buildQuery({ | ||
...commonParams, | ||
namespace: undefined, | ||
})}`, | ||
) | ||
.expect(200, 'text-data2', done); | ||
}); | ||
|
||
it('proxies a request with basePath too', done => { | ||
const { receivedUrls, response } = setUpNamespacedArtifactService({}); | ||
const configs = loadConfigs(argv, { | ||
ARTIFACTS_SERVICE_PROXY_ENABLED: 'true', | ||
}); | ||
app = new UIServer(configs); | ||
requests(app.start()) | ||
.get( | ||
`/pipeline/artifacts/get${buildQuery({ | ||
...commonParams, | ||
namespace: 'ns-any', | ||
})}`, | ||
) | ||
.expect(200, response, err => { | ||
expect(receivedUrls).toEqual( | ||
// url is the same with base path, except namespace query is omitted | ||
['/pipeline/artifacts/get?source=minio&bucket=ml-pipeline&key=hello.txt'], | ||
); | ||
done(err); | ||
}); | ||
}); | ||
}); |
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,49 @@ | ||
import * as path from 'path'; | ||
import * as os from 'os'; | ||
import * as fs from 'fs'; | ||
|
||
export function commonSetup() { | ||
const indexHtmlPath = path.resolve(os.tmpdir(), 'index.html'); | ||
const argv = ['node', 'dist/server.js', os.tmpdir(), '3000']; | ||
const buildDate = new Date().toISOString(); | ||
const commitHash = 'abcdefg'; | ||
const indexHtmlContent = ` | ||
<html> | ||
<head> | ||
<script> | ||
window.KFP_FLAGS.DEPLOYMENT=null | ||
</script> | ||
<script id="kubeflow-client-placeholder"></script> | ||
</head> | ||
</html>`; | ||
|
||
beforeAll(() => { | ||
fs.writeFileSync(path.resolve(__dirname, 'BUILD_DATE'), buildDate); | ||
fs.writeFileSync(path.resolve(__dirname, 'COMMIT_HASH'), commitHash); | ||
fs.writeFileSync(indexHtmlPath, indexHtmlContent); | ||
}); | ||
|
||
afterAll(() => { | ||
fs.unlinkSync(path.resolve(__dirname, 'BUILD_DATE')); | ||
fs.unlinkSync(path.resolve(__dirname, 'COMMIT_HASH')); | ||
fs.unlinkSync(indexHtmlPath); | ||
}); | ||
|
||
beforeEach(() => { | ||
jest.resetAllMocks(); | ||
jest.restoreAllMocks(); | ||
}); | ||
|
||
return { argv }; | ||
} | ||
|
||
export function buildQuery(queriesMap: { [key: string]: string | undefined }): string { | ||
const queryContent = Object.entries(queriesMap) | ||
.filter((entry): entry is [string, string] => entry[1] != null) | ||
.map(([key, value]) => `${key}=${encodeURIComponent(value)}`) | ||
.join('&'); | ||
if (!queryContent) { | ||
return ''; | ||
} | ||
return `?${queryContent}`; | ||
} |
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
Oops, something went wrong.