2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright © 2017-2018 AT&T Intellectual Property. All rights reserved.
6 * Copyright © 2017-2018 Amdocs
7 * ================================================================================
8 * Licensed under the Apache License, Version 2.0 (the "License");
9 * you may not use this file except in compliance with the License.
10 * You may obtain a copy of the License at
12 * http://www.apache.org/licenses/LICENSE-2.0
14 * Unless required by applicable law or agreed to in writing, software
15 * distributed under the License is distributed on an "AS IS" BASIS,
16 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
17 * See the License for the specific language governing permissions and
18 * limitations under the License.
19 * ============LICENSE_END=========================================================
21 package org.onap.aai.sa.auth;
23 import com.fasterxml.jackson.core.JsonProcessingException;
24 import com.fasterxml.jackson.databind.JsonNode;
25 import com.fasterxml.jackson.databind.ObjectMapper;
26 import org.json.simple.parser.JSONParser;
27 import org.json.simple.parser.ParseException;
28 import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
29 import org.onap.aai.cl.api.Logger;
30 import org.onap.aai.cl.eelf.LoggerFactory;
33 import java.io.FileNotFoundException;
34 import java.io.FileReader;
35 import java.io.IOException;
36 import java.util.ArrayList;
37 import java.util.HashMap;
38 import java.util.List;
40 import java.util.Timer;
42 public class SearchDbServiceAuthCore {
44 private static Logger logger = LoggerFactory.getInstance()
45 .getLogger(SearchDbServiceAuthCore.class.getName());
47 private static String GlobalAuthFileName = SearchDbConstants.SDB_AUTH_CONFIG_FILENAME;
49 private static enum HTTP_METHODS {
50 POST, GET, PUT, DELETE
56 private SearchDbServiceAuthCore() {
59 private static boolean usersInitialized = false;
60 private static HashMap<String, SearchDbAuthUser> users;
61 private static boolean timerSet = false;
62 private static Timer timer = null;
64 public synchronized static void init() {
67 SearchDbServiceAuthCore.getConfigFile();
68 SearchDbServiceAuthCore.reloadUsers();
72 public static void cleanup() {
76 public static String getConfigFile() {
77 if (GlobalAuthFileName == null) {
78 String nc = SearchDbConstants.SDB_AUTH_CONFIG_FILENAME;
80 nc = "/home/aaiadmin/etc/aaipolicy.json";
83 GlobalAuthFileName = nc;
85 return GlobalAuthFileName;
88 public synchronized static void reloadUsers() {
89 users = new HashMap<String, SearchDbAuthUser>();
92 ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
93 JSONParser parser = new JSONParser();
95 Object obj = parser.parse(new FileReader(GlobalAuthFileName));
96 // aailogger.debug(logline, "Reading from " + GlobalAuthFileName);
97 JsonNode rootNode = mapper.readTree(new File(GlobalAuthFileName));
98 JsonNode rolesNode = rootNode.path("roles");
100 for (JsonNode roleNode : rolesNode) {
101 String roleName = roleNode.path("name").asText();
103 TabularAuthRole authRole = new TabularAuthRole();
104 JsonNode usersNode = roleNode.path("users");
105 JsonNode functionsNode = roleNode.path("functions");
106 for (JsonNode functionNode : functionsNode) {
107 String function = functionNode.path("name").asText();
108 JsonNode methodsNode = functionNode.path("methods");
109 boolean hasMethods = false;
110 for (JsonNode methodNode : methodsNode) {
111 String methodName = methodNode.path("name").asText();
113 String thisFunction = methodName + ":" + function;
115 authRole.addAllowedFunction(thisFunction);
118 if (hasMethods == false) {
119 // iterate the list from HTTP_METHODS
120 for (HTTP_METHODS meth : HTTP_METHODS.values()) {
121 String thisFunction = meth.toString() + ":" + function;
123 authRole.addAllowedFunction(thisFunction);
128 for (JsonNode userNode : usersNode) {
129 // make the user lower case
130 String username = userNode.path("username").asText().toLowerCase();
131 SearchDbAuthUser authUser = null;
132 if (users.containsKey(username)) {
133 authUser = users.get(username);
135 authUser = new SearchDbAuthUser();
139 authUser.setUser(username);
140 authUser.addRole(roleName, authRole);
141 users.put(username, authUser);
144 } catch (FileNotFoundException fnfe) {
145 logger.debug("Failed to load the policy file ");
147 } catch (ParseException e) {
148 logger.debug("Failed to Parse the policy file ");
150 } catch (JsonProcessingException e) {
151 logger.debug("JSON processing error while parsing policy file: " + e.getMessage());
153 } catch (IOException e) {
154 logger.debug("IO Exception while parsing policy file: " + e.getMessage());
157 usersInitialized = true;
161 public static class SearchDbAuthUser {
162 public SearchDbAuthUser() {
163 this.roles = new HashMap<String, TabularAuthRole>();
166 private String username;
167 private HashMap<String, TabularAuthRole> roles;
169 public String getUser() {
170 return this.username;
173 public HashMap<String, TabularAuthRole> getRoles() {
177 public void addRole(String roleName, TabularAuthRole authRole) {
178 this.roles.put(roleName, authRole);
181 public boolean checkAllowed(String checkFunc) {
182 for (Map.Entry<String, TabularAuthRole> roleEntry : this.roles.entrySet()) {
183 TabularAuthRole role = roleEntry.getValue();
184 if (role.hasAllowedFunction(checkFunc)) {
185 // break out as soon as we find it
189 // we would have got positive confirmation had it been there
193 public void setUser(String myuser) {
194 this.username = myuser;
199 public static class TabularAuthRole {
200 public TabularAuthRole() {
201 this.allowedFunctions = new ArrayList<String>();
204 private List<String> allowedFunctions;
206 public void addAllowedFunction(String func) {
207 this.allowedFunctions.add(func);
210 public void delAllowedFunction(String delFunc) {
211 if (this.allowedFunctions.contains(delFunc)) {
212 this.allowedFunctions.remove(delFunc);
216 public boolean hasAllowedFunction(String afunc) {
217 if (this.allowedFunctions.contains(afunc)) {
225 public static HashMap<String, SearchDbAuthUser> getUsers(String key) {
226 if (!usersInitialized || (users == null)) {
232 public static boolean authorize(String username, String authFunction) {
233 // logline.init(component, transId, fromAppId, "authorize()");
235 if (!usersInitialized || (users == null)) {
238 if (users.containsKey(username)) {
239 if (users.get(username).checkAllowed(authFunction) == true) {