Create Helm based Certificates for Clients
[aaf/authz.git] / cadi / core / src / main / java / org / onap / aaf / cadi / config / RegistrationPropHolder.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 package org.onap.aaf.cadi.config;
22
23 import java.net.Inet4Address;
24 import java.net.UnknownHostException;
25
26 import org.onap.aaf.cadi.Access;
27 import org.onap.aaf.cadi.CadiException;
28 import org.onap.aaf.cadi.Access.Level;
29 import org.onap.aaf.cadi.util.Split;
30
31 public class RegistrationPropHolder {
32         private final String PUBLIC_NAME="%NS.%N";
33         private final String REGI="RegistrationProperty: %s='%s'";
34         private final Access access;
35         public String hostname;
36         private int port;
37         public String public_hostname;
38         private Integer public_port;
39         public Float latitude;
40         public Float longitude;
41         public final String default_fqdn;
42         public final String default_container_ns;
43         public final String default_name;
44         public final String lentries;
45         public final String lcontainer;
46         public final String default_container;
47         private static boolean firstlog = true;
48
49         public RegistrationPropHolder(final Access access, final int port) throws UnknownHostException, CadiException {
50                 this.access = access;
51                 StringBuilder errs = new StringBuilder();
52                 String str;
53                 this.port = port;
54
55                 lentries=access.getProperty(Config.AAF_LOCATOR_ENTRIES,"");
56                 default_container = access.getProperty(Config.AAF_LOCATOR_CONTAINER, "");
57                 if(firstlog) {
58                         access.printf(Level.INIT, REGI,"default_container",default_container);
59                 }
60                 if(!default_container.isEmpty()) {
61                         lcontainer=',' + default_container; // "" makes a blank default Public Entry
62                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT+'.'+default_container, null);
63                         if(str==null) {
64                                 str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
65                         }
66                 } else {
67                         lcontainer=default_container;
68                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_PORT, null);
69                 }
70                 if(str!=null) {
71                         public_port=Integer.decode(str);
72                 }
73                 if(firstlog) {
74                         access.printf(Level.INIT, "RegistrationProperty: public_port='%d'",public_port);
75                 }
76
77                 hostname = access.getProperty(Config.HOSTNAME, null);
78                 if (hostname==null) {
79                         hostname = Inet4Address.getLocalHost().getHostName();
80                 }
81                 if (hostname==null) {
82                         mustBeDefined(errs,Config.HOSTNAME);
83                 }
84                 if(firstlog) {
85                         access.printf(Level.INIT, REGI,"hostname",hostname);
86                 }
87
88                 public_hostname = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME, hostname);
89                 if(firstlog) {
90                         access.printf(Level.INIT, REGI,"public_hostname",public_hostname);
91                 }
92                                 
93                 default_name = access.getProperty(Config.AAF_LOCATOR_NAME, PUBLIC_NAME);
94                 if(firstlog) {
95                         access.printf(Level.INIT, REGI,"default_name",default_name);
96                 }
97                 
98                 latitude=null;
99                 String slatitude = access.getProperty(Config.CADI_LATITUDE, null);
100                 if(slatitude == null) {
101                         mustBeDefined(errs,Config.CADI_LATITUDE);
102                 } else {
103                         latitude = Float.parseFloat(slatitude);
104                 }
105                 if(firstlog) {
106                         access.printf(Level.INIT, REGI,"latitude",slatitude);
107                 }
108
109                 longitude=null;
110                 String slongitude = access.getProperty(Config.CADI_LONGITUDE, null);
111                 if(slongitude == null) {
112                         mustBeDefined(errs,Config.CADI_LONGITUDE);
113                 } else {
114                         longitude = Float.parseFloat(slongitude);
115                 }
116                 if(firstlog) {
117                         access.printf(Level.INIT, REGI,"longitude",slongitude);
118                 }
119
120                 String dot_le;
121                 // Note: only one of the ports can be public...  Therefore, only the last
122                 for(String le : Split.splitTrim(',', lcontainer)) {
123                         dot_le = le.isEmpty()?le :"."+le;
124                         str = access.getProperty(Config.AAF_LOCATOR_PUBLIC_HOSTNAME+dot_le,null);
125                         if( str != null && !str.isEmpty()) {
126                                 public_hostname=str;
127                                 if(firstlog) {
128                                         access.printf(Level.INIT, "RegistrationProperty: public_hostname(overloaded by %s)='%s'",dot_le,public_hostname);
129                                 }
130                         }
131                 }
132                 
133                 default_fqdn = access.getProperty(Config.AAF_LOCATOR_FQDN, hostname);
134                 if(firstlog) {
135                         access.printf(Level.INIT, REGI,"default_fqdn",default_fqdn);
136                 }
137                 default_container_ns = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS,"");
138                 if(firstlog) {
139                         access.printf(Level.INIT, REGI,"default_container_ns",default_container_ns);
140                 }
141                 if(errs.length()>0) {
142                         throw new CadiException(errs.toString());
143                 }
144                 firstlog = false;
145         }
146
147         private void mustBeDefined(StringBuilder errs, String propname) {
148                 errs.append('\n');
149                 errs.append(propname);
150                 errs.append(" must be defined.");
151                 
152         }
153
154         public String getEntryFQDN(final String entry, final String dot_le) {
155                 String str;
156                 if(public_hostname!=null && dot_le.isEmpty()) {
157                         str = public_hostname;
158                 } else {
159                         str = access.getProperty(Config.AAF_LOCATOR_FQDN+dot_le, default_fqdn);
160                 }
161                 return replacements("RegistrationPropHolder.getEntryFQDN",str,entry,dot_le);
162         }
163         
164         public String getEntryName(final String entry, final String dot_le) {
165                 String str;
166                 if(dot_le.isEmpty()) {
167                         str = PUBLIC_NAME;
168                 } else {
169                         str = access.getProperty(Config.AAF_LOCATOR_NAME+dot_le, default_name);
170                 }
171                 return replacements("RegistrationPropHolder.getEntryName",str,entry,dot_le);
172         }
173         
174         
175         private String getNS(String dot_le) {
176                 String ns;
177                 ns = access.getProperty(Config.AAF_LOCATOR_APP_NS+dot_le,null);
178                 if(ns==null) {
179                         ns = access.getProperty(Config.AAF_LOCATOR_APP_NS, "");
180                 }
181                 return ns;
182         }
183
184         
185         public String replacements(final String fromCode, final String source, final String name, final String _dot_le) {
186                 if(source == null) {
187                         return "";
188                 } else if(source.isEmpty()) {
189                         return source;
190                 }
191                 String value = source;
192                 String dot_le;
193                 if(_dot_le==null) {
194                         dot_le = default_container.isEmpty()?"":'.'+default_container;
195                 } else {
196                         dot_le = _dot_le;
197                 }
198
199         String aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL+dot_le,null);
200         if(aaf_locator_host==null) {
201                 aaf_locator_host = access.getProperty(Config.AAF_LOCATE_URL,null);
202         }
203
204         String str;
205         if(aaf_locator_host!=null) {
206                         if("https://AAF_LOCATE_URL".equals(value)) {
207                                 value = aaf_locator_host;
208                         } else {
209                         str = aaf_locator_host;
210                                 if(value.indexOf(Config.AAF_LOCATE_URL_TAG)>=0) {
211                                         if(!str.endsWith("/")) {
212                                                 str+='/';
213                                         }
214                                         if(!str.endsWith("/locate/")) {
215                                                 str+="locate/";
216                                         }
217                                         value = value.replace("https://AAF_LOCATE_URL/", str);
218                                 }
219                         }
220         }
221
222                 int atC = value.indexOf("%C"); 
223                 if(atC>=0) {
224                         // aaf_locator_container_ns
225                         str = access.getProperty(Config.AAF_LOCATOR_CONTAINER_NS+dot_le, default_container_ns);
226                         if(str.isEmpty()) {
227                                 value = value.replace("%CNS"+'.', str);
228                         }
229                         value = value.replace("%CNS", str);
230                         
231                         str = access.getProperty(Config.AAF_LOCATOR_CONTAINER+dot_le,default_container);
232                         if(str.isEmpty()) {
233                                 value = value.replace("%C"+'.', str);
234                         }
235                         value = value.replace("%C", str);
236                 }
237                 
238                 if(value.indexOf("%NS")>=0) {
239                         str = getNS(dot_le);
240                         if(str==null || str.isEmpty()) {
241                                 value = value.replace("%NS"+'.', str);
242                         }
243                         value = value.replace("%NS", str);
244                 }
245
246                 // aaf_root_ns
247                 if(value.indexOf("AAF_NS")>=0) {
248                         str = access.getProperty(Config.AAF_ROOT_NS, Config.AAF_ROOT_NS_DEF);
249                         String temp = value.replace("%AAF_NS", str);
250                         if(temp.equals(value)) { // intended
251                                 value = value.replace("AAF_NS", str); // Backward Compatibility
252                         } else {
253                                 value = temp;
254                         }
255                 }
256
257                 
258                 if(value.indexOf('%')>=0) {
259             // These shouldn't be expected to have dot elements
260             if(name!=null) {
261               value = value.replace("%N", name);
262             }
263             if(default_fqdn!=null) {
264               value = value.replace("%DF", default_fqdn);
265             }
266             if(public_hostname!=null) {
267               value = value.replace("%PH", public_hostname);
268             }
269                 }
270                 access.printf(Level.DEBUG, 
271                                 "RegistrationReplacement from %s, source: %s, dot_le: %s, value: %s",
272                                 fromCode,source,dot_le,value);
273
274                 return value;
275         }
276         
277         public int getEntryPort(final String dot_le) {
278                 return public_port!=null && dot_le.isEmpty()?
279                                 public_port:
280                                 port;
281         }
282
283         public Access access() {
284                 return access;
285         }
286 }