5835b31f6bfbf3941f5a6b34398e6779537f63aa
[aaf/authz.git] / auth / auth-certman / src / main / java / org / onap / aaf / auth / cm / validation / CertmanValidator.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.cm.validation;
23
24 import java.util.List;
25 import java.util.regex.Pattern;
26
27 import org.onap.aaf.auth.dao.cass.ArtiDAO;
28 import org.onap.aaf.auth.dao.cass.ArtiDAO.Data;
29 import org.onap.aaf.auth.validation.Validator;
30
31 /**
32  * Validator
33  * Consistently apply content rules for content (incoming)
34  * 
35  * Note: We restrict content for usability in URLs (because RESTful service), and avoid 
36  * issues with Regular Expressions, and other enabling technologies. 
37  * @author Jonathan
38  *
39  */
40 public class CertmanValidator extends Validator{
41     // Repeated Msg fragments
42     private static final String MECHID = "mechid";
43     private static final String MACHINE = "machine";
44     private static final String ARTIFACT_LIST_IS_NULL = "Artifact List is null.";
45     private static final String Y = "y.";
46     private static final String IES = "ies.";
47     private static final String ENTR = " entr";
48     private static final String MUST_HAVE_AT_LEAST = " must have at least ";
49     private static final String IS_NULL = " is null.";
50     private static final String ARTIFACTS_MUST_HAVE_AT_LEAST = "Artifacts must have at least ";
51         private static final Pattern ALPHA_NUM = Pattern.compile("[a-zA-Z0-9]*");
52         
53         private static boolean disallowTmp = true;
54         public static void allowTmp() {
55                 disallowTmp=false;
56         }
57         
58     public CertmanValidator nullBlankMin(String name, List<String> list, int min) {
59         if (list==null) {
60             msg(name + IS_NULL);
61         } else {
62             if (list.size()<min) {
63                 msg(name + MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));
64             } else {
65                 for (String s : list) {
66                     nullOrBlank("List Item",s);
67                 }
68             }
69         }
70         return this;
71     }
72
73     public CertmanValidator artisRequired(List<ArtiDAO.Data> list, int min) {
74         if (list==null) {
75             msg(ARTIFACT_LIST_IS_NULL);
76         } else {
77             if (list.size()<min) {
78                 msg(ARTIFACTS_MUST_HAVE_AT_LEAST + min + ENTR + (min==1?Y:IES));
79             } else {
80                 for (ArtiDAO.Data a : list) {
81                     allRequired(a);
82                     if(disallowTmp && a.dir!=null && a.dir.startsWith("/tmp")) {
83                         msg("Certificates may not be deployed into /tmp directory (they will be removed at a random time by O/S)");
84                     }
85                 }
86             }
87         }
88         return this;
89     }
90
91     public CertmanValidator keys(ArtiDAO.Data add) {
92         if (add==null) {
93             msg("Artifact is null.");
94         } else {
95             nullOrBlank(MECHID, add.mechid);
96             nullOrBlank(MACHINE, add.machine);
97         }
98         return this;
99     }
100     
101     private CertmanValidator allRequired(Data a) {
102         if (a==null) {
103             msg("Artifact is null.");
104         } else {
105             nullOrBlank(MECHID, a.mechid);
106             nullOrBlank(MACHINE, a.machine);
107             nullOrBlank("ca",a.ca);
108             nullOrBlank("dir",a.dir);
109                 match("NS must be dot separated AlphaNumeric",a.ns,NAME_CHARS);
110             match("O/S User must be AlphaNumeric",a.os_user,ALPHA_NUM);
111             // Note: AppName, Notify & Sponsor are currently not required
112         }
113         return this;
114     }
115
116 }