rsanchez
2017-04-18 523edc2956a42bda7d33884fcbbb6018addda4b0
#3582 feature - Changed API to take in account pack data, (valid end
date, status, ...) and some minor fixes
6 files modified
changed files
securis/src/main/java/net/curisit/securis/db/User.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/ApiResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/LicenseResource.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/exception/SeCurisServiceException.java patch | view | blame | history
securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java patch | view | blame | history
securis/src/main/resources/db/initial_data.sql patch | view | blame | history
securis/src/main/java/net/curisit/securis/db/User.java
....@@ -295,7 +295,7 @@
295295 public static final int ADMIN = 0x02;
296296 public static final int BASIC = 0x04;
297297 public static final int API_CLIENT = 0x80;
298
- public static final int[] ALL = new int[] { ADVANCE, ADMIN, BASIC }; // ALL except the special API client
298
+ public static final int[] ALL = new int[] { ADVANCE, ADMIN, BASIC, API_CLIENT };
299299 }
300300
301301 }
securis/src/main/java/net/curisit/securis/services/ApiResource.java
....@@ -34,6 +34,7 @@
3434 import net.curisit.securis.db.LicenseHistory;
3535 import net.curisit.securis.db.LicenseStatus;
3636 import net.curisit.securis.db.Pack;
37
+import net.curisit.securis.db.PackStatus;
3738 import net.curisit.securis.db.User;
3839 import net.curisit.securis.db.User.Rol;
3940 import net.curisit.securis.ioc.EnsureTransaction;
....@@ -197,7 +198,9 @@
197198
198199 /**
199200 * License validation on server side, in this case we validate that the
200
- * current licenses has not been cancelled.
201
+ * current licenses has not been cancelled and they are still in valid
202
+ * period. If the pack has reached the end valid period, the license is no
203
+ * longer valid.
201204 *
202205 * @param currentLic
203206 * @param bsc
....@@ -219,12 +222,22 @@
219222 throw new SeCurisServiceException(ErrorCodes.LICENSE_IS_EXPIRED, "The license is expired");
220223 }
221224
225
+ License existingLic = licenseHelper.getActiveLicenseFromDB(currentLic, em);
226
+
227
+ Pack pack = existingLic.getPack();
228
+ if (pack.getEndValidDate().before(new Date())) {
229
+ throw new SeCurisServiceException(ErrorCodes.LICENSE_PACK_IS_NOT_VALID, "The pack end valid date has been reached");
230
+ }
231
+ if (pack.getStatus() != PackStatus.ACTIVE) {
232
+ LOG.error("The Pack {} status is not active, is: {}", pack.getCode(), pack.getStatus());
233
+ throw new SeCurisServiceException(ErrorCodes.LICENSE_PACK_IS_NOT_VALID, "The pack status is not Active");
234
+ }
235
+
222236 try {
223237 SignatureHelper.getInstance().validateSignature(currentLic);
224238 } catch (SeCurisException ex) {
225239 throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "The license signature is not valid");
226240 }
227
- licenseHelper.assertLicenseStatusIsActive(currentLic, em);
228241
229242 return Response.ok(currentLic).build();
230243 }
....@@ -353,6 +366,10 @@
353366 } else {
354367 pack = lic.getPack();
355368 }
369
+ if (pack.getStatus() != PackStatus.ACTIVE) {
370
+ LOG.error("The Pack {} status is not active, is: {}", pack.getCode(), pack.getStatus());
371
+ throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "The pack status is not Active");
372
+ }
356373 SignedLicenseBean signedLicense;
357374 try {
358375 String licCode;
securis/src/main/java/net/curisit/securis/services/LicenseResource.java
....@@ -392,6 +392,7 @@
392392 lic.setCreatedBy(createdBy);
393393 lic.setCreationTimestamp(new Date());
394394 lic.setModificationTimestamp(lic.getCreationTimestamp());
395
+ lic.setMetadataObsolete(false);
395396
396397 em.persist(lic);
397398 em.persist(licenseHelper.createLicenseHistoryAction(lic, createdBy, LicenseHistory.Actions.CREATE));
securis/src/main/java/net/curisit/securis/services/exception/SeCurisServiceException.java
....@@ -4,46 +4,47 @@
44
55 public class SeCurisServiceException extends CurisException {
66
7
- private int errorCode = 0;
7
+ private int errorCode = 0;
88
9
- public SeCurisServiceException(int errorCode, String msg) {
10
- super(msg);
11
- this.errorCode = errorCode;
12
- }
9
+ public SeCurisServiceException(int errorCode, String msg) {
10
+ super(msg);
11
+ this.errorCode = errorCode;
12
+ }
1313
14
- public SeCurisServiceException(String msg) {
15
- super(msg);
16
- this.errorCode = ErrorCodes.UNEXPECTED_ERROR;
17
- }
14
+ public SeCurisServiceException(String msg) {
15
+ super(msg);
16
+ this.errorCode = ErrorCodes.UNEXPECTED_ERROR;
17
+ }
1818
19
- public int getStatus() {
20
- return errorCode;
21
- }
19
+ public int getStatus() {
20
+ return errorCode;
21
+ }
2222
23
- /**
23
+ /**
2424 *
2525 */
26
- private static final long serialVersionUID = 1L;
26
+ private static final long serialVersionUID = 1L;
2727
28
- public static class ErrorCodes {
29
- public static int UNEXPECTED_ERROR = 1000;
30
- public static int INVALID_CREDENTIALS = 1001;
31
- public static int UNAUTHORIZED_ACCESS = 1002;
32
- public static int NOT_FOUND = 1003;
33
- public static int INVALID_FORMAT = 1004;
34
- public static int WRONG_STATUS = 1005;
28
+ public static class ErrorCodes {
29
+ public static int UNEXPECTED_ERROR = 1000;
30
+ public static int INVALID_CREDENTIALS = 1001;
31
+ public static int UNAUTHORIZED_ACCESS = 1002;
32
+ public static int NOT_FOUND = 1003;
33
+ public static int INVALID_FORMAT = 1004;
34
+ public static int WRONG_STATUS = 1005;
3535
36
- public static int INVALID_LICENSE_REQUEST_DATA = 1100;
37
- public static int LICENSE_NOT_READY_FOR_RENEW = 1101;
38
- public static int LICENSE_DATA_IS_NOT_VALID = 1102;
39
- public static int LICENSE_IS_EXPIRED = 1103;
36
+ public static int INVALID_LICENSE_REQUEST_DATA = 1100;
37
+ public static int LICENSE_NOT_READY_FOR_RENEW = 1101;
38
+ public static int LICENSE_DATA_IS_NOT_VALID = 1102;
39
+ public static int LICENSE_IS_EXPIRED = 1103;
40
+ public static int LICENSE_PACK_IS_NOT_VALID = 1104;
4041
41
- public static int INVALID_REQUEST_DATA = 1201;
42
- public static int INVALID_REQUEST_DATA_FORMAT = 1202;
43
- public static int BLOCKED_REQUEST_DATA = 1203;
44
- public static int DUPLICATED_REQUEST_DATA = 1204;
45
- public static int NO_AVAILABLE_LICENSES = 1205;
42
+ public static int INVALID_REQUEST_DATA = 1201;
43
+ public static int INVALID_REQUEST_DATA_FORMAT = 1202;
44
+ public static int BLOCKED_REQUEST_DATA = 1203;
45
+ public static int DUPLICATED_REQUEST_DATA = 1204;
46
+ public static int NO_AVAILABLE_LICENSES = 1205;
4647
47
- public static int INVALID_DATA = 1301;
48
- }
48
+ public static int INVALID_DATA = 1301;
49
+ }
4950 }
securis/src/main/java/net/curisit/securis/services/helpers/LicenseHelper.java
....@@ -13,6 +13,11 @@
1313 import javax.persistence.EntityManager;
1414 import javax.persistence.TypedQuery;
1515
16
+import org.apache.commons.io.FileUtils;
17
+import org.apache.logging.log4j.LogManager;
18
+import org.apache.logging.log4j.Logger;
19
+
20
+import net.curisit.integrity.exception.CurisRuntimeException;
1621 import net.curisit.securis.beans.LicenseBean;
1722 import net.curisit.securis.db.License;
1823 import net.curisit.securis.db.LicenseHistory;
....@@ -24,128 +29,128 @@
2429 import net.curisit.securis.services.exception.SeCurisServiceException;
2530 import net.curisit.securis.services.exception.SeCurisServiceException.ErrorCodes;
2631
27
-import org.apache.commons.io.FileUtils;
28
-import org.apache.logging.log4j.LogManager;
29
-import org.apache.logging.log4j.Logger;
30
-
3132 @ApplicationScoped
3233 public class LicenseHelper {
3334
34
- @SuppressWarnings("unused")
35
- private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
36
- private static final long MS_PER_DAY = 24L * 3600L * 1000L;
37
- private static final int DEFAULT_VALID_LIC_PERIOD = 7;
35
+ @SuppressWarnings("unused")
36
+ private static final Logger LOG = LogManager.getLogger(LicenseHelper.class);
37
+ private static final long MS_PER_DAY = 24L * 3600L * 1000L;
3838
39
- @Inject
40
- private UserHelper userHelper;
39
+ @Inject
40
+ private UserHelper userHelper;
4141
42
- /**
43
- * Cancel the license
44
- *
45
- * @param lic
46
- * @param em
47
- */
48
- public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
49
- lic.setStatus(LicenseStatus.CANCELLED);
50
- lic.setCancelledById(bsc.getUserPrincipal().getName());
51
- lic.setModificationTimestamp(new Date());
52
- em.persist(lic);
42
+ /**
43
+ * Cancel the license
44
+ *
45
+ * @param lic
46
+ * @param em
47
+ */
48
+ public void cancelLicense(License lic, String reason, BasicSecurityContext bsc, EntityManager em) throws SeCurisServiceException {
49
+ lic.setStatus(LicenseStatus.CANCELLED);
50
+ lic.setCancelledById(bsc.getUserPrincipal().getName());
51
+ lic.setModificationTimestamp(new Date());
52
+ em.persist(lic);
5353
54
- em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
55
- }
54
+ em.persist(createLicenseHistoryAction(lic, userHelper.getUser(bsc, em), LicenseHistory.Actions.CANCEL, "Cancellation reason: " + reason));
55
+ }
5656
57
- /**
58
- * Validates that the passed license is still valid
59
- *
60
- * @param lic
61
- * @param reason
62
- * @param bsc
63
- * @param em
64
- * @throws SeCurisServiceException
65
- */
66
- public void assertLicenseStatusIsActive(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
67
- License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
68
- if (lic == null) {
69
- throw new SeCurisServiceException(ErrorCodes.NOT_FOUND, "Current license code doesn't exist");
70
- }
71
- if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
72
- throw new SeCurisServiceException(ErrorCodes.INVALID_DATA, "Current license in not active");
73
- }
74
- }
57
+ /**
58
+ * Validates that the passed license exists and is still valid
59
+ *
60
+ * @param licBean
61
+ * @param em
62
+ * @return The License instance in DB
63
+ * @throws SeCurisServiceException
64
+ */
65
+ public License getActiveLicenseFromDB(LicenseBean licBean, EntityManager em) throws SeCurisServiceException {
66
+ License lic = License.findLicenseByCode(licBean.getLicenseCode(), em);
67
+ if (lic == null) {
68
+ throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license code doesn't exist");
69
+ }
70
+ if (lic.getStatus() != LicenseStatus.ACTIVE && lic.getStatus() != LicenseStatus.PRE_ACTIVE) {
71
+ throw new SeCurisServiceException(ErrorCodes.LICENSE_DATA_IS_NOT_VALID, "Current license in not active");
72
+ }
73
+ return lic;
74
+ }
7575
76
- public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
77
- LicenseHistory lh = new LicenseHistory();
78
- lh.setLicense(lic);
79
- lh.setUser(user);
80
- lh.setCreationTimestamp(new Date());
81
- lh.setAction(action);
82
- lh.setComments(comments);
83
- return lh;
84
- }
76
+ public LicenseHistory createLicenseHistoryAction(License lic, User user, String action, String comments) {
77
+ LicenseHistory lh = new LicenseHistory();
78
+ lh.setLicense(lic);
79
+ lh.setUser(user);
80
+ lh.setCreationTimestamp(new Date());
81
+ lh.setAction(action);
82
+ lh.setComments(comments);
83
+ return lh;
84
+ }
8585
86
- public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
87
- return createLicenseHistoryAction(lic, user, action, null);
88
- }
86
+ public LicenseHistory createLicenseHistoryAction(License lic, User user, String action) {
87
+ return createLicenseHistoryAction(lic, user, action, null);
88
+ }
8989
90
- /**
91
- * Create a license file in a temporary directory
92
- *
93
- * @param lic
94
- * @param licFileName
95
- * @return
96
- * @throws IOException
97
- */
98
- public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
99
- File f = Files.createTempDirectory("securis-server").toFile();
100
- f = new File(f, licFileName);
101
- FileUtils.writeStringToFile(f, lic.getLicenseData());
102
- return f;
103
- }
90
+ /**
91
+ * Create a license file in a temporary directory
92
+ *
93
+ * @param lic
94
+ * @param licFileName
95
+ * @return
96
+ * @throws IOException
97
+ */
98
+ public File createTemporaryLicenseFile(License lic, String licFileName) throws IOException {
99
+ File f = Files.createTempDirectory("securis-server").toFile();
100
+ f = new File(f, licFileName);
101
+ FileUtils.writeStringToFile(f, lic.getLicenseData());
102
+ return f;
103
+ }
104104
105
- public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
106
- Map<String, Object> metadata = new HashMap<>();
107
- for (PackMetadata md : packMetadata) {
108
- metadata.put(md.getKey(), md.getValue());
109
- }
105
+ public Map<String, Object> extractPackMetadata(Set<PackMetadata> packMetadata) {
106
+ Map<String, Object> metadata = new HashMap<>();
107
+ for (PackMetadata md : packMetadata) {
108
+ metadata.put(md.getKey(), md.getValue());
109
+ }
110110
111
- return metadata;
112
- }
111
+ return metadata;
112
+ }
113113
114
- /**
115
- * If the action is a renew the expiration date is got form pack end valid
116
- * date, if the action is a pre-activation the expiration date is calculated
117
- * using the pack default valid period
118
- *
119
- * @param pack
120
- * @param isPreActivation
121
- * @return
122
- */
123
- public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
124
- Long validPeriod;
125
- if (isPreActivation) {
126
- validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
127
- } else {
128
- if (pack.getRenewValidPeriod() <= 0) {
129
- return pack.getEndValidDate();
130
- }
131
- validPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
132
- }
133
- Date expirationDate = new Date(new Date().getTime() + validPeriod);
134
- return expirationDate;
135
- }
114
+ /**
115
+ * If the action is a renew the expiration date is got form pack end valid
116
+ * date, if the action is a pre-activation the expiration date is calculated
117
+ * using the pack default valid period
118
+ *
119
+ * @param pack
120
+ * @param isPreActivation
121
+ * @return
122
+ */
123
+ public Date getExpirationDateFromPack(Pack pack, boolean isPreActivation) {
124
+ Long validPeriod;
125
+ if (pack.getEndValidDate().before(new Date())) {
126
+ throw new CurisRuntimeException("Pack end valid period is reached, no new licenses can be activated.");
127
+ }
128
+ if (isPreActivation) {
129
+ validPeriod = pack.getPreactivationValidPeriod() * MS_PER_DAY;
130
+ } else {
131
+ if (pack.getRenewValidPeriod() <= 0) {
132
+ return pack.getEndValidDate();
133
+ }
134
+ long renewPeriod = pack.getRenewValidPeriod() * MS_PER_DAY;
135
+ long expirationPeriod = pack.getEndValidDate().getTime() - new Date().getTime();
136
+ validPeriod = renewPeriod < expirationPeriod ? renewPeriod : expirationPeriod;
137
+ }
138
+ Date expirationDate = new Date(new Date().getTime() + validPeriod);
139
+ return expirationDate;
140
+ }
136141
137
- /**
138
- * Get the next free code suffis for a given Pack
139
- *
140
- * @param packId
141
- * @param em
142
- * @return
143
- */
144
- public int getNextCodeSuffix(int packId, EntityManager em) {
145
- TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
146
- query.setParameter("packId", packId);
147
- Integer lastCodeSuffix = query.getSingleResult();
148
- return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
149
- }
142
+ /**
143
+ * Get the next free code suffis for a given Pack
144
+ *
145
+ * @param packId
146
+ * @param em
147
+ * @return
148
+ */
149
+ public int getNextCodeSuffix(int packId, EntityManager em) {
150
+ TypedQuery<Integer> query = em.createNamedQuery("last-code-suffix-used-in-pack", Integer.class);
151
+ query.setParameter("packId", packId);
152
+ Integer lastCodeSuffix = query.getSingleResult();
153
+ return lastCodeSuffix == null ? 1 : lastCodeSuffix + 1;
154
+ }
150155
151156 }
securis/src/main/resources/db/initial_data.sql
....@@ -2,4 +2,4 @@
22
33 #Password: securis
44 insert into user (username, password, roles, first_name, last_name, creation_timestamp) values ('admin', '64f170fd736a2d4658fa87abde12043009d2554636c397032d57d71aea8556e9', 2, 'Administrator', null, now());
5
-insert into user (username, password, roles, first_name, last_name, creation_timestamp) values ('_client', '64f170fd736a2d4658fa87abde12043009d2554636c397032d57d71aea8556e9', 0, 'SeCuris client user', null, now());
5
+insert into user (username, password, roles, first_name, last_name, creation_timestamp) values ('_client', '64f170fd736a2d4658fa87abde12043009d2554636c397032d57d71aea8556e9', 128, 'SeCuris client user', null, now());