-
Notifications
You must be signed in to change notification settings - Fork 69
Add FailureAnalyzer for missing GrpcSecurity in servlet mode #340
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
81 changes: 81 additions & 0 deletions
81
.../springframework/boot/grpc/server/autoconfigure/GrpcSecurityInServletFailureAnalyzer.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,81 @@ | ||
| /* | ||
| * Copyright 2012-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.boot.grpc.server.autoconfigure; | ||
|
|
||
| import org.jspecify.annotations.Nullable; | ||
|
|
||
| import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
| import org.springframework.boot.diagnostics.AbstractFailureAnalyzer; | ||
| import org.springframework.boot.diagnostics.FailureAnalysis; | ||
| import org.springframework.boot.diagnostics.FailureAnalyzer; | ||
| import org.springframework.core.env.Environment; | ||
| import org.springframework.grpc.server.security.GrpcSecurity; | ||
| import org.springframework.util.ClassUtils; | ||
|
|
||
| /** | ||
| * {@link FailureAnalyzer} for missing {@link GrpcSecurity} in servlet-based gRPC | ||
| * applications. | ||
| * | ||
| * @author Andrey Litvitski | ||
| */ | ||
| public class GrpcSecurityInServletFailureAnalyzer extends AbstractFailureAnalyzer<NoSuchBeanDefinitionException> { | ||
|
|
||
| private static final String GRPC_SECURITY_PATH = "org.springframework.grpc.server.security.GrpcSecurity"; | ||
|
|
||
| private static final String GRPC_SERVLET_PATH = "io.grpc.servlet.jakarta.GrpcServlet"; | ||
|
|
||
| private final Environment environment; | ||
|
|
||
| public GrpcSecurityInServletFailureAnalyzer(Environment environment) { | ||
| this.environment = environment; | ||
| } | ||
|
|
||
| @Override | ||
| protected @Nullable FailureAnalysis analyze(Throwable rootFailure, NoSuchBeanDefinitionException cause) { | ||
| if (!isMissingGrpcSecurity(cause) || !isActuallyServletMode()) { | ||
| return null; | ||
| } | ||
| return new FailureAnalysis(getDescription(), getAction(), cause); | ||
| } | ||
|
|
||
| private boolean isMissingGrpcSecurity(NoSuchBeanDefinitionException ex) { | ||
| Class<?> type = ex.getBeanType(); | ||
| return type != null && GRPC_SECURITY_PATH.equals(type.getName()); | ||
| } | ||
|
|
||
| private boolean isActuallyServletMode() { | ||
| boolean servletOnClassPath = ClassUtils.isPresent(GRPC_SERVLET_PATH, getClass().getClassLoader()); | ||
| if (!servletOnClassPath) { | ||
| return false; | ||
| } | ||
| String servletPropertyName = "spring.grpc.server.servlet.enabled"; | ||
| return this.environment.getProperty(servletPropertyName, Boolean.class, true); | ||
| } | ||
|
|
||
| private String getDescription() { | ||
| return """ | ||
| GrpcSecurity is not available because this application is running the gRPC server in servlet mode (GrpcServlet). | ||
| """; | ||
| } | ||
|
|
||
| private String getAction() { | ||
| return """ | ||
| Configure security using the standard Spring Security servlet configuration (SecurityFilterChain / HttpSecurity). | ||
| """; | ||
| } | ||
|
|
||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
71 changes: 71 additions & 0 deletions
71
...ngframework/boot/grpc/server/autoconfigure/GrpcSecurityInServletFailureAnalyzerTests.java
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,71 @@ | ||
| /* | ||
| * Copyright 2012-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.boot.grpc.server.autoconfigure; | ||
|
|
||
| import static org.assertj.core.api.Assertions.assertThat; | ||
|
|
||
| import org.junit.jupiter.api.Test; | ||
|
|
||
| import org.springframework.beans.factory.NoSuchBeanDefinitionException; | ||
| import org.springframework.boot.diagnostics.FailureAnalysis; | ||
| import org.springframework.boot.test.context.runner.ApplicationContextRunner; | ||
| import org.springframework.core.env.Environment; | ||
| import org.springframework.grpc.server.security.GrpcSecurity; | ||
|
|
||
| /** | ||
| * Tests for {@link GrpcSecurityInServletFailureAnalyzer}. | ||
| * | ||
| * @author Andrey Litvitski | ||
| */ | ||
| class GrpcSecurityInServletFailureAnalyzerTests { | ||
|
|
||
| private final ApplicationContextRunner contextRunner = new ApplicationContextRunner(); | ||
|
|
||
| @Test | ||
| void whenServletModeEnabledThenGrpcSecurityFailureIsAnalyzed() { | ||
| this.contextRunner.withPropertyValues("spring.grpc.server.servlet.enabled=true").run((context) -> { | ||
| Environment environment = context.getEnvironment(); | ||
| GrpcSecurityInServletFailureAnalyzer analyzer = new GrpcSecurityInServletFailureAnalyzer(environment); | ||
| NoSuchBeanDefinitionException ex = new NoSuchBeanDefinitionException(GrpcSecurity.class); | ||
| FailureAnalysis analysis = analyzer.analyze(ex, ex); | ||
| assertThat(analysis).isNotNull(); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void whenServletModeDefaultThenGrpcSecurityFailureIsAnalyzed() { | ||
| this.contextRunner.run((context) -> { | ||
| Environment environment = context.getEnvironment(); | ||
| GrpcSecurityInServletFailureAnalyzer analyzer = new GrpcSecurityInServletFailureAnalyzer(environment); | ||
| NoSuchBeanDefinitionException ex = new NoSuchBeanDefinitionException(GrpcSecurity.class); | ||
| FailureAnalysis analysis = analyzer.analyze(ex, ex); | ||
| assertThat(analysis).isNotNull(); | ||
| }); | ||
| } | ||
|
|
||
| @Test | ||
| void whenServletModeDisabledThenGrpcSecurityFailureIsNotAnalyzed() { | ||
| this.contextRunner.withPropertyValues("spring.grpc.server.servlet.enabled=false").run((context) -> { | ||
| Environment environment = context.getEnvironment(); | ||
| GrpcSecurityInServletFailureAnalyzer analyzer = new GrpcSecurityInServletFailureAnalyzer(environment); | ||
| NoSuchBeanDefinitionException ex = new NoSuchBeanDefinitionException(GrpcSecurity.class); | ||
| FailureAnalysis analysis = analyzer.analyze(ex, ex); | ||
| assertThat(analysis).isNull(); | ||
| }); | ||
| } | ||
|
|
||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We want to call the current analyzer if and only if the thrown exception is related to
GrpcSecurityand when thespring.grpc.server.servlet.enabledoption is enabled and, accordingly,GrpcServletis present in the path.BTW, I'm not entirely sure whether
GrpcServletis always in the path or not, you can correct me if this is not the case, then we can simplify the check inisActuallyServletModea little.There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Depends what you mean by "always" doesn't it? If the failure analyzer is relevant then it will be.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I was referring to the situation where a user includes a starter, which in turn pulls in
io.grpc.grpc-nettyorio.grpc.grpc-servlet-jakarta.Basically, checking for the existence of the
GrpcServlet, and therefore checking whether it's in the path, is justified.