Search in sources :

Example 1 with ProvisioningResult

use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.

the class TremoloTarget method executeWorkFlow.

private void executeWorkFlow(String wfName, User user, Set<String> attributes, Map<String, Object> request) throws ProvisioningException {
    StringBuffer surl = new StringBuffer();
    surl.append(this.wfUrlBase).append("/services/wf/login");
    HttpGet get = new HttpGet(surl.toString());
    try {
        try {
            httpclient.execute(get);
        } catch (ClientProtocolException e1) {
        } catch (IOException e1) {
        }
    } finally {
        get.releaseConnection();
    }
    surl.setLength(0);
    surl.append(this.wfUrlBase).append("/services/wf/execute");
    HttpPost post = new HttpPost(surl.toString());
    try {
        TremoloUser tu = new TremoloUser();
        tu.setAttributes(new ArrayList<Attribute>());
        tu.setUid(user.getUserID());
        tu.setUserPassword(user.getPassword());
        for (String attrName : user.getAttribs().keySet()) {
            Attribute attr = user.getAttribs().get(attrName);
            if (attributes.size() == 0 || attributes.contains(attrName)) {
                tu.getAttributes().add(attr);
            }
        }
        WFCall wfcall = new WFCall();
        wfcall.setName(wfName);
        wfcall.setUidAttributeName(this.uidAttrName);
        wfcall.setUser(tu);
        wfcall.setRequestParams(new HashMap<String, Object>());
        wfcall.getRequestParams().put(ProvisioningParams.UNISON_EXEC_TYPE, ProvisioningParams.UNISON_EXEC_SYNC);
        Gson gson = new Gson();
        String jsonOut = gson.toJson(wfcall);
        List<NameValuePair> formparams = new ArrayList<NameValuePair>();
        formparams.add(new BasicNameValuePair("wfcall", jsonOut));
        UrlEncodedFormEntity entity = new UrlEncodedFormEntity(formparams, "UTF-8");
        post.setEntity(entity);
        HttpResponse response = httpclient.execute(post);
        BufferedReader in = new BufferedReader(new InputStreamReader(response.getEntity().getContent()));
        String line = null;
        StringBuffer res = new StringBuffer();
        while ((line = in.readLine()) != null) {
            // System.out.println(line);
            res.append(line).append('\n');
        }
        ProvisioningResult provRes = gson.fromJson(res.toString(), ProvisioningResult.class);
        if (!provRes.isSuccess()) {
            throw new ProvisioningException(provRes.getError().getError());
        }
    } catch (Exception e) {
        throw new ProvisioningException("Could not execute workflow", e);
    } finally {
        post.releaseConnection();
    }
}
Also used : HttpPost(org.apache.http.client.methods.HttpPost) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) NameValuePair(org.apache.http.NameValuePair) WFCall(com.tremolosecurity.provisioning.service.util.WFCall) InputStreamReader(java.io.InputStreamReader) Attribute(com.tremolosecurity.saml.Attribute) HttpGet(org.apache.http.client.methods.HttpGet) ProvisioningResult(com.tremolosecurity.provisioning.service.util.ProvisioningResult) ArrayList(java.util.ArrayList) Gson(com.google.gson.Gson) HttpResponse(org.apache.http.HttpResponse) IOException(java.io.IOException) UrlEncodedFormEntity(org.apache.http.client.entity.UrlEncodedFormEntity) ClientProtocolException(org.apache.http.client.ClientProtocolException) MalformedCookieException(org.apache.http.cookie.MalformedCookieException) MalformedURLException(java.net.MalformedURLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) ClientProtocolException(org.apache.http.client.ClientProtocolException) TremoloUser(com.tremolosecurity.provisioning.service.util.TremoloUser) BasicNameValuePair(org.apache.http.message.BasicNameValuePair) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) BufferedReader(java.io.BufferedReader)

Example 2 with ProvisioningResult

use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.

the class ExecuteWorkflow method doPost.

@Override
protected void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    Gson gson = new Gson();
    String wfcall = req.getParameter("wfcall");
    if (wfcall == null) {
        logger.error("Could not get workflow call");
        resp.setStatus(500);
        ProvisioningError pe = new ProvisioningError();
        ProvisioningResult pres = new ProvisioningResult();
        pres.setSuccess(false);
        pres.setError(pe);
        pe.setError("Could not get workflow call");
        gson = new Gson();
        resp.getOutputStream().print(gson.toJson(pres));
        return;
    }
    String line;
    StringBuffer json = new StringBuffer();
    BufferedReader in = new BufferedReader(new InputStreamReader(new ByteArrayInputStream(wfcall.getBytes("UTF-8"))));
    while ((line = in.readLine()) != null) {
        json.append(line).append('\n');
    }
    WFCall wfCall = gson.fromJson(json.toString(), WFCall.class);
    if (wfCall == null) {
        logger.error("Could not get workflow call");
        resp.setStatus(500);
        ProvisioningError pe = new ProvisioningError();
        pe.setError("Could not get workflow call");
        ProvisioningResult pres = new ProvisioningResult();
        pres.setSuccess(false);
        pres.setError(pe);
        gson = new Gson();
        resp.getOutputStream().print(gson.toJson(pres));
        return;
    }
    List<ApprovalData> autoApprovals = null;
    try {
        // TremoloContext.getContext().getConfigManager("proxy").getProvisioningEngine().getWorkFlow(wfCall.getName()).executeWorkflow(wfCall);
        com.tremolosecurity.provisioning.workflow.ExecuteWorkflow exec = new com.tremolosecurity.provisioning.workflow.ExecuteWorkflow();
        exec.execute(wfCall, GlobalEntries.getGlobalEntries().getConfigManager());
        ProvisioningResult res = new ProvisioningResult();
        res.setSuccess(true);
        resp.getOutputStream().print(gson.toJson(res));
    } catch (Throwable t) {
        logger.error("Error executing workflow", t);
        resp.setStatus(500);
        ProvisioningError pe = new ProvisioningError();
        pe.setError("Error executing workflow");
        ProvisioningResult pres = new ProvisioningResult();
        pres.setSuccess(false);
        pres.setError(pe);
        gson = new Gson();
        resp.getOutputStream().print(gson.toJson(pres));
    }
}
Also used : WFCall(com.tremolosecurity.provisioning.service.util.WFCall) InputStreamReader(java.io.InputStreamReader) ProvisioningResult(com.tremolosecurity.provisioning.service.util.ProvisioningResult) Gson(com.google.gson.Gson) ApprovalData(com.tremolosecurity.provisioning.workflow.ApprovalData) ProvisioningError(com.tremolosecurity.provisioning.service.util.ProvisioningError) ByteArrayInputStream(java.io.ByteArrayInputStream) BufferedReader(java.io.BufferedReader)

Example 3 with ProvisioningResult

use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.

the class ExecutedWorkflows method doGet.

@Override
protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
    String userKey = req.getParameter("user");
    Session session = null;
    try {
        session = GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getHibernateSessionFactory().openSession();
        // PreparedStatement ps = con.prepareStatement("select workflows.id,workflows.name from workflows inner join users on users.id=workflows.userid where workflows.completeTS IS NOT NULL AND userKey=?");
        Query query = session.createQuery("FROM Workflows WHERE Workflows.completeTS IS NOT NULL AND Workflows.users.userKey = :user_key");
        query.setParameter("user_key", userKey);
        List<com.tremolosecurity.provisioning.objects.Workflows> workflows = query.list();
        ArrayList<String> workflowids = new ArrayList<String>();
        for (Workflows wf : workflows) {
            if (wf.getApprovals().isEmpty()) {
                workflowids.add(wf.getName());
            } else {
                boolean approved = true;
                for (Approvals approval : wf.getApprovals()) {
                    approved = approved && (approval.getApproved() == 1 && approval.getApprovedTs() != null);
                }
            }
        }
        Gson gson = new Gson();
        ProvisioningResult resObj = new ProvisioningResult();
        resObj.setSuccess(true);
        resObj.setWorkflowIds(workflowids);
        resp.getOutputStream().println(gson.toJson(resObj));
    } catch (Exception e) {
        ProvisioningError pe = new ProvisioningError();
        pe.setError("Could not load executed workflows : " + e.getMessage());
        ProvisioningResult res = new ProvisioningResult();
        res.setSuccess(false);
        res.setError(pe);
        Gson gson = new Gson();
        resp.getWriter().write(gson.toJson(res));
    } finally {
        if (session != null) {
            session.close();
        }
    }
}
Also used : Query(org.hibernate.Query) Workflows(com.tremolosecurity.provisioning.objects.Workflows) ProvisioningResult(com.tremolosecurity.provisioning.service.util.ProvisioningResult) ArrayList(java.util.ArrayList) Approvals(com.tremolosecurity.provisioning.objects.Approvals) Gson(com.google.gson.Gson) ServletException(javax.servlet.ServletException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) SQLException(java.sql.SQLException) ProvisioningError(com.tremolosecurity.provisioning.service.util.ProvisioningError) Session(org.hibernate.Session)

Example 4 with ProvisioningResult

use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.

the class GenerateReport method doGet.

@Override
protected void doGet(final HttpServletRequest req, final HttpServletResponse resp) throws ServletException, IOException {
    final Gson gson = new Gson();
    try {
        String name = req.getParameter("name");
        ReportType reportToRunTmp = null;
        boolean foundReport = false;
        for (ReportType report : GlobalEntries.getGlobalEntries().getConfigManager().getCfg().getProvisioning().getReports().getReport()) {
            if (report.getName().equalsIgnoreCase(name)) {
                reportToRunTmp = report;
                foundReport = true;
                break;
            }
        }
        if (!foundReport) {
            reportToRunTmp = null;
        }
        final ReportType reportToRun = reportToRunTmp;
        if (reportToRun == null) {
            throw new ProvisioningException("Could not find report");
        } else {
            Connection db = null;
            PreparedStatement ps = null;
            ResultSet rs = null;
            try {
                Session session = GlobalEntries.getGlobalEntries().getConfigManager().getProvisioningEngine().getHibernateSessionFactory().openSession();
                session.doWork(new Work() {

                    public void execute(Connection connection) throws SQLException {
                        try {
                            generateReportData(req, resp, gson, reportToRun, connection);
                        } catch (IOException e) {
                            throw new SQLException("Could not run reports", e);
                        }
                    }
                });
            } finally {
            }
        }
    } catch (Exception e) {
        logger.error("Could not run report", e);
        resp.setStatus(500);
        ProvisioningError pe = new ProvisioningError();
        pe.setError("Could not run report;" + e.getMessage());
        ProvisioningResult resObj = new ProvisioningResult();
        resObj.setSuccess(false);
        resObj.setError(pe);
        resp.getOutputStream().print(gson.toJson(resObj));
    }
}
Also used : SQLException(java.sql.SQLException) ProvisioningResult(com.tremolosecurity.provisioning.service.util.ProvisioningResult) Connection(java.sql.Connection) Gson(com.google.gson.Gson) PreparedStatement(java.sql.PreparedStatement) IOException(java.io.IOException) ServletException(javax.servlet.ServletException) SQLException(java.sql.SQLException) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) IOException(java.io.IOException) ProvisioningError(com.tremolosecurity.provisioning.service.util.ProvisioningError) ProvisioningException(com.tremolosecurity.provisioning.core.ProvisioningException) ResultSet(java.sql.ResultSet) Work(org.hibernate.jdbc.Work) ReportType(com.tremolosecurity.config.xml.ReportType) Session(org.hibernate.Session)

Example 5 with ProvisioningResult

use of com.tremolosecurity.provisioning.service.util.ProvisioningResult in project OpenUnison by TremoloSecurity.

the class ScaleMain method generateReport.

private void generateReport(HttpFilterRequest request, HttpFilterResponse response, Gson gson, ReportType reportToRun, AuthInfo userData, Connection db) throws SQLException, IOException {
    PreparedStatement ps = null;
    ResultSet rs = null;
    try {
        if (logger.isDebugEnabled()) {
            logger.debug("Report SQL : '" + reportToRun.getSql() + "'");
        }
        ps = db.prepareStatement(reportToRun.getSql());
        int i = 1;
        for (String paramType : reportToRun.getParamater()) {
            switch(paramType) {
                case "currentUser":
                    String userid = userData.getAttribs().get(this.scaleConfig.getUidAttributeName()).getValues().get(0);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Current User : '" + userid + "'");
                    }
                    ps.setString(i, userid);
                    break;
                case "userKey":
                    if (logger.isDebugEnabled()) {
                        logger.debug("User Key : '" + request.getParameter("userKey") + "'");
                    }
                    ps.setString(i, request.getParameter("userKey").getValues().get(0));
                    break;
                case "beginDate":
                    String beginDate = request.getParameter("beginDate").getValues().get(0);
                    if (logger.isDebugEnabled()) {
                        logger.debug("Begin Date : '" + beginDate + "'");
                    }
                    Date d = new Date(Long.parseLong(beginDate));
                    ps.setDate(i, d);
                    break;
                case "endDate":
                    String endDate = request.getParameter("endDate").getValues().get(0);
                    if (logger.isDebugEnabled()) {
                        logger.debug("End Date : '" + endDate + "'");
                    }
                    Date de = new Date(Long.parseLong(endDate));
                    ps.setDate(i, de);
                    break;
            }
            i++;
        }
        rs = ps.executeQuery();
        String groupingVal = null;
        ReportResults res = new ReportResults();
        res.setName(reportToRun.getName());
        res.setDescription(reportToRun.getDescription());
        res.setDataFields(reportToRun.getDataFields());
        res.setHeaderFields(reportToRun.getHeaderFields());
        res.setGrouping(new ArrayList<ReportGrouping>());
        ReportGrouping grouping = null;
        if (!reportToRun.isGroupings()) {
            grouping = new ReportGrouping();
            grouping.setData(new ArrayList<Map<String, String>>());
            grouping.setHeader(new HashMap<String, String>());
            res.getGrouping().add(grouping);
        }
        logger.debug("Running report");
        while (rs.next()) {
            if (logger.isDebugEnabled()) {
                logger.debug("New row");
            }
            HashMap<String, String> row = new HashMap<String, String>();
            for (String dataField : reportToRun.getDataFields()) {
                if (logger.isDebugEnabled()) {
                    logger.debug("Field - " + dataField + "='" + rs.getString(dataField) + "'");
                }
                row.put(dataField, rs.getString(dataField));
            }
            if (reportToRun.isGroupings()) {
                String rowID = rs.getString(reportToRun.getGroupBy());
                if (logger.isDebugEnabled()) {
                    logger.debug("Grouping Val : '" + groupingVal + "'");
                    logger.debug("Group By : '" + reportToRun.getGroupBy() + "'");
                    logger.debug("Value of Group By in row : '" + rowID + "'");
                }
                if (groupingVal == null || !groupingVal.equals(rowID)) {
                    grouping = new ReportGrouping();
                    grouping.setData(new ArrayList<Map<String, String>>());
                    grouping.setHeader(new HashMap<String, String>());
                    res.getGrouping().add(grouping);
                    for (String headerField : reportToRun.getHeaderFields()) {
                        grouping.getHeader().put(headerField, rs.getString(headerField));
                    }
                    groupingVal = rowID;
                }
            }
            grouping.getData().add(row);
        }
        if (request.getParameter("excel") != null && request.getParameter("excel").getValues().get(0).equalsIgnoreCase("true")) {
            UUID id = UUID.randomUUID();
            String sid = id.toString();
            Map<String, String> map = new HashMap<String, String>();
            map.put("reportid", sid);
            request.getSession().setAttribute(sid, res);
            String json = gson.toJson(map);
            if (logger.isDebugEnabled()) {
                logger.debug("JSON : " + json);
            }
            response.setContentType("application/json");
            ScaleJSUtils.addCacheHeaders(response);
            response.getWriter().print(json);
            response.getWriter().flush();
        } else {
            ProvisioningResult pres = new ProvisioningResult();
            pres.setSuccess(true);
            pres.setReportResults(res);
            String json = gson.toJson(res);
            if (logger.isDebugEnabled()) {
                logger.debug("JSON : " + json);
            }
            response.setContentType("application/json");
            ScaleJSUtils.addCacheHeaders(response);
            response.getWriter().print(json);
            response.getWriter().flush();
        }
    } finally {
        if (rs != null) {
            try {
                rs.close();
            } catch (Throwable t) {
            }
        }
        if (ps != null) {
            try {
                ps.close();
            } catch (Throwable t) {
            }
        }
        if (db != null) {
            try {
                db.close();
            } catch (Throwable t) {
            }
        }
    }
}
Also used : HashMap(java.util.HashMap) ProvisioningResult(com.tremolosecurity.provisioning.service.util.ProvisioningResult) PreparedStatement(java.sql.PreparedStatement) ReportGrouping(com.tremolosecurity.provisioning.service.util.ReportGrouping) XSSFRichTextString(org.apache.poi.xssf.usermodel.XSSFRichTextString) RichTextString(org.apache.poi.ss.usermodel.RichTextString) Date(java.sql.Date) ResultSet(java.sql.ResultSet) ReportResults(com.tremolosecurity.provisioning.service.util.ReportResults) UUID(java.util.UUID) Map(java.util.Map) HashMap(java.util.HashMap)

Aggregations

ProvisioningResult (com.tremolosecurity.provisioning.service.util.ProvisioningResult)14 Gson (com.google.gson.Gson)12 ProvisioningError (com.tremolosecurity.provisioning.service.util.ProvisioningError)10 ProvisioningException (com.tremolosecurity.provisioning.core.ProvisioningException)9 IOException (java.io.IOException)7 Attribute (com.tremolosecurity.saml.Attribute)6 ServletException (javax.servlet.ServletException)6 ArrayList (java.util.ArrayList)5 LDAPAttribute (com.novell.ldap.LDAPAttribute)4 LDAPEntry (com.novell.ldap.LDAPEntry)4 LDAPSearchResults (com.novell.ldap.LDAPSearchResults)4 LDAPAttributeSet (com.novell.ldap.LDAPAttributeSet)3 AuthInfo (com.tremolosecurity.proxy.auth.AuthInfo)3 AzSys (com.tremolosecurity.proxy.auth.AzSys)3 MalformedURLException (java.net.MalformedURLException)3 HashMap (java.util.HashMap)3 Map (java.util.Map)3 ConfigManager (com.tremolosecurity.config.util.ConfigManager)2 OrgType (com.tremolosecurity.config.xml.OrgType)2 ReportType (com.tremolosecurity.config.xml.ReportType)2