Changed log

This commit is contained in:
Fabio Sinibaldi 2021-02-18 11:39:32 +01:00
parent 1e56b5deda
commit e626e9ebbe
3 changed files with 38 additions and 31 deletions

View File

@ -7,6 +7,7 @@
<groupId>org.gcube.spatial.data</groupId>
<version>1.0.0-SNAPSHOT</version>
</parent>
<groupId>org.gcube.data.access.geoserver</groupId>
<artifactId>gcube-geoserver-connector</artifactId>
<version>0.2.0-SNAPSHOT</version>
<name>Generic SDI Client</name>

View File

@ -1,11 +1,13 @@
package org.gcube.data.access.connector;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import java.util.Base64;
import java.util.List;
import javax.servlet.http.HttpServletRequest;
import javax.xml.bind.annotation.XmlRootElement;
import java.util.Base64;
import org.gcube.common.authorization.library.AuthorizationEntry;
import org.gcube.common.authorization.library.provider.SecurityTokenProvider;
import org.gcube.common.resources.gcore.GCoreEndpoint;
@ -20,23 +22,21 @@ import org.gcube.resources.discovery.icclient.ICFactory;
import org.gcube.smartgears.handlers.application.RequestEvent;
import org.gcube.smartgears.handlers.application.RequestHandler;
import org.gcube.smartgears.handlers.application.request.RequestError;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.util.StringUtils;
import static org.gcube.common.authorization.client.Constants.authorizationService;
import lombok.extern.slf4j.Slf4j;
@XmlRootElement(name = GeoServerConnectorRequestHandler.REQUEST_HANDLER_NAME)
@Slf4j
public class GeoServerConnectorRequestHandler extends RequestHandler {
protected static final String REQUEST_HANDLER_NAME = "authentication-filter";
private static final String GEOSERVER_CREDENTIALS = "/GeoServer/credentials/";
private Logger logger;
private GCubeCache<String, String> gCubeCache;
private GCubeRestClient restClient = new GCubeRestClient();
public GeoServerConnectorRequestHandler() {
logger = LoggerFactory.getLogger(this.getClass());
gCubeCache = new GCubeCache<>(AuthenticationUtils.TIME_TO_LIVE, AuthenticationUtils.TIMER_INTERVAL,
AuthenticationUtils.MAX_ITEMS_CACHE);
}
@ -49,7 +49,7 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
@Override
public void handleRequest(RequestEvent e) {
logger.warn("Handling request");
log.warn("Handling request");
HttpServletRequest httpServletRequest = e.request();
// get host from ApplicationContext
@ -57,15 +57,15 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
// get token from request
String token = getToken(httpServletRequest);
logger.warn("Retrieve token from request = " + token);
log.warn("Retrieve token from request = " + token);
if (StringUtils.hasText(token)) {
logger.warn("Token found: " + token);
log.warn("Token found: " + token);
if (validateToken(token)) {
// retrieve endpoint to get credentials in GeoServer
String endpoint = getEndpoint(token);
logger.warn("Endpoint found: " + endpoint);
log.warn("Endpoint found: " + endpoint);
// TODO - Can be the endpoint stored in the cache object?
if (StringUtils.hasText(endpoint)) {
@ -75,7 +75,7 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
// check current token with tokenCache
if (token.equals(tokenCache)) {
logger.warn("Set credentials attribute retrieved from cache " + usernameCache + " "
log.warn("Set credentials attribute retrieved from cache " + usernameCache + " "
+ passwordCache);
httpServletRequest.setAttribute(AuthenticationUtils.USERNAME, usernameCache);
httpServletRequest.setAttribute(AuthenticationUtils.PASSWORD, passwordCache);
@ -85,7 +85,7 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
+ AuthenticationUtils.GCUBE_QUERY_STRING + "=" + token;
// put credentials in the filter
AccessibleCredentialsEntity accessibleCredentials = restClient.getAccessibleCredentials(url);
logger.warn("Credentials: " + accessibleCredentials.getUsername() + "/"
log.warn("Credentials: " + accessibleCredentials.getUsername() + "/"
+ accessibleCredentials.getPassword());
httpServletRequest.setAttribute(AuthenticationUtils.USERNAME,
accessibleCredentials.getUsername());
@ -93,20 +93,20 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
accessibleCredentials.getPassword());
// set/update data in the cache
logger.warn("Put token in the cache: " + token);
log.warn("Put token in the cache: " + token);
gCubeCache.put(AuthenticationUtils.TOKEN_CACHE, token);
logger.warn("Put also username and password in the cache");
log.warn("Put also username and password in the cache");
gCubeCache.put(AuthenticationUtils.USERNAME, accessibleCredentials.getUsername());
gCubeCache.put(AuthenticationUtils.PASSWORD, accessibleCredentials.getPassword());
}
}
} else {
logger.error("Invalid token in the request");
log.error("Invalid token in the request");
RequestError.request_not_authorized_error.fire("Invalid token in the request");
}
} else {
logger.warn("Token not present in the request: NO/OP");
log.warn("Token not present in the request: NO/OP");
}
}
@ -121,14 +121,14 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
// case 1 - get token from gcube-token query-string
String gCubeToken = httpServletRequest.getParameter(AuthenticationUtils.GCUBE_QUERY_STRING);
if (StringUtils.hasText(gCubeToken)) {
logger.warn("Get token from query-string");
log.warn("Get token from query-string");
return gCubeToken;
}
// case 2 - get token from gcube-token header
gCubeToken = httpServletRequest.getHeader(AuthenticationUtils.GCUBE_QUERY_STRING);
if (StringUtils.hasText(gCubeToken)) {
logger.warn("Get token from gcube-token header");
log.warn("Get token from gcube-token header");
return gCubeToken;
}
@ -136,7 +136,7 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
String authorization = httpServletRequest.getHeader(AuthenticationUtils.AUTHORIZATION);
if (StringUtils.hasText(authorization)
&& StringUtils.startsWithIgnoreCase(authorization, AuthenticationUtils.BASIC)) {
logger.warn("Get token from basic authorization header");
log.warn("Get token from basic authorization header");
// header = Authorization: Basic base64credentials
String base64Credentials = StringUtils.delete(authorization, AuthenticationUtils.BASIC);
String credentials = new String(Base64.getDecoder().decode(StringUtils.trimWhitespace(base64Credentials)));
@ -146,22 +146,22 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
return values[1];
}
logger.warn("gcube-token not found in query-string, in header and in basic authorization header");
log.warn("gcube-token not found in query-string, in header and in basic authorization header");
// case 4 - get token from HTML form in the password field
gCubeToken = httpServletRequest.getParameter(AuthenticationUtils.PASSWORD);
if (StringUtils.hasText(gCubeToken)) {
logger.warn("Get token from HTML form (in the password field)");
log.warn("Get token from HTML form (in the password field)");
String user = httpServletRequest.getParameter(AuthenticationUtils.USERNAME);
logger.warn("Get username from HTML form: " + user);
log.warn("Get username from HTML form: " + user);
if (StringUtils.hasText(user) && user.equals(getUser(gCubeToken))) //check username
return gCubeToken;
logger.warn("Username doesn't match with ClientInfo of gcube");
log.warn("Username doesn't match with ClientInfo of gcube");
} else
logger.warn("gcube-token also not found in the HTML form in the password field");
log.warn("gcube-token also not found in the HTML form in the password field");
return null;
}
@ -171,7 +171,7 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
try {
AuthorizationEntry authorizationEntry = authorizationService().get(token);
String scope = authorizationEntry.getContext();
logger.warn("Set scope in to " + scope);
log.warn("Set scope in to " + scope);
ScopeProvider.instance.set(scope);
SecurityTokenProvider.instance.set(token);
@ -189,7 +189,7 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
List<GCoreEndpoint> gCoreEndpoints = client.submit(query);
int size = gCoreEndpoints.size();
logger.warn("gCoreEndpoints size = " + size);
log.warn("gCoreEndpoints size = " + size);
if (size > 0) {//I get only the first. Usually it must be only one
GCoreEndpoint gCoreEndpoint = gCoreEndpoints.get(0);
@ -198,7 +198,7 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
}
} catch (Exception ex) {
logger.error("Error in getEndpoint() method: " + ex.getMessage());
log.error("Error in getEndpoint() method: " + ex.getMessage());
}
return null;
@ -209,14 +209,14 @@ public class GeoServerConnectorRequestHandler extends RequestHandler {
AuthorizationEntry authorizationEntry = authorizationService().get(token);
return authorizationEntry.getClientInfo().getId();
} catch (Exception ex) {
logger.error("Error in getUser() method: " + ex.getMessage());
log.error("Error in getUser() method: " + ex.getMessage());
}
return null;
}
private boolean validateToken(String token) {
// TODO How to implement the validation of the token
logger.warn("Validate token in progress...");
log.warn("Validate token in progress...");
return true;
}
}

View File

@ -1,7 +1,13 @@
package org.gcube.spatial.data.clients.model;
import java.util.ArrayList;
import java.util.List;
import org.gcube.spatial.data.sdi.model.credentials.Credentials;
import lombok.Data;
import lombok.NonNull;
import lombok.experimental.Delegate;
@Data
public class ConnectionDescriptor {
@ -9,6 +15,6 @@ public class ConnectionDescriptor {
@NonNull
private String endpoint;
@Delegate
private List<Credentials> credentials=new ArrayList<Credentials>();
}