summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/aai/sa/auth/SearchDbServiceAuthCore.java
blob: e7541cbafa2f3d83aa5929ece165f575c1d6cea8 (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
/**
 * ============LICENSE_START=======================================================
 * org.onap.aai
 * ================================================================================
 * Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 * Copyright © 2017 Amdocs
 * ================================================================================
 * 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 org.onap.aai.sa.auth;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.JsonNode;
import com.fasterxml.jackson.databind.ObjectMapper;
import org.json.simple.parser.JSONParser;
import org.json.simple.parser.ParseException;
import org.onap.aai.sa.searchdbabstraction.util.SearchDbConstants;
import org.openecomp.cl.api.Logger;
import org.openecomp.cl.eelf.LoggerFactory;

import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileReader;
import java.io.IOException;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.Timer;

public class SearchDbServiceAuthCore {

  private static Logger logger = LoggerFactory.getInstance()
      .getLogger(SearchDbServiceAuthCore.class.getName());

  private static String GlobalAuthFileName = SearchDbConstants.SDB_AUTH_CONFIG_FILENAME;

  private static enum HTTP_METHODS {
    POST, GET, PUT, DELETE
  }

  ;

  // Don't instantiate
  private SearchDbServiceAuthCore() {
  }

  private static boolean usersInitialized = false;
  private static HashMap<String, SearchDbAuthUser> users;
  private static boolean timerSet = false;
  private static Timer timer = null;

  public synchronized static void init() {


    SearchDbServiceAuthCore.getConfigFile();
    SearchDbServiceAuthCore.reloadUsers();

  }

  public static void cleanup() {
    timer.cancel();
  }

  public static String getConfigFile() {
    if (GlobalAuthFileName == null) {
      String nc = SearchDbConstants.SDB_AUTH_CONFIG_FILENAME;
      if (nc == null) {
        nc = "/home/aaiadmin/etc/aaipolicy.json";
      }

      GlobalAuthFileName = nc;
    }
    return GlobalAuthFileName;
  }

  public synchronized static void reloadUsers() {
    users = new HashMap<String, SearchDbAuthUser>();


    ObjectMapper mapper = new ObjectMapper(); // can reuse, share globally
    JSONParser parser = new JSONParser();
    try {
      Object obj = parser.parse(new FileReader(GlobalAuthFileName));
      // aailogger.debug(logline, "Reading from " + GlobalAuthFileName);
      JsonNode rootNode = mapper.readTree(new File(GlobalAuthFileName));
      JsonNode rolesNode = rootNode.path("roles");

      for (JsonNode roleNode : rolesNode) {
        String roleName = roleNode.path("name").asText();

        TabularAuthRole authRole = new TabularAuthRole();
        JsonNode usersNode = roleNode.path("users");
        JsonNode functionsNode = roleNode.path("functions");
        for (JsonNode functionNode : functionsNode) {
          String function = functionNode.path("name").asText();
          JsonNode methodsNode = functionNode.path("methods");
          boolean hasMethods = false;
          for (JsonNode methodNode : methodsNode) {
            String methodName = methodNode.path("name").asText();
            hasMethods = true;
            String thisFunction = methodName + ":" + function;

            authRole.addAllowedFunction(thisFunction);
          }

          if (hasMethods == false) {
            // iterate the list from HTTP_METHODS
            for (HTTP_METHODS meth : HTTP_METHODS.values()) {
              String thisFunction = meth.toString() + ":" + function;

              authRole.addAllowedFunction(thisFunction);
            }
          }

        }
        for (JsonNode userNode : usersNode) {
          // make the user lower case
          String username = userNode.path("username").asText().toLowerCase();
          SearchDbAuthUser authUser = null;
          if (users.containsKey(username)) {
            authUser = users.get(username);
          } else {
            authUser = new SearchDbAuthUser();
          }


          authUser.setUser(username);
          authUser.addRole(roleName, authRole);
          users.put(username, authUser);
        }
      }
    } catch (FileNotFoundException fnfe) {
      logger.debug("Failed to load the policy file ");

    } catch (ParseException e) {
      logger.debug("Failed to Parse the policy file ");

    } catch (JsonProcessingException e) {
      logger.debug("JSON processing error while parsing policy file: " + e.getMessage());

    } catch (IOException e) {
      logger.debug("IO Exception while parsing policy file: " + e.getMessage());
    }

    usersInitialized = true;

  }

  public static class SearchDbAuthUser {
    public SearchDbAuthUser() {
      this.roles = new HashMap<String, TabularAuthRole>();
    }

    private String username;
    private HashMap<String, TabularAuthRole> roles;

    public String getUser() {
      return this.username;
    }

    public HashMap<String, TabularAuthRole> getRoles() {
      return this.roles;
    }

    public void addRole(String roleName, TabularAuthRole authRole) {
      this.roles.put(roleName, authRole);
    }

    public boolean checkAllowed(String checkFunc) {
      for (Map.Entry<String, TabularAuthRole> roleEntry : this.roles.entrySet()) {
        TabularAuthRole role = roleEntry.getValue();
        if (role.hasAllowedFunction(checkFunc)) {
          // break out as soon as we find it
          return true;
        }
      }
      // we would have got positive confirmation had it been there
      return false;
    }

    public void setUser(String myuser) {
      this.username = myuser;
    }

  }

  public static class TabularAuthRole {
    public TabularAuthRole() {
      this.allowedFunctions = new ArrayList<String>();
    }

    private List<String> allowedFunctions;

    public void addAllowedFunction(String func) {
      this.allowedFunctions.add(func);
    }

    public void delAllowedFunction(String delFunc) {
      if (this.allowedFunctions.contains(delFunc)) {
        this.allowedFunctions.remove(delFunc);
      }
    }

    public boolean hasAllowedFunction(String afunc) {
      if (this.allowedFunctions.contains(afunc)) {
        return true;
      } else {
        return false;
      }
    }
  }

  public static HashMap<String, SearchDbAuthUser> getUsers(String key) {
    if (!usersInitialized || (users == null)) {
      reloadUsers();
    }
    return users;
  }

  public static boolean authorize(String username, String authFunction) {
    // logline.init(component, transId, fromAppId, "authorize()");

    if (!usersInitialized || (users == null)) {
      init();
    }
    if (users.containsKey(username)) {
      if (users.get(username).checkAllowed(authFunction) == true) {

        return true;
      } else {


        return false;
      }
    } else {

      return false;
    }
  }
}