Skip to content

Commit ac8c88b

Browse files
committed
Fixes concurrency issues. Refactoring and simplifications to improve readability
1 parent 8f919fd commit ac8c88b

File tree

17 files changed

+178
-237
lines changed

17 files changed

+178
-237
lines changed

app/credentials-management/src/main/java/org/phoebus/applications/credentialsmanagement/CredentialsManagementController.java

Lines changed: 90 additions & 96 deletions
Large diffs are not rendered by default.

app/credentials-management/src/main/java/org/phoebus/applications/credentialsmanagement/Messages.java

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -31,6 +31,7 @@ public class Messages {
3131
public static String SecureStoreErrorBody;
3232
public static String Title;
3333
public static String ServiceConnectionFailure;
34+
public static String UnknownError;
3435
public static String UserNotAuthenticated;
3536

3637
static

app/credentials-management/src/main/resources/org/phoebus/applications/credentialsmanagement/messages.properties

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -32,5 +32,6 @@ SecureStoreErrorTitle=Application Launch Failed
3232
SecureStoreErrorBody=Failed to instantiate the secure store
3333
ServiceConnectionFailure=Unable to connect to service
3434
Title=Credentials Management
35+
UnkownError=Unknown error
3536
Username=Username
3637
UserNotAuthenticated=User not authenticated

app/logbook/olog/client-es/src/main/java/org/phoebus/applications/logbook/authentication/OlogServiceAuthenticationProvider.java

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
package org.phoebus.applications.logbook.authentication;
2020

2121
import org.phoebus.olog.es.api.OlogHttpClient;
22+
import org.phoebus.security.authorization.AuthenticationStatus;
2223
import org.phoebus.security.authorization.ServiceAuthenticationException;
2324
import org.phoebus.security.authorization.ServiceAuthenticationProvider;
2425
import org.phoebus.security.tokens.AuthenticationScope;
@@ -31,22 +32,29 @@ public class OlogServiceAuthenticationProvider implements ServiceAuthenticationP
3132

3233
private final AuthenticationScope ologAuthenticationScope;
3334

34-
public OlogServiceAuthenticationProvider(){
35+
private final Logger logger = Logger.getLogger(OlogServiceAuthenticationProvider.class.getName());
36+
37+
public OlogServiceAuthenticationProvider() {
3538
ologAuthenticationScope = new OlogAuthenticationScope();
3639
}
3740

3841
@Override
39-
public void authenticate(String username, String password) throws ConnectException{
42+
public AuthenticationStatus authenticate(String username, String password) {
4043
try {
4144
OlogHttpClient.builder().build().authenticate(username, password);
42-
}
43-
catch(ConnectException e){
44-
throw e;
45+
return AuthenticationStatus.AUTHENTICATED;
46+
} catch (ConnectException e) {
47+
logger.log(Level.WARNING, "Unable to connect to logbook service");
48+
return AuthenticationStatus.SERVICE_OFFLINE;
49+
} catch (ServiceAuthenticationException e){
50+
logger.log(Level.WARNING, "User " + username + " not authenticated");
51+
return AuthenticationStatus.BAD_CREDENTIALS;
4552
}
4653
catch (Exception e) {
47-
Logger.getLogger(OlogServiceAuthenticationProvider.class.getName())
48-
.log(Level.WARNING, "Failed to authenticate user " + username + " with logbook service");
49-
throw new ServiceAuthenticationException("Failed to authenticate user " + username + " with logbook service");
54+
// NOTE!!! Exception message and/or stack trace could contain request URL and consequently
55+
// user's password, so do not log or propagate it.
56+
logger.log(Level.WARNING, "Failed to authenticate user " + username + " with logbook service, reason unknown");
57+
return AuthenticationStatus.UNKNOWN_ERROR;
5058
}
5159
}
5260

app/logbook/olog/client-es/src/main/java/org/phoebus/olog/es/api/OlogHttpClient.java

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@
2626
import org.phoebus.olog.es.api.model.OlogObjectMappers;
2727
import org.phoebus.olog.es.api.model.OlogSearchResult;
2828
import org.phoebus.olog.es.authentication.LoginCredentials;
29+
import org.phoebus.security.authorization.ServiceAuthenticationException;
2930
import org.phoebus.security.store.SecureStore;
3031
import org.phoebus.security.tokens.AuthenticationScope;
3132
import org.phoebus.security.tokens.ScopedAuthenticationToken;
@@ -35,6 +36,7 @@
3536
import javax.ws.rs.core.MultivaluedHashMap;
3637
import javax.ws.rs.core.MultivaluedMap;
3738
import java.io.InputStream;
39+
import java.net.ConnectException;
3840
import java.net.CookieManager;
3941
import java.net.CookiePolicy;
4042
import java.net.URI;
@@ -362,7 +364,6 @@ public void groupLogEntries(List<Long> logEntryIds) throws LogbookException {
362364
* @throws Exception if the login fails, e.g. bad credentials or service off-line.
363365
*/
364366
public void authenticate(String userName, String password) throws Exception {
365-
366367
String stringBuilder = Preferences.olog_url +
367368
"/login";
368369
HttpRequest request = HttpRequest.newBuilder()
@@ -372,9 +373,7 @@ public void authenticate(String userName, String password) throws Exception {
372373
.build();
373374
HttpResponse<String> response = httpClient.send(request, HttpResponse.BodyHandlers.ofString());
374375
if (response.statusCode() == 401) {
375-
throw new Exception("Failed to login: user unauthorized");
376-
} else if (response.statusCode() != 200) {
377-
throw new Exception("Failed to login, got HTTP status " + response.statusCode());
376+
throw new ServiceAuthenticationException("Failed to login: user not authorized");
378377
}
379378
}
380379

app/save-and-restore/app/src/main/java/org/phoebus/applications/saveandrestore/authentication/SaveAndRestoreAuthenticationProvider.java

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -19,8 +19,9 @@
1919

2020
package org.phoebus.applications.saveandrestore.authentication;
2121

22-
import org.phoebus.applications.saveandrestore.model.UserData;
2322
import org.phoebus.applications.saveandrestore.ui.SaveAndRestoreService;
23+
import org.phoebus.security.authorization.AuthenticationStatus;
24+
import org.phoebus.security.authorization.ServiceAuthenticationException;
2425
import org.phoebus.security.authorization.ServiceAuthenticationProvider;
2526
import org.phoebus.security.tokens.AuthenticationScope;
2627

@@ -34,28 +35,30 @@
3435
public class SaveAndRestoreAuthenticationProvider implements ServiceAuthenticationProvider {
3536

3637
private final AuthenticationScope saveAndRestoreAuthenticationScope;
38+
private final Logger logger = Logger.getLogger(SaveAndRestoreAuthenticationProvider.class.getName());
3739

3840
public SaveAndRestoreAuthenticationProvider() {
3941
saveAndRestoreAuthenticationScope = new SaveAndRestoreAuthenticationScope();
4042
}
4143

4244
@Override
43-
public void authenticate(String username, String password) throws ConnectException{
45+
public AuthenticationStatus authenticate(String username, String password) {
4446
SaveAndRestoreService saveAndRestoreService = SaveAndRestoreService.getInstance();
4547
try {
46-
UserData userData = saveAndRestoreService.authenticate(username, password);
47-
Logger.getLogger(SaveAndRestoreAuthenticationProvider.class.getName())
48-
.log(Level.INFO, "User " + userData.getUserName() + " successfully signed in");
49-
}
50-
catch(ConnectException e){
51-
throw e;
52-
}
53-
catch (Exception e) {
48+
saveAndRestoreService.authenticate(username, password);
49+
logger.log(Level.INFO, "User " + username + " successfully signed in");
50+
return AuthenticationStatus.AUTHENTICATED;
51+
} catch (ConnectException e) {
52+
logger.log(Level.WARNING, "Unable to connect to save&restore service");
53+
return AuthenticationStatus.SERVICE_OFFLINE;
54+
} catch (ServiceAuthenticationException e) {
55+
logger.log(Level.WARNING, "User " + username + " not authenticated");
56+
return AuthenticationStatus.BAD_CREDENTIALS;
57+
} catch (Exception e) {
5458
// NOTE!!! Exception message and/or stack trace could contain request URL and consequently
5559
// user's password, so do not log or propagate it.
56-
Logger.getLogger(SaveAndRestoreAuthenticationProvider.class.getName())
57-
.log(Level.WARNING, "Failed to authenticate user " + username + " with save&restore service");
58-
throw new RuntimeException("Failed to authenticate user " + username + " with save&restore service");
60+
logger.log(Level.WARNING, "Failed to authenticate user " + username + " with save&restore service, reason unknown");
61+
return AuthenticationStatus.UNKNOWN_ERROR;
5962
}
6063
}
6164

app/save-and-restore/app/src/main/java/org/phoebus/applications/saveandrestore/client/SaveAndRestoreClient.java

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -30,12 +30,10 @@
3030
import org.phoebus.applications.saveandrestore.model.SnapshotItem;
3131
import org.phoebus.applications.saveandrestore.model.Tag;
3232
import org.phoebus.applications.saveandrestore.model.TagData;
33-
import org.phoebus.applications.saveandrestore.model.UserData;
3433
import org.phoebus.applications.saveandrestore.model.search.Filter;
3534
import org.phoebus.applications.saveandrestore.model.search.SearchResult;
3635

3736
import javax.ws.rs.core.MultivaluedMap;
38-
import java.net.ConnectException;
3937
import java.util.List;
4038

4139
/**
@@ -249,10 +247,9 @@ public interface SaveAndRestoreClient {
249247
*
250248
* @param userName User's account name
251249
* @param password User's password
252-
* @return A {@link UserData} object if login is successful, otherwise implementation should throw
253-
* an exception.
250+
* @throws Exception e.g. if service is off-line or if user credentials are rejected.
254251
*/
255-
UserData authenticate(String userName, String password) throws ConnectException;
252+
void authenticate(String userName, String password) throws Exception;
256253

257254
/**
258255
* Requests service to restore the specified {@link SnapshotItem}s

app/save-and-restore/app/src/main/java/org/phoebus/applications/saveandrestore/client/SaveAndRestoreClientImpl.java

Lines changed: 12 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -24,15 +24,14 @@
2424
import org.phoebus.applications.saveandrestore.model.SnapshotItem;
2525
import org.phoebus.applications.saveandrestore.model.Tag;
2626
import org.phoebus.applications.saveandrestore.model.TagData;
27-
import org.phoebus.applications.saveandrestore.model.UserData;
2827
import org.phoebus.applications.saveandrestore.model.search.Filter;
2928
import org.phoebus.applications.saveandrestore.model.search.SearchResult;
29+
import org.phoebus.security.authorization.ServiceAuthenticationException;
3030
import org.phoebus.security.store.SecureStore;
3131
import org.phoebus.security.tokens.ScopedAuthenticationToken;
3232
import org.phoebus.util.http.QueryParamsHelper;
3333

3434
import javax.ws.rs.core.MultivaluedMap;
35-
import java.net.ConnectException;
3635
import java.net.CookieHandler;
3736
import java.net.CookieManager;
3837
import java.net.CookiePolicy;
@@ -577,21 +576,17 @@ public List<Node> deleteTag(TagData tagData) {
577576
* @return {@inheritDoc}
578577
*/
579578
@Override
580-
public UserData authenticate(String userName, String password) throws ConnectException {
581-
try {
582-
String stringBuilder = Preferences.jmasarServiceUrl +
583-
"/login";
584-
HttpRequest request = HttpRequest.newBuilder()
585-
.uri(URI.create(stringBuilder))
586-
.header("Content-Type", CONTENT_TYPE_JSON)
587-
.POST(HttpRequest.BodyPublishers.ofString(OBJECT_MAPPER.writeValueAsString(new LoginCredentials(userName, password))))
588-
.build();
589-
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
590-
return OBJECT_MAPPER.readValue(response.body(), UserData.class);
591-
} catch (ConnectException e) {
592-
throw e;
593-
} catch (Exception e) {
594-
throw new RuntimeException(e);
579+
public void authenticate(String userName, String password) throws Exception {
580+
String stringBuilder = Preferences.jmasarServiceUrl +
581+
"/login";
582+
HttpRequest request = HttpRequest.newBuilder()
583+
.uri(URI.create(stringBuilder))
584+
.header("Content-Type", CONTENT_TYPE_JSON)
585+
.POST(HttpRequest.BodyPublishers.ofString(OBJECT_MAPPER.writeValueAsString(new LoginCredentials(userName, password))))
586+
.build();
587+
HttpResponse<String> response = CLIENT.send(request, HttpResponse.BodyHandlers.ofString());
588+
if (response.statusCode() == 401) {
589+
throw new ServiceAuthenticationException("User not authenticated with save&restore service");
595590
}
596591
}
597592

app/save-and-restore/app/src/main/java/org/phoebus/applications/saveandrestore/ui/SaveAndRestoreService.java

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,6 @@
3333
import org.phoebus.applications.saveandrestore.model.SnapshotItem;
3434
import org.phoebus.applications.saveandrestore.model.Tag;
3535
import org.phoebus.applications.saveandrestore.model.TagData;
36-
import org.phoebus.applications.saveandrestore.model.UserData;
3736
import org.phoebus.applications.saveandrestore.model.search.Filter;
3837
import org.phoebus.applications.saveandrestore.model.search.SearchResult;
3938
import org.phoebus.core.vtypes.VDisconnectedData;
@@ -46,7 +45,6 @@
4645
import java.time.Instant;
4746
import java.util.ArrayList;
4847
import java.util.List;
49-
import java.util.Set;
5048
import java.util.concurrent.ExecutorService;
5149
import java.util.concurrent.Future;
5250
import java.util.concurrent.LinkedBlockingQueue;
@@ -316,17 +314,15 @@ public List<Node> deleteTag(TagData tagData) throws Exception {
316314
}
317315

318316
/**
319-
* Authenticate user, needed for all non-GET endpoints if service requires it
317+
* Authenticate user, needed for all non-GET endpoints if service requires it. Note that
318+
* this is executed in a synchronous manner, i.e. an {@link ExecutorService} is not used.
320319
*
321320
* @param userName User's account name
322321
* @param password User's password
323-
* @return A {@link UserData} object
324-
* @throws Exception if authentication fails
322+
* @throws Exception if authentication fails, e.g. service off-line or invalid credentials
325323
*/
326-
public UserData authenticate(String userName, String password) throws Exception {
327-
Future<UserData> future =
328-
executor.submit(() -> saveAndRestoreClient.authenticate(userName, password));
329-
return future.get();
324+
public void authenticate(String userName, String password) throws Exception {
325+
saveAndRestoreClient.authenticate(userName, password);
330326
}
331327

332328
/**

app/save-and-restore/app/src/test/java/org/phoebus/applications/saveandrestore/client/HttpClientTestIT.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,7 +92,7 @@ public static void cleanUp() {
9292
}
9393

9494
@Test
95-
public void testAuthenticate() throws ConnectException {
95+
public void testAuthenticate() throws Exception {
9696
saveAndRestoreClient.authenticate("admin", "adminPass");
9797
}
9898

0 commit comments

Comments
 (0)