[AAF-21] Updated Copyright Headers for AAF
[aaf/cadi.git] / aaf / src / src / main / java / com / att / cadi / aaf / PermEval.java
1 /*******************************************************************************\r
2  * ============LICENSE_START====================================================\r
3  * * org.onap.aaf\r
4  * * ===========================================================================\r
5  * * Copyright © 2017 AT&T Intellectual Property. All rights reserved.\r
6  * * ===========================================================================\r
7  * * Licensed under the Apache License, Version 2.0 (the "License");\r
8  * * you may not use this file except in compliance with the License.\r
9  * * You may obtain a copy of the License at\r
10  * * \r
11  *  *      http://www.apache.org/licenses/LICENSE-2.0\r
12  * * \r
13  *  * Unless required by applicable law or agreed to in writing, software\r
14  * * distributed under the License is distributed on an "AS IS" BASIS,\r
15  * * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.\r
16  * * See the License for the specific language governing permissions and\r
17  * * limitations under the License.\r
18  * * ============LICENSE_END====================================================\r
19  * *\r
20  * * ECOMP is a trademark and service mark of AT&T Intellectual Property.\r
21  * *\r
22  ******************************************************************************/\r
23 package com.att.cadi.aaf;\r
24 \r
25 import com.att.inno.env.util.Split;\r
26 \r
27 \r
28 public class PermEval {\r
29         public static final char START_REGEX_CHAR = '!';\r
30         public static final char START_INST_KEY_CHAR=':';\r
31         public static final char ALT_START_INST_KEY_CHAR='/';\r
32         \r
33         public static final char LIST_SEP = ',';\r
34         public static final String INST_KEY_REGEX = new StringBuilder().append(START_INST_KEY_CHAR).toString();\r
35         public static final String ASTERIX = "*";\r
36         \r
37         /**\r
38          * Evaluate Instance\r
39          * \r
40          * Instance can be more complex.  It can be a string, a Regular Expression, or a ":" separated Key \r
41          * who's parts can also be a String, Regular Expression.\r
42          * \r
43          * sInst = Server's Instance\r
44          * In order to prevent false matches, keys must be the same length to count as equal\r
45          * Changing this will break existing users, like Cassandra.  9-4-2015\r
46          */\r
47          public static boolean evalInstance(String sInst, String pInst) {\r
48                   if(ASTERIX.equals(sInst)) return true;                        // If Server's String is "*", then it accepts every Instance\r
49                   char firstChar = pInst.charAt(0);\r
50                   char startChar = firstChar==ALT_START_INST_KEY_CHAR?ALT_START_INST_KEY_CHAR:START_INST_KEY_CHAR;\r
51                   switch(pInst.charAt(0)) {                                             // First char\r
52                         case START_REGEX_CHAR:                                                  // Evaluate as Regular Expression\r
53                                 String pItem = pInst.substring(1);\r
54                             for(String sItem : Split.split(LIST_SEP,sInst)) {           // allow for "," definition in Action\r
55                                 return sItem.matches(pItem);\r
56                             }\r
57                          \r
58                         case START_INST_KEY_CHAR:                                               // Evaluate a special Key field, i.e.:xyz:*:!df.*\r
59                         case ALT_START_INST_KEY_CHAR:                                   // Also allow '/' as special Key Field, i.e. /xyz/*/!.*\r
60                                 if(sInst.charAt(0)==startChar) {  // To compare key-to-key, both strings must be keys\r
61                                         String[] skeys=Split.split(startChar,sInst);\r
62                                         String[] pkeys=Split.split(startChar,pInst);\r
63                                         if(skeys.length!=pkeys.length) return false;\r
64                                         \r
65                                         boolean pass = true;\r
66                                         for(int i=1;pass && i<skeys.length;++i) {                               // We start at 1, because the first one, being ":" is always ""\r
67                                                 if(ASTERIX.equals(skeys[i]))continue;                           // Server data accepts all for this key spot\r
68                                                 pass = false;\r
69                                             for(String sItem : Split.split(LIST_SEP,skeys[i])) {                // allow for "," definition in Action\r
70                                                         if(pkeys[i].length()==0) {\r
71                                                                 if(pass=sItem.length()==0) {\r
72                                                                         break;                                                                  // Both Empty, keep checking\r
73                                                                 }\r
74 //                                                      } else if(pkeys[i].charAt(0)==START_REGEX_CHAR) { \r
75 //                                                              if(pass=sItem.matches(pkeys[i].substring(1))) {\r
76 //                                                                      break;                                                                  // Matches, keep checking\r
77 //                                                              }\r
78                                                         } else if(sItem.charAt(0)==START_REGEX_CHAR) { // Check Server side when wildcarding like *\r
79                                                                 if(pass=pkeys[i].matches(sItem.substring(1))) {\r
80                                                                         break;                                                                  // Matches, keep checking\r
81                                                                 }\r
82                                                         } else if(skeys[i].endsWith(ASTERIX)) {\r
83                                                                 if(pass=endAsterixCompare(skeys[i],pkeys[i])) {\r
84                                                                         break;\r
85                                                                 }\r
86                                                         } else {\r
87                                                                 if(pass=sItem.equals(pkeys[i]))\r
88                                                                         break;                                                                  // Equal, keep checking\r
89                                                         }\r
90                                             }\r
91                                         }\r
92                                         return pass;                                                                                    // return whether passed all key checks\r
93                                 }\r
94                                 return false;                                                           // if first chars aren't the same, further String compare not necessary\r
95                         default:                                                                                // Evaluate as String Compare\r
96                             for(String sItem : Split.split(LIST_SEP,sInst)) {   // allow for "," separator //TODO is this only for actions?\r
97                                 if(sItem.endsWith(ASTERIX)) {\r
98                                         if(endAsterixCompare(sInst, pInst));\r
99                                 } else if(sItem.equals(pInst)) {\r
100                                         return true;\r
101                                 }\r
102                             }\r
103                             return false;\r
104                   }\r
105          }\r
106          \r
107          private static boolean endAsterixCompare(String sInst, String pInst) {\r
108                         final int len = sInst.length()-1;\r
109                         if(pInst.length()<len) {\r
110                                 return false;\r
111                         }\r
112                         for(int j=0;j<len;++j) {\r
113                                 if(pInst.charAt(j)!=sInst.charAt(j)) {\r
114                                         return false;\r
115                                 }\r
116                         }\r
117                         return true;\r
118         }\r
119 \r
120         /**\r
121           * Evaluate Action\r
122           * \r
123           * sAction = Stored Action...\r
124           * pAction = Present Action... the Permission to validate against.\r
125           * Action is not quite as complex.  But we write it in this function so it can be consistent\r
126           */\r
127           public static boolean evalAction(String sAction,String pAction) {\r
128                   if(ASTERIX.equals(sAction))return true;                    // If Server's String is "*", then it accepts every Action\r
129                   for(String sItem : Split.split(LIST_SEP,sAction)) {            // allow for "," definition in Action\r
130                           if (pAction.charAt(0)==START_REGEX_CHAR?       // First char\r
131                                       sItem.matches(pAction.substring(1)):   // Evaluate as Regular Expression\r
132                                       sItem.equals(pAction))                 // Evaluate as String Compare\r
133                                                 return true;\r
134                   }             \r
135                   return false;\r
136           }\r
137          \r
138           /**\r
139            * Split.split by Char\r
140            * \r
141            * Note: I read the String Split.split and Pattern Split.split code, and we can do this more efficiently for a single Character\r
142            */\r
143 \r
144 }\r