@Test public void createsSecret() throws Exception { when(secretController.getSecretById(secret.getId())).thenReturn(Optional.of(secret)); SecretController.SecretBuilder secretBuilder = mock(SecretController.SecretBuilder.class); when(secretController.builder(secret.getName(), secret.getSecret(), user.getName(), 0)) .thenReturn(secretBuilder); when(secretBuilder.create()).thenReturn(secret); CreateSecretRequestV2 req = CreateSecretRequestV2.builder() .name(secret.getName()) .description(secret.getDescription()) .content(secret.getSecret()) .build(); Response response = resource.createSecret(user, req); assertThat(response.getStatus()).isEqualTo(201); assertThat(response.getMetadata().get(HttpHeaders.LOCATION)) .containsExactly(new URI("/admin/secrets/" + secret.getId())); }
@Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request) { logger.info("User '{}' creating secret '{}'.", user, request.name()); Secret secret; try { SecretController.SecretBuilder builder = secretController.builder(request.name(), request.content(), user.getName(), request.expiry()); if (request.description() != null) { builder.withDescription(request.description()); } if (request.metadata() != null) { builder.withMetadata(request.metadata()); } secret = builder.create(); } catch (DataAccessException e) { logger.info(format("Cannot create secret %s", request.name()), e); throw new ConflictException(format("Cannot create secret %s.", request.name())); } URI uri = UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId()); Response response = Response .created(uri) .entity(secretDetailResponseFromId(secret.getId())) .build(); if (response.getStatus() == HttpStatus.SC_CREATED) { Map<String, String> extraInfo = new HashMap<>(); if (request.description() != null) { extraInfo.put("description", request.description()); } if (request.metadata() != null) { extraInfo.put("metadata", request.metadata().toString()); } extraInfo.put("expiry", Long.toString(request.expiry())); auditLog.recordEvent( new Event(Instant.now(), EventTag.SECRET_CREATE, user.getName(), request.name(), extraInfo)); } return response; }
SecretsResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request) { logger.info("User '{}' creating secret '{}'.", user, request.name()); Secret secret; try { SecretController.SecretBuilder builder = secretController.builder(request.name(), request.content(), user.getName(), request.expiry()); if (request.description() != null) { builder.withDescription(request.description()); } if (request.metadata() != null) { builder.withMetadata(request.metadata()); } secret = builder.create(); } catch (DataAccessException e) { logger.info(format("Cannot create secret %s", request.name()), e); throw new ConflictException(format("Cannot create secret %s.", request.name())); } URI uri = UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId()); Response response = Response .created(uri) .entity(secretDetailResponseFromId(secret.getId())) .build(); if (response.getStatus() == HttpStatus.SC_CREATED) { Map<String, String> extraInfo = new HashMap<>(); if (request.description() != null) { extraInfo.put("description", request.description()); } if (request.metadata() != null) { extraInfo.put("metadata", request.metadata().toString()); } extraInfo.put("expiry", Long.toString(request.expiry())); auditLog.recordEvent( new Event(Instant.now(), EventTag.SECRET_CREATE, user.getName(), request.name(), extraInfo)); } return response; } }
SecretsResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request) { logger.info("User '{}' creating secret '{}'.", user, request.name()); Secret secret; try { SecretController.SecretBuilder builder = secretController.builder(request.name(), request.content(), user.getName(), request.expiry()); if (request.description() != null) { builder.withDescription(request.description()); } if (request.metadata() != null) { builder.withMetadata(request.metadata()); } secret = builder.create(); } catch (DataAccessException e) { logger.info(format("Cannot create secret %s", request.name()), e); throw new ConflictException(format("Cannot create secret %s.", request.name())); } URI uri = UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId()); Response response = Response .created(uri) .entity(secretDetailResponseFromId(secret.getId())) .build(); if (response.getStatus() == HttpStatus.SC_CREATED) { Map<String, String> extraInfo = new HashMap<>(); if (request.description() != null) { extraInfo.put("description", request.description()); } if (request.metadata() != null) { extraInfo.put("metadata", request.metadata().toString()); } extraInfo.put("expiry", Long.toString(request.expiry())); auditLog.recordEvent( new Event(Instant.now(), EventTag.SECRET_CREATE, user.getName(), request.name(), extraInfo)); } return response; } @SuppressWarnings("unused") @Inject  SecretsResource(SecretController secretController, AclDAOFactory aclDAOFactory,
      SecretDAOFactory secretDAOFactory, AuditLog auditLog); @VisibleForTesting  SecretsResource(SecretController secretController, AclDAO aclDAOReadOnly,
      SecretDAO secretDAOReadWrite, AuditLog auditLog); }
SecretsResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request) { logger.info("User '{}' creating secret '{}'.", user, request.name()); Secret secret; try { SecretController.SecretBuilder builder = secretController.builder(request.name(), request.content(), user.getName(), request.expiry()); if (request.description() != null) { builder.withDescription(request.description()); } if (request.metadata() != null) { builder.withMetadata(request.metadata()); } secret = builder.create(); } catch (DataAccessException e) { logger.info(format("Cannot create secret %s", request.name()), e); throw new ConflictException(format("Cannot create secret %s.", request.name())); } URI uri = UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId()); Response response = Response .created(uri) .entity(secretDetailResponseFromId(secret.getId())) .build(); if (response.getStatus() == HttpStatus.SC_CREATED) { Map<String, String> extraInfo = new HashMap<>(); if (request.description() != null) { extraInfo.put("description", request.description()); } if (request.metadata() != null) { extraInfo.put("metadata", request.metadata().toString()); } extraInfo.put("expiry", Long.toString(request.expiry())); auditLog.recordEvent( new Event(Instant.now(), EventTag.SECRET_CREATE, user.getName(), request.name(), extraInfo)); } return response; } @SuppressWarnings("unused") @Inject  SecretsResource(SecretController secretController, AclDAOFactory aclDAOFactory,
      SecretDAOFactory secretDAOFactory, AuditLog auditLog); @VisibleForTesting  SecretsResource(SecretController secretController, AclDAO aclDAOReadOnly,
      SecretDAO secretDAOReadWrite, AuditLog auditLog); @Timed @ExceptionMetered @GET Response findSecrets(@Auth User user, @DefaultValue("") @QueryParam("name") String name,
      @DefaultValue("") @QueryParam("nameOnly") String nameOnly, @QueryParam("idx") Integer idx,
      @QueryParam("num") Integer num,
      @DefaultValue("true") @QueryParam("newestFirst") Boolean newestFirst); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request); @Path("{name}") @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response createOrUpdateSecret(@Auth User user, @PathParam("name") String secretName,
      @Valid CreateOrUpdateSecretRequestV2 request); @Path("{name}/partialupdate") @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response partialUpdateSecret(@Auth User user, @PathParam("name") String secretName, @Valid
      PartialUpdateSecretRequestV2 request); @Path("{secretId}") @Timed @ExceptionMetered @GET SecretDetailResponse retrieveSecret(@Auth User user,
      @PathParam("secretId") LongParam secretId); @Timed @ExceptionMetered @GET @Path("versions/{name}") @Produces(APPLICATION_JSON) List<SanitizedSecret> secretVersions(@Auth User user,
      @PathParam("name") String name, @QueryParam("versionIdx") int versionIdx,
      @QueryParam("numVersions") int numVersions); @Path("rollback/{secretName}/{versionId}") @Timed @ExceptionMetered @POST Response resetSecretVersion(@Auth User user, @PathParam("secretName") String secretName,
      @PathParam("versionId") LongParam versionId); @Path("{secretId}") @Timed @ExceptionMetered @DELETE Response deleteSecret(@Auth User user, @PathParam("secretId") LongParam secretId); }
SecretsResource { @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) public Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request) { logger.info("User '{}' creating secret '{}'.", user, request.name()); Secret secret; try { SecretController.SecretBuilder builder = secretController.builder(request.name(), request.content(), user.getName(), request.expiry()); if (request.description() != null) { builder.withDescription(request.description()); } if (request.metadata() != null) { builder.withMetadata(request.metadata()); } secret = builder.create(); } catch (DataAccessException e) { logger.info(format("Cannot create secret %s", request.name()), e); throw new ConflictException(format("Cannot create secret %s.", request.name())); } URI uri = UriBuilder.fromResource(SecretsResource.class).path("{secretId}").build(secret.getId()); Response response = Response .created(uri) .entity(secretDetailResponseFromId(secret.getId())) .build(); if (response.getStatus() == HttpStatus.SC_CREATED) { Map<String, String> extraInfo = new HashMap<>(); if (request.description() != null) { extraInfo.put("description", request.description()); } if (request.metadata() != null) { extraInfo.put("metadata", request.metadata().toString()); } extraInfo.put("expiry", Long.toString(request.expiry())); auditLog.recordEvent( new Event(Instant.now(), EventTag.SECRET_CREATE, user.getName(), request.name(), extraInfo)); } return response; } @SuppressWarnings("unused") @Inject  SecretsResource(SecretController secretController, AclDAOFactory aclDAOFactory,
      SecretDAOFactory secretDAOFactory, AuditLog auditLog); @VisibleForTesting  SecretsResource(SecretController secretController, AclDAO aclDAOReadOnly,
      SecretDAO secretDAOReadWrite, AuditLog auditLog); @Timed @ExceptionMetered @GET Response findSecrets(@Auth User user, @DefaultValue("") @QueryParam("name") String name,
      @DefaultValue("") @QueryParam("nameOnly") String nameOnly, @QueryParam("idx") Integer idx,
      @QueryParam("num") Integer num,
      @DefaultValue("true") @QueryParam("newestFirst") Boolean newestFirst); @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response createSecret(@Auth User user, @Valid CreateSecretRequestV2 request); @Path("{name}") @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response createOrUpdateSecret(@Auth User user, @PathParam("name") String secretName,
      @Valid CreateOrUpdateSecretRequestV2 request); @Path("{name}/partialupdate") @Timed @ExceptionMetered @POST @Consumes(APPLICATION_JSON) Response partialUpdateSecret(@Auth User user, @PathParam("name") String secretName, @Valid
      PartialUpdateSecretRequestV2 request); @Path("{secretId}") @Timed @ExceptionMetered @GET SecretDetailResponse retrieveSecret(@Auth User user,
      @PathParam("secretId") LongParam secretId); @Timed @ExceptionMetered @GET @Path("versions/{name}") @Produces(APPLICATION_JSON) List<SanitizedSecret> secretVersions(@Auth User user,
      @PathParam("name") String name, @QueryParam("versionIdx") int versionIdx,
      @QueryParam("numVersions") int numVersions); @Path("rollback/{secretName}/{versionId}") @Timed @ExceptionMetered @POST Response resetSecretVersion(@Auth User user, @PathParam("secretName") String secretName,
      @PathParam("versionId") LongParam versionId); @Path("{secretId}") @Timed @ExceptionMetered @DELETE Response deleteSecret(@Auth User user, @PathParam("secretId") LongParam secretId);  }