Commit 2ca1333d authored by George Nash's avatar George Nash

Make the OCSecurityAcl part of the OcAcl class

In testing we kept seing random crashs due to segfaults.  The segfaults
are due to the fact that the native code is expecting the freed acls
to still be there.

When the OcAcl class is created the OCSecurityAcl is now passed in and becomes
a member variable. It is no longer instantly freed. To avoid memory leaks the
OCObt.freeAcl() function was moved into the Finalizer of the OcAcl
class. There is sometimes reliablility problems with Finalizers that require
by passing the GC to manualy free the native memory.  The OcAcl.delete()
method can be used to manualy free the native memory.

Some Just incase some OCSecurityAcls are owned by the iotivity-lite stack and should
not be freed by the GC a boolean input parameter memoryOwner is supplied similer to
the OcCredentials class. True means Java owns the memory and it can be freed by the
GC, false means the iotivity-lite stack owns the memory.

When possible  the `get` functions were updated to get the value requested directly
from OcAcl. This change worked its way into OcAce, OcAceResource, and
OcAceSubject classes.

The OcAceResoruce.getInterfaces() method was not even implemented so I did the
implementation based on my best guess of what the original intent of the code was
Signed-off-by: George Nash's avatarGeorge Nash <george.nash@intel.com>
parent ec6a663a
......@@ -62,12 +62,8 @@ public class AmsRepository {
OCObtAclHandler handler = (OCSecurityAcl acl) -> {
if (acl != null) {
OcAcl aclRet = new OcAcl();
aclRet.parseOCRepresentation(acl);
OcAcl aclRet = new OcAcl(acl, true);
emitter.onSuccess(aclRet);
/* Freeing the ACL structure */
OCObt.freeAcl(acl);
} else {
String error = "GET ACL error";
Timber.d(error);
......@@ -233,27 +229,6 @@ public class AmsRepository {
});
}
private List<OcAceResource> getResources(List<String> verticalResources) {
List<OcAceResource> resources = new ArrayList<>();
for (String verticalResource : verticalResources) {
OcAceResource res = new OcAceResource();
if (OcfWildcard.isWildcard(verticalResource)) {
res.setWildCard(verticalResource);
} else {
res.setHref(verticalResource);
}
/*List<String> types = new ArrayList<>();
types.add("*");
res.setResourceTypes(types);
List<String> interfaces = new ArrayList<>();
interfaces.add("*");
res.setInterfaces(interfaces);*/
resources.add(res);
}
return resources;
}
private int setAceResources(OCSecurityAce ace, List<String> resources) {
for (String resource : resources) {
OCAceResource res = OCObt.aceNewResource(ace);
......
......@@ -30,68 +30,38 @@ import java.util.List;
public class OcAce {
private Integer aceid;
private Integer permission;
OCSecurityAce ace;
private OcAceSubject subject;
private List<OcAceResource> resources;
public OcAce() {
public OcAce(OCSecurityAce ace) {
this.ace = ace;
/* subject */
this.subject = new OcAceSubject(ace.getSubjectType(), ace.getSubject());
/* resources */
this.resources = new ArrayList<>();
}
public Integer getAceid() {
return aceid;
}
OCAceResource res = ace.getResourcesListHead();
while (res != null) {
OcAceResource resource = new OcAceResource(res);
this.resources.add(resource);
public void setAceid(Integer aceid) {
this.aceid = aceid;
res = res.getNext();
}
}
public Integer getPermission() {
return permission;
public int getAceid() {
return ace.getAceid();
}
public void setPermission(Integer permission) {
this.permission = permission;
public int getPermission() {
return ace.getPermission();
}
public OcAceSubject getSubject() {
return subject;
}
public void setSubject(OcAceSubject subject) {
this.subject = subject;
}
public List<OcAceResource> getResources() {
return resources;
}
public void setResources(List<OcAceResource> resources) {
this.resources = resources;
}
public void parseOCRepresentation(OCSecurityAce ace) {
/* aceid */
Integer aceid = ace.getAceid();
this.setAceid(aceid);
/* permission */
Integer permission = ace.getPermission();
this.setPermission(permission);
/* subject */
OcAceSubject subject = new OcAceSubject();
subject.parseOCRepresentation(ace.getSubjectType(), ace.getSubject());
this.setSubject(subject);
/* resources */
OCAceResource res = ace.getResourcesListHead();
List<OcAceResource> resources = new ArrayList<>();
while (res != null) {
OcAceResource resource = new OcAceResource();
resource.parseOCRepresentation(res);
resources.add(resource);
res = res.getNext();
}
this.setResources(resources);
}
}
......@@ -23,74 +23,72 @@
package org.openconnectivity.otgc.domain.model.resource.secure.acl;
import org.iotivity.OCAceResource;
import org.iotivity.OCInterfaceMask;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
public class OcAceResource {
private String href;
private String wc;
private List<String> resourceTypes;
private List<String> interfaces;
OCAceResource aceResource;
public OcAceResource() {
resourceTypes = new ArrayList<>();
interfaces = new ArrayList<>();
public OcAceResource(OCAceResource res) {
this.aceResource = res;
}
public String getHref() {
return href;
}
public void setHref(String href) {
this.href = href;
if (aceResource.getHref() != null && !aceResource.getHref().isEmpty()) {
return aceResource.getHref();
}
return null;
}
public String getWildcard() {
return wc;
}
public void setWildCard(String wc) {
this.wc = wc;
if (aceResource.getWildcard() != null) {
/* wc */
switch (aceResource.getWildcard()) {
case OC_ACE_WC_ALL:
return "*";
case OC_ACE_WC_ALL_SECURED:
return "+";
case OC_ACE_WC_ALL_PUBLIC:
return "-";
default:
return null;
}
}
return null;
}
public List<String> getResourceTypes() {
return resourceTypes;
return Arrays.asList(aceResource.getTypes());
}
public void setResourceTypes(List<String> resourceTypes) {
this.resourceTypes = resourceTypes;
}
public List<String> getInterfaces() {
return interfaces;
}
public void setInterfaces(List<String> interfaces) {
this.interfaces = interfaces;
}
public void parseOCRepresentation(OCAceResource res) {
if (res.getHref() != null && !res.getHref().isEmpty()) {
/* href */
String href = res.getHref();
this.setHref(href);
} else if (res.getWildcard() != null) {
/* wc */
switch (res.getWildcard()) {
case OC_ACE_WC_ALL:
this.setWildCard("*");
break;
case OC_ACE_WC_ALL_SECURED:
this.setWildCard("+");
break;
case OC_ACE_WC_ALL_PUBLIC:
this.setWildCard("-");
break;
default:
break;
}
List<String> list = new ArrayList<>();
if((OCInterfaceMask.BASELINE | aceResource.getInterfaces()) == OCInterfaceMask.BASELINE) {
list.add("baseline");
}
if((OCInterfaceMask.LL | aceResource.getInterfaces()) == OCInterfaceMask.LL) {
list.add("ll");
}
if((OCInterfaceMask.B | aceResource.getInterfaces()) == OCInterfaceMask.B) {
list.add("b");
}
if((OCInterfaceMask.R | aceResource.getInterfaces()) == OCInterfaceMask.R) {
list.add("r");
}
if((OCInterfaceMask.RW | aceResource.getInterfaces()) == OCInterfaceMask.RW) {
list.add("rw");
}
if((OCInterfaceMask.S | aceResource.getInterfaces()) == OCInterfaceMask.S) {
list.add("s");
}
if((OCInterfaceMask.CREATE | aceResource.getInterfaces()) == OCInterfaceMask.CREATE) {
list.add("create");
}
return list;
}
}
......@@ -29,71 +29,47 @@ import org.iotivity.OCUuidUtil;
public class OcAceSubject {
private String type;
private String connType;
private String uuid;
private String roleId;
private String authority;
public OcAceSubject() {
OCAceSubjectType subjectType;
OCAceSubject subject;
public OcAceSubject(OCAceSubjectType subjectType, OCAceSubject subject) {
this.subjectType = subjectType;
this.subject = subject;
}
public String getType() {
return type;
}
public void setType(String type) {
this.type = type;
return subjectType.toString();
}
public String getUuid() {
return uuid;
}
public void setUuid(String uuid) {
this.uuid = uuid;
if (subjectType == OCAceSubjectType.OC_SUBJECT_UUID) {
return OCUuidUtil.uuidToString(subject.getUuid());
} else {
return null;
}
}
public String getConnType() {
return connType;
}
public void setConnType(String connType) {
this.connType = connType;
if (subjectType == OCAceSubjectType.OC_SUBJECT_CONN) {
if (subject.getConn() == OCAceConnectionType.OC_CONN_AUTH_CRYPT) {
return "auth-crypt";
} else {
return "anon-clear";
}
} else {
return null;
}
}
public String getRoleId() {
return roleId;
}
public void setRoleId(String roleId) {
this.roleId = roleId;
if (subjectType == OCAceSubjectType.OC_SUBJECT_ROLE) {
return subject.getRole();
} else {
return null;
}
}
public String getAuthority() {
return authority;
}
public void setAuthority(String authority) {
this.authority = authority;
}
public void parseOCRepresentation(OCAceSubjectType subjectType, OCAceSubject subject) {
this.setType(subjectType.toString());
if (subjectType == OCAceSubjectType.OC_SUBJECT_UUID) {
this.setUuid(OCUuidUtil.uuidToString(subject.getUuid()));
} else if (subjectType == OCAceSubjectType.OC_SUBJECT_ROLE) {
this.setRoleId(subject.getRole());
if (subject.getAuthority() != null && !subject.getAuthority().isEmpty()) {
this.setAuthority(subject.getAuthority());
}
} else if (subjectType == OCAceSubjectType.OC_SUBJECT_CONN) {
if (subject.getConn() == OCAceConnectionType.OC_CONN_AUTH_CRYPT) {
this.setConnType("auth-crypt");
} else {
this.setConnType("anon-clear");
}
}
return subject.getAuthority();
}
}
......@@ -22,6 +22,7 @@
package org.openconnectivity.otgc.domain.model.resource.secure.acl;
import org.iotivity.OCObt;
import org.iotivity.OCSecurityAce;
import org.iotivity.OCSecurityAcl;
import org.iotivity.OCUuidUtil;
......@@ -32,11 +33,16 @@ import java.util.List;
public class OcAcl extends OcResourceBase {
private OCSecurityAcl acl;
boolean memoryOwner;
private List<OcAce> aceList;
private String rownerUuid;
public OcAcl() {
public OcAcl(OCSecurityAcl acl, boolean memoryOwner) {
super();
this.acl = acl;
this.memoryOwner = memoryOwner;
parseOCRepresentation();
}
public List<OcAce> getAceList() {
......@@ -48,27 +54,33 @@ public class OcAcl extends OcResourceBase {
}
public String getRownerUuid() {
return this.rownerUuid;
return OCUuidUtil.uuidToString(acl.getRowneruuid());
}
public void setRownerUuid(String rownerUuid) {
this.rownerUuid = rownerUuid;
}
public void parseOCRepresentation(OCSecurityAcl acl) {
private void parseOCRepresentation() {
/* aclist2 */
OCSecurityAce ac = acl.getSubjectsListHead();
List<OcAce> aceList = new ArrayList<>();
while (ac != null) {
OcAce ace = new OcAce();
ace.parseOCRepresentation(ac);
OcAce ace = new OcAce(ac);
aceList.add(ace);
ac = ac.getNext();
}
this.setAceList(aceList);
/* rowneruuid */
String rowneruuid = OCUuidUtil.uuidToString(acl.getRowneruuid());
this.setRownerUuid(rowneruuid);
}
public synchronized void delete() {
if (memoryOwner) {
/* Freeing the ACL structure */
OCObt.freeAcl(acl);
aceList = null;
}
}
@Override
protected void finalize() throws Throwable {
delete();
super.finalize();
}
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment