-
Notifications
You must be signed in to change notification settings - Fork 52
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
SLCORE-1032 Wrong token error during sync - [New design]
- Loading branch information
1 parent
4d358f9
commit 5361898
Showing
41 changed files
with
709 additions
and
179 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
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
44 changes: 44 additions & 0 deletions
44
backend/core/src/main/java/org/sonarsource/sonarlint/core/connection/ConnectionManager.java
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,44 @@ | ||
/* | ||
* SonarLint Core - Implementation | ||
* Copyright (C) 2016-2025 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonarsource.sonarlint.core.connection; | ||
|
||
import java.util.Optional; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
import org.sonarsource.sonarlint.core.serverapi.ServerApi; | ||
|
||
public interface ConnectionManager { | ||
ServerConnection getConnectionOrThrow(String connectionId); | ||
Optional<ServerConnection> tryGetConnection(String connectionId); | ||
|
||
/** | ||
* Having dedicated TransientConnection class makes sense only if we handle the connection errors from there. | ||
* Which brings up the problem of managing global state for notifications because we don't know the connection ID. <br/><br/> | ||
* On other hand providing ServerApis directly, all Web API calls from transient ServerApi are not protected by checks for connection state. | ||
* So we still can spam server with unprotected requests. | ||
* It's not a big problem because we don't use such requests during scheduled sync. | ||
* They are mostly related to setting up the connection or other user-triggered actions. | ||
*/ | ||
ServerApi getTransientConnection(String token, @Nullable String organization, String baseUrl); | ||
Optional<ServerConnection> getValidConnection(String connectionId); | ||
void withValidConnection(String connectionId, Consumer<ServerConnection> serverConnectionCall); | ||
<T> Optional<T> withValidConnectionAndReturn(String connectionId, Function<ServerConnection, Optional<T>> serverConnectionCall); | ||
} |
24 changes: 24 additions & 0 deletions
24
backend/core/src/main/java/org/sonarsource/sonarlint/core/connection/ConnectionState.java
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,24 @@ | ||
/* | ||
* SonarLint Core - Implementation | ||
* Copyright (C) 2016-2025 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonarsource.sonarlint.core.connection; | ||
|
||
public enum ConnectionState { | ||
NEVER_USED, ACTIVE, INVALID_CREDENTIALS, MISSING_PERMISSION, NETWORK_ERROR | ||
} |
106 changes: 106 additions & 0 deletions
106
backend/core/src/main/java/org/sonarsource/sonarlint/core/connection/ServerConnection.java
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,106 @@ | ||
/* | ||
* SonarLint Core - Implementation | ||
* Copyright (C) 2016-2025 SonarSource SA | ||
* mailto:info AT sonarsource DOT com | ||
* | ||
* This program is free software; you can redistribute it and/or | ||
* modify it under the terms of the GNU Lesser General Public | ||
* License as published by the Free Software Foundation; either | ||
* version 3 of the License, or (at your option) any later version. | ||
* | ||
* This program is distributed in the hope that it will be useful, | ||
* but WITHOUT ANY WARRANTY; without even the implied warranty of | ||
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU | ||
* Lesser General Public License for more details. | ||
* | ||
* You should have received a copy of the GNU Lesser General Public License | ||
* along with this program; if not, write to the Free Software Foundation, | ||
* Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. | ||
*/ | ||
package org.sonarsource.sonarlint.core.connection; | ||
|
||
import java.time.Period; | ||
import java.time.ZonedDateTime; | ||
import java.util.function.Consumer; | ||
import java.util.function.Function; | ||
import javax.annotation.Nullable; | ||
import org.sonarsource.sonarlint.core.rpc.protocol.SonarLintRpcClient; | ||
import org.sonarsource.sonarlint.core.rpc.protocol.client.sync.InvalidTokenParams; | ||
import org.sonarsource.sonarlint.core.serverapi.ServerApi; | ||
import org.sonarsource.sonarlint.core.serverapi.exception.ForbiddenException; | ||
import org.sonarsource.sonarlint.core.serverapi.exception.UnauthorizedException; | ||
import org.sonarsource.sonarlint.core.sync.LastWebApiErrorNotificationService; | ||
|
||
public class ServerConnection { | ||
|
||
private static final Period WRONG_TOKEN_NOTIFICATION_INTERVAL = Period.ofDays(1); | ||
private final String connectionId; | ||
private final ServerApi serverApi; | ||
private final SonarLintRpcClient client; | ||
private ConnectionState state = ConnectionState.NEVER_USED; | ||
@Nullable | ||
private ZonedDateTime lastNotificationTime; | ||
private final LastWebApiErrorNotificationService lastWebApiErrorNotificationService; | ||
|
||
public ServerConnection(String connectionId, ServerApi serverApi, LastWebApiErrorNotificationService lastWebApiErrorNotificationService, SonarLintRpcClient client) { | ||
this.connectionId = connectionId; | ||
this.serverApi = serverApi; | ||
this.lastWebApiErrorNotificationService = lastWebApiErrorNotificationService; | ||
this.lastNotificationTime = lastWebApiErrorNotificationService.getLastWebApiErrorNotification(connectionId); | ||
this.client = client; | ||
} | ||
|
||
public boolean isSonarCloud() { | ||
return serverApi.isSonarCloud(); | ||
} | ||
|
||
public boolean isValid() { | ||
return state == ConnectionState.NEVER_USED || state == ConnectionState.ACTIVE; | ||
} | ||
|
||
public <T> T withClientApiAndReturn(Function<ServerApi, T> serverApiConsumer) { | ||
try { | ||
var result = serverApiConsumer.apply(serverApi); | ||
state = ConnectionState.ACTIVE; | ||
return result; | ||
} catch (ForbiddenException e) { | ||
state = ConnectionState.INVALID_CREDENTIALS; | ||
notifyClientAboutWrongTokenIfNeeded(); | ||
} catch (UnauthorizedException e) { | ||
state = ConnectionState.MISSING_PERMISSION; | ||
notifyClientAboutWrongTokenIfNeeded(); | ||
} | ||
return null; | ||
} | ||
|
||
public void withClientApi(Consumer<ServerApi> serverApiConsumer) { | ||
try { | ||
serverApiConsumer.accept(serverApi); | ||
state = ConnectionState.ACTIVE; | ||
} catch (ForbiddenException e) { | ||
state = ConnectionState.INVALID_CREDENTIALS; | ||
notifyClientAboutWrongTokenIfNeeded(); | ||
} catch (UnauthorizedException e) { | ||
state = ConnectionState.MISSING_PERMISSION; | ||
notifyClientAboutWrongTokenIfNeeded(); | ||
} | ||
} | ||
|
||
private boolean shouldNotifyAboutWrongToken() { | ||
if (state != ConnectionState.INVALID_CREDENTIALS && state != ConnectionState.MISSING_PERMISSION) { | ||
return false; | ||
} | ||
if (lastNotificationTime == null) { | ||
return true; | ||
} | ||
return lastNotificationTime.plus(WRONG_TOKEN_NOTIFICATION_INTERVAL).isBefore(ZonedDateTime.now()); | ||
} | ||
|
||
private void notifyClientAboutWrongTokenIfNeeded() { | ||
if (shouldNotifyAboutWrongToken()) { | ||
client.invalidToken(new InvalidTokenParams(connectionId)); | ||
lastNotificationTime = ZonedDateTime.now(); | ||
lastWebApiErrorNotificationService.setLastWebApiErrorNotification(connectionId, lastNotificationTime); | ||
} | ||
} | ||
} |
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
Oops, something went wrong.