Skip to content
This repository has been archived by the owner on Apr 4, 2023. It is now read-only.

Align token request with hosted Che #3

Merged
merged 2 commits into from
Jul 9, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 2 additions & 1 deletion package.json
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@
"@theia/plugin-packager": "latest",
"rimraf": "2.6.2",
"typescript-formatter": "7.2.2",
"typescript": "2.9.2"
"typescript": "~3.9.2",
"axios": "0.19.2"
},
"scripts": {
"prepare": "yarn run clean && yarn run build",
Expand Down
24 changes: 23 additions & 1 deletion src/che-openshift-authentication.ts
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,26 @@
import * as theia from '@theia/plugin';
import * as che from '@eclipse-che/plugin';
import { spawn } from 'child_process';
import axios, { AxiosInstance } from 'axios';

interface Attributes {
cluster: string
}

interface Data {
attributes: Attributes
}

interface OsUserResponse {
data: Data[]
}

export async function start(context: theia.PluginContext) {
const machineToken = process.env['CHE_MACHINE_TOKEN'];
const isMultiUser = !!(machineToken && machineToken.length > 0);
const axiosInstance: AxiosInstance = axios;
const cheApi = process.env['CHE_API'];
const isHostedChe = cheApi && cheApi.indexOf('https://che.openshift.io/api') !== -1;

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Can we somehow avoid hardcoding of domain name here?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We can make an API request but I think it is not a good solution.

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

isn't it looking wired to hardcode an hosted platform there ?

Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I mean, for example there is preview platform as well, etc, so maybe we need another way of saying it's an 'hosted che' ?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Do you have any other ideas how to detect Hosted Che?

// getProviders method is not supported for multi-user Mode
if (isMultiUser) {
if (!await che.oAuth.isRegistered('openshift-v3') && !await che.oAuth.isRegistered('openshift-v4')) {
Expand Down Expand Up @@ -86,7 +102,13 @@ export async function start(context: theia.PluginContext) {
}

function getServerUrl(): Promise<string> {
return new Promise<string>((resolve, reject) => {
return new Promise<string>(async (resolve, reject) => {
if (isHostedChe) {
const user = await che.user.getCurrentUser();
const osUserResponse = await axiosInstance.get<OsUserResponse>('https://api.openshift.io/api/users?filter[username]=' + user.name);
Copy link

@vitaliy-guliy vitaliy-guliy Jul 8, 2020

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think it's possible to remove declarations of isHostedChe and cheApi and always ask the API.

if (process.env['CHE_API']) {
   const user = await che.user.getCurrentUser();
   const osUserResponse = await axiosInstance.get<OsUserResponse>(process.env['CHE_API'] + '/users?filter[username]=' + user.name);
   resolve(osUserResponse.data.data[0].attributes.cluster);
   return;
}

WDYT?

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This request is redundant in case when the plugin doesn't run on Hosted Che

resolve(osUserResponse.data.data[0].attributes.cluster);
return;
}
let result = '';
const versionCommand = spawn('odo', ['version']);
// tslint:disable-next-line:no-any
Expand Down
35 changes: 28 additions & 7 deletions yarn.lock
Original file line number Diff line number Diff line change
Expand Up @@ -8,9 +8,9 @@
integrity sha512-4CgKEGCBOIOBGBNoH0dhN8TkP1Sj39fG4LGCXYw3JB7nQucVooJyq7AhIV+w7L4iZ+ln+y2KEfZugCmOIuzIeQ==

"@eclipse-che/plugin@latest":
version "0.0.1-1586429785"
resolved "https://registry.yarnpkg.com/@eclipse-che/plugin/-/plugin-0.0.1-1586429785.tgz#6dea7faca00c261a9f14f45fd8398205f4948f62"
integrity sha512-C7VVc/XMPr52een6KIXnDCjZiOcelPTqy1uj2HcSOmBEpc/aXscIrpF8UE9UN+V5rr3GUlYlYGWJqTQFMjeILw==
version "0.0.1-1594040164"
resolved "https://registry.yarnpkg.com/@eclipse-che/plugin/-/plugin-0.0.1-1594040164.tgz#5501b9a032d989dd65dc32bc8044f61d6a6682f7"
integrity sha512-QOwu77veHvBmI/w1s8utCqa2mKUqEgIRgIByXQZsVO7Ba7GVqcnPRU/t4kSrKW41y525uHlkJor2iTk++ie3eQ==
dependencies:
"@eclipse-che/api" latest

Expand Down Expand Up @@ -136,6 +136,13 @@ atob@^2.1.2:
resolved "https://registry.yarnpkg.com/atob/-/atob-2.1.2.tgz#6d9517eb9e030d2436666651e86bd9f6f13533c9"
integrity sha512-Wm6ukoaOGJi/73p/cl2GvLjTI5JM1k/O14isD73YML8StrH/7/lRFgmg8nICZgD3bZZvjwCGxtMOD3wWNAu8cg==

axios@0.19.2:
version "0.19.2"
resolved "https://registry.yarnpkg.com/axios/-/axios-0.19.2.tgz#3ea36c5d8818d0d5f8a8a97a6d36b86cdc00cb27"
integrity sha512-fjgm5MvRHLhx+osE2xoekY70AhARk3a6hkN+3Io1jc00jtquGvxYlKlsFUhmUET0V5te6CcZI7lcv2Ym61mjHA==
dependencies:
follow-redirects "1.5.10"

balanced-match@^1.0.0:
version "1.0.0"
resolved "https://registry.yarnpkg.com/balanced-match/-/balanced-match-1.0.0.tgz#89b4d199ab2bee49de164ea02b89ce462d71b767"
Expand Down Expand Up @@ -359,6 +366,13 @@ cross-spawn@^5.0.1:
shebang-command "^1.2.0"
which "^1.2.9"

debug@=3.1.0:
version "3.1.0"
resolved "https://registry.yarnpkg.com/debug/-/debug-3.1.0.tgz#5bb5a0672628b64149566ba16819e61518c67261"
integrity sha512-OX8XqP7/1a9cqkxYw2yXss15f26NKWBpDXQd0/uK/KPqdQhxbPa994hnzjcE2VqQpDslf55723cKPUOGSmMY3g==
dependencies:
ms "2.0.0"

debug@^2.2.0, debug@^2.3.3:
version "2.6.9"
resolved "https://registry.yarnpkg.com/debug/-/debug-2.6.9.tgz#5d128515df134ff327e90a4c93f4e077a536341f"
Expand Down Expand Up @@ -501,6 +515,13 @@ find-up@^3.0.0:
dependencies:
locate-path "^3.0.0"

follow-redirects@1.5.10:
version "1.5.10"
resolved "https://registry.yarnpkg.com/follow-redirects/-/follow-redirects-1.5.10.tgz#7b7a9f9aea2fdff36786a94ff643ed07f4ff5e2a"
integrity sha512-0V5l4Cizzvqt5D44aTXbFZz+FtyXV1vrDN6qrelxtfYQKW0KO0W2T/hkE8xvGa/540LkZlkaUjO4ailYTFtHVQ==
dependencies:
debug "=3.1.0"

for-in@^1.0.2:
version "1.0.2"
resolved "https://registry.yarnpkg.com/for-in/-/for-in-1.0.2.tgz#81068d295a8142ec0ac726c6e2200c30fb6d5e80"
Expand Down Expand Up @@ -1388,10 +1409,10 @@ typescript-formatter@7.2.2:
commandpost "^1.0.0"
editorconfig "^0.15.0"

typescript@2.9.2:
version "2.9.2"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-2.9.2.tgz#1cbf61d05d6b96269244eb6a3bce4bd914e0f00c"
integrity sha512-Gr4p6nFNaoufRIY4NMdpQRNmgxVIGMs4Fcu/ujdYk3nAZqk7supzBE9idmvfZIlH/Cuj//dvi+019qEue9lV0w==
typescript@~3.9.2:
version "3.9.6"
resolved "https://registry.yarnpkg.com/typescript/-/typescript-3.9.6.tgz#8f3e0198a34c3ae17091b35571d3afd31999365a"
integrity sha512-Pspx3oKAPJtjNwE92YS05HQoY7z2SFyOpHo9MqJor3BXAGNaPUs83CuVp9VISFkSjyRfiTpmKuAYGJB7S7hOxw==

union-value@^1.0.0:
version "1.0.1"
Expand Down