Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 1 | /* |
| 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 | |
| 17 | package org.onosproject.audit.impl; |
| 18 | |
| 19 | import org.onosproject.cfg.ComponentConfigService; |
| 20 | import org.onosproject.security.AuditService; |
| 21 | import org.osgi.service.component.ComponentContext; |
| 22 | import org.osgi.service.component.annotations.Component; |
| 23 | import org.osgi.service.component.annotations.Deactivate; |
| 24 | import org.osgi.service.component.annotations.Modified; |
| 25 | import org.osgi.service.component.annotations.Activate; |
| 26 | import org.osgi.service.component.annotations.Reference; |
| 27 | import org.osgi.service.component.annotations.ReferenceCardinality; |
| 28 | import org.slf4j.Logger; |
| 29 | import org.slf4j.LoggerFactory; |
| 30 | |
| 31 | import java.util.Dictionary; |
| 32 | |
| 33 | import static org.onlab.util.Tools.get; |
| 34 | import static org.onosproject.net.OsgiPropertyConstants.AUDIT_LOGGER; |
| 35 | import static org.onosproject.net.OsgiPropertyConstants.AUDIT_LOGGER_DEFAULT; |
| 36 | import static org.onosproject.net.OsgiPropertyConstants.AUDIT_ENABLED; |
| 37 | import static org.onosproject.net.OsgiPropertyConstants.AUDIT_ENABLED_DEFAULT; |
| 38 | |
| 39 | /** |
| 40 | * Component to manage audit logging. |
| 41 | */ |
| 42 | @Component( |
| 43 | immediate = true, |
Arjun E K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 44 | service = {AuditService.class}, |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 45 | property = { |
| 46 | AUDIT_ENABLED + ":Boolean=" + AUDIT_ENABLED_DEFAULT, |
| 47 | AUDIT_LOGGER + "=" + AUDIT_LOGGER_DEFAULT, |
| 48 | }) |
| 49 | public class AuditManager implements AuditService { |
| 50 | |
| 51 | private final Logger log = LoggerFactory.getLogger(getClass()); |
| 52 | |
| 53 | private Logger auditLog = log; |
| 54 | |
Arjun E K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 55 | /** |
| 56 | * Specifies whether or not audit logging is enabled. |
| 57 | */ |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 58 | private boolean auditEnabled = AUDIT_ENABLED_DEFAULT; |
| 59 | |
Arjun E K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 60 | /** |
| 61 | * Name of the audit logger. |
| 62 | */ |
| 63 | private String auditLogger = AUDIT_LOGGER_DEFAULT; |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 64 | |
| 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 K | 53a2008 | 2018-12-18 05:13:05 -0500 | [diff] [blame] | 85 | auditLogger = get(properties, AUDIT_LOGGER); |
| 86 | auditLog = LoggerFactory.getLogger(auditLogger); |
| 87 | log.info("Reconfigured; auditEnabled={}; auditLogger={}", auditEnabled, auditLogger); |
Thomas Vachuska | bd8ddfe | 2018-12-13 12:58:48 -0800 | [diff] [blame] | 88 | } |
| 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 | } |