Skip to content
This repository has been archived by the owner on Feb 8, 2024. It is now read-only.

Commit

Permalink
Use dedicated API for app FQDN resolving (#284) (#298)
Browse files Browse the repository at this point in the history
  • Loading branch information
andrejtokarcik authored May 5, 2021
1 parent dde8c93 commit 316ba2f
Show file tree
Hide file tree
Showing 3 changed files with 55 additions and 25 deletions.
47 changes: 32 additions & 15 deletions packages/teleport/src/AppLauncher/useAppLauncher.ts
Original file line number Diff line number Diff line change
Expand Up @@ -26,21 +26,9 @@ export default function useAppLauncher() {
const { attempt, setAttempt } = useAttempt('processing');

React.useEffect(() => {
service
.createAppSession(params)
.then(result => {
// make a redirect to the requested app auth endpoint
const location = window.location;
const port = location.port ? ':' + location.port : '';
const state = getUrlParameter('state', location.search);
const authUrl = `https://${result.fqdn}${port}/x-teleport-auth`;
if (state === '') {
const clusterId = params.clusterId ? params.clusterId : '';
const publicAddr = params.publicAddr ? params.publicAddr : '';
window.location.replace(`${authUrl}?cluster=${clusterId}&addr=${publicAddr}`);
} else {
window.location.replace(`${authUrl}?state=${state}#value=${result.value}`);
}
resolveRedirectUrl(params)
.then(url => {
window.location.replace(url);
})
.catch((err: Error) => {
setAttempt({
Expand All @@ -54,3 +42,32 @@ export default function useAppLauncher() {
...attempt,
};
}

function resolveRedirectUrl(params: UrlLauncherParams) {
const location = window.location;
const port = location.port ? ':' + location.port : '';
const state = getUrlParameter('state', location.search);

// no state value: let the target app know of a new auth exchange
if (!state) {
return service.getAppFqdn(params).then(result => {
const url = new URL(`https://${result.fqdn}${port}/x-teleport-auth`);
if (params.clusterId) {
url.searchParams.set('cluster', params.clusterId);
}
if (params.publicAddr) {
url.searchParams.set('addr', params.publicAddr);
}

return url.toString();
});
}

// state value received: create new session for the target app
return service.createAppSession(params).then(result => {
const url = new URL(`https://${result.fqdn}${port}/x-teleport-auth`);
url.searchParams.set('state', state);
url.hash = `#value=${result.value}`;
return url.toString();
});
}
13 changes: 12 additions & 1 deletion packages/teleport/src/config.ts
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,8 @@ const cfg = {
},

api: {
aapSession: '/v1/webapi/sessions/app',
appSession: '/v1/webapi/sessions/app',
appFqdnPath: '/v1/webapi/apps/:fqdn/:clusterId?/:publicAddr?',
applicationsPath: '/v1/webapi/sites/:clusterId/apps',
clustersPath: '/v1/webapi/sites',
clusterEventsPath: `/v1/webapi/sites/:clusterId/events/search?from=:start?&to=:end?&limit=:limit?`,
Expand Down Expand Up @@ -113,6 +114,10 @@ const cfg = {
appNodeScriptPath: '/scripts/:token/install-app.sh?name=:name&uri=:uri',
},

getAppFqdnUrl(params: UrlAppParams) {
return generatePath(cfg.api.appFqdnPath, { ...params });
},

getClusterEventsUrl(clusterId: string, params: UrlClusterEventsParams) {
return generatePath(cfg.api.clusterEventsPath, {
clusterId,
Expand Down Expand Up @@ -294,6 +299,12 @@ export interface UrlParams {
serverId?: string;
}

export interface UrlAppParams {
fqdn: string;
clusterId?: string;
publicAddr?: string;
}

export interface UrlScpParams {
clusterId: string;
serverId: string;
Expand Down
20 changes: 11 additions & 9 deletions packages/teleport/src/services/apps/apps.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

import { map } from 'lodash';
import api from 'teleport/services/api';
import cfg from 'teleport/config';
import cfg, { UrlAppParams } from 'teleport/config';
import makeApp from './makeApps';

const service = {
Expand All @@ -26,10 +26,10 @@ const service = {
.then(json => map(json.items, makeApp));
},

createAppSession(params: CreateAppSessionParams) {
createAppSession(params: UrlAppParams) {
const { fqdn, clusterId = '', publicAddr = '' } = params;
return api
.post(cfg.api.aapSession, {
.post(cfg.api.appSession, {
fqdn,
cluster_name: clusterId,
public_addr: publicAddr,
Expand All @@ -39,12 +39,14 @@ const service = {
value: json.value as string,
}));
},

getAppFqdn(params: UrlAppParams) {
return api
.get(cfg.getAppFqdnUrl(params))
.then(json => ({
fqdn: json.fqdn as string,
}));
},
};

export default service;

type CreateAppSessionParams = {
fqdn: string;
clusterId?: string;
publicAddr?: string;
};

0 comments on commit 316ba2f

Please sign in to comment.