View Javadoc
1   /*
2    * #%L
3    * wcm.io
4    * %%
5    * Copyright (C) 2018 wcm.io
6    * %%
7    * Licensed under the Apache License, Version 2.0 (the "License");
8    * you may not use this file except in compliance with the License.
9    * You may obtain a copy of the License at
10   *
11   *      http://www.apache.org/licenses/LICENSE-2.0
12   *
13   * Unless required by applicable law or agreed to in writing, software
14   * distributed under the License is distributed on an "AS IS" BASIS,
15   * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16   * See the License for the specific language governing permissions and
17   * limitations under the License.
18   * #L%
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   * Ensures that nor value providers are used in role models. They should only be used in environments.
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        // iterate over whole value and validate all string values that are found
47        process(mapReader.read(resource));
48      }
49      /*CHECKSTYLE:OFF*/ catch (Exception ex) { /*CHECKSTYLE:ON*/
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  }