Skip to content

Commit

Permalink
Fix connection lost detection (#421)
Browse files Browse the repository at this point in the history
  • Loading branch information
Or-Geva authored Oct 5, 2023
1 parent bf5eba7 commit 4a22e3a
Show file tree
Hide file tree
Showing 4 changed files with 47 additions and 20 deletions.
5 changes: 5 additions & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,11 @@
"view": "jfrog.issues",
"contents": "Your project was scanned and we didn't find any security issues.\n[Rescan](command:jfrog.scan.refresh)",
"when": "jfrog.connection.status == signedIn && !jfrog.firstScanInWorkspace"
},
{
"view": "jfrog.issues",
"contents": "Couldn't connect to your JFrog Platform. \n[Reconnect](command:jfrog.xray.reConnect) \n [Reset your connection details](command:jfrog.xray.resetConnection)",
"when": "jfrog.connection.status == connectionLost"
}
],
"viewsContainers": {
Expand Down
37 changes: 25 additions & 12 deletions src/main/connect/connectionManager.ts
Original file line number Diff line number Diff line change
Expand Up @@ -81,10 +81,8 @@ export class ConnectionManager implements ExtensionComponent, vscode.Disposable
this._context = context;
switch (await this.getConnectionStatus()) {
case SessionStatus.SignedIn:
await this.handledSignedIn();
break;
case SessionStatus.connectionLost:
await this.handledConnectionLost();
await this.handledSignedIn();
break;
case SessionStatus.SignedOut:
this.setConnectionView(SessionStatus.SignedOut);
Expand All @@ -111,16 +109,16 @@ export class ConnectionManager implements ExtensionComponent, vscode.Disposable
}

private async handledSignedIn() {
if (!(await this.loadCredential())) {
await this.disconnect();
return;
}
if (!(await this.connect())) {
this.setConnectionView(SessionStatus.connectionLost);
await this.setConnectionStatus(SessionStatus.connectionLost);
}
}

private async handledConnectionLost() {
return this.handledSignedIn();
}

private async handelUnknownState() {
if (!(await this.connect())) {
this.setConnectionView(SessionStatus.SignedOut);
Expand All @@ -130,11 +128,7 @@ export class ConnectionManager implements ExtensionComponent, vscode.Disposable

public async connect(): Promise<boolean> {
this._logManager.logMessage('Trying to read credentials from Secret Storage...', 'DEBUG');
const credentialsSet: boolean =
(await this.setUrlsFromFilesystem()) &&
(((await this.setUsernameFromFilesystem()) && (await this.getPasswordFromSecretStorage())) ||
(await this.getAccessTokenFromSecretStorage()));
if (!credentialsSet) {
if (!(await this.pingCredential())) {
this.deleteCredentialsFromMemory();
return false;
}
Expand All @@ -143,6 +137,25 @@ export class ConnectionManager implements ExtensionComponent, vscode.Disposable
return true;
}

private async pingCredential(): Promise<boolean> {
if (await this.loadCredential()) {
try {
return await ConnectionUtils.validateXrayConnection(this.xrayUrl, this._username, this._password, this._accessToken);
} catch (error) {
return false;
}
}
return false;
}

public async loadCredential(): Promise<boolean> {
return (
(await this.setUrlsFromFilesystem()) &&
(((await this.setUsernameFromFilesystem()) && (await this.getPasswordFromSecretStorage())) ||
(await this.getAccessTokenFromSecretStorage()))
);
}

public async onSuccessConnect() {
await this.setConnectionStatus(SessionStatus.SignedIn);
this.setConnectionView(SessionStatus.SignedIn);
Expand Down
15 changes: 15 additions & 0 deletions src/main/connect/connectionUtils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,21 @@ export class ConnectionUtils {
.ping();
}

/**
* Validate Xray connection.
* @param xray - xray URL
* @param username - Username
* @param password - Password
* @param accessToken - Access Token
*/
public static async validateXrayConnection(xray: string, username: string, password: string, accessToken: string): Promise<boolean> {
let jfrogClient: JfrogClient = this.createJfrogClient('', '', xray, username, password, accessToken);
return await jfrogClient
.xray()
.system()
.ping();
}

public static async isPlatformUrl(url: string, username: string, password: string, accessToken: string): Promise<boolean> {
// If URL ends with '/xray', the URL is an Xray URL
if (!url || url.endsWith('/xray') || url.endsWith('/xray/')) {
Expand Down
10 changes: 2 additions & 8 deletions src/test/tests/connectionManager.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -227,37 +227,31 @@ describe('Connection Manager Tests', () => {

it('Read username and password', async () => {
// Call the function
const result: boolean = await mockConnectionManager.connect();
const result: boolean = await mockConnectionManager.loadCredential();

// Check the return value and ensure that necessary methods are called
assert.isTrue(result);
sinon.assert.calledWith(logMessageStub, 'Trying to read credentials from Secret Storage...', 'DEBUG');
sinon.assert.calledOnce(setUrlsFromFilesystemStub);
sinon.assert.calledOnce(setUsernameFromFilesystemStub);
sinon.assert.calledOnce(getPasswordFromSecretStorageStub);
sinon.assert.notCalled(getAccessTokenFromSecretStorageStub);
sinon.assert.notCalled(deleteCredentialsFromMemoryStub);
sinon.assert.calledOnce(resolveUrlsStub);
sinon.assert.calledOnce(onSuccessConnectStub);
});
it('Read access token', async () => {
setUsernameFromFilesystemStub.resolves(false);
getPasswordFromSecretStorageStub.resolves(false);
getAccessTokenFromSecretStorageStub.resolves(true);

// Call the function
const result: boolean = await mockConnectionManager.connect();
const result: boolean = await mockConnectionManager.loadCredential();

// Check the return value and ensure that necessary methods are called
assert.isTrue(result);
sinon.assert.calledWith(logMessageStub, 'Trying to read credentials from Secret Storage...', 'DEBUG');
sinon.assert.calledOnce(setUrlsFromFilesystemStub);
sinon.assert.calledOnce(setUsernameFromFilesystemStub);
sinon.assert.notCalled(getPasswordFromSecretStorageStub);
sinon.assert.calledOnce(getAccessTokenFromSecretStorageStub);
sinon.assert.notCalled(deleteCredentialsFromMemoryStub);
sinon.assert.calledOnce(resolveUrlsStub);
sinon.assert.calledOnce(onSuccessConnectStub);
});

it('Empty KeyStore', async () => {
Expand Down

0 comments on commit 4a22e3a

Please sign in to comment.