-
Notifications
You must be signed in to change notification settings - Fork 166
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add faker for the pass through api to enable regular user testing (#806)
* Add faker for the pass through api to enable regular user testing * Add faker for backend openshift user * add impersonate function for developers * address comments and modify the doc * Update to get the access token by making API call * update doc * add error message * Fix lint issues * address comments --------- Co-authored-by: Juntao Wang <juntwang@redhat.com>
- Loading branch information
1 parent
0b996c1
commit 74188de
Showing
14 changed files
with
207 additions
and
13 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,10 @@ | ||
import { DEV_MODE } from './utils/constants'; | ||
|
||
let accessToken = ''; | ||
|
||
export const setImpersonateAccessToken = (token?: string): void => { | ||
accessToken = token || ''; | ||
}; | ||
|
||
export const isImpersonating = (): boolean => accessToken !== ''; | ||
export const getImpersonateAccessToken = (): string => (DEV_MODE ? accessToken : ''); |
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,81 @@ | ||
import { FastifyRequest } from 'fastify'; | ||
import https from 'https'; | ||
import createError from 'http-errors'; | ||
import { setImpersonateAccessToken } from '../../../devFlags'; | ||
import { KubeFastifyInstance } from '../../../types'; | ||
import { DEV_IMPERSONATE_PASSWORD, DEV_IMPERSONATE_USER } from '../../../utils/constants'; | ||
import { createCustomError } from '../../../utils/requestUtils'; | ||
import { devRoute } from '../../../utils/route-security'; | ||
|
||
export default async (fastify: KubeFastifyInstance): Promise<void> => { | ||
fastify.post( | ||
'/', | ||
devRoute(async (request: FastifyRequest<{ Body: { impersonate: boolean } }>) => { | ||
return new Promise<{ code: number; response: string }>((resolve, reject) => { | ||
const doImpersonate = request.body.impersonate; | ||
if (doImpersonate) { | ||
const apiPath = fastify.kube.config.getCurrentCluster().server; | ||
const namedHost = apiPath.slice('https://api.'.length).split(':')[0]; | ||
const url = `https://oauth-openshift.apps.${namedHost}/oauth/authorize?response_type=token&client_id=openshift-challenging-client`; | ||
const httpsRequest = https | ||
.get( | ||
url, | ||
{ | ||
headers: { | ||
Authorization: `Basic ${Buffer.from( | ||
`${DEV_IMPERSONATE_USER}:${DEV_IMPERSONATE_PASSWORD}`, | ||
).toString('base64')}`, | ||
}, | ||
}, | ||
(res) => { | ||
// 302 Found means the success of this call | ||
if (res.statusCode === 302) { | ||
/** | ||
* we will get the location in the headers like: | ||
* https://oauth-openshift.apps.juntwang.dev.datahub.redhat.com/oauth/token/implicit#access_token={ACCESS_TOKEN_WE_WANT} | ||
* &expires_in=86400&scope=user%3Afull&token_type=Bearer | ||
*/ | ||
const searchParams = new URLSearchParams(res.headers.location.split('#')[1]); | ||
const accessToken = searchParams.get('access_token'); | ||
if (accessToken) { | ||
setImpersonateAccessToken(accessToken); | ||
resolve({ code: 200, response: accessToken }); | ||
} else { | ||
reject({ | ||
code: 500, | ||
response: 'Cannot fetch the impersonate token from the server.', | ||
}); | ||
} | ||
} else { | ||
reject({ | ||
code: 403, | ||
response: | ||
'Authorization error, please check the username and password in your local env file.', | ||
}); | ||
} | ||
}, | ||
) | ||
.on('error', () => { | ||
reject({ | ||
code: 500, | ||
response: 'There are some errors on the server, please try again later.', | ||
}); | ||
}); | ||
httpsRequest.end(); | ||
} else { | ||
setImpersonateAccessToken(''); | ||
resolve({ code: 200, response: '' }); | ||
} | ||
}).catch((e: createError.HttpError) => { | ||
if (e?.code) { | ||
throw createCustomError( | ||
'Error impersonating user', | ||
e.response?.message || 'Impersonating user error', | ||
e.code, | ||
); | ||
} | ||
throw e; | ||
}); | ||
}), | ||
); | ||
}; |
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
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,11 @@ | ||
import axios from 'axios'; | ||
|
||
export const updateImpersonateSettings = (impersonate: boolean): Promise<void> => { | ||
const url = '/api/dev-impersonate'; | ||
return axios | ||
.post(url, { impersonate }) | ||
.then((response) => response.data) | ||
.catch((e) => { | ||
throw new Error(e.response.data.message); | ||
}); | ||
}; |