1 package io.apicurio.registry.maven;
2
3 import com.fasterxml.jackson.databind.JsonNode;
4 import com.fasterxml.jackson.databind.ObjectMapper;
5 import com.google.protobuf.Descriptors.FileDescriptor;
6 import io.apicurio.registry.content.ContentHandle;
7 import io.apicurio.registry.content.TypedContent;
8 import io.apicurio.registry.content.refs.ExternalReference;
9 import io.apicurio.registry.content.refs.ReferenceFinder;
10 import io.apicurio.registry.maven.refs.IndexedResource;
11 import io.apicurio.registry.maven.refs.ReferenceIndex;
12 import io.apicurio.registry.rest.client.RegistryClient;
13 import io.apicurio.registry.rest.client.models.*;
14 import io.apicurio.registry.rules.ParsedJsonSchema;
15 import io.apicurio.registry.types.ArtifactType;
16 import io.apicurio.registry.types.ContentTypes;
17 import io.apicurio.registry.types.provider.ArtifactTypeUtilProvider;
18 import io.apicurio.registry.types.provider.DefaultArtifactTypeUtilProviderImpl;
19 import io.vertx.core.Vertx;
20 import org.apache.avro.Schema;
21 import org.apache.commons.io.FileUtils;
22 import org.apache.maven.plugin.MojoExecutionException;
23 import org.apache.maven.plugin.MojoFailureException;
24 import org.apache.maven.plugins.annotations.Mojo;
25 import org.apache.maven.plugins.annotations.Parameter;
26
27 import java.io.*;
28 import java.nio.charset.StandardCharsets;
29 import java.nio.file.Files;
30 import java.nio.file.Path;
31 import java.nio.file.Paths;
32 import java.util.*;
33 import java.util.concurrent.ExecutionException;
34 import java.util.stream.Collectors;
35
36
37
38
39 @Mojo(name = "register")
40 public class RegisterRegistryMojo extends AbstractRegistryMojo {
41
42
43
44
45 @Parameter(required = false)
46 List<ExistingReference> existingReferences;
47
48
49
50
51 @Parameter(required = true)
52 List<RegisterArtifact> artifacts;
53
54
55
56
57 @Parameter(property = "skipRegister", defaultValue = "false")
58 boolean skip;
59
60
61
62
63
64 @Parameter(property = "dryRun", defaultValue = "false")
65 boolean dryRun;
66
67 DefaultArtifactTypeUtilProviderImpl utilProviderFactory = new DefaultArtifactTypeUtilProviderImpl();
68
69
70
71
72 protected boolean validate() throws MojoExecutionException {
73 if (skip) {
74 getLog().info("register is skipped.");
75 return false;
76 }
77
78 if (artifacts == null || artifacts.isEmpty()) {
79 getLog().warn("No artifacts are configured for registration.");
80 return false;
81 }
82
83 if (existingReferences == null) {
84 existingReferences = new ArrayList<>();
85 }
86
87 int idx = 0;
88 int errorCount = 0;
89 for (RegisterArtifact artifact : artifacts) {
90 if (artifact.getGroupId() == null) {
91 getLog().error(String.format(
92 "GroupId is required when registering an artifact. Missing from artifacts[%d].",
93 idx));
94 errorCount++;
95 }
96 if (artifact.getArtifactId() == null) {
97 getLog().error(String.format(
98 "ArtifactId is required when registering an artifact. Missing from artifacts[%s].",
99 idx));
100 errorCount++;
101 }
102 if (artifact.getFile() == null) {
103 getLog().error(String.format(
104 "File is required when registering an artifact. Missing from artifacts[%s].", idx));
105 errorCount++;
106 } else if (!artifact.getFile().exists()) {
107 getLog().error(
108 String.format("Artifact file to register is configured but file does not exist: %s",
109 artifact.getFile().getPath()));
110 errorCount++;
111 }
112
113 idx++;
114 }
115
116 if (errorCount > 0) {
117 throw new MojoExecutionException(
118 "Invalid configuration of the Register Artifact(s) mojo. See the output log for details.");
119 }
120 return true;
121 }
122
123 @Override
124 protected void executeInternal() throws MojoExecutionException {
125 int errorCount = 0;
126 if (validate()) {
127 Vertx vertx = createVertx();
128 RegistryClient registryClient = createClient(vertx);
129
130 for (RegisterArtifact artifact : artifacts) {
131 String groupId = artifact.getGroupId();
132 String artifactId = artifact.getArtifactId();
133 try {
134 if (artifact.getAutoRefs() != null && artifact.getAutoRefs()) {
135
136
137 ReferenceIndex index = createIndex(artifact.getFile());
138 addExistingReferencesToIndex(registryClient, index, existingReferences);
139 addExistingReferencesToIndex(registryClient, index, artifact.getExistingReferences());
140 Stack<RegisterArtifact> registrationStack = new Stack<>();
141
142 registerWithAutoRefs(registryClient, artifact, index, registrationStack);
143 } else if (artifact.getAnalyzeDirectory() != null && artifact.getAnalyzeDirectory()) {
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159 registerDirectory(registryClient, artifact);
160 } else {
161
162 List<ArtifactReference> references = new ArrayList<>();
163
164 if (hasReferences(artifact)) {
165 references = processArtifactReferences(registryClient, artifact.getReferences());
166 }
167 registerArtifact(registryClient, artifact, references);
168 }
169 } catch (Exception e) {
170 errorCount++;
171 getLog().error(String.format("Exception while registering artifact [%s] / [%s]", groupId,
172 artifactId), e);
173 }
174
175 }
176
177 if (errorCount > 0) {
178 throw new MojoExecutionException("Errors while registering artifacts ...");
179 }
180 }
181 }
182
183 private VersionMetaData registerWithAutoRefs(RegistryClient registryClient, RegisterArtifact artifact,
184 ReferenceIndex index, Stack<RegisterArtifact> registrationStack) throws IOException,
185 ExecutionException, InterruptedException, MojoExecutionException, MojoFailureException {
186 if (loopDetected(artifact, registrationStack)) {
187 throw new RuntimeException(
188 "Artifact reference loop detected (not supported): " + printLoop(registrationStack));
189 }
190 registrationStack.push(artifact);
191
192
193 ContentHandle artifactContent = readContent(artifact.getFile());
194 String artifactContentType = getContentTypeByExtension(artifact.getFile().getName());
195 TypedContent typedArtifactContent = TypedContent.create(artifactContent, artifactContentType);
196
197
198 ArtifactTypeUtilProvider provider = this.utilProviderFactory
199 .getArtifactTypeProvider(artifact.getArtifactType());
200 ReferenceFinder referenceFinder = provider.getReferenceFinder();
201 var referenceArtifactIdentifierExtractor = provider.getReferenceArtifactIdentifierExtractor();
202 Set<ExternalReference> externalReferences = referenceFinder
203 .findExternalReferences(typedArtifactContent);
204
205
206 List<ArtifactReference> registeredReferences = new ArrayList<>(externalReferences.size());
207 for (ExternalReference externalRef : externalReferences) {
208 IndexedResource iresource = index.lookup(externalRef.getResource(),
209 Paths.get(artifact.getFile().toURI()));
210
211
212 if (iresource == null) {
213 throw new RuntimeException("Reference could not be resolved. From: "
214 + artifact.getFile().getName() + " To: " + externalRef.getFullReference());
215 }
216
217
218 if (!iresource.isRegistered()) {
219
220 String artifactId = referenceArtifactIdentifierExtractor.extractArtifactId(externalRef.getResource());
221 String groupId = referenceArtifactIdentifierExtractor.extractGroupId(externalRef.getResource());
222 File localFile = getLocalFile(iresource.getPath());
223 RegisterArtifact refArtifact = buildFromRoot(artifact, artifactId, groupId);
224 refArtifact.setArtifactType(iresource.getType());
225 refArtifact.setVersion(null);
226 refArtifact.setFile(localFile);
227 refArtifact.setContentType(getContentTypeByExtension(localFile.getName()));
228 try {
229 var car = registerWithAutoRefs(registryClient, refArtifact, index, registrationStack);
230 iresource.setRegistration(car);
231 } catch (IOException | ExecutionException | InterruptedException e) {
232 throw new RuntimeException(e);
233 }
234 }
235
236 var reference = new ArtifactReference();
237 reference.setName(externalRef.getFullReference());
238 reference.setVersion(iresource.getRegistration().getVersion());
239 reference.setGroupId(iresource.getRegistration().getGroupId());
240 reference.setArtifactId(iresource.getRegistration().getArtifactId());
241 registeredReferences.add(reference);
242 }
243 registeredReferences.sort((ref1, ref2) -> ref1.getName().compareTo(ref2.getName()));
244
245 registrationStack.pop();
246 return registerArtifact(registryClient, artifact, registeredReferences);
247 }
248
249 private void registerDirectory(RegistryClient registryClient, RegisterArtifact artifact)
250 throws IOException, ExecutionException, InterruptedException, MojoExecutionException,
251 MojoFailureException {
252 switch (artifact.getArtifactType()) {
253 case ArtifactType.AVRO:
254 final AvroDirectoryParser avroDirectoryParser = new AvroDirectoryParser(registryClient);
255 final ParsedDirectoryWrapper<Schema> schema = avroDirectoryParser.parse(artifact.getFile());
256 registerArtifact(registryClient, artifact, avroDirectoryParser
257 .handleSchemaReferences(artifact, schema.getSchema(), schema.getSchemaContents()));
258 break;
259 case ArtifactType.PROTOBUF:
260 final ProtobufDirectoryParser protobufDirectoryParser = new ProtobufDirectoryParser(
261 registryClient);
262 final ParsedDirectoryWrapper<FileDescriptor> protoSchema = protobufDirectoryParser
263 .parse(artifact.getFile());
264 registerArtifact(registryClient, artifact, protobufDirectoryParser.handleSchemaReferences(
265 artifact, protoSchema.getSchema(), protoSchema.getSchemaContents()));
266 break;
267 case ArtifactType.JSON:
268 final JsonSchemaDirectoryParser jsonSchemaDirectoryParser = new JsonSchemaDirectoryParser(
269 registryClient);
270 final ParsedDirectoryWrapper<ParsedJsonSchema> jsonSchema = jsonSchemaDirectoryParser
271 .parse(artifact.getFile());
272 registerArtifact(registryClient, artifact, jsonSchemaDirectoryParser.handleSchemaReferences(
273 artifact, jsonSchema.getSchema(), jsonSchema.getSchemaContents()));
274 break;
275 default:
276 throw new IllegalArgumentException(
277 String.format("Artifact type not recognized for analyzing a directory structure %s",
278 artifact.getArtifactType()));
279 }
280 }
281
282 private VersionMetaData registerArtifact(RegistryClient registryClient, RegisterArtifact artifact,
283 List<ArtifactReference> references) throws FileNotFoundException, ExecutionException,
284 InterruptedException, MojoExecutionException, MojoFailureException {
285 if (artifact.getFile() != null) {
286 return registerArtifact(registryClient, artifact, new FileInputStream(artifact.getFile()),
287 references);
288 } else {
289 return getArtifactVersionMetadata(registryClient, artifact);
290 }
291 }
292
293 private VersionMetaData getArtifactVersionMetadata(RegistryClient registryClient,
294 RegisterArtifact artifact) {
295 String groupId = artifact.getGroupId();
296 String artifactId = artifact.getArtifactId();
297 String version = artifact.getVersion();
298
299 VersionMetaData amd = registryClient.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId)
300 .versions().byVersionExpression(version).get();
301 getLog().info(String.format("Successfully processed artifact [%s] / [%s]. GlobalId is [%d]", groupId,
302 artifactId, amd.getGlobalId()));
303
304 return amd;
305 }
306
307 private VersionMetaData registerArtifact(RegistryClient registryClient, RegisterArtifact artifact,
308 InputStream artifactContent, List<ArtifactReference> references)
309 throws ExecutionException, InterruptedException, MojoFailureException, MojoExecutionException {
310 String groupId = artifact.getGroupId();
311 String artifactId = artifact.getArtifactId();
312 String version = artifact.getVersion();
313 String type = artifact.getArtifactType();
314 Boolean canonicalize = artifact.getCanonicalize();
315 Boolean isDraft = artifact.getIsDraft();
316 String ct = artifact.getContentType() == null ? ContentTypes.APPLICATION_JSON
317 : artifact.getContentType();
318 String data = null;
319 try {
320 if (artifact.getMinify() != null && artifact.getMinify()) {
321 ObjectMapper objectMapper = new ObjectMapper();
322 JsonNode jsonNode = objectMapper.readValue(artifactContent, JsonNode.class);
323 data = jsonNode.toString();
324 } else {
325 data = new String(artifactContent.readAllBytes(), StandardCharsets.UTF_8);
326 }
327 } catch (IOException e) {
328 throw new RuntimeException(e);
329 }
330
331 CreateArtifact createArtifact = new CreateArtifact();
332 createArtifact.setArtifactId(artifactId);
333 createArtifact.setArtifactType(type);
334
335 CreateVersion createVersion = new CreateVersion();
336 createVersion.setVersion(version);
337 createVersion.setIsDraft(isDraft);
338 createArtifact.setFirstVersion(createVersion);
339
340 VersionContent content = new VersionContent();
341 content.setContent(data);
342 content.setContentType(ct);
343 content.setReferences(references.stream().map(r -> {
344 ArtifactReference ref = new ArtifactReference();
345 ref.setArtifactId(r.getArtifactId());
346 ref.setGroupId(r.getGroupId());
347 ref.setVersion(r.getVersion());
348 ref.setName(r.getName());
349 return ref;
350 }).collect(Collectors.toList()));
351 createVersion.setContent(content);
352
353 try {
354 var vmd = registryClient.groups().byGroupId(groupId).artifacts().post(createArtifact, config -> {
355 if (artifact.getIfExists() != null) {
356 config.queryParameters.ifExists = IfArtifactExists
357 .forValue(artifact.getIfExists().value());
358 if (dryRun) {
359 config.queryParameters.dryRun = true;
360 }
361 }
362 config.queryParameters.canonical = canonicalize;
363 });
364
365 getLog().info(String.format("Successfully registered artifact [%s] / [%s]. GlobalId is [%d]",
366 groupId, artifactId, vmd.getVersion().getGlobalId()));
367
368
369 return vmd.getVersion();
370 } catch (RuleViolationProblemDetails | ProblemDetails e) {
371
372
373 if (artifact.getIsDraft() && e.getResponseStatusCode() == 409) {
374 try {
375 registryClient.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId)
376 .versions().byVersionExpression(version).content()
377 .put(content, config -> {
378
379 });
380 getLog().info(String.format("Successfully updated artifact [%s] / [%s].",
381 groupId, artifactId));
382
383 return registryClient.groups().byGroupId(groupId).artifacts().byArtifactId(artifactId).versions().byVersionExpression(version).get();
384 } catch (RuleViolationProblemDetails | ProblemDetails pd) {
385 logAndThrow(pd);
386 return null;
387 }
388 } else {
389 logAndThrow(e);
390 return null;
391 }
392 }
393 }
394
395 private static boolean hasReferences(RegisterArtifact artifact) {
396 return artifact.getReferences() != null && !artifact.getReferences().isEmpty();
397 }
398
399 private List<ArtifactReference> processArtifactReferences(RegistryClient registryClient,
400 List<RegisterArtifactReference> referencedArtifacts) throws FileNotFoundException,
401 ExecutionException, InterruptedException, MojoExecutionException, MojoFailureException {
402 List<ArtifactReference> references = new ArrayList<>();
403 for (RegisterArtifactReference artifact : referencedArtifacts) {
404 List<ArtifactReference> nestedReferences = new ArrayList<>();
405
406
407 if (hasReferences(artifact)) {
408 nestedReferences = processArtifactReferences(registryClient, artifact.getReferences());
409 }
410 final VersionMetaData artifactMetaData = registerArtifact(registryClient, artifact,
411 nestedReferences);
412 references.add(buildReferenceFromMetadata(artifactMetaData, artifact.getName()));
413 }
414 return references;
415 }
416
417 public void setArtifacts(List<RegisterArtifact> artifacts) {
418 this.artifacts = artifacts;
419 }
420
421 public void setSkip(boolean skip) {
422 this.skip = skip;
423 }
424
425 private static ArtifactReference buildReferenceFromMetadata(VersionMetaData metaData,
426 String referenceName) {
427 ArtifactReference reference = new ArtifactReference();
428 reference.setName(referenceName);
429 reference.setArtifactId(metaData.getArtifactId());
430 reference.setGroupId(metaData.getGroupId());
431 reference.setVersion(metaData.getVersion());
432 return reference;
433 }
434
435 private static boolean isFileAllowedInIndex(File file) {
436 return file.isFile() && (
437 file.getName().toLowerCase().endsWith(".json") ||
438 file.getName().toLowerCase().endsWith(".yml") ||
439 file.getName().toLowerCase().endsWith(".yaml") ||
440 file.getName().toLowerCase().endsWith(".xml") ||
441 file.getName().toLowerCase().endsWith(".xsd") ||
442 file.getName().toLowerCase().endsWith(".wsdl") ||
443 file.getName().toLowerCase().endsWith(".graphql") ||
444 file.getName().toLowerCase().endsWith(".avsc") ||
445 file.getName().toLowerCase().endsWith(".proto")
446 );
447 }
448
449
450
451
452
453
454 private static ReferenceIndex createIndex(File file) {
455 ReferenceIndex index = new ReferenceIndex(file.getParentFile().toPath());
456 Collection<File> allFiles = FileUtils.listFiles(file.getParentFile(), null, true);
457 allFiles.stream().filter(RegisterRegistryMojo::isFileAllowedInIndex).forEach(f -> {
458 index.index(f.toPath(), readContent(f));
459 });
460 return index;
461 }
462
463 private void addExistingReferencesToIndex(RegistryClient registryClient, ReferenceIndex index,
464 List<ExistingReference> existingReferences) throws ExecutionException, InterruptedException {
465 if (existingReferences != null && !existingReferences.isEmpty()) {
466 for (ExistingReference ref : existingReferences) {
467 VersionMetaData vmd;
468 if (ref.getVersion() == null || "LATEST".equalsIgnoreCase(ref.getVersion())) {
469 vmd = registryClient.groups().byGroupId(ref.getGroupId()).artifacts()
470 .byArtifactId(ref.getArtifactId()).versions().byVersionExpression("branch=latest")
471 .get();
472 } else {
473 vmd = new VersionMetaData();
474 vmd.setGroupId(ref.getGroupId());
475 vmd.setArtifactId(ref.getArtifactId());
476 vmd.setVersion(ref.getVersion());
477 }
478 index.index(ref.getResourceName(), vmd);
479 }
480 }
481 }
482
483 protected static ContentHandle readContent(File file) {
484 try {
485 return ContentHandle.create(Files.readAllBytes(file.toPath()));
486 } catch (IOException e) {
487 throw new RuntimeException("Failed to read schema file: " + file, e);
488 }
489 }
490
491 protected static RegisterArtifact buildFromRoot(RegisterArtifact rootArtifact, String artifactId, String groupId) {
492 RegisterArtifact nestedSchema = new RegisterArtifact();
493 nestedSchema.setCanonicalize(rootArtifact.getCanonicalize());
494 nestedSchema.setArtifactId(artifactId);
495 nestedSchema.setGroupId(groupId == null ? rootArtifact.getGroupId() : groupId);
496 nestedSchema.setContentType(rootArtifact.getContentType());
497 nestedSchema.setArtifactType(rootArtifact.getArtifactType());
498 nestedSchema.setMinify(rootArtifact.getMinify());
499 nestedSchema.setContentType(rootArtifact.getContentType());
500 nestedSchema.setIfExists(rootArtifact.getIfExists());
501 nestedSchema.setAutoRefs(rootArtifact.getAutoRefs());
502 return nestedSchema;
503 }
504
505 private static File getLocalFile(Path path) {
506 return path.toFile();
507 }
508
509
510
511
512
513
514
515 private static boolean loopDetected(RegisterArtifact artifact,
516 Stack<RegisterArtifact> registrationStack) {
517 for (RegisterArtifact stackArtifact : registrationStack) {
518 if (artifact.getFile().equals(stackArtifact.getFile())) {
519 return true;
520 }
521 }
522 return false;
523 }
524
525 private static String printLoop(Stack<RegisterArtifact> registrationStack) {
526 return registrationStack.stream().map(artifact -> artifact.getFile().getName())
527 .collect(Collectors.joining(" -> "));
528 }
529
530 }