summaryrefslogtreecommitdiffstats
path: root/authz-service/src/main/java/com/att/authz/service/api/API_Mgmt.java
blob: 6df0845449535bd0d09d206edfed335953a390ee (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
/*******************************************************************************
 * ============LICENSE_START====================================================
 * * org.onap.aaf
 * * ===========================================================================
 * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * * ===========================================================================
 * * Licensed under the Apache License, Version 2.0 (the "License");
 * * you may not use this file except in compliance with the License.
 * * You may obtain a copy of the License at
 * * 
 *  *      http://www.apache.org/licenses/LICENSE-2.0
 * * 
 *  * Unless required by applicable law or agreed to in writing, software
 * * distributed under the License is distributed on an "AS IS" BASIS,
 * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * * See the License for the specific language governing permissions and
 * * limitations under the License.
 * * ============LICENSE_END====================================================
 * *
 * * ECOMP is a trademark and service mark of AT&T Intellectual Property.
 * *
 ******************************************************************************/
package com.att.authz.service.api;

import static com.att.authz.layer.Result.OK;
import static com.att.cssa.rserv.HttpMethods.DELETE;
import static com.att.cssa.rserv.HttpMethods.POST;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.att.aft.dme2.internal.jetty.http.HttpStatus;
import com.att.authz.common.Define;
import com.att.authz.env.AuthzTrans;
import com.att.authz.facade.AuthzFacade;
import com.att.authz.layer.Result;
import com.att.authz.service.AuthAPI;
import com.att.authz.service.Code;
import com.att.authz.service.mapper.Mapper.API;
import com.att.cadi.taf.dos.DenialOfServiceTaf;
import com.att.dao.aaf.cass.Status;
import com.att.dao.aaf.hl.Question;
import com.att.dao.session.SessionFilter;
import com.att.inno.env.Trans;

/**
 * User Role APIs
 *
 */
public class API_Mgmt {

	private static final String SUCCESS = "SUCCESS";

	/**
	 * Normal Init level APIs
	 * 
	 * @param authzAPI
	 * @param facade
	 * @throws Exception
	 */
	public static void init(final AuthAPI authzAPI, AuthzFacade facade) throws Exception {

		/**
		 * Clear Cache Segment
		 */
		authzAPI.route(DELETE,"/mgmt/cache/:area/:segments",API.VOID,new Code(facade,"Clear Cache by Segment", true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				Result<Void> r = context.cacheClear(trans, pathParam(req,"area"), pathParam(req,"segments"));
				switch(r.status) {
					case OK:
						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.OK_200); 
						break;
					default:
						context.error(trans,resp,r);
				}
			}
		});
		
		/**
		 * Clear Cache
		 */
		authzAPI.route(DELETE,"/mgmt/cache/:area",API.VOID,new Code(facade,"Clear Cache", true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				Result<Void> r;
				String area;
				r = context.cacheClear(trans, area=pathParam(req,"area"));
				switch(r.status) {
					case OK:
						trans.audit().log("Cache " + area + " has been cleared by "+trans.user());
						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.OK_200); 
						break;
					default:
						context.error(trans,resp,r);
				}
			}
		});

		/**
		 * Clear DB Sessions
		 */
		authzAPI.route(DELETE,"/mgmt/dbsession",API.VOID,new Code(facade,"Clear DBSessions", true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				try {
					if(req.isUserInRole(Define.ROOT_NS+".db|pool|clear")) {
						SessionFilter.clear();
						context.dbReset(trans);

						trans.audit().log("DB Sessions have been cleared by "+trans.user());

						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.OK_200);
						return;
					}
					context.error(trans,resp,Result.err(Result.ERR_Denied,"%s is not allowed to clear dbsessions",trans.user()));
				} catch(Exception e) {
					trans.error().log(e, "clearing dbsession");
					context.error(trans,resp,Result.err(e));
				}
			}
		});

		/**
		 * Deny an IP 
		 */
		authzAPI.route(POST, "/mgmt/deny/ip/:ip", API.VOID, new Code(facade,"Deny IP",true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				String ip = pathParam(req,":ip");
				if(req.isUserInRole(Define.ROOT_NS+".deny|"+Define.ROOT_COMPANY+"|ip")) {
					if(DenialOfServiceTaf.denyIP(ip)) {
						trans.audit().log(ip+" has been set to deny by "+trans.user());
						trans.checkpoint(SUCCESS,Trans.ALWAYS);

						resp.setStatus(HttpStatus.CREATED_201);
					} else {
						context.error(trans,resp,Result.err(Status.ERR_ConflictAlreadyExists, 
								ip + " is already being denied"));
					}
				} else {
					trans.audit().log(trans.user(),"has attempted to deny",ip,"without authorization");
					context.error(trans,resp,Result.err(Status.ERR_Denied, 
						trans.getUserPrincipal().getName() + " is not allowed to set IP Denial"));
				}
			}
		});
		
		/**
		 * Stop Denying an IP
		 */
		authzAPI.route(DELETE, "/mgmt/deny/ip/:ip", API.VOID, new Code(facade,"Stop Denying IP",true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				String ip = pathParam(req,":ip");
				if(req.isUserInRole(Define.ROOT_NS+".deny|"+Define.ROOT_COMPANY+"|ip")) {
					if(DenialOfServiceTaf.removeDenyIP(ip)) {
						trans.audit().log(ip+" has been removed from denial by "+trans.user());
						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.OK_200);
					} else {
						context.error(trans,resp,Result.err(Status.ERR_NotFound, 
								ip + " is not on the denial list"));
					}
				} else {
					trans.audit().log(trans.user(),"has attempted to remove",ip," from being denied without authorization");
					context.error(trans,resp,Result.err(Status.ERR_Denied, 
						trans.getUserPrincipal().getName() + " is not allowed to remove IP Denial"));
				}
			}
		});

		/**
		 * Deny an ID 
		 */
		authzAPI.route(POST, "/mgmt/deny/id/:id", API.VOID, new Code(facade,"Deny ID",true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				String id = pathParam(req,":id");
				if(req.isUserInRole(Define.ROOT_NS+".deny|"+Define.ROOT_COMPANY+"|id")) {
					if(DenialOfServiceTaf.denyID(id)) {
						trans.audit().log(id+" has been set to deny by "+trans.user());
						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.CREATED_201);
					} else {
						context.error(trans,resp,Result.err(Status.ERR_ConflictAlreadyExists, 
								id + " is already being denied"));
					}
				} else {
					trans.audit().log(trans.user(),"has attempted to deny",id,"without authorization");
					context.error(trans,resp,Result.err(Status.ERR_Denied, 
						trans.getUserPrincipal().getName() + " is not allowed to set ID Denial"));
				}
			}
		});
		
		/**
		 * Stop Denying an ID
		 */
		authzAPI.route(DELETE, "/mgmt/deny/id/:id", API.VOID, new Code(facade,"Stop Denying ID",true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				String id = pathParam(req,":id");
				if(req.isUserInRole(Define.ROOT_NS+".deny|"+Define.ROOT_COMPANY+"|id")) {
					if(DenialOfServiceTaf.removeDenyID(id)) {
						trans.audit().log(id+" has been removed from denial by " + trans.user());
						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.OK_200);
					} else {
						context.error(trans,resp,Result.err(Status.ERR_NotFound, 
								id + " is not on the denial list"));
					}
				} else {
					trans.audit().log(trans.user(),"has attempted to remove",id," from being denied without authorization");
					context.error(trans,resp,Result.err(Status.ERR_Denied, 
						trans.getUserPrincipal().getName() + " is not allowed to remove ID Denial"));
				}
			}
		});

		/**
		 * Deny an ID 
		 */
		authzAPI.route(POST, "/mgmt/log/id/:id", API.VOID, new Code(facade,"Special Log ID",true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				String id = pathParam(req,":id");
				if(req.isUserInRole(Define.ROOT_NS+".log|"+Define.ROOT_COMPANY+"|id")) {
					if(Question.specialLogOn(trans,id)) {
						trans.audit().log(id+" has been set to special Log by "+trans.user());
						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.CREATED_201);
					} else {
						context.error(trans,resp,Result.err(Status.ERR_ConflictAlreadyExists, 
								id + " is already being special Logged"));
					}
				} else {
					trans.audit().log(trans.user(),"has attempted to special Log",id,"without authorization");
					context.error(trans,resp,Result.err(Status.ERR_Denied, 
						trans.getUserPrincipal().getName() + " is not allowed to set ID special Logging"));
				}
			}
		});
		
		/**
		 * Stop Denying an ID
		 */
		authzAPI.route(DELETE, "/mgmt/log/id/:id", API.VOID, new Code(facade,"Stop Special Log ID",true) {
			@Override
			public void handle(AuthzTrans trans, HttpServletRequest req, HttpServletResponse resp) throws Exception {
				String id = pathParam(req,":id");
				if(req.isUserInRole(Define.ROOT_NS+".log|"+Define.ROOT_COMPANY+"|id")) {
					if(Question.specialLogOff(trans,id)) {
						trans.audit().log(id+" has been removed from special Logging by " + trans.user());
						trans.checkpoint(SUCCESS,Trans.ALWAYS);
						resp.setStatus(HttpStatus.OK_200);
					} else {
						context.error(trans,resp,Result.err(Status.ERR_NotFound, 
								id + " is not on the special Logging list"));
					}
				} else {
					trans.audit().log(trans.user(),"has attempted to remove",id," from being special Logged without authorization");
					context.error(trans,resp,Result.err(Status.ERR_Denied, 
						trans.getUserPrincipal().getName() + " is not allowed to remove ID special Logging"));
				}
			}
		});


	}
}