blob: 2a8e483d48bc6c25b87aabbd18ef520ee6dc3aa8 [file] [log] [blame]
Arjun E K4554ecd2018-12-24 02:19:10 -05001/*
2 * Copyright 2016-present Open Networking Foundation
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package org.onosproject.audit.impl;
18
19import org.apache.felix.scr.annotations.Component;
20import org.apache.felix.scr.annotations.Service;
21import org.apache.felix.scr.annotations.ReferenceCardinality;
22import org.apache.felix.scr.annotations.Reference;
23import org.apache.felix.scr.annotations.Property;
24import org.apache.felix.scr.annotations.Activate;
25import org.apache.felix.scr.annotations.Deactivate;
26import org.apache.felix.scr.annotations.Modified;
27import org.onosproject.cfg.ComponentConfigService;
28import org.onosproject.security.AuditService;
29import org.osgi.service.component.ComponentContext;
30import org.slf4j.Logger;
31import org.slf4j.LoggerFactory;
32
33import java.util.Dictionary;
34
35import static org.onlab.util.Tools.get;
36
37/**
38 * Component to manage audit logging.
39 */
40@Component(immediate = true)
41@Service
42public class AuditManager implements AuditService {
43
44 private final Logger log = LoggerFactory.getLogger(getClass());
45
46 private Logger auditLog = log;
47
48 private static final String AUDIT_ENABLED = "auditEnabled";
49 private static final boolean AUDIT_ENABLED_DEFAULT = false;
50
51 private static final String AUDIT_LOGGER = "auditLogger";
52 private static final String AUDIT_LOGGER_DEFAULT = "securityAudit";
53
54
55 @Property(name = AUDIT_ENABLED, boolValue = AUDIT_ENABLED_DEFAULT,
56 label = "Specifies whether or not audit logging is enabled.")
57 private boolean auditEnabled = AUDIT_ENABLED_DEFAULT;
58
59 @Property(name = AUDIT_LOGGER, value = AUDIT_LOGGER_DEFAULT,
60 label = "Name of the audit logger.")
61 private String auditLogger = AUDIT_LOGGER_DEFAULT;
62
63 @Reference(cardinality = ReferenceCardinality.MANDATORY_UNARY)
64 protected ComponentConfigService cfgService;
65
66 @Activate
67 protected void activate(ComponentContext ctx) {
68 cfgService.registerProperties(getClass());
69 modified(ctx);
70 log.info("Started");
71 }
72
73 @Deactivate
74 protected void deactivate(ComponentContext ctx) {
75 log.info("Stopped");
76 }
77
78 @Modified
79 protected void modified(ComponentContext ctx) {
80 Dictionary<?, ?> properties = ctx.getProperties();
81 if (properties != null) {
82 auditEnabled = Boolean.parseBoolean(get(properties, AUDIT_ENABLED));
83 auditLogger = get(properties, AUDIT_LOGGER);
84 auditLog = LoggerFactory.getLogger(auditLogger);
85 log.info("Reconfigured; auditEnabled={}; auditLogger={}", auditEnabled, auditLogger);
86 }
87 }
88
89 @Override
90 public boolean isAuditing() {
91 return auditEnabled;
92 }
93
94 @Override
95 public void logUserAction(String user, String action) {
96 if (auditEnabled) {
97 action = action.concat(" | " + auditLogger);
98 auditLog.info("user={}; action={}", user, action);
99 }
100 }
101
102}