Monday, August 21, 2023

Set TaskStatus

import org.apache.commons.logging.Log;

import sailpoint.api.SailPointContext;

import sailpoint.object.TaskResult;

import sailpoint.tools.GeneralException;

public class SetTaskStatus {

static Log log = null;

static SailPointContext context = null;

public static void main(String[] args) {

int count = 316;

TaskResult taskResult = null;

try {

taskResult = context.getObjectByName(TaskResult.class, "Mighty Task");

if (taskResult != null) {

taskResult.addAttribute("count", count);

taskResult.setCompletionStatus(taskResult.getCompletionStatus().Success);

}

} catch (GeneralException e) {

log.error("GeneralException: " + e.getMessage());

taskResult.setCompletionStatus(taskResult.getCompletionStatus().Error);

}

}

}

 

GroupAggregation Rule for Description

import java.util.HashMap;

import java.util.Locale;

import java.util.Map;

import org.apache.commons.logging.Log;

import sailpoint.api.SailPointContext;

import sailpoint.object.Application;

import sailpoint.object.Identity;

import sailpoint.object.ManagedAttribute;

import sailpoint.object.ResourceObject;

import sailpoint.tools.GeneralException;

public class GroupAggrgationRuleForDesc {

static Log log = null;

static SailPointContext context = null;

static Map environment = null;

static ResourceObject obj = null;

static Application groupApplication = null;

static ManagedAttribute accountGroup = null;

public static void main(String[] args) {

Identity workGroup = null;

Map map = new HashMap();

try {

workGroup = context.getObjectByName(Identity.class, accountGroup.getValue());

accountGroup.setOwner(workGroup.getOwner());

accountGroup.setRequestable(false);

map.put(Locale.US.toString(), workGroup.getDescription());

accountGroup.setDescriptions(map);

} catch (GeneralException e) {

log.error("GeneralException: " + e.getMessage());

}

}

}


Exclusion Rule for Entitlement1

import java.util.ArrayList;

import java.util.Iterator;

import java.util.List;

import org.apache.commons.logging.Log;

import sailpoint.api.SailPointContext;

import sailpoint.object.Attributes;

import sailpoint.object.Certifiable;

import sailpoint.object.EntitlementGroup;

import sailpoint.object.Filter;

import sailpoint.object.ManagedAttribute;

import sailpoint.tools.GeneralException;


public class WorkgroupECertExclusionRule {

static SailPointContext context = null;

static Log log = null;

static List items = null;

static List itemsToExclude = null;

static String explanation = null;

static String excludeEntitlement() {

                log.debug("Entering into Exclusion Rule");

ManagedAttribute access = null;

// ManagedAttribute access = null;

String accessCert = null;

Attributes attrs = null;

String appName = null;

String entAttrName = null;

String entAttrVal = null;

Filter masterFilter = null;

List entitlementList = new ArrayList();

for (Iterator iterator = items.iterator(); iterator.hasNext();) {

try {

Certifiable certifiable = (Certifiable) iterator.next();

if (certifiable instanceof EntitlementGroup) {

EntitlementGroup entitlementGroup = (EntitlementGroup) certifiable;

appName = entitlementGroup.getApplicationName();

attrs = entitlementGroup.getAttributes();

entAttrName = attrs.getKeys().get(0).toString();

entAttrVal = attrs.getString(entAttrName);

masterFilter = Filter.and(new Filter[] { Filter.eq("value", entAttrVal),

Filter.eq("attribute", entAttrName), Filter.eq("application.name", appName) });

access = context.getUniqueObject(ManagedAttribute.class, masterFilter);

accessCert = access.getAttribute("certifiable").toString();

if (!accessCert.equalsIgnoreCase("True")) {

itemsToExclude.add(certifiable);

iterator.remove();

explanation = "No need to certify these items";

}

}

} catch (GeneralException e) {

log.error("GeneralException in exclusion: " + e.getMessage());

}

}

items.addAll(entitlementList);

                log.debug("Exiting from Exclusion Rule");

return explanation;

}

public static void main(String[] args) {

String message = excludeEntitlement();

}

}


Friday, August 18, 2023

Password Generation2

 import java.util.Random;

public class PasswordGeneration2 {

static String passwordGenerator2() {

int length = 15;

String password = "";

String numbers = "1234567890";

String speacialCharacters = "!@#$%";

// String speacialCharacters = "";

String upperCaseLetters = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";

String lowerCaseLetters = "abcdefghijklmnopqrstuvwxyz";

String passowrConcat = upperCaseLetters + lowerCaseLetters + speacialCharacters + numbers;

Random random = new Random();

for (int i = 0; i < length; i++) {

int index = random.nextInt(passowrConcat.length());

password += passowrConcat.charAt(index);

}

return password;

}

public static void main(String[] args) {

String retValue = passwordGenerator2();

}

}


Password Generation1

import sailpoint.api.PasswordGenerator;
import sailpoint.api.SailPointContext;
import sailpoint.object.Application;
import sailpoint.object.Identity;
import sailpoint.tools.GeneralException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class PasswordGeneration1 {

static SailPointContext context = null;
static Application application = null;
static Identity identity = null;
static Log logger = LogFactory.getLog("com.mightypedia.password.rule");

private static String generatePassword() throws GeneralException {

String returnPassword = "";
PasswordGenerator passwordGenerator = null;

try {
passwordGenerator = new PasswordGenerator(context);
returnPassword = passwordGenerator.generatePassword(identity, application);
} catch (GeneralException e) {

logger.error("GeneralException: " + e.getMessage());
returnPassword = passwordGenerator.generatePassword();
}
return returnPassword;
}

public static void main(String[] args) {

try {
String password = generatePassword();
} catch (GeneralException e) {
logger.error("GeneralException: " + e.getMessage());
}
}
}

Tuesday, August 15, 2023

Account Expiration Notification Rule

import java.text.ParseException;

import java.text.SimpleDateFormat;

import java.util.Calendar;

import java.util.Date;

import java.util.HashMap;

import java.util.List;

import java.util.Map;

import org.apache.log4j.Logger;

import sailpoint.api.IdentityService;

import sailpoint.api.SailPointContext;

import sailpoint.object.Application;

import sailpoint.object.EmailOptions;

import sailpoint.object.EmailTemplate;

import sailpoint.object.Filter;

import sailpoint.object.Identity;

import sailpoint.object.Link;

import sailpoint.object.QueryOptions;

import sailpoint.tools.GeneralException;


public class AccountExpirationRule {

static SailPointContext context = null;

static Logger log = null;


static void sendEmail(String identityName, String emailTemplate, String daysRemaining, Date endDate) {

String company = null;

String mgrFirstName = null;

String mgrLastName = null;

String EMAIL_ADMIN = "reddy@mightypedia.com";

String recipient = null;

String ccRecipient = null;

Map args = null;


EmailOptions options = null;

EmailTemplate eTemp = null;

Identity mgr = null;

Identity identity = null;


try {

identity = context.getObjectByName(Identity.class, identityName);


if (identity.getAttribute("company") != null) {

company = identity.getAttribute("company").toString();

}

if (identity.getManager() != null) {

mgr = identity.getManager();


if (mgr.getFirstname() != null) {

mgrFirstName = mgr.getFirstname().toString();

}

if (mgr.getLastname() != null) {

mgrLastName = mgr.getLastname().toString();

}

if (mgr.getEmail() != null) {

recipient = mgr.getEmail().trim().toString();

} else {

log.debug("Manager email is null and setting recipient to admin");

recipient = EMAIL_ADMIN;

}

} else {

log.debug("Identity Manager is null and setting recipient to admin");

recipient = EMAIL_ADMIN;

}


SimpleDateFormat formatDate = new SimpleDateFormat("MM/dd/yyyy");

String startDateFor = "";

String endDateFor = null;

if (identity.getAttribute("startDate").toString() != null) {

startDateFor = formatDate.format(identity.getAttribute("startDate").toString());

}

endDateFor = formatDate.format(endDate);

if (identity.getFirstname() != null && identity.getLastname() != null && identity.getEmail() != null

&& startDateFor != null) {

args = new HashMap();

args.put("identity", identity);

args.put("daysRemaining", daysRemaining);

args.put("firstName", identity.getFirstname());

args.put("lastName", identity.getLastname());

args.put("email", identity.getEmail());

args.put("startDate", startDateFor);

args.put("endDate", endDateFor);

args.put("managerFirstName", mgrFirstName);

args.put("managerLastName", mgrLastName);

args.put("company", company);

}

options = new EmailOptions();

options.setVariables(args);

options.setTo(recipient);

options.setCc(ccRecipient);


eTemp = context.getObject(EmailTemplate.class, emailTemplate);


if (eTemp != null) {

context.sendEmailNotification(eTemp, options);

}

} catch (GeneralException e) {

log.error("GeneralException: " + e.getMessage());

} finally {

try {

context.decache(identity);

context.decache(mgr);

context.decache(eTemp);

} catch (GeneralException e) {

log.error("GeneralException: " + e.getMessage());

}

}

}


public static void main(String[] args) {


Date today = null;

Date todayPlus15 = null;

Date todayPlus30 = null;

Date accuntExpDate = null;

Calendar cal = null;

SimpleDateFormat format = null;


String identityName = null;

String accountExpires = null;

String todayPlus15Conversion = null;

String todayPlus30Conversion = null;

String accuntExpDateConversion = null;

String filter = "(inactive == false &amp;&amp; (type == \"consultant\"))";


List<Link> identityLinks = null;

List<Identity> identities = null;

Application application = null;

IdentityService iDS = new IdentityService(context);

QueryOptions ops = null;


try {

application = context.getObjectByName(Application.class, "AD");

ops = new QueryOptions();

Filter identityFilter = Filter.compile(filter);

ops.addFilter(identityFilter);


identities = context.getObjects(Identity.class, ops);


if (identities != null) {

for (Identity identity : identities) {

identityName = identity.getName();

identityLinks = iDS.getLinks(identity, application);


for (Link identityLink : identityLinks) {

Boolean isDisabled = identityLink.isDisabled();


if (!isDisabled) {

if (identityLink.getAttribute("accountExpires") != null

&& !identityLink.getAttribute("accountExpires").equals("") && !identityLink

.getAttribute("accountExpires").toString().equalsIgnoreCase("never")) {


accountExpires = identityLink.getAttribute("accountExpires").toString();

format = new SimpleDateFormat("MM/dd/yyyy HH:mm:ss aa ZZZ");

try {

accuntExpDate = format.parse(accountExpires);

} catch (ParseException e) {

log.error("ParseException: " + e.getMessage());

}


today = new Date();

cal = Calendar.getInstance();

cal.setTime(today);

cal.set(Calendar.HOUR_OF_DAY, 12);

cal.set(Calendar.MINUTE, 0);

cal.set(Calendar.SECOND, 0);

cal.set(Calendar.MILLISECOND, 0);


cal.add(Calendar.DATE, 15);

todayPlus15 = cal.getTime();


cal.add(Calendar.DATE, 15);

todayPlus30 = cal.getTime();


if (accuntExpDate != null) {

todayPlus15Conversion = new SimpleDateFormat("MM/dd/yyyy").format(todayPlus15);

todayPlus30Conversion = new SimpleDateFormat("MM/dd/yyyy").format(todayPlus30);

accuntExpDateConversion = new SimpleDateFormat("MM/dd/yyyy").format(accuntExpDate);


if (todayPlus15Conversion.equals(accuntExpDateConversion)) {

sendEmail(identity.getName(), "Account Expiry", "15", todayPlus15);

} else if (todayPlus30Conversion.equals(accuntExpDateConversion)) {

sendEmail(identity.getName(), "Account Expiry", "30", todayPlus30);

} else {

log.debug("Criteria is not matched...");

}


}


}

}

}

}

} else {

log.debug("No Identities found...");

}


} catch (GeneralException e) {

log.error("GeneralException: " + e.getMessage());

}


finally {

try {

context.decache(application);

} catch (GeneralException e) {

log.error("GeneralException: " + e.getMessage());

}


}

}

}


Wednesday, August 9, 2023

Username Generation

import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import sailpoint.api.SailPointContext;
import sailpoint.connector.Connector;
import sailpoint.connector.ConnectorFactory;
import sailpoint.object.Application;
import sailpoint.object.Filter;
import sailpoint.object.Identity;
import sailpoint.object.QueryOptions;
import sailpoint.tools.Util;
import sailpoint.object.ResourceObject;
import sailpoint.tools.CloseableIterator;
import sailpoint.tools.GeneralException;
import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

public class UsernameGeneration{

static SailPointContext context = null;
static Identity identity = null;
static Log log = LogFactory.getLog("com.mighty.rule");

static boolean issAMAccountNameUniueInAD2(SailPointContext context, String sAMAccountName) {
if (sAMAccountName == null || sAMAccountName.isEmpty()) {
log.debug("sAMAccountName is null / empty");
return false;
}
boolean unique = true;
String searchDN = null;
String appConnName = null;
Application application = null;
Application appCopy = null;

String searchFilter = "(sAMAccountName=" + sAMAccountName + ")";
List dnList = null;
Map setUpMap = new HashMap();
Connector appConnector = null;
CloseableIterator iterator = null;
try {
application = context.getObjectByName(Application.class, "AD");
appCopy = (Application) application.deepCopy(context);
appCopy.setPasswordPolicies(null);
appConnName = appCopy.getConnector();

if (appCopy.getAttributes().getMap().get("domainSettings").get(0).get("domainDN") != null) {
searchDN = appCopy.getAttributes().getMap().get("domainSettings").get(0).get("domainDN");
}

setUpMap.put("searchDN", searchDN);
setUpMap.put("searchFilter", searchFilter);
dnList.add(0, setUpMap);

appCopy.setAttribute("searchDN", dnList);
appCopy.setAttribute("referral", "ignore");
appCopy.setAttribute("useHasMoreElements", true);
appCopy.setCustomizationRule(null);

appConnector = ConnectorFactory.getConnector(appCopy, null);
iterator = appConnector.iterateObjects(Connector.TYPE_ACCOUNT, null, null);

try {
if (iterator != null && iterator.hasNext()) {
ResourceObject user = (ResourceObject) iterator.next();
unique = false;
}
} catch (Exception e) {
}

} catch (GeneralException e) {
log.error("GeneralException: " + e.getMessage());
} finally {
if (iterator != null) {
iterator.close();
}
}
return unique;
}

static boolean issAMAccountNameUniueInAD(SailPointContext context, String sAMAccountName) {

if (sAMAccountName == null || sAMAccountName.isEmpty()) {
log.debug("sAMAccountName is null / empty");
return false;
}
boolean unique = true;
String appConnName = null;
Application application = null;
Application appCopy = null;

int i = 0;
String searchFilter = "(sAMAccountName=" + sAMAccountName + ")";
List<HashMap> dnList = null;
Map setUpMap = new HashMap();
CloseableIterator iterator = null;
try {
application = context.getObjectByName(Application.class, "AD");
appCopy = (Application) application.deepCopy(context);
appCopy.setPasswordPolicies(null);
appConnName = appCopy.getConnector();
dnList = (List) appCopy.getAttributes().getMap().get("domainSettings");

for (HashMap domain : dnList) {
String searchDN = domain.get("domainDN").toString();
setUpMap.put("searchDN", searchDN);
setUpMap.put("searchFilter", searchFilter);
dnList.add(i, domain);
i++;
}
appCopy.setAttribute("searchDN", dnList);
appCopy.setAttribute("referral", "ignore");
appCopy.setAttribute("useHasMoreElements", true);
appCopy.setCustomizationRule(null);

Connector appConnector = sailpoint.connector.ConnectorFactory.getConnector(appCopy, null);
iterator = appConnector.iterateObjects(Connector.TYPE_ACCOUNT, null, null);

try {
if (iterator != null && iterator.hasNext()) {
ResourceObject user = (ResourceObject) iterator.next();
unique = false;
}
} catch (Exception e) {
}

} catch (GeneralException e) {
log.error("GeneralException: " + e.getMessage());
} finally {
if (iterator != null) {
iterator.close();
}
}
return unique;
}

static boolean isNameUniueInIIQ(SailPointContext context, String cubeName) {
if (cubeName == null || cubeName.isEmpty()) {
return false;
}
boolean unique = true;
Identity identityLookup = null;
try {
identityLookup = context.getObjectByName(Identity.class, cubeName);
if (identityLookup != null) {
unique = false;
return unique;
} else {
return unique;
}
} catch (GeneralException e) {
log.error("GeneralException" + e.getMessage());
}
return false;
}

static boolean isNameUniueInHR(SailPointContext context, String userName) {
if (userName == null || userName.isEmpty()) {
return false;
}
boolean unique = true;
Filter filter = null;
QueryOptions ops = new QueryOptions();
filter = Filter.eq(userName, userName);
try {
Iterator iterator = context.search(Identity.class, ops);
if (null != iterator && iterator.hasNext()) {
unique = false;
return unique;
} else {
return unique;
}
} catch (GeneralException e) {
log.error("GeneralException: " + e.getMessage());
}
return false;
}

static String generateUserName(SailPointContext context, Identity identity) {

String retVal = null;
String type = "";
String firstName = null;
String lastName = null;
String firstNameSub = null;
String lastNameSub = null;
int firstLength = 0;
int lastLenght = 0;
int index = 1;

if (null != identity && null != identity.getAttribute("userType")) {

type = identity.getAttribute("userType").toString();
if (type.equalsIgnoreCase("Employee")) {

if (Util.isNotNullOrEmpty(identity.getFirstname()) && Util.isNotNullOrEmpty(identity.getLastname())) {

firstName = identity.getFirstname().toLowerCase();
lastName = identity.getLastname().toLowerCase();

firstLength = firstName.length();
lastLenght = lastName.length();

firstNameSub = firstName;
lastNameSub = lastName;

if (lastLenght > 11) {
lastNameSub = lastNameSub.substring(0, 11);
}
retVal = firstName.substring(0, 1) + lastNameSub;
if (retVal != null) {
String baseName = retVal;
while (issAMAccountNameUniueInAD(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;

if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}
while (isNameUniueInIIQ(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;

if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}

while (isNameUniueInHR(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}
}
}
} else if (type.equalsIgnoreCase("Consultant")) {
if (Util.isNotNullOrEmpty(identity.getFirstname()) && Util.isNotNullOrEmpty(identity.getLastname())) {

firstName = identity.getFirstname().toLowerCase();
lastName = identity.getLastname().toLowerCase();

firstLength = firstName.length();
lastLenght = lastName.length();

firstNameSub = firstName;
lastNameSub = lastName;

if (lastLenght > 9) {
lastNameSub = lastNameSub.substring(0, 9);
}

retVal = "c-" + firstName.substring(0, 1) + lastNameSub;

if (retVal != null) {
String baseName = retVal;

while (!issAMAccountNameUniueInAD(context, retVal)) {

if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;

if (retVal.length() > 10) {
retVal = retVal.substring(0, 10);
}
} else {
return null;
}
}
while (!isNameUniueInIIQ(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;

if (retVal.length() > 10) {
retVal = retVal.substring(0, 10);
}
} else {
return null;
}
}

while (!isNameUniueInHR(context, retVal)) {
if (firstLength >= (++index)) {
retVal = firstName.substring(0, index) + lastNameSub;
if (retVal.length() > 12) {
retVal = retVal.substring(0, 12);
}
} else {
return null;
}
}
}
}
} else {
return null;
}
}
return retVal;
}

public static void main(String[] args) {
String sAMAccountName = null;
generateUserName(context, identity);
issAMAccountNameUniueInAD2(context, sAMAccountName);
}
}

Logs

import org.apache.commons.logging.Log;

import org.apache.commons.logging.LogFactory;

Log log = LogFactory.getLog("mighty.rule.TestLoggers");

log.info("Hi");

log.debug("Hello");

log.trace("Dear");

log.error("Something serious");


Note: Log naming convention is project.objectType.Unique Identifier

Sunday, August 6, 2023

Bulk roles import from a CSV file..

Ref: Bulk import roles from a CSV file - Compass (sailpoint.com)

Workflow Libraries

  What is a workflow library?

  • Workflow Libraries are sets of compiled Java methods accessible to workflows.
What are the workflow default libraries?
  • Identity
  • Role
  • PolicyViolation
  • LCM libraries
IdentityLibrary: This is used to
  • getManager()
  • activate, de-activate role assignment
  • Refresh Identities
  • Compiling Provisioning plan
  • Build, Assimilate Provisioning Forms
  • Auditing etc.,
IdentityRequestLibrary: This is used to
  • Create Identity Request, update Identity Request state
  • Refresh Identity Request afterApproval and Provisioning etc.,
ApprovalLibrary: This is used to
  • Get Object(Approval) owner and name
  • Get NewObject(Approval) owner and name
  • Checks whether it's a self-Approval or not etc.,
Policy Violation Library: This is used to
  • Get the remediateViolation: Remediate SOD violations by removing roles named in the remediations argument.
  • Delete the current approval object associated with this workflow.
Role Library: This is used to invoke the methods
  • Enable the role
  • Disable the role
  • buildOwnerApproval
LCM Library: All the methods are moved to a handler
  • audit
  • addLaunchMessage
  • Commit
Sailpoint provided the following rule libraries:

Workflow Library: This is mostly used to get the properties of an identity
which are like displayName, email, managerName, managerEmail, and getting
value of a system configuration property.

LCM Workflow Library: This is used for assimilating WorkItem
ApprovalSet, performs auditing for actions like forward, comment on workItem
and for filtering ApprovalSet.

Approval Library: This is used for getting a list of Approval owners, Role
owner, manager from a provisioning plan, etc.,

VA Installation & Configuration

Definition : A VA is a Linux-based virtual machine that connects to your sources and apps using SailPoint APIs, connectors, and integrations...

Featured Articles