-
Notifications
You must be signed in to change notification settings - Fork 8.3k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Migrate url shortener service (#50896)
- Loading branch information
Showing
16 changed files
with
504 additions
and
89 deletions.
There are no files selected for viewing
Validating CODEOWNERS rules …
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 |
---|---|---|
@@ -1,6 +1,6 @@ | ||
{ | ||
"id": "share", | ||
"version": "kibana", | ||
"server": false, | ||
"server": true, | ||
"ui": true | ||
} |
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,37 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { CoreSetup, Plugin, PluginInitializerContext } from 'kibana/server'; | ||
import { createRoutes } from './routes/create_routes'; | ||
|
||
export class SharePlugin implements Plugin { | ||
constructor(private readonly initializerContext: PluginInitializerContext) {} | ||
|
||
public async setup(core: CoreSetup) { | ||
createRoutes(core, this.initializerContext.logger.get()); | ||
} | ||
|
||
public start() { | ||
this.initializerContext.logger.get().debug('Starting plugin'); | ||
} | ||
|
||
public stop() { | ||
this.initializerContext.logger.get().debug('Stopping plugin'); | ||
} | ||
} |
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,32 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { CoreSetup, Logger } from 'kibana/server'; | ||
|
||
import { shortUrlLookupProvider } from './lib/short_url_lookup'; | ||
import { createGotoRoute } from './goto'; | ||
import { createShortenUrlRoute } from './shorten_url'; | ||
|
||
export function createRoutes({ http }: CoreSetup, logger: Logger) { | ||
const shortUrlLookup = shortUrlLookupProvider({ logger }); | ||
const router = http.createRouter(); | ||
|
||
createGotoRoute({ router, shortUrlLookup, http }); | ||
createShortenUrlRoute({ router, shortUrlLookup }); | ||
} |
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,64 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { CoreSetup, IRouter } from 'kibana/server'; | ||
import { schema } from '@kbn/config-schema'; | ||
|
||
import { shortUrlAssertValid } from './lib/short_url_assert_valid'; | ||
import { ShortUrlLookupService } from './lib/short_url_lookup'; | ||
|
||
export const createGotoRoute = ({ | ||
router, | ||
shortUrlLookup, | ||
http, | ||
}: { | ||
router: IRouter; | ||
shortUrlLookup: ShortUrlLookupService; | ||
http: CoreSetup['http']; | ||
}) => { | ||
router.get( | ||
{ | ||
path: '/goto/{urlId}', | ||
validate: { | ||
params: schema.object({ urlId: schema.string() }), | ||
}, | ||
}, | ||
router.handleLegacyErrors(async function(context, request, response) { | ||
const url = await shortUrlLookup.getUrl(request.params.urlId, { | ||
savedObjects: context.core.savedObjects.client, | ||
}); | ||
shortUrlAssertValid(url); | ||
|
||
const uiSettings = context.core.uiSettings.client; | ||
const stateStoreInSessionStorage = await uiSettings.get('state:storeInSessionStorage'); | ||
if (!stateStoreInSessionStorage) { | ||
return response.redirected({ | ||
headers: { | ||
location: http.basePath.prepend(url), | ||
}, | ||
}); | ||
} | ||
return response.redirected({ | ||
headers: { | ||
location: http.basePath.prepend('/goto_LP/' + request.params.urlId), | ||
}, | ||
}); | ||
}) | ||
); | ||
}; |
63 changes: 63 additions & 0 deletions
63
src/plugins/share/server/routes/lib/short_url_assert_valid.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,63 @@ | ||
/* | ||
* Licensed to Elasticsearch B.V. under one or more contributor | ||
* license agreements. See the NOTICE file distributed with | ||
* this work for additional information regarding copyright | ||
* ownership. Elasticsearch B.V. licenses this file to you under | ||
* the Apache License, Version 2.0 (the "License"); you may | ||
* not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
|
||
import { shortUrlAssertValid } from './short_url_assert_valid'; | ||
|
||
describe('shortUrlAssertValid()', () => { | ||
const invalid = [ | ||
['protocol', 'http://localhost:5601/app/kibana'], | ||
['protocol', 'https://localhost:5601/app/kibana'], | ||
['protocol', 'mailto:foo@bar.net'], | ||
['protocol', 'javascript:alert("hi")'], // eslint-disable-line no-script-url | ||
['hostname', 'localhost/app/kibana'], | ||
['hostname and port', 'local.host:5601/app/kibana'], | ||
['hostname and auth', 'user:pass@localhost.net/app/kibana'], | ||
['path traversal', '/app/../../not-kibana'], | ||
['deep path', '/app/kibana/foo'], | ||
['deep path', '/app/kibana/foo/bar'], | ||
['base path', '/base/app/kibana'], | ||
]; | ||
|
||
invalid.forEach(([desc, url]) => { | ||
it(`fails when url has ${desc}`, () => { | ||
try { | ||
shortUrlAssertValid(url); | ||
throw new Error(`expected assertion to throw`); | ||
} catch (err) { | ||
if (!err || !err.isBoom) { | ||
throw err; | ||
} | ||
} | ||
}); | ||
}); | ||
|
||
const valid = [ | ||
'/app/kibana', | ||
'/app/monitoring#angular/route', | ||
'/app/text#document-id', | ||
'/app/some?with=query', | ||
'/app/some?with=query#and-a-hash', | ||
]; | ||
|
||
valid.forEach(url => { | ||
it(`allows ${url}`, () => { | ||
shortUrlAssertValid(url); | ||
}); | ||
}); | ||
}); |
Oops, something went wrong.