|
| 1 | +/* |
| 2 | + * Copyright The Cryostat 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 | + * http://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 io.cryostat.events; |
| 17 | + |
| 18 | +import java.io.IOException; |
| 19 | +import java.util.List; |
| 20 | +import java.util.Objects; |
| 21 | +import java.util.Optional; |
| 22 | + |
| 23 | +import io.cryostat.ConfigProperties; |
| 24 | +import io.cryostat.Producers; |
| 25 | +import io.cryostat.StorageBuckets; |
| 26 | +import io.cryostat.libcryostat.templates.Template; |
| 27 | +import io.cryostat.libcryostat.templates.TemplateType; |
| 28 | +import io.cryostat.recordings.ArchivedRecordingMetadataService; |
| 29 | +import io.cryostat.util.CRUDService; |
| 30 | +import io.cryostat.util.HttpMimeType; |
| 31 | + |
| 32 | +import com.fasterxml.jackson.databind.ObjectMapper; |
| 33 | +import io.quarkus.arc.lookup.LookupIfProperty; |
| 34 | +import io.quarkus.runtime.StartupEvent; |
| 35 | +import io.smallrye.common.annotation.Identifier; |
| 36 | +import jakarta.enterprise.context.ApplicationScoped; |
| 37 | +import jakarta.enterprise.event.Observes; |
| 38 | +import jakarta.inject.Inject; |
| 39 | +import org.apache.commons.codec.binary.Base64; |
| 40 | +import org.eclipse.microprofile.config.inject.ConfigProperty; |
| 41 | +import org.jboss.logging.Logger; |
| 42 | +import software.amazon.awssdk.core.sync.RequestBody; |
| 43 | +import software.amazon.awssdk.services.s3.S3Client; |
| 44 | +import software.amazon.awssdk.services.s3.model.DeleteObjectRequest; |
| 45 | +import software.amazon.awssdk.services.s3.model.GetObjectRequest; |
| 46 | +import software.amazon.awssdk.services.s3.model.ListObjectsV2Request; |
| 47 | +import software.amazon.awssdk.services.s3.model.PutObjectRequest; |
| 48 | + |
| 49 | +@ApplicationScoped |
| 50 | +@LookupIfProperty( |
| 51 | + name = ConfigProperties.STORAGE_METADATA_EVENT_TEMPLATES_STORAGE_MODE, |
| 52 | + stringValue = ArchivedRecordingMetadataService.METADATA_STORAGE_MODE_BUCKET) |
| 53 | +class BucketedEventTemplateMetadataService implements CRUDService<String, Template, Template> { |
| 54 | + |
| 55 | + @ConfigProperty(name = ConfigProperties.AWS_BUCKET_NAME_METADATA) |
| 56 | + String bucket; |
| 57 | + |
| 58 | + @ConfigProperty(name = ConfigProperties.AWS_METADATA_PREFIX_EVENT_TEMPLATES) |
| 59 | + String prefix; |
| 60 | + |
| 61 | + @Inject S3Client storage; |
| 62 | + @Inject StorageBuckets buckets; |
| 63 | + |
| 64 | + @Inject |
| 65 | + @Identifier(Producers.BASE64_URL) |
| 66 | + Base64 base64Url; |
| 67 | + |
| 68 | + @Inject ObjectMapper mapper; |
| 69 | + |
| 70 | + @Inject Logger logger; |
| 71 | + |
| 72 | + void onStart(@Observes StartupEvent evt) { |
| 73 | + buckets.createIfNecessary(bucket); |
| 74 | + } |
| 75 | + |
| 76 | + @Override |
| 77 | + public List<Template> list() throws IOException { |
| 78 | + var builder = ListObjectsV2Request.builder().bucket(bucket).prefix(prefix); |
| 79 | + var objs = storage.listObjectsV2(builder.build()).contents(); |
| 80 | + return objs.stream() |
| 81 | + .map( |
| 82 | + t -> { |
| 83 | + // TODO this entails a remote file read over the network and then some |
| 84 | + // minor processing of the received file. More time will be spent |
| 85 | + // retrieving the data than processing it, so this should be |
| 86 | + // parallelized. |
| 87 | + try { |
| 88 | + return read(t.key()).orElseThrow(); |
| 89 | + } catch (IOException e) { |
| 90 | + logger.error(e); |
| 91 | + return null; |
| 92 | + } |
| 93 | + }) |
| 94 | + .filter(Objects::nonNull) |
| 95 | + .toList(); |
| 96 | + } |
| 97 | + |
| 98 | + @Override |
| 99 | + public void create(String k, Template template) throws IOException { |
| 100 | + storage.putObject( |
| 101 | + PutObjectRequest.builder() |
| 102 | + .bucket(bucket) |
| 103 | + .key(prefix(k)) |
| 104 | + .contentType(HttpMimeType.JFC.mime()) |
| 105 | + .build(), |
| 106 | + RequestBody.fromBytes(mapper.writeValueAsBytes(TemplateMeta.from(template)))); |
| 107 | + } |
| 108 | + |
| 109 | + @Override |
| 110 | + public Optional<Template> read(String k) throws IOException { |
| 111 | + try (var stream = |
| 112 | + storage.getObject( |
| 113 | + GetObjectRequest.builder().bucket(bucket).key(prefix(k)).build())) { |
| 114 | + return Optional.of(mapper.readValue(stream, TemplateMeta.class)) |
| 115 | + .map(TemplateMeta::asTemplate); |
| 116 | + } |
| 117 | + } |
| 118 | + |
| 119 | + @Override |
| 120 | + public void delete(String k) throws IOException { |
| 121 | + storage.deleteObject(DeleteObjectRequest.builder().bucket(bucket).key(prefix(k)).build()); |
| 122 | + } |
| 123 | + |
| 124 | + private String prefix(String key) { |
| 125 | + return String.format("%s/%s", prefix, key); |
| 126 | + } |
| 127 | + |
| 128 | + // just a thin serialization adapter. Jackson ObjectMapper complains about not being able to |
| 129 | + // instantiate the Template type directly. |
| 130 | + static record TemplateMeta(String label, String provider, String description) { |
| 131 | + static TemplateMeta from(Template template) { |
| 132 | + return new TemplateMeta( |
| 133 | + template.getName(), template.getProvider(), template.getDescription()); |
| 134 | + } |
| 135 | + |
| 136 | + Template asTemplate() { |
| 137 | + return new Template(label, description, provider, TemplateType.CUSTOM); |
| 138 | + } |
| 139 | + } |
| 140 | +} |
0 commit comments