dimanche 28 mars 2010

Validate Essbase filters

Here is a small but efficient piece of Java code (+JAPI library) made to list, verify and validate Essbase filters.
I was inspired by the JAPI code samples available in APS (Provider Services) directory (%APS_HOME%\samples\japi) and the rest was guided by my imagination and needs. By the way it also displays user and groups assigned to the filter.
We run this script every night and then add it to the server export package together with other maxl exports, Essbase.cfg and SEC's backup. So we have a daily file ready for any security audit.

I did not post any compiled code as this is not a big deal doing it in Eclipse or Netbeans. just don't forget adding JAPI library before compiling (%APS_HOME%\lib\ess_japi.jar).

import com.essbase.api.base.EssException;
import com.essbase.api.base.IEssIterator;
import com.essbase.api.datasource.IEssCube;
import com.essbase.api.datasource.IEssCube.IEssSecurityFilter;
import com.essbase.api.datasource.IEssOlapApplication;
import com.essbase.api.datasource.IEssOlapServer;
import com.essbase.api.domain.IEssDomain;
import com.essbase.api.session.IEssbase;
import java.util.Arrays;

/* Liste, détaille et valide les filtres Essbase
 * Liste les utilisateurs et groupes associés au filtre
 *
 * Display, detail and validate Essbase filters
 * Display filter associated users and groups
 *
 * Version : 1.0 (20 mars 2010)
 * Paramètres : utilisateur, mot de passe, serveur
 * Auteur : Sébastien Roux
 * Mail : roux.sebastien@gmail.com
 *
 * Disclaimer : you may freely use, reuse, modify and distribute
 * the following code at your own risks.
 */
public class ValidateEssFilter {

    private static String essuser = "";
    private static String esspwd = "";
    private static String esssvr = "";
    private static final int FAILURE_CODE = 1;
    private static final String DELIM = "\t";

    public static void main(String[] args) {
        int sts = 0;
        IEssbase essbase = null;
        IEssOlapServer olapSvr = null;

        try {
            // Check arguments
            acceptArgs(args);

            essbase = IEssbase.Home.create(IEssbase.JAPI_VERSION);
            IEssDomain dom = essbase.signOn(essuser, esspwd, false, null, "http://" + esssvr + ":13080/aps/JAPI");
            olapSvr = (IEssOlapServer) dom.getOlapServer(esssvr);
            olapSvr.connect();

            getApplications(olapSvr);

        } catch (EssException x) {
            System.out.println("Erreur " + x.getLocalizedMessage());
            //x.printStackTrace();
            sts = FAILURE_CODE;
        } catch (Exception e) {
            System.out.println("Erreur " + e.getLocalizedMessage());
            //e.printStackTrace();
        } finally {
            try {
                if (essbase != null && essbase.isSignedOn() == true) {
                    essbase.signOff();
                }
            } catch (EssException x) {
                System.err.println("Error: " + x.getLocalizedMessage());
            }
        }
        if (sts == FAILURE_CODE) {
            System.exit(FAILURE_CODE);
        }
    }

    static void getApplications(IEssOlapServer olapSvr) {

        try {
            // List applications
            IEssIterator usrApp = olapSvr.getApplications();

            // Header
            System.out.println("Application" + DELIM + "Database" + DELIM
                    + "Filtre" + DELIM + "Accès" + DELIM + "Détail" + DELIM
                    + "Validation" + DELIM + "Utilisateurs et groupes affectés");

            // Loop through applications
            for (int i = 0; i < usrApp.getCount(); i++) {
                IEssOlapApplication app = (IEssOlapApplication) usrApp.getAt(i);

                // List databases for each application
                IEssIterator usrDb = app.getCubes();

                // Loop through each database
                for (int j = 0; j < usrDb.getCount(); j++) {
                    IEssCube db = (IEssCube) usrDb.getAt(j);

                    // List filters for each database
                    IEssIterator dbFilter = db.getSecurityFilters();

                    // Loop through filters
                    for (int k = 0; k < dbFilter.getCount(); k++) {
                        IEssSecurityFilter filter = (IEssSecurityFilter) dbFilter.getAt(k);

                        // List filter content
                        IEssCube.IEssSecurityFilter.IEssFilterRow filterRow = filter.getFilterRow();

                        while (filterRow.getRowString() != null) {

                            // Verify/validate filter
                            String[] rowStrings = {filterRow.getRowString()};
                            String verify = "";

                            try {
                                //filter OK
                                filter.verifyFilter(rowStrings);
                                verify = "Filtre OK";
                            } catch (Exception e) {
                                //filter KO - error in filter
                                verify = "Filtre KO " + e.getLocalizedMessage();
                            }

                            // List filter associated users and groups
                            String[] filterUsers = filter.getSecurityFilterList();
                            // Sort asc
                            Arrays.sort(filterUsers);

                            String userList = "";
                            // Array to comma delimiter string
                            if (filterUsers.length == 0) {
                                // Filter is not associated to any user or group
                                userList = "Aucune affectation";
                            } else {
                                for (int l = 0; l < filterUsers.length; l++) {
                                    if (l == 0) {
                                        userList = filterUsers[0];
                                    } else {
                                        userList = userList + ", " + filterUsers[l];
                                    }
                                }
                            }

                            // Output
                            System.out.println(
                                    app.getName()
                                    + DELIM + db.getName()
                                    + DELIM + filter.getName()
                                    + DELIM + getAccessLabel(filterRow.getAccess())
                                    + DELIM + filterRow.getRowString()
                                    + DELIM + verify
                                    + DELIM + userList);

                            filterRow = filter.getFilterRow();
                        }
                    }
                }
            }
        } catch (Exception x) {
            System.out.println("Erreur " + x.getLocalizedMessage());
            //x.printStackTrace();
        }
    }

    // Access level translation
    static String getAccessLabel(short accessID) {
        String access = "";

        switch (accessID) {
            case 0:
                access = "none";
                break;
            case 273:
                access = "read";
                break;
            case 275:
                access = "write";
                break;
            case 280:
                access = "metaread";
                break;
        }
        return access;
    }

    // Arguments management
    static void acceptArgs(String[] args) throws EssException {
        if (args.length == 3) {
            essuser = args[0];
            esspwd = args[1];
            esssvr = args[2];
        } else if (args.length < 3 || args.length > 3) {
            System.err.println("Execution error");
            System.err.println(
                    "Usage: java -jar " + ValidateEssFilter.class.getName() + " user password server" + "\n"
                    + "Description: display, detail and validate Essbase filters. Display filter associated users and groups" + "\n"
                    + "Requirements: JRE 1.5 or higher" + "\n"
                    + "Version: 1.0" + "\n"
                    + "Author: Sebastien Roux roux.sebastien@mail.com");
            System.exit(1);
        }
    }
}

Aucun commentaire:

Enregistrer un commentaire

your comment here please!