1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20 package io.wcm.devops.conga.tooling.maven.plugin.validation;
21
22 import java.util.List;
23 import java.util.Map;
24
25 import org.apache.maven.plugin.MojoFailureException;
26 import org.yaml.snakeyaml.Yaml;
27 import org.yaml.snakeyaml.constructor.Constructor;
28
29 import io.wcm.devops.conga.generator.util.VariableStringResolver;
30 import io.wcm.devops.conga.model.reader.AbstractModelReader;
31 import io.wcm.devops.conga.model.reader.ModelReader;
32 import io.wcm.devops.conga.model.util.YamlUtil;
33 import io.wcm.devops.conga.resource.Resource;
34
35
36
37
38 public final class NoValueProviderInRoleValidator implements DefinitionValidator<Void> {
39
40 private final ModelReader<Map<String, Object>> mapReader = new MapReader();
41
42 @Override
43 @SuppressWarnings("PMD.PreserveStackTrace")
44 public Void validate(Resource resource, String pathForLog) throws MojoFailureException {
45 try {
46
47 process(mapReader.read(resource));
48 }
49 catch (Exception ex) {
50 throw new MojoFailureException("Role definition " + pathForLog + " is invalid:\n" + ex.getMessage());
51 }
52 return null;
53 }
54
55 @SuppressWarnings("unchecked")
56 private void process(Object value) {
57 if (value instanceof String) {
58 validate((String)value);
59 }
60 else if (value instanceof Map) {
61 ((Map)value).values().forEach(this::process);
62 }
63 else if (value instanceof List) {
64 ((List)value).forEach(this::process);
65 }
66 }
67
68 private void validate(String value) {
69 if (VariableStringResolver.hasValueProviderReference(value)) {
70 throw new IllegalStateException("Role definitions must not reference value providers: " + value);
71 }
72 }
73
74 private static class MapReader extends AbstractModelReader<Map<String, Object>> {
75 MapReader() {
76 super(getYaml());
77 }
78 private static Yaml getYaml() {
79 Constructor constructor = new Constructor(Map.class, YamlUtil.createLoaderOptions());
80 return new Yaml(constructor);
81 }
82 }
83
84
85 }