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, |
| 44 | service = { AuditService.class }, |
| 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 | |
| 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 | } |