Skip to content

Commit

Permalink
chore: update cors config
Browse files Browse the repository at this point in the history
  • Loading branch information
NilsOveTen committed Dec 10, 2024
1 parent 9369f7a commit 63059e4
Show file tree
Hide file tree
Showing 11 changed files with 28 additions and 12 deletions.
2 changes: 2 additions & 0 deletions deploy/demo/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ spec:
secretKeyRef:
name: commonurl-demo
key: DATASET_CATALOG_BASE_URI
- name: CORS_ORIGIN_PATTERNS
value: https://*.demo.fellesdatakatalog.digdir.no
2 changes: 2 additions & 0 deletions deploy/prod/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ spec:
secretKeyRef:
name: commonurl-prod
key: DATASET_CATALOG_BASE_URI
- name: CORS_ORIGIN_PATTERNS
value: https://*.fellesdatakatalog.digdir.no
2 changes: 2 additions & 0 deletions deploy/staging/env.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -76,3 +76,5 @@ spec:
secretKeyRef:
name: commonurl-staging
key: DATASET_CATALOG_BASE_URI
- name: CORS_ORIGIN_PATTERNS
value: https://*.staging.fellesdatakatalog.digdir.no,http://localhost:*
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,6 @@ import org.springframework.boot.context.properties.ConfigurationProperties

@ConfigurationProperties("security")
data class SecurityProperties(
val fdkIssuer: String
)
val fdkIssuer: String,
val corsOriginPatterns: List<String>
)
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,10 @@ package no.fdk.dataset_catalog.controller
import no.fdk.dataset_catalog.service.DatasetService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.CrossOrigin
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.RestController

@RestController
@CrossOrigin
class ApplicationStatusController(private val datasetService: DatasetService) {

@GetMapping("/ping")
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,6 @@ import org.springframework.web.bind.annotation.*
private val logger = LoggerFactory.getLogger(CatalogController::class.java)

@RestController
@CrossOrigin
@RequestMapping(value = ["/catalogs"])
class CatalogController(
private val catalogService: CatalogService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ import org.springframework.web.bind.annotation.*
private val logger = LoggerFactory.getLogger(DatasetController::class.java)

@RestController
@CrossOrigin
@RequestMapping(value = ["/catalogs/{catalogId}/datasets"])
class DatasetController(
private val datasetService: DatasetService,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@ import org.springframework.http.ResponseEntity
import org.springframework.web.bind.annotation.*

@RestController
@CrossOrigin
@RequestMapping(
value = ["/catalogs"],
produces = ["text/turtle", "text/n3", "application/rdf+json", "application/ld+json", "application/rdf+xml",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,6 @@ import org.springframework.web.bind.annotation.*
private val logger = LoggerFactory.getLogger(SearchController::class.java)

@RestController
@CrossOrigin
@RequestMapping(value = ["/search"])
class SearchController (
private val searchService: SearchService) {
Expand All @@ -36,4 +35,3 @@ class SearchController (
ResponseEntity(HttpStatus.BAD_REQUEST)
}
}

19 changes: 16 additions & 3 deletions src/main/kotlin/no/fdk/dataset_catalog/security/SecurityConfig.kt
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import org.springframework.security.oauth2.jwt.*
import org.springframework.security.web.SecurityFilterChain
import org.springframework.security.web.util.matcher.RequestMatcher
import jakarta.servlet.http.HttpServletRequest
import org.springframework.web.cors.CorsConfiguration

@Configuration
open class SecurityConfig(
Expand All @@ -20,15 +21,27 @@ open class SecurityConfig(

@Bean
open fun filterChain(http: HttpSecurity): SecurityFilterChain {
http.csrf().disable()
.cors().and()
http
.cors { cors ->
cors.configurationSource { _ ->
val config = CorsConfiguration()
config.allowCredentials = false
config.allowedHeaders = listOf("*")
config.maxAge = 3600L
config.allowedOriginPatterns = securityProperties.corsOriginPatterns
config.allowedMethods = listOf("GET", "POST", "OPTIONS", "DELETE", "PUT", "PATCH")

config
}
}
.csrf { it.disable() }
.authorizeHttpRequests{ authorize ->
authorize.requestMatchers(RDFMatcher()).permitAll()
.requestMatchers(HttpMethod.OPTIONS).permitAll()
.requestMatchers(HttpMethod.GET,"/ping").permitAll()
.requestMatchers(HttpMethod.GET,"/ready").permitAll()
.anyRequest().authenticated() }
.oauth2ResourceServer { resourceServer -> resourceServer.jwt() }
.oauth2ResourceServer { resourceServer -> resourceServer.jwt { } }
return http.build()
}

Expand Down
3 changes: 3 additions & 0 deletions src/main/resources/application.yml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ application:
exchangeName: harvests
security:
fdkIssuer: ${OIDC_ISSUER:https://sso.staging.fellesdatakatalog.digdir.no/auth/realms/fdk}
corsOriginPatterns: "${CORS_ORIGIN_PATTERNS}"

---
spring:
Expand All @@ -53,6 +54,7 @@ application:
catalogHarvestRoute: dataset.publisher.HarvestTrigger
newDataSourceRoute: dataset.publisher.NewDataSource
exchangeName: harvests
security.corsOriginPatterns: "*"

---
spring:
Expand All @@ -67,3 +69,4 @@ application:
catalogHarvestRoute: dataset.publisher.HarvestTrigger
newDataSourceRoute: dataset.publisher.NewDataSource
exchangeName: harvests
security.corsOriginPatterns: "*"

0 comments on commit 63059e4

Please sign in to comment.