refactoring code

This commit is contained in:
Roberto Cirillo 2021-05-12 17:11:18 +02:00
parent 922c000518
commit e4710d4932
7 changed files with 77 additions and 46 deletions

View File

@ -243,14 +243,29 @@ public class UserAccountingConsumer extends Thread{
}
private void report(OpLogRemoteObject record, String scope, String totVolume, String totCount) throws MalformedURLException, OpenIdConnectRESTHelperException{
private void report(OpLogRemoteObject record, String scope, String totVolume, String totCount){
// ACCOUNTING CALL TYPE: STORAGE USAGE
StorageUsageRecord sur=report.setGenericProperties(null, "storage-usage", record.getLastUser(), scope, record.getCreationTime(), record.getLastAccess(), record.getOwner(), record.getLastOperation(), record.getLength()+"");
sur=report.setSpecificProperties(sur, record.getFilename(), "STORAGE", record.getCallerIp(), record.getId());
logger.info("[accounting call] type: storage usage ");
URL oidcAddress=new URL(oidcEndpoint);
Utils.setToken(oidcAddress, clientId, secret);
logger.info("storage usage record ready");
logger.debug("try to call: "+oidcEndpoint);
URL oidcAddress;
try {
logger.trace("preparing URL");
oidcAddress = new URL(oidcEndpoint);
logger.trace("URL ready: "+oidcAddress);
Utils.setToken(oidcAddress, clientId, secret);
logger.trace("token ready");
} catch (MalformedURLException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("Malformed URL ",e);
} catch (OpenIdConnectRESTHelperException e) {
// TODO Auto-generated catch block
e.printStackTrace();
logger.error("OpenIdConnectRESTHelperException ",e);
}
logger.debug("sending record to the accounting");
report.printRecord(sur);
report.send(sur);
@ -278,29 +293,6 @@ public class UserAccountingConsumer extends Thread{
return scope;
}else logger.error("Scope bad format: scope not retrieved from string: "+filename);
return null;
}
public JWTToken setToken(String oidcEndpoint, String clientId, String secret) {
String context=ScopeProvider.instance.get();
logger.info(clientId+" getting the UMAtoken for context "+context);
URL oidcAddress = null;
JWTToken token =null;
try {
oidcAddress = new URL(oidcEndpoint);
} catch (MalformedURLException e) {
logger.error("Malformed URL "+e);
e.printStackTrace();
}
try {
logger.debug("from "+oidcEndpoint);
token = OpenIdConnectRESTHelper.queryUMAToken(oidcAddress, clientId, secret, context, null);
logger.info("UMA token retrieved for context "+context);
UmaJWTProvider.instance.set(token.toString());
} catch (OpenIdConnectRESTHelperException e) {
logger.error( "failed to contacting the OIDC provider!", e );
}
return token;
}
}
}

View File

@ -75,7 +75,7 @@ public class ReadingMongoOplog extends Thread{
while (cursor.hasNext()) {
try{
DBObject x = cursor.next();
logger.debug("oplog current object: "+x);
logger.trace("oplog current object: "+x);
ts = (BSONTimestamp) x.get("ts");
String ns=(String)x.get("ns");
// check if discard or process the current DB record

View File

@ -52,7 +52,7 @@ public class MongoDB {
}
public MongoDB(String[] server, String user, String password){
logger.debug("MongoDB init with "+user+" "+password);
logger.debug("MongoDB try to authenticate ");
this.server=server;
this.pwd=password;
this.user=user;

View File

@ -1,13 +1,19 @@
package org.gcube.contentmanager.storageserver.utils;
import java.net.MalformedURLException;
import java.io.IOException;
import java.io.Reader;
import java.io.StringReader;
import java.net.URL;
import java.util.Objects;
import org.gcube.common.authorization.library.provider.UmaJWTProvider;
import org.gcube.common.scope.api.ScopeProvider;
import org.gcube.oidc.rest.JWTToken;
import org.gcube.oidc.rest.OpenIdConnectRESTHelper;
import org.gcube.oidc.rest.OpenIdConnectRESTHelperException;
import org.json.simple.JSONObject;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
@ -16,14 +22,45 @@ public class Utils {
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
public static JWTToken setToken(URL oidcAddress, String clientId, String secret) throws OpenIdConnectRESTHelperException {
String context=ScopeProvider.instance.get();
logger.info(clientId+" getting the UMAtoken for context "+context);
JWTToken token =null;
logger.debug("from "+oidcAddress);
token = OpenIdConnectRESTHelper.queryUMAToken(oidcAddress, clientId, secret, context, null);
logger.info("UMA token retrieved for context "+context);
UmaJWTProvider.instance.set(token.toString());
return token;
JWTToken token=null;
try {
logger.trace("setToken method");
String context=ScopeProvider.instance.get();
logger.trace("we are in "+context);
if((!Objects.isNull(context)) && (!Objects.isNull(clientId))&& (!Objects.isNull(secret))) {
logger.info(clientId+" getting the UMAtoken for context "+context);
logger.debug("from "+oidcAddress);
token = OpenIdConnectRESTHelper.queryUMAToken(oidcAddress, clientId, secret, context, null);
logger.debug("UMA token retrieved for context "+context);
String tokenExtracted=getTokenFromJson(token);
UmaJWTProvider.instance.set(tokenExtracted);
}else {
logger.error("problem initializing token, one or more field not set");
throw new RuntimeException("problem initializing token, one or more field not set");
}
}catch(Throwable t) {
logger.error("Exception: ", t);
}
return token;
}
public static String getTokenFromJson(JWTToken jwtToken) {
logger.debug("extracting token from json");
JSONParser parser= new JSONParser();
String token=null;
try {
Reader reader= new StringReader(jwtToken.toString());
JSONObject jsonObject = (JSONObject) parser.parse(reader);
token = (String) jsonObject.get("access_token");
logger.info("access token for context "+ScopeProvider.instance.get()+" retrieved");
} catch (IOException e) {
logger.error("IOEXception", e);
e.printStackTrace();
} catch (ParseException e) {
logger.error("ParseException", e);
e.printStackTrace();
}
return token;
}
}

View File

@ -9,7 +9,7 @@ import org.junit.Test;
public class ConfigurationTest {
static String scope="/d4science.research-infrastructures.eu";
static String scope="/gcube";
static String user=null;
static String password=null;
static String serviceClass="DataTransformation";

View File

@ -19,7 +19,7 @@ public class TokenTest {
URL oidcEndpoint = null;
String clientId="storage-manager-trigger";//"robcomp";//"storage-manager-trigger";//"robcomp";//
String secret="e2591a99-b694-4dbe-8f7b-9755a3db80af";//"0fec31cb-23c3-44e2-9359-d6db6784b7d3";//"e2591a99-b694-4dbe-8f7b-9755a3db80af";//"0fec31cb-23c3-44e2-9359-d6db6784b7d3";/
String secret="";
String context ="/gcube"; // "/gcube/devNext/NextNext";
private static Logger logger= LoggerFactory.getLogger(TokenTest.class);
@ -33,16 +33,18 @@ public class TokenTest {
e.printStackTrace();
}
ScopeProvider.instance.set(context);
JWTToken token=null;
JWTToken jwtToken=null;
try {
token = Utils.setToken(oidcEndpoint, clientId, secret);
jwtToken = Utils.setToken(oidcEndpoint, clientId, secret);
assertNotNull(jwtToken);
} catch (Exception e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
assertNotNull(token);
assertNotNull(jwtToken);
logger.info("token found "+token);
logger.info("token found "+jwtToken);
}
}

View File

@ -11,7 +11,7 @@ public class ValidationScopeTest {
private String scope="/d4science.research-infrastructures.eu/FARM";
@Test
public void test() {
public void validationScope() {
assertTrue(ValidationUtils.validationScope(scope));
}