|
| 1 | +/* |
| 2 | + * Copyright 2022 the original author or authors. |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package org.springframework.vault.repository.support; |
| 17 | + |
| 18 | +import java.util.ArrayList; |
| 19 | +import java.util.Collections; |
| 20 | +import java.util.List; |
| 21 | +import java.util.Map; |
| 22 | +import java.util.Optional; |
| 23 | + |
| 24 | +import org.springframework.data.domain.Page; |
| 25 | +import org.springframework.data.domain.PageImpl; |
| 26 | +import org.springframework.data.domain.Pageable; |
| 27 | +import org.springframework.data.history.Revision; |
| 28 | +import org.springframework.data.history.Revisions; |
| 29 | +import org.springframework.data.repository.core.EntityInformation; |
| 30 | +import org.springframework.data.repository.history.RevisionRepository; |
| 31 | +import org.springframework.lang.Nullable; |
| 32 | +import org.springframework.util.Assert; |
| 33 | +import org.springframework.vault.core.VaultKeyValueMetadataOperations; |
| 34 | +import org.springframework.vault.core.VaultOperations; |
| 35 | +import org.springframework.vault.core.VaultVersionedKeyValueOperations; |
| 36 | +import org.springframework.vault.core.util.KeyValueDelegate; |
| 37 | +import org.springframework.vault.repository.convert.SecretDocument; |
| 38 | +import org.springframework.vault.repository.convert.VaultConverter; |
| 39 | +import org.springframework.vault.repository.core.VaultKeyValueTemplate; |
| 40 | +import org.springframework.vault.support.VaultMetadataResponse; |
| 41 | +import org.springframework.vault.support.Versioned; |
| 42 | + |
| 43 | +/** |
| 44 | + * Vault-based {@link RevisionRepository} providing revision metadata for versioned |
| 45 | + * secrets. |
| 46 | + * |
| 47 | + * @author Mark Paluch |
| 48 | + * @since 2.4 |
| 49 | + */ |
| 50 | +public class VaultRevisionRepository<T> implements RevisionRepository<T, String, Integer> { |
| 51 | + |
| 52 | + private final EntityInformation<T, String> metadata; |
| 53 | + |
| 54 | + private final String keyspacePath; |
| 55 | + |
| 56 | + private final VaultVersionedKeyValueOperations operations; |
| 57 | + |
| 58 | + private final VaultKeyValueMetadataOperations metadataOperations; |
| 59 | + |
| 60 | + private final VaultConverter converter; |
| 61 | + |
| 62 | + public VaultRevisionRepository(EntityInformation<T, String> metadata, String keyspace, |
| 63 | + VaultKeyValueTemplate keyValueTemplate) { |
| 64 | + |
| 65 | + Assert.notNull(metadata, "EntityInformation must not be null"); |
| 66 | + Assert.notNull(keyValueTemplate, "VaultKeyValueTemplate must not be null"); |
| 67 | + |
| 68 | + this.metadata = metadata; |
| 69 | + this.converter = keyValueTemplate.getConverter(); |
| 70 | + |
| 71 | + VaultOperations vaultOperations = keyValueTemplate.getVaultOperations(); |
| 72 | + |
| 73 | + KeyValueDelegate delegate = new KeyValueDelegate(vaultOperations); |
| 74 | + KeyValueDelegate.MountInfo mountInfo = delegate.getMountInfo(keyspace); |
| 75 | + |
| 76 | + if (!mountInfo.isAvailable()) { |
| 77 | + throw new IllegalStateException("Mount not available under " + keyspace); |
| 78 | + } |
| 79 | + |
| 80 | + if (!delegate.isVersioned(keyspace)) { |
| 81 | + throw new IllegalStateException("Mount under " + keyspace + " is not versioned"); |
| 82 | + } |
| 83 | + |
| 84 | + this.keyspacePath = keyspace.substring(mountInfo.getPath().length()); |
| 85 | + this.operations = vaultOperations.opsForVersionedKeyValue(mountInfo.getPath()); |
| 86 | + this.metadataOperations = this.operations.opsForKeyValueMetadata(); |
| 87 | + } |
| 88 | + |
| 89 | + @Override |
| 90 | + public Optional<Revision<Integer, T>> findLastChangeRevision(String id) { |
| 91 | + |
| 92 | + Assert.notNull(id, "Identifier must not be null"); |
| 93 | + |
| 94 | + return toRevision(operations.get(getPath(id)), id); |
| 95 | + } |
| 96 | + |
| 97 | + @Override |
| 98 | + public Revisions<Integer, T> findRevisions(String id) { |
| 99 | + |
| 100 | + VaultMetadataResponse metadata = metadataOperations.get(getPath(id)); |
| 101 | + |
| 102 | + if (metadata == null) { |
| 103 | + return Revisions.none(); |
| 104 | + } |
| 105 | + |
| 106 | + return Revisions.of(collectRevisions(id, metadata.getVersions())); |
| 107 | + } |
| 108 | + |
| 109 | + private List<Revision<Integer, T>> collectRevisions(String id, List<Versioned.Metadata> versions) { |
| 110 | + |
| 111 | + List<Revision<Integer, T>> revisions = new ArrayList<>(); |
| 112 | + |
| 113 | + for (Versioned.Metadata version : versions) { |
| 114 | + |
| 115 | + Versioned<Map<String, Object>> versioned = operations.get(getPath(id), version.getVersion()); |
| 116 | + |
| 117 | + if (versioned == null) { |
| 118 | + continue; |
| 119 | + } |
| 120 | + |
| 121 | + T entity = versioned.hasData() ? converter.read(this.metadata.getJavaType(), createDocument(id, versioned)) |
| 122 | + : null; |
| 123 | + revisions.add(Revision.of(new VaultRevisionMetadata(versioned), entity)); |
| 124 | + } |
| 125 | + return revisions; |
| 126 | + } |
| 127 | + |
| 128 | + @Override |
| 129 | + public Page<Revision<Integer, T>> findRevisions(String id, Pageable pageable) { |
| 130 | + |
| 131 | + if (pageable.isUnpaged()) { |
| 132 | + return new PageImpl<>(Collections.emptyList()); |
| 133 | + } |
| 134 | + |
| 135 | + VaultMetadataResponse metadata = metadataOperations.get(getPath(id)); |
| 136 | + if (metadata == null || pageable.getOffset() > metadata.getVersions().size()) { |
| 137 | + return Page.empty(pageable); |
| 138 | + } |
| 139 | + |
| 140 | + List<Versioned.Metadata> versions = metadata.getVersions(); |
| 141 | + |
| 142 | + int toIndex = Math.min(versions.size(), Math.toIntExact(pageable.getOffset() + pageable.getPageSize())); |
| 143 | + List<Versioned.Metadata> metadataPage = versions.subList(Math.toIntExact(pageable.getOffset()), toIndex); |
| 144 | + |
| 145 | + List<Revision<Integer, T>> revisions = collectRevisions(id, metadataPage); |
| 146 | + |
| 147 | + return new PageImpl<>(revisions, pageable, versions.size()); |
| 148 | + } |
| 149 | + |
| 150 | + @Override |
| 151 | + public Optional<Revision<Integer, T>> findRevision(String id, Integer revisionNumber) { |
| 152 | + |
| 153 | + Assert.notNull(id, "Identifier must not be null"); |
| 154 | + Assert.notNull(revisionNumber, "Revision number must not be null"); |
| 155 | + |
| 156 | + return toRevision(operations.get(getPath(id), Versioned.Version.from(revisionNumber)), id); |
| 157 | + } |
| 158 | + |
| 159 | + private Optional<Revision<Integer, T>> toRevision(@Nullable Versioned<Map<String, Object>> versioned, String id) { |
| 160 | + |
| 161 | + if (versioned == null) { |
| 162 | + return Optional.empty(); |
| 163 | + } |
| 164 | + |
| 165 | + T entity = versioned.hasData() ? converter.read(metadata.getJavaType(), createDocument(id, versioned)) : null; |
| 166 | + return Optional.of(Revision.of(new VaultRevisionMetadata(versioned), entity)); |
| 167 | + } |
| 168 | + |
| 169 | + private String getPath(String id) { |
| 170 | + return keyspacePath + "/" + id; |
| 171 | + } |
| 172 | + |
| 173 | + private SecretDocument createDocument(String id, Versioned<Map<String, Object>> versioned) { |
| 174 | + return new SecretDocument(id, versioned.getVersion().getVersion(), versioned.getRequiredData()); |
| 175 | + } |
| 176 | + |
| 177 | +} |
0 commit comments