summaryrefslogtreecommitdiff
path: root/zookeeper-server/src/main/java/org/apache/zookeeper/audit/AuditHelper.java
blob: 5aca1711f7a770da9de5a8a8809c68b56f58bdc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
/*
 * Licensed to the Apache Software Foundation (ASF) under one
 * or more contributor license agreements.  See the NOTICE file
 * distributed with this work for additional information
 * regarding copyright ownership.  The ASF licenses this file
 * to you under the Apache License, Version 2.0 (the
 * "License"); you may not use this file except in compliance
 * with the License.  You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.apache.zookeeper.audit;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;
import org.apache.jute.Record;
import org.apache.zookeeper.CreateMode;
import org.apache.zookeeper.KeeperException;
import org.apache.zookeeper.MultiOperationRecord;
import org.apache.zookeeper.Op;
import org.apache.zookeeper.ZKUtil;
import org.apache.zookeeper.ZooDefs;
import org.apache.zookeeper.audit.AuditEvent.Result;
import org.apache.zookeeper.proto.CreateRequest;
import org.apache.zookeeper.proto.DeleteRequest;
import org.apache.zookeeper.proto.SetACLRequest;
import org.apache.zookeeper.proto.SetDataRequest;
import org.apache.zookeeper.server.DataTree.ProcessTxnResult;
import org.apache.zookeeper.server.Request;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

/**
 * Helper class to decouple audit log code.
 */
public final class AuditHelper {
    private static final Logger LOG = LoggerFactory.getLogger(AuditHelper.class);

    public static void addAuditLog(Request request, ProcessTxnResult rc) {
        addAuditLog(request, rc, false);
    }

    /**
     * Add audit log if audit log is enabled and operation is of type which to be audit logged.
     *
     * @param request   user request
     * @param txnResult ProcessTxnResult
     * @param failedTxn whether audit is being done failed transaction for normal transaction
     */
    public static void addAuditLog(Request request, ProcessTxnResult txnResult, boolean failedTxn) {
        if (!ZKAuditProvider.isAuditEnabled()) {
            return;
        }
        String op = null;
        //For failed transaction rc.path is null
        String path = txnResult.path;
        String acls = null;
        String createMode = null;
        try {
            switch (request.type) {
                case ZooDefs.OpCode.create:
                case ZooDefs.OpCode.create2:
                case ZooDefs.OpCode.createContainer:
                    op = AuditConstants.OP_CREATE;
                    CreateRequest createRequest = new CreateRequest();
                    deserialize(request, createRequest);
                    createMode = getCreateMode(createRequest);
                    if (failedTxn) {
                        path = createRequest.getPath();
                    }
                    break;
                case ZooDefs.OpCode.delete:
                case ZooDefs.OpCode.deleteContainer:
                    op = AuditConstants.OP_DELETE;
                    if (failedTxn) {
                        DeleteRequest deleteRequest = new DeleteRequest();
                        deserialize(request, deleteRequest);
                        path = deleteRequest.getPath();
                    }
                    break;
                case ZooDefs.OpCode.setData:
                    op = AuditConstants.OP_SETDATA;
                    if (failedTxn) {
                        SetDataRequest setDataRequest = new SetDataRequest();
                        deserialize(request, setDataRequest);
                        path = setDataRequest.getPath();
                    }
                    break;
                case ZooDefs.OpCode.setACL:
                    op = AuditConstants.OP_SETACL;
                    SetACLRequest setACLRequest = new SetACLRequest();
                    deserialize(request, setACLRequest);
                    acls = ZKUtil.aclToString(setACLRequest.getAcl());
                    if (failedTxn) {
                        path = setACLRequest.getPath();
                    }
                    break;
                case ZooDefs.OpCode.multi:
                    if (failedTxn) {
                        op = AuditConstants.OP_MULTI_OP;
                    } else {
                        logMultiOperation(request, txnResult);
                        //operation si already logged
                        return;
                    }
                    break;
                case ZooDefs.OpCode.reconfig:
                    op = AuditConstants.OP_RECONFIG;
                    break;
                default:
                    //Not an audit log operation
                    return;
            }
            Result result = getResult(txnResult, failedTxn);
            log(request, path, op, acls, createMode, result);
        } catch (Throwable e) {
            LOG.error("Failed to audit log request {}", request.type, e);
        }
    }

    private static void deserialize(Request request, Record record) throws IOException {
        request.readRequestRecord(record);
    }

    private static Result getResult(ProcessTxnResult rc, boolean failedTxn) {
        if (failedTxn) {
            return Result.FAILURE;
        } else {
            return rc.err == KeeperException.Code.OK.intValue() ? Result.SUCCESS : Result.FAILURE;
        }
    }

    private static void logMultiOperation(Request request, ProcessTxnResult rc) throws IOException, KeeperException {
        Map<String, String> createModes = AuditHelper.getCreateModes(request);
        boolean multiFailed = false;
        for (ProcessTxnResult subTxnResult : rc.multiResult) {
            switch (subTxnResult.type) {
                case ZooDefs.OpCode.create:
                case ZooDefs.OpCode.create2:
                case ZooDefs.OpCode.createTTL:
                case ZooDefs.OpCode.createContainer:
                    log(request, subTxnResult.path, AuditConstants.OP_CREATE, null,
                            createModes.get(subTxnResult.path), Result.SUCCESS);
                    break;
                case ZooDefs.OpCode.delete:
                case ZooDefs.OpCode.deleteContainer:
                    log(request, subTxnResult.path, AuditConstants.OP_DELETE, null,
                            null, Result.SUCCESS);
                    break;
                case ZooDefs.OpCode.setData:
                    log(request, subTxnResult.path, AuditConstants.OP_SETDATA, null,
                            null, Result.SUCCESS);
                    break;
                case ZooDefs.OpCode.error:
                    multiFailed = true;
                    break;
                default:
                    // Do nothing, it ok, we do not log all multi operations
            }
        }
        if (multiFailed) {
            log(request, rc.path, AuditConstants.OP_MULTI_OP, null,
                    null, Result.FAILURE);
        }
    }

    private static void log(Request request, String path, String op, String acls, String createMode, Result result) {
        log(request.getUsersForAudit(), op, path, acls, createMode,
                request.cnxn.getSessionIdHex(), request.cnxn.getHostAddress(), result);
    }

    private static void log(String user, String operation, String znode, String acl,
                            String createMode, String session, String ip, Result result) {
        ZKAuditProvider.log(user, operation, znode, acl, createMode, session, ip, result);
    }

    private static String getCreateMode(CreateRequest createRequest) throws KeeperException {
        return CreateMode.fromFlag(createRequest.getFlags()).toString().toLowerCase();
    }

    private static Map<String, String> getCreateModes(Request request)
            throws IOException, KeeperException {
        Map<String, String> createModes = new HashMap<>();
        if (!ZKAuditProvider.isAuditEnabled()) {
            return createModes;
        }
        MultiOperationRecord multiRequest = new MultiOperationRecord();
        deserialize(request, multiRequest);
        for (Op op : multiRequest) {
            if (op.getType() == ZooDefs.OpCode.create || op.getType() == ZooDefs.OpCode.create2
                    || op.getType() == ZooDefs.OpCode.createContainer) {
                CreateRequest requestRecord = (CreateRequest) op.toRequestRecord();
                createModes.put(requestRecord.getPath(),
                        getCreateMode(requestRecord));
            }
        }
        return createModes;
    }

}