PropertyPathEndpoint.java

/*
 * Copyright 2015-present the original author or authors.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      https://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package org.springframework.cloud.config.monitor;

import java.util.Arrays;
import java.util.Collections;
import java.util.HashMap;
import java.util.LinkedHashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
import java.util.regex.Pattern;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;

import org.springframework.cloud.config.server.environment.JGitEnvironmentRepository;
import org.springframework.http.HttpHeaders;
import org.springframework.http.MediaType;
import org.springframework.util.ObjectUtils;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

/**
 * HTTP endpoint for webhooks coming from repository providers.
 *
 * @author Dave Syer
 *
 */
@RestController
@RequestMapping(
		path = "${spring.cloud.config.server.monitor.endpoint.path:${spring.cloud.config.monitor.endpoint.path:}}/monitor")
public class PropertyPathEndpoint {

	private static Log log = LogFactory.getLog(PropertyPathEndpoint.class);

	private final PropertyPathNotificationExtractor extractor;

	private final List<PropertyPathNotifier> notifiers;

	/**
	 * Upper bound on the number of dash-separated segments processed when guessing a
	 * service name. Prevents a long, heavily dashed path from producing an unbounded
	 * number of candidate service names (and refresh events). Configurable via
	 * {@code spring.cloud.config.server.monitor.max-dashes}.
	 */
	private final int maxDashes;

	/**
	 * Upper bound on the number of paths processed from a notification. Prevents an
	 * externally supplied notification from causing an unbounded amount of path
	 * processing. Configurable via {@code spring.cloud.config.server.monitor.max-paths}.
	 */
	private final int maxPaths;

	private final List<Pattern> ignoredPaths;

	/**
	 * Git repositories of this config server. Only repositories identified by the
	 * notification have their refresh rate expired before applications are notified.
	 */
	private final List<JGitEnvironmentRepository> gitRepositories;

	public PropertyPathEndpoint(PropertyPathNotificationExtractor extractor, PropertyPathNotifier notifier) {
		this(extractor, List.of(notifier), MonitorConfigurationProperties.DEFAULT_MAX_DASHES);
	}

	public PropertyPathEndpoint(PropertyPathNotificationExtractor extractor, PropertyPathNotifier notifier,
			int maxDashes) {
		this(extractor, List.of(notifier), maxDashes);
	}

	public PropertyPathEndpoint(PropertyPathNotificationExtractor extractor, List<PropertyPathNotifier> notifiers,
			int maxDashes) {
		this(extractor, notifiers, maxDashes, MonitorConfigurationProperties.DEFAULT_MAX_PATHS,
				Collections.emptyList());
	}

	public PropertyPathEndpoint(PropertyPathNotificationExtractor extractor, List<PropertyPathNotifier> notifiers,
			int maxDashes, int maxPaths, List<String> ignoredPaths) {
		this(extractor, notifiers, maxDashes, maxPaths, ignoredPaths, Collections.emptyList());
	}

	public PropertyPathEndpoint(PropertyPathNotificationExtractor extractor, List<PropertyPathNotifier> notifiers,
			int maxDashes, int maxPaths, List<String> ignoredPaths, List<JGitEnvironmentRepository> gitRepositories) {
		this.extractor = extractor;
		this.gitRepositories = gitRepositories == null ? Collections.emptyList() : gitRepositories;
		this.notifiers = notifiers;
		this.maxDashes = maxDashes;
		this.maxPaths = maxPaths;
		this.ignoredPaths = ignoredPaths == null ? Collections.emptyList()
				: ignoredPaths.stream().map(Pattern::compile).toList();
	}

	@PostMapping
	public Set<String> notifyByPath(@RequestHeader HttpHeaders headers, @RequestBody Map<String, Object> request) {
		PropertyPathNotification notification = this.extractor.extract(headers, request);
		if (notification != null) {

			Set<String> services = new LinkedHashSet<>();

			String[] paths = notification.getPaths();
			if (paths != null) {
				int pathCount = Math.min(paths.length, this.maxPaths);
				for (int i = 0; i < pathCount; i++) {
					String path = paths[i];
					if (!isIgnored(path)) {
						services.addAll(guessServiceName(path));
					}
				}
				if (paths.length > this.maxPaths) {
					log.warn("Number of paths in notification exceeds the configured maximum of " + this.maxPaths
							+ " (spring.cloud.config.server.monitor.max-paths); stopping path processing early");
				}
			}
			if (!services.isEmpty()) {
				for (String service : services) {
					log.info("Refresh for: " + service);
				}
				expireRefreshRates(notification);
				for (PropertyPathNotifier notifier : this.notifiers) {
					notifier.notifyApplications(services);
				}
				return services;
			}

		}
		return Collections.emptySet();
	}

	@PostMapping(consumes = MediaType.APPLICATION_FORM_URLENCODED_VALUE)
	public Set<String> notifyByForm(@RequestHeader HttpHeaders headers, @RequestParam("path") List<String> request) {
		Map<String, Object> map = new HashMap<>();
		String key = "path";
		map.put(key, request);
		return notifyByPath(headers, map);
	}

	/**
	 * Expires the refresh rate of the Git repositories that the notification identifies,
	 * so the next request fetches the change that triggered the webhook. Expiry is best
	 * effort: a failure is logged and does not stop applications from being notified.
	 * @param notification the notification extracted from the webhook
	 */
	private void expireRefreshRates(PropertyPathNotification notification) {
		if (this.gitRepositories.isEmpty()) {
			return;
		}
		String[] repositoryUris = notification.getRepositoryUris();
		if (ObjectUtils.isEmpty(repositoryUris)) {
			log.debug("Notification does not identify a Git repository; refresh rate not expired");
			return;
		}
		boolean expired = false;
		boolean failed = false;
		for (String repositoryUri : repositoryUris) {
			for (JGitEnvironmentRepository repository : this.gitRepositories) {
				try {
					expired |= repository.expireRefreshRate(repositoryUri);
				}
				catch (RuntimeException ex) {
					failed = true;
					log.warn("Could not expire the refresh rate for " + repositoryUri, ex);
				}
			}
		}
		if (!expired && !failed) {
			log.debug("Notification repository URIs " + Arrays.toString(repositoryUris)
					+ " do not match a configured Git repository; refresh rate not expired");
		}
	}

	private Set<String> guessServiceName(String path) {
		Set<String> services = new LinkedHashSet<>();
		if (path != null) {
			String stem = StringUtils.stripFilenameExtension(StringUtils.getFilename(StringUtils.cleanPath(path)));
			// TODO: correlate with service registry
			String name = stem + "-";
			int index;
			// support application name with dashes, but bound the number of segments
			// processed so a long, heavily dashed path can't produce an
			// unbounded number of candidate service names
			for (int count = 0; count < this.maxDashes && (index = name.lastIndexOf("-")) >= 0; count++) {
				name = name.substring(0, index);
				if ("application".equals(name)) {
					services.add("*");
				}
				else {
					services.add(name);
				}
			}
			// if dashes remain we stopped early because the limit was reached
			if (name.lastIndexOf("-") >= 0) {
				log.warn("Number of dashes in path '" + path + "' exceeds the configured maximum of " + this.maxDashes
						+ " (spring.cloud.config.server.monitor.max-dashes); stopping service name resolution early");
			}
		}
		return services;
	}

	private boolean isIgnored(String path) {
		return path != null && this.ignoredPaths.stream().anyMatch(pattern -> pattern.matcher(path).matches());
	}

}