use of org.kohsuke.accmod.Restricted in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method doCheckNewSource.
@POST
@Restricted(NoExternalUse.class)
public FormValidation doCheckNewSource(@QueryParameter String newSource) {
Jenkins.get().checkPermission(Jenkins.ADMINISTER);
String normalizedSource = Util.fixEmptyAndTrim(newSource);
if (normalizedSource == null) {
// empty, do nothing
return FormValidation.ok();
}
List<String> candidateSources = new ArrayList<>();
for (String candidateSource : inputToCandidateSources(normalizedSource)) {
File file = new File(candidateSource);
if (!file.exists() && !ConfigurationAsCode.isSupportedURI(candidateSource)) {
return FormValidation.error("Configuration cannot be applied. File or URL cannot be parsed or do not exist.");
}
candidateSources.add(candidateSource);
}
try {
List<YamlSource> candidates = getConfigFromSources(candidateSources);
final Map<Source, String> issues = checkWith(candidates);
final JSONArray errors = collectProblems(issues, "error");
if (!errors.isEmpty()) {
return FormValidation.error(errors.toString());
}
final JSONArray warnings = collectProblems(issues, "warning");
if (!warnings.isEmpty()) {
return FormValidation.warning(warnings.toString());
}
return FormValidation.okWithMarkup("The configuration can be applied");
} catch (ConfiguratorException | IllegalArgumentException e) {
return FormValidation.error(e, e.getCause() == null ? e.getMessage() : e.getCause().getMessage());
}
}
use of org.kohsuke.accmod.Restricted in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method getBundledCasCURIs.
@Restricted(NoExternalUse.class)
public List<String> getBundledCasCURIs() {
final String cascFile = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH;
final String cascDirectory = "/WEB-INF/" + DEFAULT_JENKINS_YAML_PATH + ".d/";
List<String> res = new ArrayList<>();
final ServletContext servletContext = Jenkins.get().servletContext;
try {
URL bundled = servletContext.getResource(cascFile);
if (bundled != null) {
res.add(bundled.toString());
}
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to load " + cascFile, e);
}
PathMatcher matcher = FileSystems.getDefault().getPathMatcher(YAML_FILES_PATTERN);
Set<String> resources = servletContext.getResourcePaths(cascDirectory);
if (resources != null) {
// sort to execute them in a deterministic order
for (String cascItem : new TreeSet<>(resources)) {
try {
URL bundled = servletContext.getResource(cascItem);
if (bundled != null && matcher.matches(new File(bundled.getPath()).toPath())) {
res.add(bundled.toString());
}
// TODO: else do some handling?
} catch (IOException e) {
LOGGER.log(Level.WARNING, "Failed to execute " + res, e);
}
}
}
return res;
}
use of org.kohsuke.accmod.Restricted in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method doViewExport.
@RequirePOST
@Restricted(NoExternalUse.class)
public void doViewExport(StaplerRequest req, StaplerResponse res) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.SYSTEM_READ)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
ByteArrayOutputStream out = new ByteArrayOutputStream();
export(out);
req.setAttribute("export", out.toString(StandardCharsets.UTF_8.name()));
req.getView(this, "viewExport.jelly").forward(req, res);
}
use of org.kohsuke.accmod.Restricted in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method configs.
/**
* Recursive search for all {@link #YAML_FILES_PATTERN} in provided base path
*
* @param path base path to start (can be file or directory)
* @return list of all paths matching pattern. Only base file itself if it is a file matching pattern
*/
@Restricted(NoExternalUse.class)
public List<Path> configs(String path) throws ConfiguratorException {
final Path root = Paths.get(path);
if (!Files.exists(root)) {
throw new ConfiguratorException("Invalid configuration: '" + path + "' isn't a valid path.");
}
if (Files.isRegularFile(root) && Files.isReadable(root)) {
return Collections.singletonList(root);
}
final PathMatcher matcher = FileSystems.getDefault().getPathMatcher(YAML_FILES_PATTERN);
try (Stream<Path> stream = Files.find(Paths.get(path), Integer.MAX_VALUE, (next, attrs) -> !attrs.isDirectory() && !isHidden(next) && matcher.matches(next), FileVisitOption.FOLLOW_LINKS)) {
return stream.sorted().collect(toList());
} catch (IOException e) {
throw new IllegalStateException("failed config scan for " + path, e);
}
}
use of org.kohsuke.accmod.Restricted in project configuration-as-code-plugin by jenkinsci.
the class ConfigurationAsCode method doCheck.
@RequirePOST
@Restricted(NoExternalUse.class)
public void doCheck(StaplerRequest req, StaplerResponse res) throws Exception {
if (!Jenkins.get().hasPermission(Jenkins.ADMINISTER)) {
res.sendError(HttpServletResponse.SC_FORBIDDEN);
return;
}
final Map<Source, String> issues = checkWith(YamlSource.of(req));
res.setContentType("application/json");
final JSONArray warnings = new JSONArray();
issues.entrySet().stream().map(e -> new JSONObject().accumulate("line", e.getKey().line).accumulate("warning", e.getValue())).forEach(warnings::add);
warnings.write(res.getWriter());
}
Aggregations