blob: 0cef9f2df76ed338df53c0ef4969b5dfd72fb053 [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,
Arjun E K53a20082018-12-18 05:13:05 -050044 service = {AuditService.class},
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080045 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
Arjun E K53a20082018-12-18 05:13:05 -050055 /**
56 * Specifies whether or not audit logging is enabled.
57 */
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080058 private boolean auditEnabled = AUDIT_ENABLED_DEFAULT;
59
Arjun E K53a20082018-12-18 05:13:05 -050060 /**
61 * Name of the audit logger.
62 */
63 private String auditLogger = AUDIT_LOGGER_DEFAULT;
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080064
65 @Reference(cardinality = ReferenceCardinality.MANDATORY)
66 protected ComponentConfigService cfgService;
67
68 @Activate
69 protected void activate(ComponentContext ctx) {
70 cfgService.registerProperties(getClass());
71 modified(ctx);
72 log.info("Started");
73 }
74
75 @Deactivate
76 protected void deactivate(ComponentContext ctx) {
77 log.info("Stopped");
78 }
79
80 @Modified
81 protected void modified(ComponentContext ctx) {
82 Dictionary<?, ?> properties = ctx.getProperties();
83 if (properties != null) {
84 auditEnabled = Boolean.parseBoolean(get(properties, AUDIT_ENABLED));
Arjun E K53a20082018-12-18 05:13:05 -050085 auditLogger = get(properties, AUDIT_LOGGER);
86 auditLog = LoggerFactory.getLogger(auditLogger);
87 log.info("Reconfigured; auditEnabled={}; auditLogger={}", auditEnabled, auditLogger);
Thomas Vachuskabd8ddfe2018-12-13 12:58:48 -080088 }
89 }
90
91 @Override
92 public boolean isAuditing() {
93 return auditEnabled;
94 }
95
96 @Override
97 public void logUserAction(String user, String action) {
98 if (auditEnabled) {
99 auditLog.info("user={}; action={}", user, action);
100 }
101 }
102
103}