refactoring code
This commit is contained in:
parent
922c000518
commit
e4710d4932
|
@ -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
|
// 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()+"");
|
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());
|
sur=report.setSpecificProperties(sur, record.getFilename(), "STORAGE", record.getCallerIp(), record.getId());
|
||||||
logger.info("[accounting call] type: storage usage ");
|
logger.info("storage usage record ready");
|
||||||
URL oidcAddress=new URL(oidcEndpoint);
|
logger.debug("try to call: "+oidcEndpoint);
|
||||||
Utils.setToken(oidcAddress, clientId, secret);
|
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");
|
logger.debug("sending record to the accounting");
|
||||||
report.printRecord(sur);
|
report.printRecord(sur);
|
||||||
report.send(sur);
|
report.send(sur);
|
||||||
|
@ -278,29 +293,6 @@ public class UserAccountingConsumer extends Thread{
|
||||||
return scope;
|
return scope;
|
||||||
}else logger.error("Scope bad format: scope not retrieved from string: "+filename);
|
}else logger.error("Scope bad format: scope not retrieved from string: "+filename);
|
||||||
return null;
|
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -75,7 +75,7 @@ public class ReadingMongoOplog extends Thread{
|
||||||
while (cursor.hasNext()) {
|
while (cursor.hasNext()) {
|
||||||
try{
|
try{
|
||||||
DBObject x = cursor.next();
|
DBObject x = cursor.next();
|
||||||
logger.debug("oplog current object: "+x);
|
logger.trace("oplog current object: "+x);
|
||||||
ts = (BSONTimestamp) x.get("ts");
|
ts = (BSONTimestamp) x.get("ts");
|
||||||
String ns=(String)x.get("ns");
|
String ns=(String)x.get("ns");
|
||||||
// check if discard or process the current DB record
|
// check if discard or process the current DB record
|
||||||
|
|
|
@ -52,7 +52,7 @@ public class MongoDB {
|
||||||
}
|
}
|
||||||
|
|
||||||
public MongoDB(String[] server, String user, String password){
|
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.server=server;
|
||||||
this.pwd=password;
|
this.pwd=password;
|
||||||
this.user=user;
|
this.user=user;
|
||||||
|
|
|
@ -1,13 +1,19 @@
|
||||||
package org.gcube.contentmanager.storageserver.utils;
|
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.net.URL;
|
||||||
|
import java.util.Objects;
|
||||||
|
|
||||||
import org.gcube.common.authorization.library.provider.UmaJWTProvider;
|
import org.gcube.common.authorization.library.provider.UmaJWTProvider;
|
||||||
import org.gcube.common.scope.api.ScopeProvider;
|
import org.gcube.common.scope.api.ScopeProvider;
|
||||||
import org.gcube.oidc.rest.JWTToken;
|
import org.gcube.oidc.rest.JWTToken;
|
||||||
import org.gcube.oidc.rest.OpenIdConnectRESTHelper;
|
import org.gcube.oidc.rest.OpenIdConnectRESTHelper;
|
||||||
import org.gcube.oidc.rest.OpenIdConnectRESTHelperException;
|
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.Logger;
|
||||||
import org.slf4j.LoggerFactory;
|
import org.slf4j.LoggerFactory;
|
||||||
|
|
||||||
|
@ -16,14 +22,45 @@ public class Utils {
|
||||||
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
|
private static final Logger logger = LoggerFactory.getLogger(Utils.class);
|
||||||
|
|
||||||
public static JWTToken setToken(URL oidcAddress, String clientId, String secret) throws OpenIdConnectRESTHelperException {
|
public static JWTToken setToken(URL oidcAddress, String clientId, String secret) throws OpenIdConnectRESTHelperException {
|
||||||
String context=ScopeProvider.instance.get();
|
JWTToken token=null;
|
||||||
logger.info(clientId+" getting the UMAtoken for context "+context);
|
try {
|
||||||
JWTToken token =null;
|
logger.trace("setToken method");
|
||||||
logger.debug("from "+oidcAddress);
|
String context=ScopeProvider.instance.get();
|
||||||
token = OpenIdConnectRESTHelper.queryUMAToken(oidcAddress, clientId, secret, context, null);
|
logger.trace("we are in "+context);
|
||||||
logger.info("UMA token retrieved for context "+context);
|
if((!Objects.isNull(context)) && (!Objects.isNull(clientId))&& (!Objects.isNull(secret))) {
|
||||||
UmaJWTProvider.instance.set(token.toString());
|
logger.info(clientId+" getting the UMAtoken for context "+context);
|
||||||
return token;
|
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;
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -9,7 +9,7 @@ import org.junit.Test;
|
||||||
|
|
||||||
public class ConfigurationTest {
|
public class ConfigurationTest {
|
||||||
|
|
||||||
static String scope="/d4science.research-infrastructures.eu";
|
static String scope="/gcube";
|
||||||
static String user=null;
|
static String user=null;
|
||||||
static String password=null;
|
static String password=null;
|
||||||
static String serviceClass="DataTransformation";
|
static String serviceClass="DataTransformation";
|
||||||
|
|
|
@ -19,7 +19,7 @@ public class TokenTest {
|
||||||
|
|
||||||
URL oidcEndpoint = null;
|
URL oidcEndpoint = null;
|
||||||
String clientId="storage-manager-trigger";//"robcomp";//"storage-manager-trigger";//"robcomp";//
|
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";
|
String context ="/gcube"; // "/gcube/devNext/NextNext";
|
||||||
private static Logger logger= LoggerFactory.getLogger(TokenTest.class);
|
private static Logger logger= LoggerFactory.getLogger(TokenTest.class);
|
||||||
|
|
||||||
|
@ -33,16 +33,18 @@ public class TokenTest {
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
ScopeProvider.instance.set(context);
|
ScopeProvider.instance.set(context);
|
||||||
JWTToken token=null;
|
JWTToken jwtToken=null;
|
||||||
try {
|
try {
|
||||||
token = Utils.setToken(oidcEndpoint, clientId, secret);
|
jwtToken = Utils.setToken(oidcEndpoint, clientId, secret);
|
||||||
|
assertNotNull(jwtToken);
|
||||||
} catch (Exception e) {
|
} catch (Exception e) {
|
||||||
// TODO Auto-generated catch block
|
// TODO Auto-generated catch block
|
||||||
e.printStackTrace();
|
e.printStackTrace();
|
||||||
}
|
}
|
||||||
assertNotNull(token);
|
assertNotNull(jwtToken);
|
||||||
|
|
||||||
logger.info("token found "+token);
|
logger.info("token found "+jwtToken);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -11,7 +11,7 @@ public class ValidationScopeTest {
|
||||||
private String scope="/d4science.research-infrastructures.eu/FARM";
|
private String scope="/d4science.research-infrastructures.eu/FARM";
|
||||||
|
|
||||||
@Test
|
@Test
|
||||||
public void test() {
|
public void validationScope() {
|
||||||
assertTrue(ValidationUtils.validationScope(scope));
|
assertTrue(ValidationUtils.validationScope(scope));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue