Thursday, October 23, 2025

Dangling Entitlement Report

 import java.util.ArrayList;

  import java.util.Iterator;

  import java.util.List;

  import java.util.regex.Matcher;

  import java.util.regex.Pattern;

  import org.apache.commons.logging.Log;

  import org.apache.commons.logging.LogFactory;

  import sailpoint.api.IdentityService;

  import sailpoint.api.SailPointContext;

  import sailpoint.object.Application;

  import sailpoint.object.EmailFileAttachment;

  import sailpoint.object.EmailOptions;

  import sailpoint.object.EmailTemplate;

  import sailpoint.object.Filter;

  import sailpoint.object.Identity;

  import sailpoint.object.IdentityEntitlement;

  import sailpoint.object.Link;

  import sailpoint.object.QueryOptions;

  import sailpoint.tools.GeneralException;

  import sailpoint.tools.Util;


  public static boolean isValidUser(String name) {

    String regex = "^-?\\d+$";

    Pattern pattern = Pattern.compile(regex);

    Matcher matcher = pattern.matcher(name);

    return matcher.matches();

  }


  boolean flag = false;

  String name = null;

  String entitlement = null;

  List adLinks = null;

  List ticket = null;

  Identity identity = null;

  Application adApp = null;

  IdentityService adIdentityService = null;

  Iterator iterator = null;


  try {

    adLinks = new ArrayList();

    ticket = new ArrayList();

    ticket.add("name,Entitlement");


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

    if (adApp == null) {

      logger.error("AD application not found");

      return "AD application not found";

    }


    Filter filter = Filter.and(Filter.eq("type", "Entitlement"), Filter.eq("aggregationState", "Connected"), Filter.eq("application", adApp));

    QueryOptions qo = new QueryOptions();

    qo.addFilter(filter);


    iterator = context.search(IdentityEntitlement.class, qo);


    while (iterator.hasNext()) {

      IdentityEntitlement ide = iterator.next();


      if ((ide.getSourceAssignableRoles() == null @or ide.getSourceAssignableRoles().isEmpty()) @and ide.getIdentity() != null @and ide.getIdentity().isCorrelated()) {


        identity = ide.getIdentity();

        name = identity.getName();

        flag = isValidUser(name);


        if (flag) {

          adIdentityService = new IdentityService(context);

          adLinks = adIdentityService.getLinks(identity, adApp);


          if (adLinks != null @and !adLinks.isEmpty()) {

            for (Link link : adLinks) {

              if (link.getAttribute("serviceAccount") == null) {

                

                entitlement = ide.getValue().toString();

                ticket.add("\n" + name + "," + entitlement);

              }

            }

          }

        }

      }

    }


    EmailOptions emOptions = new EmailOptions();

    EmailTemplate snowTemplate = context.getObjectByName(EmailTemplate.class, "AD-Dang-ENT-TicketEmail");


    if (ticket != null @and !ticket.isEmpty()) {


      String tickerDetails = ticket.toString().replace("[","").replace("]","");

      byte[] ticketDetailsDataByte = tickerDetails.getBytes();

      EmailFileAttachment ticFileAttachment = new EmailFileAttachment("AD-Dangling-ENTS.csv", EmailFileAttachment.MimeType.MIME_CSV, ticketDetailsDataByte);


      emOptions.addAttachment(ticFileAttachment);

      emOptions.setVariable("createIncident", "##CREATEINCIDENT##");

      emOptions.setVariable("assignmentGroup", "Pedia-Enterprice");

      emOptions.setTo("admin@example.com");


      context.sendEmailNotification(snowTemplate, emOptions);


    }


  } catch (GeneralException e) {

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

  }

  finally {

    if (iterator != null) {

      Util.flushIterator(iterator);

    }

    if (adLinks != null) {

      adLinks.clear();

    }

    if (ticket != null) {

      ticket.clear();

    }

  }


No comments:

Post a Comment

Access Intelligence Center

  Search Overview - SailPoint Identity Services https://documentation.sailpoint.com/saas/help/search/index.html