| .. | .. |
|---|
| 28 | 28 | import net.curisit.integrity.commons.JsonUtils; |
|---|
| 29 | 29 | import net.curisit.integrity.commons.Utils; |
|---|
| 30 | 30 | import net.curisit.securis.DefaultExceptionHandler; |
|---|
| 31 | | -import net.curisit.securis.LicenseGenerator; |
|---|
| 32 | | -import net.curisit.securis.ReqGenerator; |
|---|
| 33 | 31 | import net.curisit.securis.SeCurisException; |
|---|
| 34 | | -import net.curisit.securis.beans.LicenseBean; |
|---|
| 35 | 32 | import net.curisit.securis.beans.RequestBean; |
|---|
| 36 | 33 | import net.curisit.securis.db.License; |
|---|
| 37 | 34 | import net.curisit.securis.db.LicenseHistory; |
|---|
| 38 | | -import net.curisit.securis.db.LicenseType; |
|---|
| 39 | 35 | import net.curisit.securis.db.Pack; |
|---|
| 40 | 36 | import net.curisit.securis.db.User; |
|---|
| 41 | 37 | import net.curisit.securis.security.BasicSecurityContext; |
|---|
| .. | .. |
|---|
| 46 | 42 | import org.apache.commons.io.IOUtils; |
|---|
| 47 | 43 | import org.apache.logging.log4j.LogManager; |
|---|
| 48 | 44 | import org.apache.logging.log4j.Logger; |
|---|
| 49 | | -import org.bouncycastle.jce.provider.asymmetric.ec.ECUtil; |
|---|
| 50 | | -import org.jboss.resteasy.plugins.providers.multipart.MultipartFormDataInput; |
|---|
| 51 | 45 | |
|---|
| 52 | 46 | import com.google.inject.persist.Transactional; |
|---|
| 53 | 47 | |
|---|
| .. | .. |
|---|
| 67 | 61 | |
|---|
| 68 | 62 | @Inject |
|---|
| 69 | 63 | Provider<EntityManager> emProvider; |
|---|
| 70 | | - |
|---|
| 71 | | - @Inject |
|---|
| 72 | | - LicenseGenerator licenseGenerator; |
|---|
| 73 | 64 | |
|---|
| 74 | 65 | /** |
|---|
| 75 | 66 | * |
|---|
| .. | .. |
|---|
| 290 | 281 | return Response.ok(lic).build(); |
|---|
| 291 | 282 | } |
|---|
| 292 | 283 | |
|---|
| 293 | | - /** |
|---|
| 294 | | - * Extract the corresponding metadata for the Request license given |
|---|
| 295 | | - * @param req |
|---|
| 296 | | - * @return |
|---|
| 297 | | - * @throws SeCurisException |
|---|
| 298 | | - */ |
|---|
| 299 | | - @SuppressWarnings("unchecked") |
|---|
| 300 | | - private Map<String, Object> getLicenseMetadata(RequestBean req) throws SeCurisException { |
|---|
| 301 | | - // TODO: The dummy metadata file is temporal, this info should be read from DB |
|---|
| 302 | | - File dummyMetadata = new File(System.getProperty("user.home") + File.separator + ".SeCuris" + File.separator + "dummy_metadata.json"); |
|---|
| 303 | | - Map<String, Object> metadata = null; |
|---|
| 304 | | - try { |
|---|
| 305 | | - String metadataJson = IOUtils.toString(dummyMetadata.toURI()); |
|---|
| 306 | | - metadata = (Map<String, Object>)JsonUtils.json2map(metadataJson).get(req.getAppCode()); |
|---|
| 307 | | - if (metadata == null) |
|---|
| 308 | | - throw new SeCurisException("App code in request is unknown: " + req.getAppCode()); |
|---|
| 309 | | - metadata = new TreeMap<>(metadata); |
|---|
| 310 | | - } catch (IOException e) { |
|---|
| 311 | | - LOG.error("Error reading dummy metadata file", e); |
|---|
| 312 | | - throw new SeCurisException("Error reading dummy metadata file"); |
|---|
| 313 | | - } |
|---|
| 314 | | - |
|---|
| 315 | | - return metadata; |
|---|
| 316 | | - } |
|---|
| 317 | | - |
|---|
| 318 | | - private License getLicenseData(RequestBean req) throws SeCurisException { |
|---|
| 319 | | - // TODO: The dummy expiration date is temporal, this info should be read from DB |
|---|
| 320 | | - License lic = new License(); |
|---|
| 321 | | - lic.setExpirationDate(new Date(new Date().getTime() + (1000L * 3600 * 24 * 365 * 10))); |
|---|
| 322 | | - lic.setCode(req.getAppCode() + "-LIC-INTERNAL"); |
|---|
| 323 | | - LicenseType lt = new LicenseType(); |
|---|
| 324 | | - lt.setCode("TYPE-" + req.getAppCode()); |
|---|
| 325 | | - Pack pack = new Pack(); |
|---|
| 326 | | - pack.setLicenseType(lt); |
|---|
| 327 | | - lic.setPack(pack); |
|---|
| 328 | | - return lic; |
|---|
| 329 | | - } |
|---|
| 330 | | - |
|---|
| 331 | | - /** |
|---|
| 332 | | - * Request a new license file based in a RequestBean object sent as parameter |
|---|
| 333 | | - * @param mpfdi |
|---|
| 334 | | - * @param bsc |
|---|
| 335 | | - * @return |
|---|
| 336 | | - * @throws IOException |
|---|
| 337 | | - * @throws SeCurisServiceException |
|---|
| 338 | | - */ |
|---|
| 339 | | - @POST |
|---|
| 340 | | - @Path("/request") |
|---|
| 341 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 342 | | - //TODO: Enable this: @Securable |
|---|
| 343 | | - @Produces({ |
|---|
| 344 | | - MediaType.APPLICATION_JSON |
|---|
| 345 | | - }) |
|---|
| 346 | | - @Transactional |
|---|
| 347 | | - public Response createFromRequest(RequestBean request, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 348 | | - LOG.info("Request to get license: {}", request); |
|---|
| 349 | 284 | |
|---|
| 350 | | - Map<String, Object> metadata = getLicenseMetadata(request); |
|---|
| 351 | | - License licDB = getLicenseData(request); |
|---|
| 352 | | - |
|---|
| 353 | | - Date expirationDate = licDB.getExpirationDate(); |
|---|
| 354 | | - String licenseTypeCode = licDB.getPack().getLicenseType().getCode(); |
|---|
| 355 | | - String licenseCode = licDB.getCode(); |
|---|
| 356 | | - LicenseBean lic = licenseGenerator.generateLicense(request, metadata, expirationDate, licenseTypeCode, licenseCode); |
|---|
| 357 | | - return Response.ok(lic).build(); |
|---|
| 358 | | - } |
|---|
| 359 | | - |
|---|
| 360 | | - /** |
|---|
| 361 | | - * Returns a License file in JSON format from an uploaded Request file |
|---|
| 362 | | - * @param mpfdi |
|---|
| 363 | | - * @param bsc |
|---|
| 364 | | - * @return |
|---|
| 365 | | - * @throws IOException |
|---|
| 366 | | - * @throws SeCurisServiceException |
|---|
| 367 | | - * @throws SeCurisException |
|---|
| 368 | | - */ |
|---|
| 369 | | - @POST |
|---|
| 370 | | - @Path("/request") |
|---|
| 371 | | - @Consumes(MediaType.MULTIPART_FORM_DATA) |
|---|
| 372 | | - @Securable |
|---|
| 373 | | - @Produces({ |
|---|
| 374 | | - MediaType.APPLICATION_JSON |
|---|
| 375 | | - }) |
|---|
| 376 | | - @Transactional |
|---|
| 377 | | - @SuppressWarnings("unchecked") |
|---|
| 378 | | - public Response createFromRequestFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 379 | | - RequestBean req = new RequestBean(); |
|---|
| 380 | | - req.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null)); |
|---|
| 381 | | - req.setArch(mpfdi.getFormDataPart("arch", String.class, null)); |
|---|
| 382 | | - req.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null)); |
|---|
| 383 | | - req.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null)); |
|---|
| 384 | | - req.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null)); |
|---|
| 385 | | - req.setOsName(mpfdi.getFormDataPart("osName", String.class, null)); |
|---|
| 386 | | - |
|---|
| 387 | | - return createFromRequest(req, bsc); |
|---|
| 388 | | - } |
|---|
| 389 | | - |
|---|
| 390 | | - /** |
|---|
| 391 | | - * Create a new License file based in a previous one |
|---|
| 392 | | - * |
|---|
| 393 | | - * @param request |
|---|
| 394 | | - * @param bsc |
|---|
| 395 | | - * @return |
|---|
| 396 | | - * @throws IOException |
|---|
| 397 | | - * @throws SeCurisServiceException |
|---|
| 398 | | - * @throws SeCurisException |
|---|
| 399 | | - */ |
|---|
| 400 | | - @POST |
|---|
| 401 | | - @Path("/renew") |
|---|
| 402 | | - @Consumes(MediaType.APPLICATION_JSON) |
|---|
| 403 | | - //TODO: Enable this: @Securable |
|---|
| 404 | | - @Produces({ |
|---|
| 405 | | - MediaType.APPLICATION_JSON |
|---|
| 406 | | - }) |
|---|
| 407 | | - @Transactional |
|---|
| 408 | | - public Response renewFromPreviousLicense(LicenseBean previousLic, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 409 | | - LOG.info("Request to get license: {}", previousLic); |
|---|
| 410 | | - |
|---|
| 411 | | - Map<String, Object> metadata = getLicenseMetadata(previousLic); |
|---|
| 412 | | - License licDB = getLicenseData(previousLic); |
|---|
| 413 | | - |
|---|
| 414 | | - Date expirationDate = licDB.getExpirationDate(); |
|---|
| 415 | | - String licenseTypeCode = licDB.getPack().getLicenseType().getCode(); |
|---|
| 416 | | - String licenseCode = licDB.getCode(); |
|---|
| 417 | | - LicenseBean lic = licenseGenerator.generateLicense(previousLic, metadata, expirationDate, licenseTypeCode, licenseCode); |
|---|
| 418 | | - return Response.ok(lic).build(); |
|---|
| 419 | | - } |
|---|
| 420 | | - |
|---|
| 421 | | - /** |
|---|
| 422 | | - * Returns a new License file in JSON format based in a previous license |
|---|
| 423 | | - * @param mpfdi |
|---|
| 424 | | - * @param bsc |
|---|
| 425 | | - * @return |
|---|
| 426 | | - * @throws IOException |
|---|
| 427 | | - * @throws SeCurisServiceException |
|---|
| 428 | | - * @throws SeCurisException |
|---|
| 429 | | - */ |
|---|
| 430 | | - @POST |
|---|
| 431 | | - @Path("/renew") |
|---|
| 432 | | - @Consumes(MediaType.MULTIPART_FORM_DATA) |
|---|
| 433 | | - @Securable |
|---|
| 434 | | - @Produces({ |
|---|
| 435 | | - MediaType.APPLICATION_JSON |
|---|
| 436 | | - }) |
|---|
| 437 | | - @Transactional |
|---|
| 438 | | - @SuppressWarnings("unchecked") |
|---|
| 439 | | - public Response renewFromLicenseFile(MultipartFormDataInput mpfdi, @Context BasicSecurityContext bsc) throws IOException, SeCurisServiceException, SeCurisException { |
|---|
| 440 | | - LicenseBean lic = new LicenseBean(); |
|---|
| 441 | | - // TODO: Add more license parameters |
|---|
| 442 | | - lic.setAppCode(mpfdi.getFormDataPart("appCode", String.class, null)); |
|---|
| 443 | | - lic.setArch(mpfdi.getFormDataPart("arch", String.class, null)); |
|---|
| 444 | | - lic.setCrcLogo(mpfdi.getFormDataPart("crcLogo", String.class, null)); |
|---|
| 445 | | - lic.setCustomerCode(mpfdi.getFormDataPart("customerCode", String.class, null)); |
|---|
| 446 | | - lic.setMacAddresses(mpfdi.getFormDataPart("macAddresses", List.class, null)); |
|---|
| 447 | | - lic.setOsName(mpfdi.getFormDataPart("osName", String.class, null)); |
|---|
| 448 | | - |
|---|
| 449 | | - return createFromRequest(lic, bsc); |
|---|
| 450 | | - } |
|---|
| 451 | | - |
|---|
| 452 | | - |
|---|
| 453 | | - |
|---|
| 454 | 285 | |
|---|
| 455 | 286 | @PUT |
|---|
| 456 | 287 | @POST |
|---|