All Downloads are FREE. Search and download functionalities are using the official Maven repository.

com.github.nagyesta.lowkeyvault.controller.v7_5.KeyController Maven / Gradle / Ivy

There is a newer version: 2.5.81
Show newest version
package com.github.nagyesta.lowkeyvault.controller.v7_5;

import com.github.nagyesta.lowkeyvault.controller.common.CommonKeyController;
import com.github.nagyesta.lowkeyvault.mapper.common.registry.KeyConverterRegistry;
import com.github.nagyesta.lowkeyvault.model.common.ApiConstants;
import com.github.nagyesta.lowkeyvault.model.common.KeyVaultItemListModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.DeletedKeyVaultKeyItemModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.DeletedKeyVaultKeyModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.KeyVaultKeyItemModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.KeyVaultKeyModel;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.request.CreateKeyRequest;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.request.ImportKeyRequest;
import com.github.nagyesta.lowkeyvault.model.v7_2.key.request.UpdateKeyRequest;
import com.github.nagyesta.lowkeyvault.service.key.ReadOnlyKeyVaultKeyEntity;
import com.github.nagyesta.lowkeyvault.service.key.id.VersionedKeyEntityId;
import com.github.nagyesta.lowkeyvault.service.vault.VaultService;
import jakarta.validation.Valid;
import jakarta.validation.constraints.Pattern;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.lang.NonNull;
import org.springframework.stereotype.Component;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.net.URI;

import static com.github.nagyesta.lowkeyvault.model.common.ApiConstants.API_VERSION_7_5;
import static com.github.nagyesta.lowkeyvault.model.common.ApiConstants.V_7_5;
import static org.springframework.http.MediaType.APPLICATION_JSON_VALUE;

@Slf4j
@RestController
@Validated
@Component("KeyControllerV75")
public class KeyController extends CommonKeyController {

    @Autowired
    public KeyController(@NonNull final KeyConverterRegistry registry, @NonNull final VaultService vaultService) {
        super(registry, vaultService);
    }

    @Override
    @PostMapping(value = {"/keys/{keyName}/create", "/keys/{keyName}/create/"},
            params = API_VERSION_7_5,
            consumes = APPLICATION_JSON_VALUE,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity create(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri,
            @Valid @RequestBody final CreateKeyRequest request) {
        return super.create(keyName, baseUri, request);
    }

    @Override
    @PutMapping(value = {"/keys/{keyName}", "/keys/{keyName}/"},
            params = API_VERSION_7_5,
            consumes = APPLICATION_JSON_VALUE,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity importKey(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri,
            @Valid @RequestBody final ImportKeyRequest request) {
        return super.importKey(keyName, baseUri, request);
    }

    @Override
    @DeleteMapping(value = {"/keys/{keyName}", "/keys/{keyName}/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity delete(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri) {
        return super.delete(keyName, baseUri);
    }

    @Override
    @GetMapping(value = {"/keys/{keyName}/versions", "/keys/{keyName}/versions/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity> versions(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri,
            @RequestParam(name = MAX_RESULTS_PARAM, required = false, defaultValue = DEFAULT_MAX) final int maxResults,
            @RequestParam(name = SKIP_TOKEN_PARAM, required = false, defaultValue = SKIP_ZERO) final int skipToken) {
        return super.versions(keyName, baseUri, maxResults, skipToken);
    }

    @Override
    @GetMapping(value = {"/keys", "/keys/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity> listKeys(
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri,
            @RequestParam(name = MAX_RESULTS_PARAM, required = false, defaultValue = DEFAULT_MAX) final int maxResults,
            @RequestParam(name = SKIP_TOKEN_PARAM, required = false, defaultValue = SKIP_ZERO) final int skipToken) {
        return super.listKeys(baseUri, maxResults, skipToken);
    }

    @Override
    @GetMapping(value = {"/deletedkeys", "/deletedkeys/",
            "/deletedKeys", "/deletedKeys/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity> listDeletedKeys(
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri,
            @RequestParam(name = MAX_RESULTS_PARAM, required = false, defaultValue = DEFAULT_MAX) final int maxResults,
            @RequestParam(name = SKIP_TOKEN_PARAM, required = false, defaultValue = SKIP_ZERO) final int skipToken) {
        return super.listDeletedKeys(baseUri, maxResults, skipToken);
    }

    @Override
    @GetMapping(value = {"/keys/{keyName}", "/keys/{keyName}/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity get(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri) {
        return super.get(keyName, baseUri);
    }

    @Override
    @GetMapping(value = {"/keys/{keyName}/{keyVersion}", "/keys/{keyName}/{keyVersion}/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity getWithVersion(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @PathVariable @Valid @Pattern(regexp = VERSION_NAME_PATTERN) final String keyVersion,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri) {
        return super.getWithVersion(keyName, keyVersion, baseUri);
    }

    @Override
    @PatchMapping(value = {"/keys/{keyName}/{keyVersion}", "/keys/{keyName}/{keyVersion}/"},
            params = API_VERSION_7_5,
            consumes = APPLICATION_JSON_VALUE,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity updateVersion(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @PathVariable @Valid @Pattern(regexp = VERSION_NAME_PATTERN) final String keyVersion,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri,
            @NonNull @Valid @RequestBody final UpdateKeyRequest request) {
        return super.updateVersion(keyName, keyVersion, baseUri, request);
    }

    @PostMapping(value = {"/keys/{keyName}/rotate", "/keys/{keyName}/rotate/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity rotateKey(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri) {
        log.info("Received request to {} rotate key: {} using API version: {}",
                baseUri.toString(), keyName, apiVersion());

        final VersionedKeyEntityId rotatedKeyId = getVaultByUri(baseUri).rotateKey(entityId(baseUri, keyName));
        final ReadOnlyKeyVaultKeyEntity keyVaultKeyEntity = getEntityByNameAndVersion(baseUri, keyName, rotatedKeyId.version());
        return ResponseEntity.ok(convertDetails(keyVaultKeyEntity, baseUri));
    }

    @Override
    @GetMapping(value = {"/deletedkeys/{keyName}", "/deletedkeys/{keyName}/",
            "/deletedKeys/{keyName}", "/deletedKeys/{keyName}/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity getDeletedKey(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri) {
        return super.getDeletedKey(keyName, baseUri);
    }

    @Override
    @PostMapping(value = {"/deletedkeys/{keyName}/recover", "/deletedkeys/{keyName}/recover/",
            "/deletedKeys/{keyName}/recover", "/deletedKeys/{keyName}/recover/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity recoverDeletedKey(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri) {
        return super.recoverDeletedKey(keyName, baseUri);
    }

    @Override
    @DeleteMapping(value = {"/deletedkeys/{keyName}", "/deletedkeys/{keyName}/",
            "/deletedKeys/{keyName}", "/deletedKeys/{keyName}/"},
            params = API_VERSION_7_5,
            produces = APPLICATION_JSON_VALUE)
    public ResponseEntity purgeDeleted(
            @PathVariable @Valid @Pattern(regexp = NAME_PATTERN) final String keyName,
            @RequestAttribute(name = ApiConstants.REQUEST_BASE_URI) final URI baseUri) {
        return super.purgeDeleted(keyName, baseUri);
    }

    @Override
    protected String apiVersion() {
        return V_7_5;
    }
}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy