Lucio Lelii 2016-05-02 16:43:26 +00:00
parent 09eef72564
commit fea33de67b
5 changed files with 134 additions and 29 deletions

View File

@ -8,7 +8,7 @@ import javax.xml.bind.annotation.XmlRootElement;
@XmlAccessorType(XmlAccessType.FIELD) @XmlAccessorType(XmlAccessType.FIELD)
public class Service2ServicePolicy extends Policy{ public class Service2ServicePolicy extends Policy{
private ServiceAccess client; private ServiceEntity client;
private String context; private String context;
private ServiceAccess serviceAccess; private ServiceAccess serviceAccess;
private Action mode = Action.ALL; private Action mode = Action.ALL;
@ -16,14 +16,14 @@ public class Service2ServicePolicy extends Policy{
protected Service2ServicePolicy(){} protected Service2ServicePolicy(){}
public Service2ServicePolicy(String context, ServiceAccess serviceAccess, public Service2ServicePolicy(String context, ServiceAccess serviceAccess,
ServiceAccess client) { ServiceEntity client) {
this.context = context; this.context = context;
this.serviceAccess = serviceAccess; this.serviceAccess = serviceAccess;
this.client = client; this.client = client;
} }
public Service2ServicePolicy(String context, ServiceAccess serviceAccess, public Service2ServicePolicy(String context, ServiceAccess serviceAccess,
ServiceAccess client, Action mode) { ServiceEntity client, Action mode) {
this(context, serviceAccess, client); this(context, serviceAccess, client);
this.mode = mode; this.mode = mode;
} }
@ -35,10 +35,10 @@ public class Service2ServicePolicy extends Policy{
@Override @Override
public String getPolicyAsString() { public String getPolicyAsString() {
return this.context+","+serviceAccess.getAsString()+","+serviceAccess.getAsString()+","+mode.toString(); return this.context+","+serviceAccess.getAsString()+","+client.getAsString()+","+mode.toString();
} }
public ServiceAccess getClient() { public ServiceEntity getClient() {
return client; return client;
} }

View File

@ -33,14 +33,17 @@ public class ServiceAccess {
this.serviceId = serviceId; this.serviceId = serviceId;
} }
/* public boolean isSubsetOf(ServiceAccess access){
public ServiceAccess(String name, String serviceClass, String serviceId, if (this.equals(access)) return true;
HashMap<String, String> serviceSpecificPolices) { if (access.getServiceClass()==null) return true;
this(name, serviceClass, serviceId);
this.serviceSpecificPolices = serviceSpecificPolices;
}*/
if (access.getServiceClass().equals(this.serviceClass)){
if (access.getName()==null) return true;
if (access.getName().equals(this.name) && access.getServiceId()==null)
return true;
}
return false;
}
public String getAsString(){ public String getAsString(){
if (serviceClass == null) if (serviceClass == null)
@ -74,18 +77,6 @@ public class ServiceAccess {
return serviceId; return serviceId;
} }
public boolean isSubsetOf(ServiceAccess access){
if (this.equals(access)) return true;
if (access.getServiceClass()==null) return true;
if (access.getServiceClass().equals(this.serviceClass)){
if (access.getName()==null) return true;
if (access.getName().equals(this.name) && access.getServiceId()==null)
return true;
}
return false;
}
@Override @Override
public int hashCode() { public int hashCode() {
final int prime = 31; final int prime = 31;
@ -136,9 +127,8 @@ public class ServiceAccess {
@Override @Override
public String toString() { public String toString() {
return "ServiceAccess ["+getAsString()+"]"; return getAsString();
} }
} }

View File

@ -0,0 +1,96 @@
package org.gcube.common.authorization.library.policies;
import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import javax.xml.bind.annotation.XmlAccessType;
import javax.xml.bind.annotation.XmlAccessorType;
import javax.xml.bind.annotation.XmlRootElement;
@XmlRootElement
@XmlAccessorType(XmlAccessType.FIELD)
public class ServiceEntity {
private ServiceAccess service = null;
private List<ServiceAccess> excludes = null;
@SuppressWarnings("unused")
private ServiceEntity() {}
protected ServiceEntity(ServiceAccess service) {
if (service==null) throw new IllegalArgumentException("serviceAccess cannot be null");
this.service = service;
}
protected ServiceEntity(List<ServiceAccess> excludes) {
if (excludes==null || excludes.isEmpty()) throw new IllegalArgumentException("list of excludes cannot be empty");
List<ServiceAccess> newExcludes = new ArrayList<ServiceAccess>();
check:
for (ServiceAccess sa: excludes){
if (sa.getClass()==null) throw new IllegalArgumentException("excluding all services to a deny policy is illegal");
for (ServiceAccess alreadySaved : newExcludes)
if (sa.isSubsetOf(alreadySaved))
break check;
newExcludes.add(sa);
}
this.excludes = newExcludes;
}
public ServiceAccess getService() {
return service;
}
public List<ServiceAccess> getExcludes() {
return Collections.unmodifiableList(excludes);
}
@Override
public int hashCode() {
final int prime = 31;
int result = 1;
result = prime * result
+ ((excludes == null) ? 0 : excludes.hashCode());
result = prime * result + ((service == null) ? 0 : service.hashCode());
return result;
}
@Override
public boolean equals(Object obj) {
if (this == obj)
return true;
if (obj == null)
return false;
if (getClass() != obj.getClass())
return false;
ServiceEntity other = (ServiceEntity) obj;
if (excludes == null) {
if (other.excludes != null)
return false;
} else if (!excludes.equals(other.excludes))
return false;
if (service == null) {
if (other.service != null)
return false;
} else if (!service.equals(other.service))
return false;
return true;
}
@Override
public String toString() {
if (service!=null)
return "ServiceEntity [service=" + service + "]";
return "ServiceEntity [ allExcept : " + excludes+ "]";
}
public String getAsString() {
if (service!=null)
return service.getAsString();
else return "allExcept"+ excludes;
}
}

View File

@ -0,0 +1,18 @@
package org.gcube.common.authorization.library.policies;
import java.util.Arrays;
public class Services {
public static ServiceEntity all(){
return new ServiceEntity(new ServiceAccess());
}
public static ServiceEntity specialized(ServiceAccess serviceAccess){
return new ServiceEntity(serviceAccess);
}
public static ServiceEntity allExcept(ServiceAccess ... serviceAccesses){
return new ServiceEntity(Arrays.asList(serviceAccesses));
}
}

View File

@ -30,7 +30,8 @@ public class SerializationTest {
@Test @Test
public void serializeServicePolicy() throws Exception{ public void serializeServicePolicy() throws Exception{
Service2ServicePolicy sp = new Service2ServicePolicy("/gcube", new ServiceAccess("ServiceName","ServiceClass"),new ServiceAccess("ServiceName2", "ServiceClass2")); Service2ServicePolicy sp = new Service2ServicePolicy("/gcube", new ServiceAccess("ServiceName","ServiceClass"),
Services.allExcept(new ServiceAccess("ServiceName2", "ServiceClass2"),new ServiceAccess("ServiceClass2")));
StringWriter sw = new StringWriter(); StringWriter sw = new StringWriter();
context.createMarshaller().marshal(sp, sw); context.createMarshaller().marshal(sp, sw);
Service2ServicePolicy spCopy = (Service2ServicePolicy)context.createUnmarshaller().unmarshal(new StringReader(sw.toString())); Service2ServicePolicy spCopy = (Service2ServicePolicy)context.createUnmarshaller().unmarshal(new StringReader(sw.toString()));