Skip to content

Commit af806de

Browse files
committed
Update oc command parsing to extract correct cluster url and token
This PR fixes #1967. Signed-off-by: Denis Golovin dgolovin@redhat.com
1 parent 30453ed commit af806de

File tree

3 files changed

+33
-13
lines changed

3 files changed

+33
-13
lines changed

package.json

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -209,6 +209,7 @@
209209
"onCommand:openshift.explorer.login",
210210
"onCommand:openshift.explorer.login.credentialsLogin",
211211
"onCommand:openshift.explorer.login.tokenLogin",
212+
"onCommand:openshift.explorer.login.clipboard",
212213
"onCommand:openshift.explorer.logout",
213214
"onCommand:openshift.explorer.refresh",
214215
"onCommand:openshift.componentTypesView.refresh",
@@ -347,6 +348,11 @@
347348
"title": "Login into Cluster with credentials",
348349
"category": "OpenShift"
349350
},
351+
{
352+
"command": "openshift.explorer.login.clipboard",
353+
"title": "Login into Cluster using URL and token from clipboard",
354+
"category": "OpenShift"
355+
},
350356
{
351357
"command": "openshift.explorer.switchContext",
352358
"title": "Switch Contexts",

src/openshift/cluster.ts

Lines changed: 24 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -306,7 +306,7 @@ export class Cluster extends OpenShiftItem {
306306
}
307307

308308
@vsCommand('openshift.explorer.login.tokenLogin')
309-
static async tokenLogin(userClusterUrl: string, skipConfirmation = false): Promise<string | null> {
309+
static async tokenLogin(userClusterUrl: string, skipConfirmation = false, userToken?: string): Promise<string | null> {
310310
let token: string;
311311
const response = await Cluster.requestLoginConfirmation(skipConfirmation);
312312

@@ -328,21 +328,36 @@ export class Cluster extends OpenShiftItem {
328328
clusterURL = await Cluster.getUrl();
329329
}
330330

331-
const ocToken = await window.showInputBox({
332-
value: token,
333-
prompt: 'Provide Bearer token for authentication to the API server',
334-
ignoreFocusOut: true,
335-
password: true
336-
});
337-
if (!ocToken) return null;
338-
331+
let ocToken: string;
332+
if (!userToken) {
333+
ocToken = await window.showInputBox({
334+
value: token,
335+
prompt: 'Provide Bearer token for authentication to the API server',
336+
ignoreFocusOut: true,
337+
password: true
338+
});
339+
if (!ocToken) return null;
340+
} else {
341+
ocToken = userToken;
342+
}
339343
return Progress.execFunctionWithProgress(`Login to the cluster: ${clusterURL}`,
340344
() => Cluster.odo.execute(Command.odoLoginWithToken(clusterURL, ocToken.trim()))
341345
.then((result) => Cluster.loginMessage(clusterURL, result))
342346
.catch((error) => Promise.reject(new VsCommandError(`Failed to login to cluster '${clusterURL}' with '${Filters.filterToken(error.message)}'!`, 'Failed to login to cluster')))
343347
);
344348
}
345349

350+
@vsCommand('openshift.explorer.login.clipboard')
351+
static async loginUsingClipboardInfo(): Promise<string | null> {
352+
const clipboard = await Cluster.readFromClipboard();
353+
if(!Cluster.ocLoginCommandMatches(clipboard)) {
354+
throw new VsCommandError('Cannot parse login command in clipboard.')
355+
}
356+
const url = Cluster.clusterURL(clipboard);
357+
const token = Cluster.getToken(clipboard);
358+
return Cluster.tokenLogin(url, true, token);
359+
}
360+
346361
static async loginMessage(clusterURL: string, result: CliExitData): Promise<string | undefined> {
347362
if (result.stderr === '') {
348363
Cluster.explorer.refresh();

src/openshift/openshiftItem.ts

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -84,13 +84,12 @@ export default class OpenShiftItem {
8484
}
8585

8686
static clusterURL(value: string): string | null {
87-
const urlRegex = value.match('(https?://[^ ]*)');
88-
return urlRegex ? urlRegex[0] : null;
87+
const urlRegex = value.match(/--server=(https?:\/\/[^ ]*)/);
88+
return urlRegex ? urlRegex[1] : null;
8989
}
9090

9191
static ocLoginCommandMatches(value: string): string | null {
92-
const ocLoginRegex = /oc login (http|https):(.*?) --token=(.*)/;
93-
return ocLoginRegex.test(value) ? value : null;
92+
return OpenShiftItem.clusterURL(value) !== null && OpenShiftItem.getToken(value) !== null ? value : null;
9493
}
9594

9695
static getToken(value: string): string | null {

0 commit comments

Comments
 (0)