From 175e851cb746550379fce0686b11752f4df04070 Mon Sep 17 00:00:00 2001 From: Scott Frederick Date: Mon, 18 May 2020 16:23:30 -0500 Subject: [PATCH] Add ClientNotConfiguredFailureAnalyzer --- .../ClientNotConfiguredFailureAnalyzer.java | 70 +++++++++++++++++++ .../main/resources/META-INF/spring.factories | 3 + 2 files changed, 73 insertions(+) create mode 100644 spring-credhub-starter/src/main/java/org/springframework/credhub/diagnostics/ClientNotConfiguredFailureAnalyzer.java diff --git a/spring-credhub-starter/src/main/java/org/springframework/credhub/diagnostics/ClientNotConfiguredFailureAnalyzer.java b/spring-credhub-starter/src/main/java/org/springframework/credhub/diagnostics/ClientNotConfiguredFailureAnalyzer.java new file mode 100644 index 0000000..27b2631 --- /dev/null +++ b/spring-credhub-starter/src/main/java/org/springframework/credhub/diagnostics/ClientNotConfiguredFailureAnalyzer.java @@ -0,0 +1,70 @@ +/* + * Copyright 2016-2020 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.credhub.diagnostics; + +import org.springframework.beans.BeansException; +import org.springframework.beans.factory.BeanFactory; +import org.springframework.beans.factory.BeanFactoryAware; +import org.springframework.beans.factory.NoSuchBeanDefinitionException; +import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; +import org.springframework.boot.diagnostics.FailureAnalysis; +import org.springframework.boot.diagnostics.FailureAnalyzer; + +/** + * A {@link FailureAnalyzer} that detects the condition when Spring CredHub has been configured + * with an OAuth2 client but beans required for this configuration are missing. + * + * @author Scott Frederick + */ +class ClientNotConfiguredFailureAnalyzer extends AbstractFailureAnalyzer + implements BeanFactoryAware { + + private BeanFactory beanFactory; + + @Override + protected FailureAnalysis analyze(Throwable rootFailure, NoSuchBeanDefinitionException cause) { + if (hasCredHubProperties() && isMissingSpringSecurityOAuth2Bean(cause.getBeanType())) { + return new FailureAnalysis(getDescription(), getAction(), cause); + } + return null; + } + + private boolean hasCredHubProperties() { + return this.beanFactory.containsBean("credHubProperties"); + } + + private boolean isMissingSpringSecurityOAuth2Bean(Class notFoundBean) { + return notFoundBean != null && (notFoundBean.getName().contains("ClientRegistrationRepository") || + notFoundBean.getName().contains("OAuth2AuthorizedClientRepository")); + } + + private String getDescription() { + return "A CredHub OAuth2 client registration is configured " + + "but Spring Security is not available or the Spring Security OAuth2 " + + "client registration not configured correctly."; + } + + private String getAction() { + return "Add Spring Security to the application classpath and configure properties for the " + + "OAuth2 client registration under 'spring.security.oauth2.client.registration'."; + } + + @Override + public void setBeanFactory(BeanFactory beanFactory) throws BeansException { + this.beanFactory = beanFactory; + } +} diff --git a/spring-credhub-starter/src/main/resources/META-INF/spring.factories b/spring-credhub-starter/src/main/resources/META-INF/spring.factories index c47d2d7..041302d 100644 --- a/spring-credhub-starter/src/main/resources/META-INF/spring.factories +++ b/spring-credhub-starter/src/main/resources/META-INF/spring.factories @@ -2,3 +2,6 @@ org.springframework.boot.autoconfigure.EnableAutoConfiguration=\ org.springframework.credhub.autoconfig.CredHubAutoConfiguration,\ org.springframework.credhub.autoconfig.CredHubOAuth2AutoConfiguration,\ org.springframework.credhub.autoconfig.CredHubTemplateAutoConfiguration + +org.springframework.boot.diagnostics.FailureAnalyzer=\ +org.springframework.credhub.diagnostics.ClientNotConfiguredFailureAnalyzer