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