]>
git.argeo.org Git - lgpl/argeo-commons.git/blob - org.argeo.cms/src/org/argeo/cms/internal/kernel/FirstInit.java
7406b67e4308839dc72ce37e719ad987e7559390
1 package org
.argeo
.cms
.internal
.kernel
;
3 import static org
.argeo
.cms
.internal
.kernel
.KernelUtils
.getFrameworkProp
;
6 import java
.io
.FileFilter
;
7 import java
.io
.IOException
;
8 import java
.net
.InetAddress
;
10 import java
.nio
.file
.Files
;
11 import java
.nio
.file
.Path
;
12 import java
.security
.KeyStore
;
13 import java
.util
.ArrayList
;
14 import java
.util
.Arrays
;
15 import java
.util
.Dictionary
;
16 import java
.util
.Hashtable
;
17 import java
.util
.List
;
19 import javax
.security
.auth
.x500
.X500Principal
;
21 import org
.apache
.commons
.io
.FileUtils
;
22 import org
.apache
.commons
.logging
.Log
;
23 import org
.apache
.commons
.logging
.LogFactory
;
24 import org
.argeo
.cms
.CmsException
;
25 import org
.argeo
.cms
.internal
.jcr
.RepoConf
;
26 import org
.argeo
.node
.NodeConstants
;
27 import org
.argeo
.osgi
.useradmin
.UserAdminConf
;
28 import org
.eclipse
.equinox
.http
.jetty
.JettyConstants
;
31 * Interprets framework properties in order to generate the initial deploy
35 private final static Log log
= LogFactory
.getLog(FirstInit
.class);
38 log
.info("## FIRST INIT ##");
41 /** Override the provided config with the framework properties */
42 Dictionary
<String
, Object
> getNodeRepositoryConfig(Dictionary
<String
, Object
> provided
) {
43 Dictionary
<String
, Object
> props
= provided
!= null ? provided
: new Hashtable
<String
, Object
>();
44 for (RepoConf repoConf
: RepoConf
.values()) {
45 Object value
= getFrameworkProp(NodeConstants
.NODE_REPO_PROP_PREFIX
+ repoConf
.name());
47 props
.put(repoConf
.name(), value
);
49 props
.put(NodeConstants
.CN
, NodeConstants
.NODE
);
50 // props.put(NodeConstants.JCR_REPOSITORY_ALIAS, NodeConstants.NODE);
54 /** Override the provided config with the framework properties */
55 Dictionary
<String
, Object
> getHttpServerConfig(Dictionary
<String
, Object
> provided
) {
56 String httpPort
= getFrameworkProp("org.osgi.service.http.port");
57 String httpsPort
= getFrameworkProp("org.osgi.service.http.port.secure");
58 /// TODO make it more generic
59 String httpHost
= getFrameworkProp(JettyConstants
.PROPERTY_PREFIX
+ '.' + JettyConstants
.HTTP_HOST
);
60 String httpsHost
= getFrameworkProp(JettyConstants
.PROPERTY_PREFIX
+ '.' + JettyConstants
.HTTPS_HOST
);
62 final Hashtable
<String
, Object
> props
= new Hashtable
<String
, Object
>();
64 if (httpPort
!= null || httpsPort
!= null) {
65 if (httpPort
!= null) {
66 props
.put(JettyConstants
.HTTP_PORT
, httpPort
);
67 props
.put(JettyConstants
.HTTP_ENABLED
, true);
69 if (httpsPort
!= null) {
70 props
.put(JettyConstants
.HTTPS_PORT
, httpsPort
);
71 props
.put(JettyConstants
.HTTPS_ENABLED
, true);
72 Path keyStorePath
= KernelUtils
.getOsgiInstancePath(KernelConstants
.DEFAULT_KEYSTORE_PATH
);
73 String keyStorePassword
= getFrameworkProp(
74 JettyConstants
.PROPERTY_PREFIX
+ '.' + JettyConstants
.SSL_PASSWORD
);
75 if (keyStorePassword
== null)
76 keyStorePassword
= "changeit";
77 if (!Files
.exists(keyStorePath
))
78 createSelfSignedKeyStore(keyStorePath
, keyStorePassword
);
79 props
.put(JettyConstants
.SSL_KEYSTORETYPE
, "PKCS12");
80 props
.put(JettyConstants
.SSL_KEYSTORE
, keyStorePath
.toString());
81 props
.put(JettyConstants
.SSL_PASSWORD
, keyStorePassword
);
82 props
.put(JettyConstants
.SSL_WANTCLIENTAUTH
, true);
85 props
.put(JettyConstants
.HTTP_HOST
, httpHost
);
86 if (httpsHost
!= null)
87 props
.put(JettyConstants
.HTTPS_HOST
, httpHost
);
89 props
.put(NodeConstants
.CN
, NodeConstants
.DEFAULT
);
94 List
<Dictionary
<String
, Object
>> getUserDirectoryConfigs() {
95 List
<Dictionary
<String
, Object
>> res
= new ArrayList
<>();
96 File nodeBaseDir
= KernelUtils
.getOsgiInstancePath(KernelConstants
.DIR_NODE
).toFile();
97 List
<String
> uris
= new ArrayList
<>();
100 String nodeRolesUri
= getFrameworkProp(NodeConstants
.ROLES_URI
);
101 String baseNodeRoleDn
= NodeConstants
.ROLES_BASEDN
;
102 if (nodeRolesUri
== null) {
103 nodeRolesUri
= baseNodeRoleDn
+ ".ldif";
104 File nodeRolesFile
= new File(nodeBaseDir
, nodeRolesUri
);
105 if (!nodeRolesFile
.exists())
107 FileUtils
.copyInputStreamToFile(getClass().getResourceAsStream(baseNodeRoleDn
+ ".ldif"),
109 } catch (IOException e
) {
110 throw new CmsException("Cannot copy demo resource", e
);
112 // nodeRolesUri = nodeRolesFile.toURI().toString();
114 uris
.add(nodeRolesUri
);
117 String userAdminUris
= getFrameworkProp(NodeConstants
.USERADMIN_URIS
);
118 if (userAdminUris
== null) {
119 String demoBaseDn
= "dc=example,dc=com";
120 userAdminUris
= demoBaseDn
+ ".ldif";
121 File businessRolesFile
= new File(nodeBaseDir
, userAdminUris
);
122 if (!businessRolesFile
.exists())
124 FileUtils
.copyInputStreamToFile(getClass().getResourceAsStream(demoBaseDn
+ ".ldif"),
126 } catch (IOException e
) {
127 throw new CmsException("Cannot copy demo resource", e
);
129 // userAdminUris = businessRolesFile.toURI().toString();
130 log
.warn("## DEV Using dummy base DN " + demoBaseDn
);
131 // TODO downgrade security level
133 for (String userAdminUri
: userAdminUris
.split(" "))
134 uris
.add(userAdminUri
);
137 for (String uri
: uris
) {
141 if (u
.getPath() == null)
142 throw new CmsException("URI " + uri
+ " must have a path in order to determine base DN");
143 if (u
.getScheme() == null) {
144 if (uri
.startsWith("/") || uri
.startsWith("./") || uri
.startsWith("../"))
145 u
= new File(uri
).getCanonicalFile().toURI();
146 else if (!uri
.contains("/")) {
147 // u = KernelUtils.getOsgiInstanceUri(KernelConstants.DIR_NODE + '/' + uri);
150 throw new CmsException("Cannot interpret " + uri
+ " as an uri");
151 } else if (u
.getScheme().equals("file")) {
152 u
= new File(u
).getCanonicalFile().toURI();
154 } catch (Exception e
) {
155 throw new CmsException("Cannot interpret " + uri
+ " as an uri", e
);
157 Dictionary
<String
, Object
> properties
= UserAdminConf
.uriAsProperties(u
.toString());
165 * Called before node initialisation, in order populate OSGi instance are with
166 * some files (typically LDIF, etc).
168 static void prepareInstanceArea() {
169 String nodeInit
= getFrameworkProp(NodeConstants
.NODE_INIT
);
170 if (nodeInit
== null)
171 nodeInit
= "../../init";
172 if (nodeInit
.startsWith("http")) {
173 // remoteFirstInit(nodeInit);
177 // TODO use java.nio.file
179 if (nodeInit
.startsWith("."))
180 initDir
= KernelUtils
.getExecutionDir(nodeInit
);
182 initDir
= new File(nodeInit
);
183 // TODO also uncompress archives
184 if (initDir
.exists())
186 FileUtils
.copyDirectory(initDir
, KernelUtils
.getOsgiInstanceDir(), new FileFilter() {
189 public boolean accept(File pathname
) {
190 if (pathname
.getName().equals(".svn") || pathname
.getName().equals(".git"))
195 log
.info("CMS initialized from " + initDir
.getCanonicalPath());
196 } catch (IOException e
) {
197 throw new CmsException("Cannot initialize from " + initDir
, e
);
201 private void createSelfSignedKeyStore(Path keyStorePath
, String keyStorePassword
) {
202 // for (Provider provider : Security.getProviders())
203 // System.out.println(provider.getName());
204 File keyStoreFile
= keyStorePath
.toFile();
205 char[] ksPwd
= keyStorePassword
.toCharArray();
206 char[] keyPwd
= Arrays
.copyOf(ksPwd
, ksPwd
.length
);
207 if (!keyStoreFile
.exists()) {
209 keyStoreFile
.getParentFile().mkdirs();
210 KeyStore keyStore
= PkiUtils
.getKeyStore(keyStoreFile
, ksPwd
);
211 PkiUtils
.generateSelfSignedCertificate(keyStore
,
212 new X500Principal("CN=" + InetAddress
.getLocalHost().getHostName() + ",OU=UNSECURE,O=UNSECURE"),
214 PkiUtils
.saveKeyStore(keyStoreFile
, ksPwd
, keyStore
);
215 if (log
.isDebugEnabled())
216 log
.debug("Created self-signed unsecure keystore " + keyStoreFile
);
217 } catch (Exception e
) {
218 if (keyStoreFile
.length() == 0)
219 keyStoreFile
.delete();
220 log
.error("Cannot create keystore " + keyStoreFile
, e
);
223 throw new CmsException("Keystore " + keyStorePath
+ " already exists");