src/main/java/net/curisit/securis/ConnectionManager.java
.. .. @@ -1,5 +1,6 @@ 1 1 package net.curisit.securis; 2 2 3 +import java.awt.PageAttributes.MediaType;3 4 import java.io.IOException; 4 5 import java.io.UnsupportedEncodingException; 5 6 import java.security.KeyManagementException; .. .. @@ -8,6 +9,8 @@ 8 9 import java.security.NoSuchAlgorithmException; 9 10 import java.security.cert.CertificateException; 10 11 import java.security.cert.X509Certificate; 12 +13 +import javax.activation.MimeType;11 14 12 15 import net.curisit.securis.beans.RequestBean; 13 16 import net.curisit.securis.utils.JsonUtils; .. .. @@ -34,6 +37,10 @@ 34 37 public class ConnectionManager { 35 38 36 39 private static final Logger LOG = LogManager.getLogger(ConnectionManager.class); 40 + private static final int HTTP_STATUS_APP_ERRROR = 418;41 + private static final String JSON_MEDIA_TYPE= "application/json";42 + private static final String ERROR_MESSAGE_HEADER = "X-SECURIS-ERROR-MSG";43 + private static final String ERROR_CODE_MESSAGE_HEADER = "X-SECURIS-ERROR-CODE";37 44 38 45 private static ConnectionManager singleton; 39 46 .. .. @@ -78,8 +85,9 @@ 78 85 79 86 public <T> T executePost(String command, Class<T> returnType, RequestBean req) throws SeCurisException { 80 87 HttpPost postRequest = new HttpPost(String.format("%s/%s", serverUrl, command)); 81 - postRequest.addHeader("accept", "application/json");82 - postRequest.addHeader("content-type", "application/json");88 + postRequest.addHeader("accept", JSON_MEDIA_TYPE);89 +90 + postRequest.addHeader("content-type", JSON_MEDIA_TYPE);83 91 try { 84 92 postRequest.setEntity(new StringEntity(JsonUtils.toJSON(req))); 85 93 } catch (UnsupportedEncodingException | SeCurisException e1) { .. .. @@ -88,27 +96,38 @@ 88 96 HttpResponse response; 89 97 try { 90 98 response = httpClient.execute(postRequest); 91 - if (response.getStatusLine().getStatusCode() != 200) {92 - throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());93 - }99 +100 + checkErrors(command, response);101 +94 102 String jsonLic = IOUtils.toString(response.getEntity().getContent()); 95 - LOG.info("License read OK: {}", jsonLic);103 + LOG.debug("Response content read OK: {}", jsonLic);96 104 T responseBean = JsonUtils.json2object(jsonLic, returnType); 97 105 98 - LOG.info("Response bean read OK: {}", responseBean);99 - LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));106 + LOG.debug("Response bean read OK: {}", responseBean);100 107 101 108 return responseBean; 102 109 } catch (IOException e) { 103 - LOG.error("Error acessing SeCuris server", e);110 + LOG.error("Error accessing SeCuris server", e);104 111 throw new SeCurisException("Error accessing SeCuris server"); 105 112 } 106 113 } 107 114 115 + private void checkErrors(String command, HttpResponse response) throws SeCurisException {116 + if (response.getStatusLine().getStatusCode() != 200) {117 + if (response.getStatusLine().getStatusCode() == HTTP_STATUS_APP_ERRROR) {118 + String errorCode = response.getFirstHeader(ERROR_CODE_MESSAGE_HEADER).getValue();119 + String errorMsg = response.getFirstHeader(ERROR_MESSAGE_HEADER).getValue();120 + throw new SeCurisException(String.format("[%s] - %s", errorCode, errorMsg));121 + }122 + LOG.error("Unexpected error executing {}, Reason: {}", command, response.getStatusLine().getReasonPhrase());123 + throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode());124 + }125 +126 + }108 127 109 128 public <T> T executeGet(String command, Class<T> returnType) throws SeCurisException { 110 129 HttpGet getRequest = new HttpGet(String.format("%s/%s", serverUrl, command)); 111 - getRequest.addHeader("accept", "application/json");130 + getRequest.addHeader("accept", JSON_MEDIA_TYPE);112 131 113 132 HttpResponse response; 114 133 try { .. .. @@ -117,11 +136,10 @@ 117 136 throw new SeCurisException("Error executing command " + command + ", status: " + response.getStatusLine().getStatusCode()); 118 137 } 119 138 String jsonLic = IOUtils.toString(response.getEntity().getContent()); 120 - LOG.info("License read OK: {}", jsonLic);139 + LOG.debug("Response content read OK: {}", jsonLic);121 140 T responseBean = JsonUtils.json2object(jsonLic, returnType); 122 141 123 - LOG.info("Response bean read OK: {}", responseBean);124 - LOG.info("JSON to write in file: {}", JsonUtils.toJSON(responseBean));142 + LOG.debug("Response bean read OK: {}", responseBean);125 143 126 144 return responseBean; 127 145 } catch (IOException e) { src/main/java/net/curisit/securis/License.java
.. .. @@ -73,6 +73,9 @@ 73 73 if (filename == null) 74 74 filename = "./license.lic"; 75 75 File file = new File(filename); 76 + if (!file.exists()) {77 + throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());78 + }76 79 try { 77 80 LicenseManager.getInstance().validateLicense(file); 78 81 LOG.info("License file {} is valid", file.getAbsolutePath()); .. .. @@ -86,7 +89,7 @@ 86 89 87 90 if (cmd.hasOption('c')) { 88 91 SignedLicenseBean lic = LicenseManager.getInstance().requestLicense(); 89 - String filename = cmd.getOptionValue("lic_file");92 + String filename = cmd.getOptionValue("c");90 93 if (filename == null) 91 94 filename = "./license.lic"; 92 95 File file = new File(filename); .. .. @@ -105,7 +108,16 @@ 105 108 if (cmd.hasOption('r')) { 106 109 String licFilename = cmd.getOptionValue("renew"); 107 110 checkMandatoryParameter(licFilename, "renew"); 108 - LOG.warn("This command is not yet implemented");111 + File file = new File(licFilename);112 + if (!file.exists()) {113 + throw new SeCurisException("The license file doesn't exist: " + file.getAbsolutePath());114 + }115 + SignedLicenseBean newLic = LicenseManager.getInstance().renew(file);116 + File oldLicFile = new File(file.getAbsoluteFile() + ".old");117 + file.renameTo(oldLicFile);118 + LOG.info("Old license file has been renamed to: {}", oldLicFile.getAbsolutePath());119 + LicenseManager.getInstance().save(newLic, file);120 + LOG.info("New license file saved as: {}", file.getAbsolutePath());109 121 System.exit(0); 110 122 } 111 123 src/main/java/net/curisit/securis/LicenseManager.java
.. .. @@ -6,6 +6,7 @@ 6 6 import java.nio.file.Files; 7 7 import java.nio.file.Paths; 8 8 import java.nio.file.StandardOpenOption; 9 +import java.util.Date;9 10 10 11 import net.curisit.securis.ConnectionManager.Command; 11 12 import net.curisit.securis.beans.LicenseBean; .. .. @@ -57,29 +58,55 @@ 57 58 return licBean; 58 59 } 59 60 60 - /**61 - * Validates the license stored in {@code licFile} and get the corresponding LicenseBean62 - * <p>63 - * The validation includes:64 - * <ul>65 - * <li>Signature</li>66 - * <li>HW data</li>67 - * <li>Logo CRC</li>68 - * </ul>69 - * </p>70 - *71 - * @param licFile72 - * @return The license bean stored in file73 - * @throws SeCurisException74 - */75 - public LicenseBean validateLicense(File licFile) throws SeCurisException {76 - LicenseBean licBean = load(licFile);77 - SignatureHelper.getInstance().validateSignature(licBean);78 - LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));79 - LicenseValidator.getInstance().validateLogo(licBean);61 + /**62 + * Validates the license stored in {@code licFile} and get the corresponding LicenseBean63 + * <p>64 + * The validation includes:65 + * <ul>66 + * <li>Signature</li>67 + * <li>HW data</li>68 + * <li>Logo CRC</li>69 + * </ul>70 + * </p>71 + *72 + * @param licFile73 + * @return The license bean stored in file74 + * @throws SeCurisException75 + */76 + public LicenseBean validateLicense(File licFile) throws SeCurisException {80 77 81 - return licBean;82 - }78 + return validateLicense(licFile, false);79 + }80 +81 + /**82 + * Validates the license stored in {@code licFile} and get the corresponding LicenseBean. The License date is not validated83 + * <p>84 + * The validation includes:85 + * <ul>86 + * <li>Signature</li>87 + * <li>HW data</li>88 + * <li>Logo CRC</li>89 + * </ul>90 + * </p>91 + *92 + * @param licFile93 + * @return The license bean stored in file94 + * @throws SeCurisException95 + */96 + public LicenseBean validateLicense(File licFile, boolean excludeDateValidation) throws SeCurisException {97 + LicenseBean licBean = load(licFile);98 + SignatureHelper.getInstance().validateSignature(licBean);99 + LicenseValidator.getInstance().validateHW(licBean, Params.get(Params.KEYS.APPLICATION_CODE), Params.get(Params.KEYS.CUSTOMER_CODE));100 + LicenseValidator.getInstance().validateLogo(licBean);101 +102 + if (!excludeDateValidation) {103 + if (new Date().after(licBean.getExpirationDate())) {104 + throw new SeCurisException("License has expired");105 + }106 + }107 +108 + return licBean;109 + }83 110 84 111 /** 85 112 * Request to server for a valid license