blob: a871f3c018f18ca77d4ebed361f36056d47c0f68 [file] [log] [blame]
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -08001/*
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.onosproject.cfg.ComponentConfigService;
20import org.onosproject.security.AuditService;
21import org.osgi.service.component.ComponentContext;
22import org.osgi.service.component.annotations.Component;
23import org.osgi.service.component.annotations.Deactivate;
24import org.osgi.service.component.annotations.Modified;
25import org.osgi.service.component.annotations.Activate;
26import org.osgi.service.component.annotations.Reference;
27import org.osgi.service.component.annotations.ReferenceCardinality;
28import org.slf4j.Logger;
29import org.slf4j.LoggerFactory;
30
31import java.util.Dictionary;
32
33import static org.onlab.util.Tools.get;
34import static org.onosproject.net.OsgiPropertyConstants.AUDIT_LOGGER;
35import static org.onosproject.net.OsgiPropertyConstants.AUDIT_LOGGER_DEFAULT;
36import static org.onosproject.net.OsgiPropertyConstants.AUDIT_ENABLED;
37import static org.onosproject.net.OsgiPropertyConstants.AUDIT_ENABLED_DEFAULT;
38
39/**
40 * Component to manage audit logging.
41 */
42@Component(
43 immediate = true,
44 service = { AuditService.class },
45 property = {
46 AUDIT_ENABLED + ":Boolean=" + AUDIT_ENABLED_DEFAULT,
47 AUDIT_LOGGER + "=" + AUDIT_LOGGER_DEFAULT,
48 })
49public class AuditManager implements AuditService {
50
51 private final Logger log = LoggerFactory.getLogger(getClass());
52
53 private Logger auditLog = log;
54
55 /** Specifies whether or not audit logging is enabled. */
56 private boolean auditEnabled = AUDIT_ENABLED_DEFAULT;
57
58 /** Name of the audit logger. */
59 private String auditFile = AUDIT_LOGGER_DEFAULT;
60
61 @Reference(cardinality = ReferenceCardinality.MANDATORY)
62 protected ComponentConfigService cfgService;
63
64 @Activate
65 protected void activate(ComponentContext ctx) {
66 cfgService.registerProperties(getClass());
67 modified(ctx);
68 log.info("Started");
69 }
70
71 @Deactivate
72 protected void deactivate(ComponentContext ctx) {
73 log.info("Stopped");
74 }
75
76 @Modified
77 protected void modified(ComponentContext ctx) {
78 Dictionary<?, ?> properties = ctx.getProperties();
79 if (properties != null) {
80 auditEnabled = Boolean.parseBoolean(get(properties, AUDIT_ENABLED));
81 auditFile = get(properties, AUDIT_LOGGER);
82 auditLog = LoggerFactory.getLogger(auditFile);
83 log.info("Reconfigured; auditEnabled={}; auditFile={}", auditEnabled, auditFile);
84 }
85 }
86
87 @Override
88 public boolean isAuditing() {
89 return auditEnabled;
90 }
91
92 @Override
93 public void logUserAction(String user, String action) {
94 if (auditEnabled) {
95 auditLog.info("user={}; action={}", user, action);
96 }
97 }
98
99}