1 package org
.argeo
.cms
.internal
.runtime
;
4 import java
.io
.FileFilter
;
5 import java
.io
.IOException
;
7 import java
.net
.InetAddress
;
9 import java
.net
.UnknownHostException
;
10 import java
.nio
.charset
.StandardCharsets
;
11 import java
.nio
.file
.Files
;
12 import java
.nio
.file
.Path
;
13 import java
.nio
.file
.Paths
;
14 import java
.security
.KeyStore
;
15 import java
.util
.ArrayList
;
16 import java
.util
.Arrays
;
17 import java
.util
.Collections
;
18 import java
.util
.HashMap
;
19 import java
.util
.List
;
20 import java
.util
.Locale
;
22 import java
.util
.Objects
;
23 import java
.util
.StringJoiner
;
24 import java
.util
.UUID
;
26 import javax
.security
.auth
.login
.Configuration
;
28 import org
.apache
.commons
.io
.FileUtils
;
29 import org
.argeo
.api
.cms
.CmsConstants
;
30 import org
.argeo
.api
.cms
.CmsLog
;
31 import org
.argeo
.api
.cms
.CmsState
;
32 import org
.argeo
.api
.uuid
.UuidFactory
;
33 import org
.argeo
.cms
.CmsDeployProperty
;
34 import org
.argeo
.cms
.auth
.ident
.IdentClient
;
37 * Implementation of a {@link CmsState}, initialising the required services.
39 public class CmsStateImpl
implements CmsState
{
40 private final static CmsLog log
= CmsLog
.getLog(CmsStateImpl
.class);
43 private Long availableSince
;
46 // private final boolean cleanState;
47 private String hostname
;
49 private UuidFactory uuidFactory
;
51 private final Map
<CmsDeployProperty
, String
> deployPropertyDefaults
;
53 public CmsStateImpl() {
54 Map
<CmsDeployProperty
, String
> deployPropertyDefaults
= new HashMap
<>();
55 deployPropertyDefaults
.put(CmsDeployProperty
.NODE_INIT
, "../../init");
56 deployPropertyDefaults
.put(CmsDeployProperty
.LOCALE
, Locale
.getDefault().toString());
59 deployPropertyDefaults
.put(CmsDeployProperty
.SSL_KEYSTORETYPE
, PkiUtils
.PKCS12
);
60 deployPropertyDefaults
.put(CmsDeployProperty
.SSL_PASSWORD
, PkiUtils
.DEFAULT_KEYSTORE_PASSWORD
);
61 Path keyStorePath
= getDataPath(PkiUtils
.DEFAULT_KEYSTORE_PATH
);
62 deployPropertyDefaults
.put(CmsDeployProperty
.SSL_KEYSTORE
, keyStorePath
.toAbsolutePath().toString());
64 Path trustStorePath
= getDataPath(PkiUtils
.DEFAULT_TRUSTSTORE_PATH
);
65 deployPropertyDefaults
.put(CmsDeployProperty
.SSL_TRUSTSTORETYPE
, PkiUtils
.PKCS12
);
66 deployPropertyDefaults
.put(CmsDeployProperty
.SSL_TRUSTSTOREPASSWORD
, PkiUtils
.DEFAULT_KEYSTORE_PASSWORD
);
67 deployPropertyDefaults
.put(CmsDeployProperty
.SSL_TRUSTSTORE
, trustStorePath
.toAbsolutePath().toString());
69 this.deployPropertyDefaults
= Collections
.unmodifiableMap(deployPropertyDefaults
);
73 // Runtime.getRuntime().addShutdownHook(new CmsShutdown());
79 if (log
.isTraceEnabled())
80 log
.trace("CMS State started");
82 // String stateUuidStr = KernelUtils.getFrameworkProp(Constants.FRAMEWORK_UUID);
83 // this.uuid = UUID.fromString(stateUuidStr);
84 this.uuid
= uuidFactory
.timeUUID();
85 // this.cleanState = stateUuid.equals(frameworkUuid);
87 this.hostname
= InetAddress
.getLocalHost().getHostName();
88 } catch (UnknownHostException e
) {
89 log
.error("Cannot set hostname: " + e
);
92 availableSince
= System
.currentTimeMillis();
93 if (log
.isDebugEnabled()) {
94 // log.debug("## CMS starting... stateUuid=" + this.stateUuid + (cleanState ? "
95 // (clean state) " : " "));
96 StringJoiner sb
= new StringJoiner("\n");
97 CmsDeployProperty
[] deployProperties
= CmsDeployProperty
.values();
98 Arrays
.sort(deployProperties
, (o1
, o2
) -> o1
.name().compareTo(o2
.name()));
99 for (CmsDeployProperty deployProperty
: deployProperties
) {
100 List
<String
> values
= getDeployProperties(deployProperty
);
101 for (int i
= 0; i
< values
.size(); i
++) {
102 String value
= values
.get(i
);
104 boolean isDefault
= deployPropertyDefaults
.containsKey(deployProperty
)
105 && value
.equals(deployPropertyDefaults
.get(deployProperty
));
106 String line
= deployProperty
.getProperty() + (i
== 0 ?
"" : "." + i
) + "=" + value
107 + (isDefault ?
" (default)" : "");
112 log
.debug("## CMS starting... (" + uuid
+ ")\n" + sb
+ "\n");
117 if (!Files
.exists(getDataPath(CmsConstants
.NODE
))) {// first init
121 } catch (RuntimeException
| IOException e
) {
122 log
.error("## FATAL: CMS activator failed", e
);
126 private void initSecurity() {
127 if (getDeployProperty(CmsDeployProperty
.JAVA_LOGIN_CONFIG
) == null) {
128 String jaasConfig
= KernelConstants
.JAAS_CONFIG
;
129 URL url
= getClass().getResource(jaasConfig
);
130 // System.setProperty(KernelConstants.JAAS_CONFIG_PROP,
131 // url.toExternalForm());
132 KernelUtils
.setJaasConfiguration(url
);
134 // explicitly load JAAS configuration
135 Configuration
.getConfiguration();
137 boolean initSsl
= getDeployProperty(CmsDeployProperty
.HTTPS_PORT
) != null;
143 private void initCertificates() {
144 // server certificate
145 Path keyStorePath
= Paths
.get(getDeployProperty(CmsDeployProperty
.SSL_KEYSTORE
));
146 Path pemKeyPath
= getDataPath(PkiUtils
.DEFAULT_PEM_KEY_PATH
);
147 Path pemCertPath
= getDataPath(PkiUtils
.DEFAULT_PEM_CERT_PATH
);
148 char[] keyStorePassword
= getDeployProperty(CmsDeployProperty
.SSL_PASSWORD
).toCharArray();
151 // if PEM files both exists, update the PKCS12 file
152 if (Files
.exists(pemCertPath
) && Files
.exists(pemKeyPath
)) {
153 // TODO check certificate update time? monitor changes?
154 KeyStore keyStore
= PkiUtils
.getKeyStore(keyStorePath
, keyStorePassword
,
155 getDeployProperty(CmsDeployProperty
.SSL_KEYSTORETYPE
));
156 try (Reader key
= Files
.newBufferedReader(pemKeyPath
, StandardCharsets
.US_ASCII
);
157 Reader cert
= Files
.newBufferedReader(pemCertPath
, StandardCharsets
.US_ASCII
);) {
158 PkiUtils
.loadPem(keyStore
, key
, keyStorePassword
, cert
);
159 PkiUtils
.saveKeyStore(keyStorePath
, keyStorePassword
, keyStore
);
160 if (log
.isDebugEnabled())
161 log
.debug("PEM certificate stored in " + keyStorePath
);
162 } catch (IOException e
) {
163 log
.error("Cannot read PEM files " + pemKeyPath
+ " and " + pemCertPath
, e
);
168 Path trustStorePath
= Paths
.get(getDeployProperty(CmsDeployProperty
.SSL_TRUSTSTORE
));
169 char[] trustStorePassword
= getDeployProperty(CmsDeployProperty
.SSL_TRUSTSTOREPASSWORD
).toCharArray();
172 Path ipaCaCertPath
= Paths
.get(PkiUtils
.IPA_PEM_CA_CERT_PATH
);
173 if (Files
.exists(ipaCaCertPath
)) {
174 KeyStore trustStore
= PkiUtils
.getKeyStore(trustStorePath
, trustStorePassword
,
175 getDeployProperty(CmsDeployProperty
.SSL_TRUSTSTORETYPE
));
176 try (Reader cert
= Files
.newBufferedReader(ipaCaCertPath
, StandardCharsets
.US_ASCII
);) {
177 PkiUtils
.loadPem(trustStore
, null, trustStorePassword
, cert
);
178 PkiUtils
.saveKeyStore(trustStorePath
, trustStorePassword
, trustStore
);
179 if (log
.isDebugEnabled())
180 log
.debug("IPA CA certificate stored in " + trustStorePath
);
181 } catch (IOException e
) {
182 log
.error("Cannot trust CA certificate", e
);
186 if (!Files
.exists(keyStorePath
))
187 PkiUtils
.createSelfSignedKeyStore(keyStorePath
, keyStorePassword
, PkiUtils
.PKCS12
);
188 // props.put(JettyHttpConstants.SSL_KEYSTORETYPE, PkiUtils.PKCS12);
189 // props.put(JettyHttpConstants.SSL_KEYSTORE, keyStorePath.toString());
190 // props.put(JettyHttpConstants.SSL_PASSWORD, new String(keyStorePassword));
192 // props.put(InternalHttpConstants.SSL_KEYSTORETYPE, "PKCS11");
193 // props.put(InternalHttpConstants.SSL_KEYSTORE, "../../nssdb");
194 // props.put(InternalHttpConstants.SSL_PASSWORD, keyStorePassword);
199 if (log
.isDebugEnabled())
200 log
.debug("CMS stopping... (" + this.uuid
+ ")");
202 long duration
= ((System
.currentTimeMillis() - availableSince
) / 1000) / 60;
203 log
.info("## ARGEO CMS STOPPED after " + (duration
/ 60) + "h " + (duration
% 60) + "min uptime ##");
206 private void firstInit() throws IOException
{
207 log
.info("## FIRST INIT ##");
208 List
<String
> nodeInits
= getDeployProperties(CmsDeployProperty
.NODE_INIT
);
209 // if (nodeInits == null)
210 // nodeInits = "../../init";
211 CmsStateImpl
.prepareFirstInitInstanceArea(nodeInits
);
215 public String
getDeployProperty(String property
) {
216 CmsDeployProperty deployProperty
= CmsDeployProperty
.find(property
);
217 if (deployProperty
== null) {
219 if (property
.startsWith("argeo.node.")) {
220 return doGetDeployProperty(property
);
222 if (property
.equals("argeo.i18n.locales")) {
223 String value
= doGetDeployProperty(property
);
225 log
.warn("Property " + property
+ " was ignored (value=" + value
+ ")");
230 throw new IllegalArgumentException("Unsupported deploy property " + property
);
232 int index
= CmsDeployProperty
.getPropertyIndex(property
);
233 return getDeployProperty(deployProperty
, index
);
237 public List
<String
> getDeployProperties(String property
) {
238 CmsDeployProperty deployProperty
= CmsDeployProperty
.find(property
);
239 if (deployProperty
== null)
240 return new ArrayList
<>();
241 return getDeployProperties(deployProperty
);
244 public static List
<String
> getDeployProperties(CmsState cmsState
, CmsDeployProperty deployProperty
) {
245 return ((CmsStateImpl
) cmsState
).getDeployProperties(deployProperty
);
248 public List
<String
> getDeployProperties(CmsDeployProperty deployProperty
) {
249 List
<String
> res
= new ArrayList
<>(deployProperty
.getMaxCount());
250 for (int i
= 0; i
< deployProperty
.getMaxCount(); i
++) {
251 // String propertyName = i == 0 ? deployProperty.getProperty() :
252 // deployProperty.getProperty() + "." + i;
253 String value
= getDeployProperty(deployProperty
, i
);
259 public static String
getDeployProperty(CmsState cmsState
, CmsDeployProperty deployProperty
) {
260 return ((CmsStateImpl
) cmsState
).getDeployProperty(deployProperty
);
263 public String
getDeployProperty(CmsDeployProperty deployProperty
) {
264 String value
= getDeployProperty(deployProperty
, 0);
268 public String
getDeployProperty(CmsDeployProperty deployProperty
, int index
) {
269 String propertyName
= deployProperty
.getProperty() + (index
== 0 ?
"" : "." + index
);
270 String value
= doGetDeployProperty(propertyName
);
271 if (value
== null && index
== 0) {
273 if (deployPropertyDefaults
.containsKey(deployProperty
)) {
274 value
= deployPropertyDefaults
.get(deployProperty
);
275 if (deployProperty
.isSystemPropertyOnly())
276 System
.setProperty(deployProperty
.getProperty(), value
);
280 // try legacy properties
281 String legacyProperty
= switch (deployProperty
) {
282 case DIRECTORY
-> "argeo.node.useradmin.uris";
283 case DB_URL
-> "argeo.node.dburl";
284 case DB_USER
-> "argeo.node.dbuser";
285 case DB_PASSWORD
-> "argeo.node.dbpassword";
286 case HTTP_PORT
-> "org.osgi.service.http.port";
287 case HTTPS_PORT
-> "org.osgi.service.http.port.secure";
288 case HOST
-> "org.eclipse.equinox.http.jetty.http.host";
289 case LOCALE
-> "argeo.i18n.defaultLocale";
293 if (legacyProperty
!= null) {
294 value
= doGetDeployProperty(legacyProperty
);
296 log
.warn("Retrieved deploy property " + deployProperty
.getProperty()
297 + " through deprecated property " + legacyProperty
);
302 if (index
== 0 && deployProperty
.isSystemPropertyOnly()) {
303 String systemPropertyValue
= System
.getProperty(deployProperty
.getProperty());
304 if (!Objects
.equals(value
, systemPropertyValue
))
305 throw new IllegalStateException(
306 "Property " + deployProperty
+ " must be a ssystem property, but its value is " + value
307 + ", while the system property value is " + systemPropertyValue
);
309 return value
!= null ? value
.toString() : null;
312 protected String
getLegacyProperty(String legacyProperty
, CmsDeployProperty deployProperty
) {
313 String value
= doGetDeployProperty(legacyProperty
);
315 log
.warn("Retrieved deploy property " + deployProperty
.getProperty() + " through deprecated property "
316 + legacyProperty
+ ".");
321 protected String
doGetDeployProperty(String property
) {
322 return KernelUtils
.getFrameworkProp(property
);
326 public Path
getDataPath(String relativePath
) {
327 return KernelUtils
.getOsgiInstancePath(relativePath
);
331 public Long
getAvailableSince() {
332 return availableSince
;
338 public String
getHostname() {
343 public UUID
getUuid() {
347 public void setUuidFactory(UuidFactory uuidFactory
) {
348 this.uuidFactory
= uuidFactory
;
352 * Called before node initialisation, in order populate OSGi instance are with
353 * some files (typically LDIF, etc).
355 public static void prepareFirstInitInstanceArea(List
<String
> nodeInits
) {
357 for (String nodeInit
: nodeInits
) {
359 if (nodeInit
.startsWith("http")) {
361 // registerRemoteInit(nodeInit);
364 // TODO use java.nio.file
366 if (nodeInit
.startsWith("."))
367 initDir
= KernelUtils
.getExecutionDir(nodeInit
);
369 initDir
= new File(nodeInit
);
370 // TODO also uncompress archives
371 if (initDir
.exists())
373 // TODO use NIO utilities
374 FileUtils
.copyDirectory(initDir
, KernelUtils
.getOsgiInstancePath("").toFile(),
378 public boolean accept(File pathname
) {
379 if (pathname
.getName().equals(".svn") || pathname
.getName().equals(".git"))
384 log
.info("CMS initialized from " + initDir
.getCanonicalPath());
385 } catch (IOException e
) {
386 throw new RuntimeException("Cannot initialize from " + initDir
, e
);
395 public static IdentClient
getIdentClient(String remoteAddr
) {
396 if (!IdentClient
.isDefaultAuthdPassphraseFileAvailable())
398 // TODO make passphrase more configurable
399 return new IdentClient(remoteAddr
);