rsanchez
2014-12-17 06f5e0b58dd77a11519f5f3d8e3b9ffe99b3b291
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
package net.curisit.securis;
import java.io.File;
import java.io.IOException;
import java.io.UnsupportedEncodingException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.nio.file.StandardOpenOption;
import java.util.Date;
import java.util.HashMap;
import java.util.Map;
import net.curisit.securis.ConnectionManager.Command;
import net.curisit.securis.beans.LicenseBean;
import net.curisit.securis.beans.RequestBean;
import net.curisit.securis.beans.SignedLicenseBean;
import net.curisit.securis.beans.StatusBean;
import net.curisit.securis.utils.JsonUtils;
import net.curisit.securis.utils.Params;
import net.curisit.securis.utils.SignatureHelper;
import org.apache.commons.io.FileUtils;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
/**
 * Manage all licenses tasks, just like validation, renew, requesting, ...
 * 
 * @author roberto <roberto.sanchez@curisit.net>
 */
public class LicenseManager {
   private static final Logger LOG = LogManager.getLogger(License.class);
   private static LicenseManager singleton = new LicenseManager();
   public static final String PING_MESSAGE = "SeCuris API OK";
   public static final String HEADER_LICENSE_NAME_OR_REFERENCE = "X-SECURIS-LIC-NAMEREF";
   public static final String HEADER_LICENSE_EMAIL = "X-SECURIS-LIC-EMAIL";
   private LicenseManager() {}
   public static LicenseManager getInstance() {
       return singleton;
   }
   /**
    * Loads a license from file
    * 
    * @param licFile
    * @return The license bean
    * @throws SeCurisException
    */
   public LicenseBean load(File licFile) throws SeCurisException {
       LicenseBean licBean;
       try {
           licBean = JsonUtils.json2object(FileUtils.readFileToString(licFile), LicenseBean.class);
       } catch (IOException e) {
           throw new SeCurisException("Error getting license data from file: " + licFile, e);
       }
       return licBean;
   }
   /**
    * Validates the license stored in {@code licFile} and get the corresponding
    * LicenseBean
    * <p>
    * The validation includes:
    * <ul>
    * <li>Signature</li>
    * <li>HW data</li>
    * <li>Logo CRC</li>
    * </ul>
    * </p>
    * 
    * @param licFile
    * @return The license bean stored in file
    * @throws SeCurisException
    */
   public LicenseBean validateLicense(File licFile) throws SeCurisException {
       return validateLicense(licFile, false);
   }
   /**
    * Validates the license stored in {@code licFile} and get the corresponding
    * LicenseBean. The License date is not validated
    * <p>
    * The validation includes:
    * <ul>
    * <li>Signature</li>
    * <li>HW data</li>
    * <li>Logo CRC</li>
    * </ul>
    * </p>
    * 
    * @param licFile
    * @return The license bean stored in file
    * @throws SeCurisException
    */
   public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {
       LicenseBean licBean = load(licFile);
       SignatureHelper.getInstance().validateSignature(licBean);
       LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
               Params.get(Params.KEYS.PACK_CODE));
       LicenseValidator.getInstance().validateLogo(licBean);
       if (!excludeDateValidation) {
           if (new Date().after(licBean.getExpirationDate())) {
               throw new ExpiredLicenseException();
           }
       }
       return licBean;
   }
   /**
    * Request to server for a valid license
    * 
    * @return The license bean returned by the server
    * @throws SeCurisException
    */
   public SignedLicenseBean requestLicense(String nameOrReference, String email) throws SeCurisException {
       RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
               Params.get(Params.KEYS.PACK_CODE));
       SignedLicenseBean lic = requestLicenseToServer(req, nameOrReference, email);
       return lic;
   }
   /**
    * Generate a license file using a {@link LicenseBean}
    * 
    * @param license
    * @param file
    * @throws SeCurisException
    */
   public void save(LicenseBean license, File file) throws SeCurisException {
       SignedLicenseBean signedLic = new SignedLicenseBean(license);
       save(signedLic, file);
   }
   /**
    * Generate a license file using a {@link LicenseBean}
    * 
    * @param license
    * @param file
    * @throws SeCurisException
    */
   public void save(SignedLicenseBean signedLic, File file) throws SeCurisException {
       byte[] json;
       try {
           json = JsonUtils.toPrettyJSON(signedLic).getBytes("utf-8");
           Files.write(Paths.get(file.toURI()), json, StandardOpenOption.CREATE, StandardOpenOption.TRUNCATE_EXISTING);
       } catch (UnsupportedEncodingException e) {
           LOG.error("Error creating json doc from license: " + signedLic, e);
           throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
       } catch (IOException e) {
           LOG.error("Error creating license file: " + file, e);
           throw new SeCurisException("Error creating json doc from license: " + signedLic, e);
       }
       LOG.debug("License saved in {}", file);
   }
   private SignedLicenseBean requestLicenseToServer(RequestBean req, String nameOrReference, String email) throws SeCurisException {
       Map<String, String> headers = new HashMap<String, String>();
       headers.put(HEADER_LICENSE_NAME_OR_REFERENCE, nameOrReference);
       headers.put(HEADER_LICENSE_EMAIL, email);
       SignedLicenseBean lic = ConnectionManager.getInstance().executePost(Command.CREATE_LIC, SignedLicenseBean.class, req, headers);
       return lic;
   }
   /**
    * Creates a new request file with current hardware in the File passed as
    * parameter
    * 
    * @param outputRequestFile
    *            File where the request data will be saved
    * @return The generated request bean
    * @throws SeCurisException
    */
   public RequestBean createRequestFile(File outputRequestFile) throws SeCurisException {
       RequestBean req = ReqGenerator.getInstance().createRequest(Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE),
               Params.get(Params.KEYS.PACK_CODE));
       ReqGenerator.getInstance().save(req, outputRequestFile);
       return req;
   }
   /**
    * Send the current license file to server, which is previously validated,
    * to get a renewed one if it is prepared in server side.
    * 
    * @param licenseFile
    *            Current and valid License file
    * @return New license bean if server creates a new one, otherwise the same
    *         current License bean will be returned
    * @throws SeCurisException
    */
   public SignedLicenseBean renew(File licenseFile) throws SeCurisException {
       LicenseBean lic = validateLicense(licenseFile);
       SignedLicenseBean newLic = ConnectionManager.getInstance().executePost(Command.RENEW_LIC, SignedLicenseBean.class, lic);
       return newLic;
   }
   /**
    * Check on SeCuris server if current license is still valid in server DB.
    * 
    * @param licenseFile
    * @throws SeCurisException
    */
   public void assertLicenseIsValid(File licenseFile) throws SeCurisException, IOException {
       LicenseBean lic = validateLicense(licenseFile);
       // We need to snd the signed version to validate signature on server
       ConnectionManager.getInstance().executePost(Command.VALIDATE, LicenseBean.class, new SignedLicenseBean(lic));
   }
   public void testServer() throws SeCurisException {
       StatusBean status = ConnectionManager.getInstance().executeGet(Command.TEST, StatusBean.class);
       if (!PING_MESSAGE.equals(status.getMessage())) {
           throw new SeCurisException("SeCuris Server is not running in given URL");
       }
   }
   public static void main(String[] args) throws SeCurisException {
       String filename = null;
       if (filename == null)
           filename = "./license.req";
       File file = new File(filename);
       LicenseManager.getInstance().createRequestFile(file);
       LOG.info("Request file {} generated OK", file.getAbsolutePath());
   }
}