Remove Tabs, per Jococo
[aaf/authz.git] / auth / auth-service / src / main / java / org / onap / aaf / auth / service / validation / ServiceValidator.java
1 /**
2  * ============LICENSE_START====================================================
3  * org.onap.aaf
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
10  * 
11  *      http://www.apache.org/licenses/LICENSE-2.0
12  * 
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====================================================
19  *
20  */
21
22 package org.onap.aaf.auth.service.validation;
23
24 import org.onap.aaf.auth.dao.cass.CredDAO;
25 import org.onap.aaf.auth.dao.cass.DelegateDAO;
26 import org.onap.aaf.auth.dao.cass.Namespace;
27 import org.onap.aaf.auth.dao.cass.PermDAO;
28 import org.onap.aaf.auth.dao.cass.RoleDAO;
29 import org.onap.aaf.auth.dao.cass.UserRoleDAO;
30 import org.onap.aaf.auth.env.AuthzTrans;
31 import org.onap.aaf.auth.layer.Result;
32 import org.onap.aaf.auth.org.Organization;
33 import org.onap.aaf.auth.rserv.Pair;
34 import org.onap.aaf.auth.validation.Validator;
35
36 /**
37  * Validator
38  * Consistently apply content rules for content (incoming)
39  * 
40  * Note: We restrict content for usability in URLs (because RESTful service), and avoid 
41  * issues with Regular Expressions, and other enabling technologies. 
42  * @author Jonathan
43  *
44  */
45 public class ServiceValidator extends Validator {
46     public ServiceValidator perm(Result<PermDAO.Data> rpd) {
47         if (rpd.notOK()) {
48             msg(rpd.details);
49         } else {
50             perm(rpd.value);
51         }
52         return this;
53     }
54
55
56     public ServiceValidator perm(PermDAO.Data pd) {
57         if (pd==null) {
58             msg("Perm Data is null.");
59         } else {
60             if(!pd.ns.contains("@")) { 
61                 ns(pd.ns);
62             }
63             permType(pd.type,pd.ns);
64             permInstance(pd.instance);
65             permAction(pd.action);
66             if (pd.roles!=null) { 
67                 for (String role : pd.roles) {
68                     role(role);
69                 }
70             }
71             if (pd.roles!=null) {
72                 for (String r : pd.roles) {
73                     role(r);
74                 }
75             }
76             description("Perm",pd.description);
77         }
78         return this;
79     }
80
81     public ServiceValidator role(Result<RoleDAO.Data> rrd) {
82         if (rrd.notOK()) {
83             msg(rrd.details);
84         } else {
85             role(rrd.value);
86         }
87         return this;
88     }
89     
90     public ServiceValidator role(RoleDAO.Data pd) {
91         if (pd==null) {
92             msg("Role Data is null.");
93         } else {
94             ns(pd.ns);
95             role(pd.name);
96             if (pd.perms!=null) {
97                 for (String perm : pd.perms) {
98                     String[] ps = perm.split("\\|");
99                     if (ps.length!=3) {
100                         msg("Perm [" + perm + "] in Role [" + pd.fullName() + "] is not correctly separated with '|'");
101                     } else {
102                         permType(ps[0],null);
103                         permInstance(ps[1]);
104                         permAction(ps[2]);
105                     }
106                 }
107             }
108             description("Role",pd.description);
109         }
110         return this;
111     }
112
113     public ServiceValidator delegate(Organization org, Result<DelegateDAO.Data> rdd) {
114         if (rdd.notOK()) {
115             msg(rdd.details);
116         } else {
117             delegate(org, rdd.value);
118         }
119         return this;
120     }
121
122     public ServiceValidator delegate(Organization org, DelegateDAO.Data dd) {
123         if (dd==null) {
124             msg("Delegate Data is null.");
125         } else {
126             user(org,dd.user);
127             user(org,dd.delegate);
128         }
129         return this;
130     }
131
132
133     public ServiceValidator cred(AuthzTrans trans, Organization org, Result<CredDAO.Data> rcd, boolean isNew) {
134         if (rcd.notOK()) {
135             msg(rcd.details);
136         } else {
137             cred(trans, org,rcd.value,isNew);
138         }
139         return this;
140     }
141
142     public ServiceValidator cred(AuthzTrans trans, Organization org, CredDAO.Data cd, boolean isNew) {
143         if (cd==null) {
144             msg("Cred Data is null.");
145         } else {
146             if (!org.isValidCred(trans, cd.id)) {
147                 msg("ID [" + cd.id + "] is invalid in " + org.getName());
148             }
149             String str = cd.id;
150             int idx = str.indexOf('@');
151             if (idx>0) {
152                 str = str.substring(0,idx);
153             }
154             
155             if (org.supportsRealm(cd.id)) {
156                 String resp = org.isValidID(trans, str);
157                 if (isNew && (resp!=null && resp.length()>0)) {
158                     msg(cd.id,str);
159                 }
160             }
161     
162             if (cd.type==null) {
163                 msg("Credential Type must be set");
164             } else {
165                 switch(cd.type) {
166                     case CredDAO.BASIC_AUTH_SHA256:
167                     case CredDAO.FQI:
168                         // ok
169                         break;
170                     default:
171                         msg("Credential Type [",Integer.toString(cd.type),"] is invalid");
172                 }
173             }
174         }
175         return this;
176     }
177
178
179     public ServiceValidator user(Organization org, String user) {
180         if (nob(user,ID_CHARS)) {
181             msg("User [",user,"] is invalid.");
182         }
183         return this;
184     }
185
186     public ServiceValidator ns(Result<Namespace> nsd) {
187         notOK(nsd);
188         ns(nsd.value);
189         return this;
190     }
191
192     public ServiceValidator ns(Namespace ns) {
193         ns(ns.name);
194         for (String s : ns.admin) {
195             if (nob(s,ID_CHARS)) {
196                 msg("Admin [" + s + "] is invalid.");        
197             }
198             
199         }
200         for (String s : ns.owner) {
201             if (nob(s,ID_CHARS)) {
202                 msg("Responsible [" + s + "] is invalid.");        
203             }
204             
205         }
206         
207         if (ns.attrib!=null) {
208             for (Pair<String, String> at : ns.attrib) {
209                 if (nob(at.x,NAME_CHARS)) {
210                     msg("Attribute tag [" + at.x + "] is invalid.");
211                 }
212                 if (nob(at.x,NAME_CHARS)) {
213                     msg("Attribute value [" + at.y + "] is invalid.");
214                 }
215             }
216         }
217
218         description("Namespace",ns.description);
219         return this;
220     }
221
222     public ServiceValidator user_role(String user, UserRoleDAO.Data urdd) {
223         role(user,urdd.role);
224         if(!urdd.role.startsWith(user)) { 
225             nullOrBlank("UserRole.ns",urdd.ns);
226             nullOrBlank("UserRole.rname",urdd.rname);
227         }
228         return this;
229     }
230
231     
232     public ServiceValidator user_role(UserRoleDAO.Data urdd) {
233         if (urdd==null) {
234             msg("UserRole is null");
235         } else {
236             role(urdd.role);
237             nullOrBlank("UserRole.ns",urdd.ns);
238             nullOrBlank("UserRole.rname",urdd.rname);
239         }
240         return this;
241     }
242
243     public ServiceValidator nullOrBlank(PermDAO.Data pd) {
244         if (pd==null) {
245             msg("Permission is null");
246         } else {
247             nullOrBlank("NS",pd.ns).
248             nullOrBlank("Type",pd.type).
249             nullOrBlank("Instance",pd.instance).
250             nullOrBlank("Action",pd.action);
251         }
252         return this;
253     }
254
255     public ServiceValidator nullOrBlank(RoleDAO.Data rd) {
256         if (rd==null) {
257             msg("Role is null");
258         } else {
259             nullOrBlank("NS",rd.ns).
260             nullOrBlank("Name",rd.name);
261         }
262         return this;
263     }
264 }