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