-
Notifications
You must be signed in to change notification settings - Fork 10
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
Feat: Add API Key security #91
Merged
mhellmeier
merged 22 commits into
eclipse-tractusx:main
from
FraunhoferISST:feat/security
Nov 27, 2023
Merged
Changes from all commits
Commits
Show all changes
22 commits
Select commit
Hold shift + click to select a range
9f5d7c0
feat: initial code for api key security
tom-rm-meyer-ISST 4873303
refactor: moved all to security package
tom-rm-meyer-ISST 9f83cce
feat: added api key authentication statically
tom-rm-meyer-ISST 5288373
feat: externalized api key to application properties
tom-rm-meyer-ISST d3635b8
refactor: renamed security classes to ApiKey*
tom-rm-meyer-ISST 087f603
feat: added apiKey to stockView
tom-rm-meyer-ISST 0b24d38
fix: renamed wrong environment properties for local deployment
tom-rm-meyer-ISST c031590
feat: added cors integration
tom-rm-meyer-ISST 18741d1
ci: added backend api key to integrationtest env
tom-rm-meyer-ISST f775683
feat: added api-key usage to PartnerStockSFC
tom-rm-meyer-ISST 20f31a2
feat: prepared asset creation and dynamic EDR in transfer for api key
tom-rm-meyer-ISST 8358eb2
Merge branch 'main' into feat/security
tom-rm-meyer-ISST b831fa2
feat: got dynamic EDR running with api key by configutation
tom-rm-meyer-ISST eb8d39e
feat: added dynamic endpoint for EDR and noted hints for 0.5.x
tom-rm-meyer-ISST 755c3f3
feat: excluded swagger from api key security
tom-rm-meyer-ISST b7ffb78
chore: added missing license headers
tom-rm-meyer-ISST cfd77aa
chore: changed intendation
tom-rm-meyer-ISST 03fd06f
feat: adjusted to framework usage
tom-rm-meyer-ISST ec71dc9
chore: added class documentation
tom-rm-meyer-ISST c221a8e
chore: increased springdoc version
tom-rm-meyer-ISST 6783a83
fix: removed duplicate registration of filter
tom-rm-meyer-ISST aaaa1fb
fix: removed unnecessary exclusions
tom-rm-meyer-ISST 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
67 changes: 67 additions & 0 deletions
67
backend/src/main/java/org/eclipse/tractusx/puris/backend/common/security/SecurityConfig.java
This file contains 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,67 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.common.security; | ||
|
||
|
||
import lombok.AllArgsConstructor; | ||
import org.eclipse.tractusx.puris.backend.common.security.logic.ApiKeyAuthenticationFilter; | ||
import org.springframework.context.annotation.Bean; | ||
import org.springframework.context.annotation.Configuration; | ||
import org.springframework.security.config.Customizer; | ||
import org.springframework.security.config.annotation.web.builders.HttpSecurity; | ||
import org.springframework.security.config.annotation.web.configuration.EnableWebSecurity; | ||
import org.springframework.security.config.annotation.web.configurers.AbstractHttpConfigurer; | ||
import org.springframework.security.config.http.SessionCreationPolicy; | ||
import org.springframework.security.web.SecurityFilterChain; | ||
import org.springframework.security.web.authentication.UsernamePasswordAuthenticationFilter; | ||
|
||
@Configuration | ||
@EnableWebSecurity | ||
@AllArgsConstructor | ||
public class SecurityConfig { | ||
|
||
private final ApiKeyAuthenticationFilter apiKeyAuthenticationFilter; | ||
|
||
/** | ||
* Configuration of API Key Authentication for all routes except docker | ||
*/ | ||
@Bean | ||
public SecurityFilterChain filterChain(HttpSecurity http) throws Exception { | ||
http.csrf(AbstractHttpConfigurer::disable) | ||
.authorizeHttpRequests( | ||
// any request in spring context | ||
(authorizeHttpRequests) -> authorizeHttpRequests | ||
.requestMatchers("/stockView/**", "/partners/**", "/materials/**", "/materialpartnerrelations/**", "/product-stock/**", "/edrendpoint/**").authenticated() | ||
.requestMatchers("/swagger-ui/**", "/v3/api-docs/**").permitAll() | ||
) | ||
.httpBasic( | ||
AbstractHttpConfigurer::disable | ||
) | ||
.sessionManagement( | ||
(sessionManagement) -> sessionManagement.sessionCreationPolicy(SessionCreationPolicy.STATELESS) | ||
) | ||
.cors(Customizer.withDefaults()); | ||
|
||
http.addFilterBefore(apiKeyAuthenticationFilter, UsernamePasswordAuthenticationFilter.class); | ||
|
||
return http.build(); | ||
} | ||
|
||
} |
74 changes: 74 additions & 0 deletions
74
.../java/org/eclipse/tractusx/puris/backend/common/security/domain/ApiKeyAuthentication.java
This file contains 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,74 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.common.security.domain; | ||
|
||
import lombok.AllArgsConstructor; | ||
import lombok.Getter; | ||
import lombok.Setter; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.GrantedAuthority; | ||
|
||
import java.util.Collection; | ||
|
||
/** | ||
* Authentication holding apiKey as principal and authenticated flag. No authorities given as the key is set per config. | ||
*/ | ||
@Getter | ||
@Setter | ||
@AllArgsConstructor | ||
public class ApiKeyAuthentication implements Authentication { | ||
|
||
private final String apiKey; | ||
private final boolean authenticatedFlag; | ||
@Override | ||
public Collection<? extends GrantedAuthority> getAuthorities() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object getCredentials() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object getDetails() { | ||
return null; | ||
} | ||
|
||
@Override | ||
public Object getPrincipal() { | ||
return apiKey; | ||
} | ||
|
||
@Override | ||
public boolean isAuthenticated() { | ||
return authenticatedFlag; | ||
} | ||
|
||
@Override | ||
public void setAuthenticated(boolean isAuthenticated) throws IllegalArgumentException { | ||
|
||
} | ||
|
||
@Override | ||
public String getName() { | ||
return null; | ||
} | ||
} |
56 changes: 56 additions & 0 deletions
56
.../org/eclipse/tractusx/puris/backend/common/security/logic/ApiKeyAuthenticationFilter.java
This file contains 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,56 @@ | ||
/* | ||
* Copyright (c) 2023 Volkswagen AG | ||
* Copyright (c) 2023 Contributors to the Eclipse Foundation | ||
* | ||
* See the NOTICE file(s) distributed with this work for additional | ||
* information regarding copyright ownership. | ||
* | ||
* This program and the accompanying materials are made available under the | ||
* terms of the Apache License, Version 2.0 which is available 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. | ||
* | ||
* SPDX-License-Identifier: Apache-2.0 | ||
*/ | ||
package org.eclipse.tractusx.puris.backend.common.security.logic; | ||
|
||
import jakarta.servlet.FilterChain; | ||
import jakarta.servlet.ServletException; | ||
import jakarta.servlet.http.HttpServletRequest; | ||
import jakarta.servlet.http.HttpServletResponse; | ||
import lombok.AllArgsConstructor; | ||
import org.eclipse.tractusx.puris.backend.common.security.domain.ApiKeyAuthentication; | ||
import org.springframework.security.core.Authentication; | ||
import org.springframework.security.core.context.SecurityContextHolder; | ||
import org.springframework.stereotype.Component; | ||
import org.springframework.web.filter.OncePerRequestFilter; | ||
|
||
import java.io.IOException; | ||
|
||
/** | ||
* Authentication filter that checks if X-API-KEY header is given and set to value from config | ||
*/ | ||
@Component | ||
@AllArgsConstructor | ||
public class ApiKeyAuthenticationFilter extends OncePerRequestFilter { | ||
|
||
public final String API_KEY_HEADER = "X-API-KEY"; | ||
private final ApiKeyAuthenticationProvider apiKeyAuthenticationProvider; | ||
@Override | ||
protected void doFilterInternal(HttpServletRequest request, HttpServletResponse response, FilterChain filterChain) throws ServletException, IOException { | ||
String headerKey = request.getHeader(API_KEY_HEADER); | ||
|
||
if (headerKey != null){ | ||
ApiKeyAuthentication apiKeyAuthentication = new ApiKeyAuthentication(headerKey, false); | ||
Authentication authenticatedObject = apiKeyAuthenticationProvider.authenticate(apiKeyAuthentication); | ||
SecurityContextHolder.getContext().setAuthentication(authenticatedObject); | ||
} | ||
|
||
filterChain.doFilter(request,response); | ||
} | ||
} |
Oops, something went wrong.
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.
Those TODOs should be extracted into the issue board of the project to keep track of it.