Security/ Package Name changes
[portal.git] / ecomp-portal-BE-os / src / main / java / org / onap / portalapp / portal / logging / aop / EPEELFLoggerAspect.java
1 /*-
2  * ============LICENSE_START==========================================
3  * ONAP Portal
4  * ===================================================================
5  * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6  * ===================================================================
7  *
8  * Unless otherwise specified, all software contained herein is licensed
9  * under the Apache License, Version 2.0 (the "License");
10  * you may not use this software except in compliance with the License.
11  * You may obtain a copy of the License at
12  *
13  *             http://www.apache.org/licenses/LICENSE-2.0
14  *
15  * Unless required by applicable law or agreed to in writing, software
16  * distributed under the License is distributed on an "AS IS" BASIS,
17  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
18  * See the License for the specific language governing permissions and
19  * limitations under the License.
20  *
21  * Unless otherwise specified, all documentation contained herein is licensed
22  * under the Creative Commons License, Attribution 4.0 Intl. (the "License");
23  * you may not use this documentation except in compliance with the License.
24  * You may obtain a copy of the License at
25  *
26  *             https://creativecommons.org/licenses/by/4.0/
27  *
28  * Unless required by applicable law or agreed to in writing, documentation
29  * distributed under the License is distributed on an "AS IS" BASIS,
30  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
31  * See the License for the specific language governing permissions and
32  * limitations under the License.
33  *
34  * ============LICENSE_END============================================
35  *
36  * ECOMP is a trademark and service mark of AT&T Intellectual Property.
37  */
38 package org.onap.portalapp.portal.logging.aop;
39
40 import org.aspectj.lang.ProceedingJoinPoint;
41 import org.aspectj.lang.annotation.Around;
42 import org.aspectj.lang.annotation.Aspect;
43 import org.aspectj.lang.annotation.Pointcut;
44 import org.onap.portalapp.portal.logging.aop.EPAuditLog;
45 import org.onap.portalapp.portal.logging.aop.EPEELFLoggerAdvice;
46 import org.onap.portalapp.portal.logging.aop.EPMetricsLog;
47 import org.onap.portalapp.portal.transport.FieldsValidator;
48 import org.onap.portalapp.portal.utils.EcompPortalUtils;
49 import org.onap.portalsdk.core.logging.logic.EELFLoggerDelegate;
50 import org.onap.portalsdk.core.util.SystemProperties.SecurityEventTypeEnum;
51 import org.springframework.beans.factory.annotation.Autowired;
52
53 @Aspect
54 @org.springframework.context.annotation.Configuration
55 public class EPEELFLoggerAspect {
56         
57         EELFLoggerDelegate logger = EELFLoggerDelegate.getLogger(EPEELFLoggerAspect.class);
58
59         @Autowired
60         EPEELFLoggerAdvice epAdvice;
61         
62         /*
63          * Point-cut expression to handle all INCOMING_REST_MESSAGES
64          */
65         @Pointcut("execution(public * org.onap.portalapp.portal.controller.*.*(..))")
66         public void incomingAuditMessages() {}
67         
68         /*
69          * Handles all INCOMING_REST_MESSAGES from kpiDashboard
70          */
71         @Pointcut("execution(public * org.onap.portalapp.kpidash.controller.*.*(..))")
72         public void kpiDashboardAuditMessages() {}
73
74         /*
75          * Point-cut expression to handle all session management INCOMING_REST_MESSAGES
76          */
77         @Pointcut("execution(public * org.onap.portalapp.controller.sessionmgt.*.*(..))")
78         public void sessionMgtIncomingAuditMessages() {}
79         
80         /*
81          * Point-cut expression to handle UserProfileController INCOMING_REST_MESSAGES
82          */
83         @Pointcut("execution(public * org.onap.portalapp.controller.core.UserProfileController.*(..))")
84         public void userProfileIncomingAuditMessages() {}
85         
86         /*
87          * Point-cut expression to handle UserProfileController INCOMING_REST_MESSAGES
88          */
89         @Pointcut("execution(public * org.onap.portalapp.controller.ONAPWelcomeController.*(..))")
90         public void welcomeIncomingAuditMessages() {}
91         
92         /*
93          * Point-cut expression to handle INCOMING Logout Rest Messages
94          */
95         @Pointcut("execution(public * org.onap.portalapp.controller.ECOMPLogoutController.*(..))")
96         public void logoutAuditMessages() {}
97
98         
99         /*
100          * Point-cut expression which handles all the OUTGOING_REST_MESSAGES.
101          */
102         @Pointcut("execution(public * org.onap.portalapp.portal.service.ApplicationsRestClientServiceImpl.*(..))")
103         public void outgoingAuditMessages() {}
104         
105         /*
106          * Point-cut expression to handle all the session management OUTGOING_REST_MESSAGES.
107          */
108         @Pointcut("execution(public * org.onap.portalapp.service.sessionmgt.SessionCommunication.*(..))")
109         public void sessionMgtOutgoingAuditMessages() {}
110         
111         /*
112          * Point-cut expression which handles all the LDAP_PHONEBOOK_USER_SEARCH calls.
113          */
114         @Pointcut("execution(public * org.onap.portalapp.portal.service.EPLdapServiceImpl.*(..))")
115         public void phoneBookSearchAuditMessages() {}
116         
117         /*
118          * Handles Audit, Metrics & Debug logging for the point-cut
119          * expression defined at class-level
120          */
121         @Around("(incomingAuditMessages() || kpiDashboardAuditMessages() || sessionMgtIncomingAuditMessages() || "
122                         + "userProfileIncomingAuditMessages() || welcomeIncomingAuditMessages()) && @within(epAuditLog)")
123         public Object incomingAuditMessagesAroundClass(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
124                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
125         }
126         
127         /*
128          * Handles Audit, Metrics & Debug logging for the point-cut
129          * expression defined at class-level
130          */
131         @Around("(outgoingAuditMessages() || sessionMgtOutgoingAuditMessages()) && @within(epAuditLog)")
132         public Object outgoingAuditMessagesAroundClass(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
133                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.OUTGOING_REST_MESSAGE);
134         }
135         
136         
137         /*
138          * Handles Audit, Metrics & Debug logging for the point-cut
139          * expression defined at method-level
140          */
141         @Around("(outgoingAuditMessages() || sessionMgtOutgoingAuditMessages()) && @annotation(epAuditLog)")
142         public Object outgoingAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
143                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.OUTGOING_REST_MESSAGE);
144         }
145         
146         /*
147          * Handles Audit, Metrics & Debug logging for the point-cut
148          * expression defined at method-level
149          */
150         @Around("(incomingAuditMessages() || kpiDashboardAuditMessages() || sessionMgtIncomingAuditMessages() ||"
151                         + "userProfileIncomingAuditMessages() || welcomeIncomingAuditMessages()) && @annotation(epAuditLog)")
152         public Object incomingAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
153                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.INCOMING_REST_MESSAGE);
154         }
155         
156         @Around("@annotation(epAuditLog)")
157         public Object loginAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
158                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.FE_LOGIN_ATTEMPT);
159         }
160         
161         @Around("logoutAuditMessages() && @annotation(epAuditLog)")
162         public Object logoutAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
163                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.FE_LOGOUT);
164         }
165         
166         @Around("phoneBookSearchAuditMessages() && @annotation(epAuditLog)")
167         public Object phonebookSearchAuditMessagesAroundMethod(ProceedingJoinPoint joinPoint, EPAuditLog epAuditLog) throws Throwable {
168                 return this.logAroundMethod(joinPoint, SecurityEventTypeEnum.LDAP_PHONEBOOK_USER_SEARCH);
169         }
170         
171         private Object logAroundMethod(ProceedingJoinPoint joinPoint, SecurityEventTypeEnum securityEventType) throws Throwable {
172                 //Before
173                 Object[] passOnArgs = new Object[] {joinPoint.getSignature().getDeclaringType().getName(), joinPoint.getSignature().getName()};
174                 Object[] returnArgs = epAdvice.before(securityEventType, joinPoint.getArgs(), passOnArgs);
175                 
176                 //Call the actual method
177                 Object result = null;
178                 String statusCode = "COMPLETE";
179                 String responseCode = "200";
180                 try {
181                         result = joinPoint.proceed();
182                 } catch(Exception e) {
183                         logger.error(EELFLoggerDelegate.errorLogger, "logAroundMethod failed", e);
184                         statusCode = "ERROR";
185                         responseCode = "500"; //Internal server error
186                 }
187                 
188                 //Check the result
189                 if (securityEventType!=null) {
190                         if (result==null) {
191                                 statusCode = "ERROR";
192                                 //Check if there is an internal response code
193                                 //and use it if the caller function has configured it.
194                                 responseCode = epAdvice.getInternalResponseCode();
195                                 if (responseCode==null||responseCode=="") {
196                                         responseCode = "500"; //Internal server error
197                                 }
198                         } else if (result instanceof FieldsValidator) {
199                                 FieldsValidator fieldsValidator = (FieldsValidator) result;
200                                 if (fieldsValidator!=null && fieldsValidator.httpStatusCode!=null) {
201                                         responseCode = fieldsValidator.httpStatusCode.toString();
202                                 }
203                         }
204                 }
205                 
206                 //After
207                 epAdvice.after(securityEventType, statusCode, responseCode, joinPoint.getArgs(), returnArgs, passOnArgs);
208                 
209                 return result;
210         }
211         
212         //Metrics Logging
213         @Pointcut("execution(* *(..))")
214     public void performMetricsLogging() {}
215         
216         @Around("performMetricsLogging() && @within(epMetricsLog)")
217         public Object metricsLoggingAroundClass(ProceedingJoinPoint joinPoint, EPMetricsLog epMetricsLog) throws Throwable {
218                 return this.logAroundMethod(joinPoint, null);
219         }
220         
221         @Around("performMetricsLogging() && @annotation(epMetricsLog)")
222         public Object metricsLoggingAroundMethod(ProceedingJoinPoint joinPoint, EPMetricsLog epMetricsLog) throws Throwable {
223                 return this.logAroundMethod(joinPoint, null);
224         }
225 }