Aharoni, Pavel (pa0916) | ca3cb01 | 2018-10-22 15:29:57 +0300 | [diff] [blame] | 1 | /*-
|
| 2 | * ============LICENSE_START=======================================================
|
| 3 | * OSAM
|
| 4 | * ================================================================================
|
| 5 | * Copyright (C) 2018 AT&T
|
| 6 | * ================================================================================
|
| 7 | * Licensed under the Apache License, Version 2.0 (the "License");
|
| 8 | * you may not use this file except in compliance with the License.
|
| 9 | * You may obtain a copy of the License at
|
| 10 | *
|
| 11 | * http://www.apache.org/licenses/LICENSE-2.0
|
| 12 | *
|
| 13 | * Unless required by applicable law or agreed to in writing, software
|
| 14 | * distributed under the License is distributed on an "AS IS" BASIS,
|
| 15 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
| 16 | * See the License for the specific language governing permissions and
|
| 17 | * limitations under the License.
|
| 18 | * ============LICENSE_END=========================================================
|
| 19 | */
|
| 20 |
|
| 21 |
|
| 22 |
|
| 23 | package org.onap.osam.controllers;
|
| 24 |
|
| 25 | import ch.qos.logback.classic.LoggerContext;
|
| 26 | import ch.qos.logback.core.Appender;
|
| 27 | import ch.qos.logback.core.FileAppender;
|
| 28 | import ch.qos.logback.core.spi.AppenderAttachable;
|
| 29 | import org.apache.commons.io.input.ReversedLinesFileReader;
|
| 30 | import org.apache.commons.lang3.StringUtils;
|
| 31 | import org.onap.portalsdk.core.controller.RestrictedBaseController;
|
| 32 | import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
|
| 33 | import org.onap.osam.model.ExceptionResponse;
|
| 34 | import org.onap.osam.roles.Role;
|
| 35 | import org.onap.osam.roles.RoleProvider;
|
| 36 | import org.onap.osam.utils.Streams;
|
| 37 | import org.slf4j.LoggerFactory;
|
| 38 | import org.springframework.beans.factory.annotation.Autowired;
|
| 39 | import org.springframework.http.HttpStatus;
|
| 40 | import org.springframework.web.bind.annotation.*;
|
| 41 |
|
| 42 | import javax.servlet.http.HttpServletRequest;
|
| 43 | import javax.ws.rs.InternalServerErrorException;
|
| 44 | import javax.ws.rs.NotAuthorizedException;
|
| 45 | import java.io.File;
|
| 46 | import java.io.IOException;
|
| 47 | import java.util.List;
|
| 48 | import java.util.Objects;
|
| 49 | import java.util.function.Supplier;
|
| 50 | import java.util.stream.Collectors;
|
| 51 | import java.util.stream.Stream;
|
| 52 |
|
| 53 | import static com.att.eelf.configuration.Configuration.GENERAL_LOGGER_NAME;
|
| 54 |
|
| 55 |
|
| 56 | @RestController
|
| 57 | @RequestMapping("logger")
|
| 58 | public class LoggerController extends RestrictedBaseController {
|
| 59 |
|
| 60 | private static final EELFLoggerDelegate LOGGER = EELFLoggerDelegate.getLogger(LoggerController.class);
|
| 61 |
|
| 62 | @Autowired
|
| 63 | RoleProvider roleProvider;
|
| 64 |
|
| 65 | @RequestMapping(value = "/{loggerName:audit|error|metrics}", method = RequestMethod.GET)
|
| 66 | public String getLog(@PathVariable String loggerName, HttpServletRequest request,
|
| 67 | @RequestParam(value="limit", defaultValue = "5000") Integer limit) throws IOException {
|
| 68 |
|
| 69 | List<Role> roles = roleProvider.getUserRoles(request);
|
| 70 | boolean userPermitted = roleProvider.userPermissionIsReadLogs(roles);
|
| 71 | if (!userPermitted) {
|
| 72 | throw new NotAuthorizedException("User not authorized to get logs");
|
| 73 | }
|
| 74 |
|
| 75 | String logfilePath = getLogfilePath(loggerName);
|
| 76 |
|
| 77 | try (final ReversedLinesFileReader reader = new ReversedLinesFileReader(new File(logfilePath))) {
|
| 78 | Supplier<String> reverseLinesSupplier = () -> {
|
| 79 | try {
|
| 80 | return reader.readLine();
|
| 81 | } catch (NullPointerException e) {
|
| 82 | // EOF Reached
|
| 83 | return null;
|
| 84 | } catch (IOException e) {
|
| 85 | throw new InternalServerErrorException("error while reading " + logfilePath, e);
|
| 86 | }
|
| 87 | };
|
| 88 |
|
| 89 | return Streams.takeWhile(
|
| 90 | Stream.generate(reverseLinesSupplier),
|
| 91 | line -> !StringUtils.contains(line, "Logging is started"))
|
| 92 | .limit(limit)
|
| 93 | .limit(5_000)
|
| 94 | .filter(Objects::nonNull)
|
| 95 | .collect(Collectors.joining("\n"));
|
| 96 | }
|
| 97 | }
|
| 98 |
|
| 99 | private String getLogfilePath(String loggerName) {
|
| 100 | LoggerContext context = (LoggerContext) LoggerFactory.getILoggerFactory();
|
| 101 | return context.getLoggerList().stream()
|
| 102 | .filter(logger -> logger.getName().equals(GENERAL_LOGGER_NAME + "." + loggerName))
|
| 103 | .flatMap(this::pullSubAppenders)
|
| 104 | .flatMap(appender -> {
|
| 105 | // Appender might be "attachable", if so - roll-up its sub-appenders
|
| 106 | return (appender instanceof AppenderAttachable) ?
|
| 107 | pullSubAppenders((AppenderAttachable<?>) appender) : Stream.of(appender);
|
| 108 | })
|
| 109 | .filter(appender -> appender instanceof FileAppender)
|
| 110 | .map(appender -> (FileAppender<?>) appender)
|
| 111 | .map(FileAppender::getFile)
|
| 112 | .findFirst()
|
| 113 | .orElseThrow(() -> new InternalServerErrorException("logfile for " + loggerName + " not found"));
|
| 114 | }
|
| 115 |
|
| 116 | private <T> Stream<Appender<T>> pullSubAppenders(AppenderAttachable<T> logger) {
|
| 117 | return Streams.fromIterator(logger.iteratorForAppenders());
|
| 118 | }
|
| 119 |
|
| 120 | @ExceptionHandler({ NotAuthorizedException.class })
|
| 121 | @ResponseStatus(HttpStatus.UNAUTHORIZED)
|
| 122 | public String notAuthorizedHandler(NotAuthorizedException e) {
|
| 123 | return "UNAUTHORIZED";
|
| 124 | }
|
| 125 |
|
| 126 | @ExceptionHandler({ IOException.class, InternalServerErrorException.class })
|
| 127 | @ResponseStatus(HttpStatus.INTERNAL_SERVER_ERROR)
|
| 128 | public ExceptionResponse ioExceptionHandler(Exception e) {
|
| 129 | return org.onap.osam.controllers.ControllersUtils.handleException(e, LOGGER);
|
| 130 | }
|
| 131 |
|
| 132 | }
|