1
+ #! /usr/bin/groovy
2
+ import org.apache.commons.io.FileUtils
3
+ import org.apache.commons.io.filefilter.PrefixFileFilter
4
+ import org.apache.commons.io.filefilter.TrueFileFilter
5
+
6
+ import java.security.MessageDigest
7
+
8
+ /**
9
+ * Short name of the product to configure.
10
+ */
11
+ ProductName = ' Silverpeas-Spnego' ;
12
+
13
+ /**
14
+ * Path of the directory in which the libraries of the new product version are available.
15
+ */
16
+ SourceLibsPath = " ${ SILVERPEAS_HOME} /repository/java" ;
17
+
18
+ /**
19
+ * The jarfile in the common/lib that will serve as reference jar to compare the version
20
+ * between the product carried by the installer and the actual one in JBoss.
21
+ */
22
+
23
+ ReferenceLibDescriptors = [[
24
+ mavenArtifactIds : " spnego-r8" ,
25
+ webSpnegoHttpFilter : " net.sourceforge.spnego.SpnegoHttpFilter"
26
+ ], [
27
+ mavenArtifactIds : " silverpeas-spnego" ,
28
+ webSpnegoHttpFilter : " org.silverpeas.spnego.SpnegoHttpFilter"
29
+ ]];
30
+
31
+ /**
32
+ * Below are defined the descriptor of the common library to configure.
33
+ */
34
+
35
+ JBossCommonLibs = [
36
+ path : " common/lib"
37
+ ]
38
+
39
+ def checkServerHome (String serverHome ) {
40
+
41
+ if (serverHome == null || serverHome. trim(). isEmpty()) {
42
+ println " Missing parameter 'server home'"
43
+ System . exit(1 )
44
+ } else if (! new File (serverHome). exists()) {
45
+ println " The server home $serverHome doesn't exist"
46
+ System . exit(1 )
47
+ }
48
+
49
+ if (! Const . MODIFY_FILES )
50
+ println " WARNING: This is a dry run. No files modified!"
51
+
52
+ }
53
+
54
+ def printSpnegoConfigurationDetection () {
55
+ println " SSO with Spnego/Kerberos configuration has been detected"
56
+ println " Verifying version of ${ ProductName} library ..."
57
+ }
58
+
59
+ void die (String msg ) {
60
+ throw new RuntimeException (msg)
61
+ }
62
+
63
+ def getLibFrom (String path ) {
64
+ File givenPath = new File (path);
65
+ def libs = [];
66
+ ReferenceLibDescriptors . each { descriptor ->
67
+ FileUtils . listFiles(
68
+ givenPath,
69
+ new PrefixFileFilter (descriptor. mavenArtifactIds),
70
+ TrueFileFilter . TRUE ). each { lib ->
71
+ libs. add([
72
+ file : lib,
73
+ descriptor : descriptor
74
+ ])
75
+ };
76
+ }
77
+ if (libs. empty) {
78
+ return null ;
79
+ }
80
+ if (libs. size() > 1 ) {
81
+ def message = " ERROR: several Silverpeas Spnego librairies are detected in same environment:\n " ;
82
+ libs. each { lib ->
83
+ message + = " - " + lib. file. getPath() + " \n " ;
84
+ }
85
+ message + = " Please choose one of the above listed libraries in order to delete manually the other(s)" ;
86
+ die message;
87
+ }
88
+ return libs[0 ];
89
+ }
90
+
91
+ String md5sum (File file ) {
92
+ MessageDigest digest = MessageDigest . getInstance(" MD5" )
93
+ file. withInputStream() { input ->
94
+ byte [] buffer = new byte [8192 ]
95
+ int read;
96
+ while ((read = input. read(buffer)) > 0 ) {
97
+ digest. update(buffer, 0 , read);
98
+ }
99
+ }
100
+ byte [] md5sum = digest. digest()
101
+ return new BigInteger (1 , md5sum). toString(16 ). padLeft(32 , ' 0' )
102
+ }
103
+
104
+ def setUpBackup () {
105
+ BACKUP_DIR = new File (" ${ FileUtils.getTempDirectoryPath()} /${ ProductName} " )
106
+ RESTORE_ERROR = false
107
+ if (BACKUP_DIR . exists())
108
+ try {
109
+ FileUtils . forceDelete(BACKUP_DIR )
110
+ } catch (Exception ex) {
111
+ die " ERROR: cannot remove a previous temporary directory: ${ ex.message} "
112
+ }
113
+ if (! BACKUP_DIR . mkdirs())
114
+ die ' ERROR: cannot create temporary directory!'
115
+ }
116
+
117
+ def tearDownBackup () {
118
+ try {
119
+ FileUtils . forceDelete(BACKUP_DIR )
120
+ } catch (Exception ex) {
121
+ println " WARNING: ${ ex.message} "
122
+ }
123
+ }
124
+
125
+ def setupSpnegoHttpFilter (String serverHome , def oldLibDescriptor , def newLibDescriptor ) {
126
+ if (oldLibDescriptor. descriptor. webSpnegoHttpFilter != newLibDescriptor. descriptor. webSpnegoHttpFilter) {
127
+ File webFile = FileUtils . getFile(serverHome, " deploy/jbossweb.sar/web.xml" );
128
+ String content = FileUtils . readFileToString(webFile);
129
+ if (content. contains((String ) oldLibDescriptor. descriptor. webSpnegoHttpFilter)) {
130
+ FileUtils . write(webFile, content. replace(
131
+ (String ) oldLibDescriptor. descriptor. webSpnegoHttpFilter,
132
+ (String ) newLibDescriptor. descriptor. webSpnegoHttpFilter));
133
+ return true ;
134
+ }
135
+ }
136
+ return false ;
137
+ }
138
+
139
+ def configure () {
140
+ def sourceLibDesc;
141
+ def oldDestinationLibDesc;
142
+ try {
143
+ sourceLibDesc = getLibFrom SourceLibsPath ;
144
+ oldDestinationLibDesc = getLibFrom " ${ JBOSS_HOME} /${ JBossCommonLibs.path} " ;
145
+ } catch (Exception ex) {
146
+ printSpnegoConfigurationDetection();
147
+ println ex. message;
148
+ return 1 ;
149
+ }
150
+
151
+ if (oldDestinationLibDesc == null || ! oldDestinationLibDesc. file. isFile()) {
152
+ // No SSO Spnego configuration detected
153
+ return 0 ;
154
+ }
155
+
156
+ // SSO Spnego / Kerberos configuration has been detected
157
+ printSpnegoConfigurationDetection();
158
+
159
+ if (sourceLibDesc == null || ! sourceLibDesc. file. isFile()) {
160
+ println " ... error during the verification because the source library is missing or is not a file!" ;
161
+ println " (${ sourceLibDesc.file.getName} )" ;
162
+ return 1 ;
163
+ }
164
+
165
+ String currentMD5 = md5sum sourceLibDesc. file;
166
+ String newerMD5 = md5sum oldDestinationLibDesc. file;
167
+ if (currentMD5 != newerMD5) {
168
+ setUpBackup();
169
+
170
+ println " --- Backup of old library ${ oldDestinationLibDesc.file.getPath()} " ;
171
+ File oldDestinationLibBackup = FileUtils . getFile(BACKUP_DIR , oldDestinationLibDesc. file. getName());
172
+ try {
173
+ FileUtils . moveFile(oldDestinationLibDesc. file, oldDestinationLibBackup);
174
+ } catch (Exception ex) {
175
+ println " ... error during the backup!" ;
176
+ println ex. message;
177
+ return 1 ;
178
+ }
179
+
180
+ File destinationLib = FileUtils . getFile(oldDestinationLibDesc. file. getParentFile(), sourceLibDesc. file. getName());
181
+ println " --- Copying ${ sourceLibDesc.file.getPath()} into ${ destinationLib.getParentFile().getPath()} " ;
182
+ try {
183
+ FileUtils . copyFile(sourceLibDesc. file, destinationLib);
184
+ } catch (Exception ex) {
185
+ println " ... error during the copy!" ;
186
+ println ex. message;
187
+ println " Restoring the library backup" ;
188
+ FileUtils . copyFile(oldDestinationLibBackup, oldDestinationLibDesc. file);
189
+ return 1 ;
190
+ }
191
+
192
+ println " --- Applying right settings on SpnegoHttpFilter" ;
193
+ try {
194
+ if (setupSpnegoHttpFilter(JBOSS_SERVER , oldDestinationLibDesc, sourceLibDesc)) {
195
+ println " settings have been configured" ;
196
+ } else {
197
+ println " settings were already configured" ;
198
+ }
199
+ } catch (Exception ex) {
200
+ println " ... error during the setup of SpnegoHttpFilter!" ;
201
+ println ex. message;
202
+ println " Deleting the new library" ;
203
+ FileUtils . forceDelete(destinationLib);
204
+ println " Restoring the old library from the backup" ;
205
+ FileUtils . copyFile(oldDestinationLibBackup, oldDestinationLibDesc. file);
206
+ return 1 ;
207
+ }
208
+ println " --- Deleting the backup directory" ;
209
+ tearDownBackup();
210
+ println " SSO with Spnego/Kerberos configuration done\n " ;
211
+ } else {
212
+ println " ... the configuration is up to date." ;
213
+ }
214
+ return 0 ;
215
+ }
216
+
217
+ if (INSTALL_CONTEXT == ' install' ) {
218
+ checkServerHome(JBOSS_SERVER );
219
+ }
220
+
221
+ def returnExecutionCode = configure();
222
+ if (returnExecutionCode != 0 ) {
223
+ println " Stop the Silverpeas installation process" ;
224
+ println " Please checking carefully the Silverpeas installer settings and retry a full Silverpeas install" ;
225
+ System . exit(returnExecutionCode);
226
+ }
0 commit comments