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.audit.ESAuditBackend;
025import org.nuxeo.elasticsearch.http.readonly.AbstractSearchRequestFilterImpl;
026
027/**
028 * Define a elasticsearch passthrough filter for audit index. Only administrator can access the audit index.
029 *
030 * @since 7.4
031 */
032public class AuditRequestFilter extends AbstractSearchRequestFilterImpl {
033
034    public void init(CoreSession session, String indices, String types, String rawQuery, String payload) {
035        this.principal = (NuxeoPrincipal) session.getPrincipal();
036        if (!this.principal.isAdministrator()) {
037            throw new IllegalArgumentException("Invalid index submitted: " + indices);
038        }
039        this.indices = ESAuditBackend.IDX_NAME;
040        this.types = ESAuditBackend.IDX_TYPE;
041        this.rawQuery = rawQuery;
042        this.payload = payload;
043        if (payload == null && !principal.isAdministrator()) {
044            // here we turn the UriSearch query_string into a body search
045            extractPayloadFromQuery();
046        }
047    }
048
049    @Override
050    public String getPayload() throws JSONException {
051        return payload;
052    }
053
054    @Override
055    public String getIndices() {
056        return ESAuditBackend.IDX_NAME;
057    }
058
059    @Override
060    public String getTypes() {
061        return ESAuditBackend.IDX_TYPE;
062    }
063
064}