001/*
002 * (C) Copyright 2015 Nuxeo SA (http://nuxeo.com/) and contributors.
003 *
004 * All rights reserved. This program and the accompanying materials
005 * are made available under the terms of the GNU Lesser General Public License
006 * (LGPL) version 2.1 which accompanies this distribution, and is available at
007 * http://www.gnu.org/licenses/lgpl-2.1.html
008 *
009 * This library is distributed in the hope that it will be useful,
010 * but WITHOUT ANY WARRANTY; without even the implied warranty of
011 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
012 * Lesser General Public License for more details.
013 *
014 * Contributors:
015 *     <a href="mailto:grenard@nuxeo.com">Guillaume Renard</a>
016 *
017 */
018
019package org.nuxeo.elasticsearch.http.readonly.filter;
020
021import org.json.JSONException;
022import org.nuxeo.ecm.core.api.CoreSession;
023import org.nuxeo.ecm.core.api.NuxeoPrincipal;
024import org.nuxeo.elasticsearch.ElasticSearchConstants;
025import org.nuxeo.elasticsearch.api.ElasticSearchAdmin;
026import org.nuxeo.elasticsearch.http.readonly.AbstractSearchRequestFilterImpl;
027import org.nuxeo.runtime.api.Framework;
028
029/**
030 * Define a elasticsearch passthrough filter for audit index. Only administrator can access the audit index.
031 *
032 * @since 7.4
033 */
034public class AuditRequestFilter extends AbstractSearchRequestFilterImpl {
035
036    @Override
037    public void init(CoreSession session, String indices, String types, String rawQuery, String payload) {
038        principal = (NuxeoPrincipal) session.getPrincipal();
039        if (!principal.isAdministrator()) {
040            throw new IllegalArgumentException("Invalid index submitted: " + indices);
041        }
042        ElasticSearchAdmin esa = Framework.getService(ElasticSearchAdmin.class);
043        this.indices = esa.getIndexNameForType(ElasticSearchConstants.ENTRY_TYPE);
044        this.types = ElasticSearchConstants.ENTRY_TYPE;
045        this.rawQuery = rawQuery;
046        this.payload = payload;
047        if (payload == null && !principal.isAdministrator()) {
048            // here we turn the UriSearch query_string into a body search
049            extractPayloadFromQuery();
050        }
051    }
052
053    @Override
054    public String getPayload() throws JSONException {
055        return payload;
056    }
057
058}