2 * ============LICENSE_START====================================================
4 * ===========================================================================
5 * Copyright (c) 2018 AT&T Intellectual Property. All rights reserved.
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
11 * http://www.apache.org/licenses/LICENSE-2.0
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====================================================
22 package org.onap.aaf.cadi.aaf;
24 import org.onap.aaf.misc.env.util.Split;
27 public class PermEval {
28 public static final char START_REGEX_CHAR = '!';
29 public static final char START_INST_KEY_CHAR=':';
30 public static final char ALT_START_INST_KEY_CHAR='/';
32 public static final char LIST_SEP = ',';
33 public static final String ASTERIX = "*";
38 * Instance can be more complex. It can be a string, a Regular Expression, or a ":" separated Key
39 * who's parts can also be a String, Regular Expression.
41 * sInst = Server's Instance
42 * In order to prevent false matches, keys must be the same length to count as equal
43 * Changing this will break existing users, like Cassandra. Jonathan 9-4-2015
45 public static boolean evalInstance(String sInst, String pInst) {
46 if (sInst == null || pInst == null) {
49 if (sInst.equals("") || pInst.equals("")) {
52 if (ASTERIX.equals(sInst)) {
53 return true; // If Server's String is "*", then it accepts every Instance
55 char firstChar = pInst.charAt(0);
56 char startChar = firstChar==ALT_START_INST_KEY_CHAR?ALT_START_INST_KEY_CHAR:START_INST_KEY_CHAR;
57 switch(pInst.charAt(0)) { // First char
58 case START_REGEX_CHAR: // Evaluate as Regular Expression
59 String pItem = pInst.substring(1);
60 String first = Split.split(LIST_SEP,sInst)[0]; // allow for "," definition in Action
61 return first.matches(pItem);
63 case START_INST_KEY_CHAR: // Evaluate a special Key field, i.e.:xyz:*:!df.*
64 case ALT_START_INST_KEY_CHAR: // Also allow '/' as special Key Field, i.e. /xyz/*/!.*
65 if (sInst.charAt(0)==startChar) { // To compare key-to-key, both strings must be keys
66 String[] skeys=Split.split(startChar,sInst);
67 String[] pkeys=Split.split(startChar,pInst);
68 if (pkeys.length<skeys.length) {
70 } else if(pkeys.length > skeys.length &&
71 (skeys.length==0 || !ASTERIX.equals(skeys[skeys.length-1]))) {
76 for (int i=1;pass && i<skeys.length;++i) { // We start at 1, because the first one, being ":" is always ""
77 if (ASTERIX.equals(skeys[i])) {
78 if(i==skeys.length-1) {
82 continue; // Server data accepts all for this key spot
85 for (String sItem : Split.split(LIST_SEP,skeys[i])) { // allow for "," definition in Action
86 if (pkeys[i].length()==0) {
87 if (pass=sItem.length()==0) {
88 break; // Both Empty, keep checking
90 } else if (sItem.length()>0 && sItem.charAt(0)==START_REGEX_CHAR) { // Check Server side when wildcarding like *
91 if (pass=pkeys[i].matches(sItem.substring(1))) {
92 break; // Matches, keep checking
94 } else if (skeys[i].endsWith(ASTERIX)) {
95 if (pass=endAsterixCompare(skeys[i],pkeys[i])) {
98 } else if (pass=sItem.equals(pkeys[i])) {
99 break; // Equal, keep checking
103 return pass; // return whether passed all key checks
105 return false; // if first chars aren't the same, further String compare not necessary
106 default: // Evaluate as String Compare
107 for (String sItem : Split.split(LIST_SEP,sInst)) { // allow for "," separator //TODO is this only for actions?
108 if ((sItem.endsWith(ASTERIX)) && (endAsterixCompare(sInst, pInst))) {
110 } else if (sItem.equals(pInst)) {
118 private static boolean endAsterixCompare(String sInst, String pInst) {
119 final int len = sInst.length()-1;
120 if (pInst.length()<len) {
123 for (int j=0;j<len;++j) {
124 if (pInst.charAt(j)!=sInst.charAt(j)) {
134 * sAction = Stored Action...
135 * pAction = Present Action... the Permission to validate against.
136 * Action is not quite as complex. But we write it in this function so it can be consistent
138 public static boolean evalAction(String sAction,String pAction) {
139 if (ASTERIX.equals(sAction)) return true; // If Server's String is "*", then it accepts every Action
140 if (pAction.equals("")) return false;
141 for (String sItem : Split.split(LIST_SEP,sAction)) { // allow for "," definition in Action
142 if (pAction.charAt(0)==START_REGEX_CHAR? // First char
143 sItem.matches(pAction.substring(1)): // Evaluate as Regular Expression
144 sItem.equals(pAction)) // Evaluate as String Compare