public IdmLightApplication() {
logger.info("starting idmlight .... ");
config = new IdmLightConfig();
- if (!config.load())
- logger.error("unable to load idmlight config ");
- else
- config.log();
+ if (!config.load()) {
+ logger.error("unable to load idmlight config ");
+ }
+ else {
+ config.log();
+ }
StoreBuilder storeBuilder = new StoreBuilder();
- if (!storeBuilder.exists())
- storeBuilder.init();
+ if (!storeBuilder.exists()) {
+ storeBuilder.init();
+ }
}
@Override
claim = cache.get(creds);
if (claim == null) {
claim = dbAuthenticate(creds, domainName);
- if (claim != null)
+ if (claim != null) {
cache.put(creds, claim);
+ }
}
}
}
}
public static synchronized void clearClaimCache() {
- for (Map<PasswordCredentials, Claim> cache : claimCache.values())
+ for (Map<PasswordCredentials, Claim> cache : claimCache.values()) {
cache.clear();
+ }
}
private static Claim dbAuthenticate(PasswordCredentials creds, String domainName) {
claim.setUserId(user.getUserid().toString());
claim.setUser(creds.username());
claim.setDomain(domainName);
- for (int z=0;z<roles.size();z++)
+ for (int z=0;z<roles.size();z++) {
claim.addRole(roles.get(z));
+ }
return claim.build();
}
catch (StoreException se) {
try {
Users users = userStore.getUsers(userName);
List<User> userList = users.getUsers();
- if (userList.size()==0)
+ if (userList.size()==0) {
return null;
+ }
User user = userList.get(0);
return user.getUserid().toString();
}
}
private static final void debug(String msg) {
- if (logger.isDebugEnabled())
+ if (logger.isDebugEnabled()) {
logger.debug(msg);
+ }
}
}
throw new StoreException("Cannot connect to database server "+ e);
}
}
- else
+ else {
return dbConnection;
+ }
}
catch (SQLException sqe) {
throw new StoreException("Cannot connect to database server "+ sqe);
statement.setString(2,domain.getDescription());
statement.setInt(3,domain.getEnabled()?1:0);
int affectedRows = statement.executeUpdate();
- if (affectedRows == 0)
+ if (affectedRows == 0) {
throw new StoreException("Creating domain failed, no rows affected.");
+ }
ResultSet generatedKeys = statement.getGeneratedKeys();
- if (generatedKeys.next())
+ if (generatedKeys.next()) {
key = generatedKeys.getInt(1);
- else
+ }
+ else {
throw new StoreException("Creating domain failed, no generated key obtained.");
+ }
domain.setDomainid(key);
dbClose();
return domain;
public Domain putDomain(Domain domain) throws StoreException {
Domain savedDomain = this.getDomain(domain.getDomainid());
- if (savedDomain==null)
+ if (savedDomain==null) {
return null;
+ }
- if (domain.getDescription()!=null)
+ if (domain.getDescription()!=null) {
savedDomain.setDescription(domain.getDescription());
- if (domain.getName()!=null)
+ }
+ if (domain.getName()!=null) {
savedDomain.setName(domain.getName());
- if (domain.getEnabled()!=null)
+ }
+ if (domain.getEnabled()!=null) {
savedDomain.setEnabled(domain.getEnabled());
+ }
Connection conn = dbConnect();
try {
public Domain deleteDomain(Domain domain) throws StoreException {
Domain savedDomain = this.getDomain(domain.getDomainid());
- if (savedDomain==null)
+ if (savedDomain==null) {
return null;
+ }
Connection conn = dbConnect();
Statement stmt=null;
}
private static final void debug(String msg) {
- if (logger.isDebugEnabled())
+ if (logger.isDebugEnabled()) {
logger.debug(msg);
+ }
}
}
throw new StoreException("Cannot connect to database server "+ e);
}
}
- else
+ else {
return dbConnection;
+ }
}
catch (SQLException sqe) {
throw new StoreException("Cannot connect to database server "+ sqe);
statement.setInt(3,grant.getUserid());
statement.setInt(4,grant.getRoleid());
int affectedRows = statement.executeUpdate();
- if (affectedRows == 0)
+ if (affectedRows == 0) {
throw new StoreException("Creating grant failed, no rows affected.");
+ }
ResultSet generatedKeys = statement.getGeneratedKeys();
- if (generatedKeys.next())
+ if (generatedKeys.next()) {
key = generatedKeys.getInt(1);
- else
+ }
+ else {
throw new StoreException("Creating grant failed, no generated key obtained.");
+ }
grant.setGrantid(key);
dbClose();
return grant;
public Grant deleteGrant(Grant grant) throws StoreException {
Grant savedGrant = this.getGrant(grant.getGrantid());
- if (savedGrant==null)
+ if (savedGrant==null) {
return null;
+ }
Connection conn = dbConnect();
Statement stmt=null;
}
private static final void debug(String msg) {
- if (logger.isDebugEnabled())
+ if (logger.isDebugEnabled()) {
logger.debug(msg);
+ }
}
}
throw new StoreException("Cannot connect to database server "+ e);
}
}
- else
+ else {
return dbConnection;
+ }
}
catch (SQLException sqe) {
throw new StoreException("Cannot connect to database server "+ sqe);
statement.setString(1,role.getName());
statement.setString(2,role.getDescription());
int affectedRows = statement.executeUpdate();
- if (affectedRows == 0)
+ if (affectedRows == 0) {
throw new StoreException("Creating role failed, no rows affected.");
+ }
ResultSet generatedKeys = statement.getGeneratedKeys();
- if (generatedKeys.next())
+ if (generatedKeys.next()) {
key = generatedKeys.getInt(1);
- else
+ }
+ else {
throw new StoreException("Creating role failed, no generated key obtained.");
+ }
role.setRoleid(key);
dbClose();
return role;
public Role putRole(Role role) throws StoreException {
Role savedRole = this.getRole(role.getRoleid());
- if (savedRole==null)
+ if (savedRole==null) {
return null;
+ }
- if (role.getDescription()!=null)
+ if (role.getDescription()!=null) {
savedRole.setDescription(role.getDescription());
- if (role.getName()!=null)
+ }
+ if (role.getName()!=null) {
savedRole.setName(role.getName());
+ }
Connection conn = dbConnect();
try {
public Role deleteRole(Role role) throws StoreException {
Role savedRole = this.getRole(role.getRoleid());
- if (savedRole==null)
+ if (savedRole==null) {
return null;
+ }
Connection conn = dbConnect();
Statement stmt=null;
}
private static final void debug(String msg) {
- if (logger.isDebugEnabled())
+ if (logger.isDebugEnabled()) {
logger.debug(msg);
+ }
}
}
throw new StoreException("Cannot connect to database server "+ e);
}
}
- else
+ else {
return dbConnection;
+ }
}
catch (SQLException sqe) {
throw new StoreException("Cannot connect to database server "+ sqe);
statement.setString(4,user.getDescription());
statement.setInt(5,user.getEnabled()?1:0);
int affectedRows = statement.executeUpdate();
- if (affectedRows == 0)
+ if (affectedRows == 0) {
throw new StoreException("Creating user failed, no rows affected.");
+ }
ResultSet generatedKeys = statement.getGeneratedKeys();
- if (generatedKeys.next())
+ if (generatedKeys.next()) {
key = generatedKeys.getInt(1);
- else
+ }
+ else {
throw new StoreException("Creating user failed, no generated key obtained.");
+ }
user.setUserid(key);
dbClose();
return user;
public User putUser(User user) throws StoreException {
User savedUser = this.getUser(user.getUserid());
- if (savedUser==null)
+ if (savedUser==null) {
return null;
+ }
- if (user.getDescription()!=null)
+ if (user.getDescription()!=null) {
savedUser.setDescription(user.getDescription());
- if (user.getName()!=null)
+ }
+ if (user.getName()!=null) {
savedUser.setName(user.getName());
- if (user.getEnabled()!=null)
+ }
+ if (user.getEnabled()!=null) {
savedUser.setEnabled(user.getEnabled());
- if (user.getEmail()!=null)
+ }
+ if (user.getEmail()!=null) {
savedUser.setEmail(user.getEmail());
- if (user.getPassword()!=null)
+ }
+ if (user.getPassword()!=null) {
savedUser.setPassword(user.getPassword());
+ }
Connection conn = dbConnect();
try {
public User deleteUser(User user) throws StoreException {
User savedUser = this.getUser(user.getUserid());
- if (savedUser==null)
+ if (savedUser==null) {
return null;
+ }
Connection conn = dbConnect();
Statement stmt=null;
}
private static final void debug(String msg) {
- if (logger.isDebugEnabled())
+ if (logger.isDebugEnabled()) {
logger.debug(msg);
+ }
}
}
/**
*
- * @author peter.mellquist@hp.com
+ * @author peter.mellquist@hp.com
*
*/
private static DomainStore domainStore = new DomainStore();
private static UserStore userStore = new UserStore();
private static RoleStore roleStore = new RoleStore();
- private static GrantStore grantStore = new GrantStore();
-
+ private static GrantStore grantStore = new GrantStore();
+
@GET
@Produces("application/json")
public Response getDomains() {
public Response createDomain(@Context UriInfo info,Domain domain) {
logger.info("Post /domains");
try {
- if (domain.getEnabled()==null)
+ if (domain.getEnabled()==null) {
domain.setEnabled(false);
- if (domain.getName()==null)
+ }
+ if (domain.getName()==null) {
domain.setName("");
- if (domain.getDescription()==null)
+ }
+ if (domain.getDescription()==null) {
domain.setDescription("");
+ }
domain = domainStore.createDomain(domain);
}
catch (StoreException se) {
idmerror.setMessage("Internal error creating domain");
idmerror.setDetails(se.message);
return Response.status(500).entity(idmerror).build();
- }
+ }
return Response.status(201).entity(domain).build();
- }
+ }
@PUT
@Path("/{id}")
@Path("/{did}/users/{uid}/roles")
@Consumes("application/json")
@Produces("application/json")
- public Response createGrant( @Context UriInfo info,
+ public Response createGrant( @Context UriInfo info,
@PathParam("did") String did,
- @PathParam("uid") String uid,
+ @PathParam("uid") String uid,
Grant grant) {
logger.info("Post /domains/"+did+"/users/"+uid+"/roles");
Domain domain=null;
long longUid=0;
long longRid=0;
- if (grant.getDescription()==null)
+ if (grant.getDescription()==null) {
grant.setDescription("");
+ }
// validate domain id
try {
return Response.status(404).entity(idmerror).build();
}
- // see if grant already exists for this
+ // see if grant already exists for this
try {
Grant existingGrant = grantStore.getGrant(longDid,longUid,longRid);
if (existingGrant != null) {
}
- // create grant
+ // create grant
try {
grant = grantStore.createGrant(grant);
}
return Response.status(400).entity(idmerror).build();
}
- // find userid for user
+ // find userid for user
try {
Users users = userStore.getUsers(username);
List<User> userList = users.getUsers();
if (!userPwd.equals(reqPwd)) {
IDMError idmerror = new IDMError();
idmerror.setMessage("password does not match for username: "+username);
- return Response.status(401).entity(idmerror).build();
+ return Response.status(401).entity(idmerror).build();
}
claim.setDomainid((int)longDid);
claim.setUsername(username);
idmerror.setDetails(se.message);
return Response.status(500).entity(idmerror).build();
}
- claim.setRoles(roleList);
+ claim.setRoles(roleList);
}
catch(StoreException se) {
logger.error("StoreException : " + se);
}
return Response.ok(claim).build();
- }
+ }
@GET
@Path("/{did}/users/{uid}/roles")
idmerror.setMessage("Not found! Role id :" + rid);
return Response.status(404).entity(idmerror).build();
}
-
- // see if grant already exists
+
+ // see if grant already exists
try {
Grant existingGrant = grantStore.getGrant(longDid,longUid,longRid);
if (existingGrant == null) {
/**
*
- * @author peter.mellquist@hp.com
+ * @author peter.mellquist@hp.com
*
*/
import org.opendaylight.aaa.idm.persistence.RoleStore;
import org.opendaylight.aaa.idm.persistence.StoreException;
import org.opendaylight.aaa.idm.IdmLightProxy;
-
+
@Path("/v1/roles")
public class RoleHandler {
private static Logger logger = LoggerFactory.getLogger(RoleHandler.class);
private static RoleStore roleStore = new RoleStore();
-
+
@GET
@Produces("application/json")
public Response getRoles() {
public Response createRole(@Context UriInfo info,Role role) {
logger.info("Post /roles");
try {
- // TODO: role names should be unique!
+ // TODO: role names should be unique!
// name
- if (role.getName()==null)
+ if (role.getName()==null) {
return new IDMError(404,"name must be defined on role create","").response();
- else
- if (role.getName().length()>RoleStore.MAX_FIELD_LEN)
- return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
-
- // description
- if (role.getDescription()==null)
+ }
+ else if (role.getName().length()>RoleStore.MAX_FIELD_LEN) {
+ return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
+ }
+
+ // description
+ if (role.getDescription()==null) {
role.setDescription("");
- else
- if (role.getDescription().length()>RoleStore.MAX_FIELD_LEN)
- return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
+ }
+ else if (role.getDescription().length()>RoleStore.MAX_FIELD_LEN) {
+ return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
+ }
role = roleStore.createRole(role);
}
catch (StoreException se) {
return new IDMError(500,"internal error creating role",se.message).response();
- }
+ }
return Response.status(201).entity(role).build();
- }
+ }
@PUT
@Path("/{id}")
// name
// TODO: names should be unique
- if ((role.getName()!=null) && (role.getName().length()>RoleStore.MAX_FIELD_LEN))
+ if ((role.getName()!=null) && (role.getName().length()>RoleStore.MAX_FIELD_LEN)) {
return new IDMError(400,"role name max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
+ }
// description
- if ((role.getDescription()!=null) && (role.getDescription().length()>RoleStore.MAX_FIELD_LEN))
+ if ((role.getDescription()!=null) && (role.getDescription().length()>RoleStore.MAX_FIELD_LEN)) {
return new IDMError(400,"role description max length is :" + RoleStore.MAX_FIELD_LEN,"").response();
+ }
role = roleStore.putRole(role);
if (role==null) {
/**
*
- * @author peter.mellquist@hp.com
+ * @author peter.mellquist@hp.com
*
*/
import org.opendaylight.aaa.idm.persistence.UserStore;
import org.opendaylight.aaa.idm.persistence.StoreException;
import org.opendaylight.aaa.idm.IdmLightProxy;
-
+
@Path("/v1/users")
public class UserHandler {
private static Logger logger = LoggerFactory.getLogger(UserHandler.class);
private static UserStore userStore = new UserStore();
protected final static String DEFAULT_PWD = "changeme";
-
+
@GET
@Produces("application/json")
public Response getUsers() {
}
// obsfucate pwd
- for (int z=0;z<users.getUsers().size();z++)
+ for (int z=0;z<users.getUsers().size();z++) {
users.getUsers().get(z).setPassword("**********");
-
+ }
+
return Response.ok(users).build();
}
return new IDMError(500,"internal error getting user",se.message).response();
}
if (user==null) {
- return new IDMError(404,"user not found! id:" + id,"").response();
+ return new IDMError(404,"user not found! id:" + id,"").response();
}
// obsfucate pwd
user.setPassword("*********");
logger.info("post /users");
try {
// enabled by default
- if (user.getEnabled()==null)
+ if (user.getEnabled()==null) {
user.setEnabled(true);
-
+ }
+
// user name is required
- if (user.getName()==null)
+ if (user.getName()==null) {
return new IDMError(400,"user name is required","").response();
- else
- if (user.getName().length()>UserStore.MAX_FIELD_LEN)
- return new IDMError(400,"user name max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
+ else if (user.getName().length()>UserStore.MAX_FIELD_LEN) {
+ return new IDMError(400,"user name max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
// user description is optional
- if (user.getDescription()==null)
+ if (user.getDescription()==null) {
user.setDescription("");
- else
- if (user.getDescription().length()>UserStore.MAX_FIELD_LEN)
- return new IDMError(400,"user description max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
+ else if (user.getDescription().length()>UserStore.MAX_FIELD_LEN) {
+ return new IDMError(400,"user description max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
// user email is optional
- if (user.getEmail()==null)
+ if (user.getEmail()==null) {
user.setEmail("");
- else
- if (user.getEmail().length()>UserStore.MAX_FIELD_LEN)
- return new IDMError(400,"user email max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
+ else if (user.getEmail().length()>UserStore.MAX_FIELD_LEN) {
+ return new IDMError(400,"user email max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
// user password optional and will default if not provided
- if (user.getPassword()==null)
+ if (user.getPassword()==null) {
user.setPassword(DEFAULT_PWD);
- else
- if (user.getPassword().length()>UserStore.MAX_FIELD_LEN)
- return new IDMError(400,"user password max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
+ else if (user.getPassword().length()>UserStore.MAX_FIELD_LEN) {
+ return new IDMError(400,"user password max length is :" + UserStore.MAX_FIELD_LEN,"").response();
+ }
// create user
user = userStore.createUser(user);
}
catch (StoreException se) {
return new IDMError(500,"internal error creating user",se.message).response();
- }
+ }
// created!
return Response.status(201).entity(user).build();
- }
+ }
@PUT
return Response.status(204).build();
}
-
+
}