Skip to content

Commit

Permalink
Merge pull request #300 from catenax-ng/feat/Gate_Auth_Configurable_P…
Browse files Browse the repository at this point in the history
…ermission

Feat(Gate): Configured Spring Security to use roles from JWT for API …
  • Loading branch information
nicoprow authored Jun 23, 2023
2 parents 92e0319 + a3df8fc commit f141875
Show file tree
Hide file tree
Showing 8 changed files with 82 additions and 14 deletions.
5 changes: 5 additions & 0 deletions bpdm-gate/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,11 @@
<artifactId>postgresql</artifactId>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.security</groupId>
<artifactId>spring-security-test</artifactId>
<scope>test</scope>
</dependency>
</dependencies>

<build>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,33 @@
package org.eclipse.tractusx.bpdm.gate.config

import org.springframework.boot.context.properties.ConfigurationProperties
import org.springframework.context.annotation.Configuration


@Configuration
@ConfigurationProperties(prefix = "bpdm.gate-security")
data class GateSecurityConfigProperties(
val oauth2ClientRegistration: String = "gate-client",
val poolSecurityEnabled: Boolean = false
)
var oauth2ClientRegistration: String = "gate-client",
var poolSecurityEnabled: Boolean = false,
var readCompanyInputData: String = "read_company_input_data",
var changeCompanyInputData: String = "change_company_input_data",
var readCompanyOutputData: String = "read_company_output_data",
var changeCompanyOutputData: String = "change_company_output_data"
){

fun getReadCompanyInputDataAsRole(): String {
return "ROLE_$readCompanyInputData"
}

fun getChangeCompanyInputDataAsRole(): String {
return "ROLE_$changeCompanyInputData"
}

fun getReadCompanyOutputDataAsRole(): String {
return "ROLE_$readCompanyOutputData"
}

fun getChangeCompanyOutputDataAsRole(): String {
return "ROLE_$changeCompanyOutputData"
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -27,22 +27,28 @@ import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateInputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.AddressGateOutputResponse
import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties
import org.eclipse.tractusx.bpdm.gate.config.GateSecurityConfigProperties
import org.eclipse.tractusx.bpdm.gate.containsDuplicates
import org.eclipse.tractusx.bpdm.gate.service.AddressService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.RestController

@RestController
class AddressController(
private val addressService: AddressService,
private val apiConfigProperties: ApiConfigProperties
private val apiConfigProperties: ApiConfigProperties,
val gateSecurityConfigProperties: GateSecurityConfigProperties
) : GateAddressApi {


@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getChangeCompanyInputDataAsRole())")
override fun upsertAddresses(addresses: Collection<AddressGateInputRequest>): ResponseEntity<Unit> {
if (addresses.size > apiConfigProperties.upsertLimit || addresses.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
}

if (addresses.any {
(it.siteExternalId == null && it.legalEntityExternalId == null) || (it.siteExternalId != null && it.legalEntityExternalId != null)
}) {
Expand All @@ -52,28 +58,34 @@ class AddressController(
return ResponseEntity(HttpStatus.OK)
}


@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getAddressByExternalId(externalId: String): AddressGateInputResponse {
gateSecurityConfigProperties.getReadCompanyOutputDataAsRole()
return addressService.getAddressByExternalId(externalId)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getAddressesByExternalIds(
paginationRequest: PaginationRequest,
externalIds: Collection<String>
): PageResponse<AddressGateInputResponse> {
return addressService.getAddresses(page = paginationRequest.page, size = paginationRequest.size, externalIds = externalIds)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getAddresses(paginationRequest: PaginationRequest): PageResponse<AddressGateInputResponse> {
return addressService.getAddresses(page = paginationRequest.page, size = paginationRequest.size)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyOutputDataAsRole())")
override fun getAddressesOutput(
paginationRequest: PaginationRequest,
externalIds: Collection<String>?
): PageResponse<AddressGateOutputResponse> {
return addressService.getAddressesOutput(externalIds = externalIds, page = paginationRequest.page, size = paginationRequest.size)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.gateSecurityConfigProperties.getChangeCompanyOutputDataAsRole())")
override fun putAddressesOutput(addresses: Collection<AddressGateOutputRequest>): ResponseEntity<Unit> {
if (addresses.size > apiConfigProperties.upsertLimit || addresses.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,22 +25,27 @@ import org.eclipse.tractusx.bpdm.gate.api.GateChangelogApi
import org.eclipse.tractusx.bpdm.gate.api.model.request.ChangeLogSearchRequest
import org.eclipse.tractusx.bpdm.gate.api.model.response.ChangelogResponse
import org.eclipse.tractusx.bpdm.gate.api.model.response.PageChangeLogResponse
import org.eclipse.tractusx.bpdm.gate.config.GateSecurityConfigProperties
import org.eclipse.tractusx.bpdm.gate.service.ChangelogService
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.validation.annotation.Validated
import org.springframework.web.bind.annotation.RestController

@RestController
@Validated
class ChangelogController(
private val changelogService: ChangelogService
private val changelogService: ChangelogService,
val gateSecurityConfigProperties: GateSecurityConfigProperties
) : GateChangelogApi {

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getInputChangelog(
paginationRequest: PaginationRequest, searchRequest: ChangeLogSearchRequest
): PageChangeLogResponse<ChangelogResponse> {
return changelogService.getChangeLogEntries(searchRequest.externalIds, searchRequest.lsaTypes, searchRequest.fromTime,OutputInputEnum.Input, paginationRequest.page, paginationRequest.size)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyOutputDataAsRole())")
override fun getOutputChangelog(paginationRequest: PaginationRequest,
searchRequest: ChangeLogSearchRequest): PageChangeLogResponse<ChangelogResponse> {

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateInputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.LegalEntityGateOutputResponse
import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties
import org.eclipse.tractusx.bpdm.gate.config.GateSecurityConfigProperties
import org.eclipse.tractusx.bpdm.gate.containsDuplicates
import org.eclipse.tractusx.bpdm.gate.service.LegalEntityService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.RestController

@RestController
class LegalEntityController(
val legalEntityService: LegalEntityService,
val apiConfigProperties: ApiConfigProperties
val apiConfigProperties: ApiConfigProperties,
val gateSecurityConfigProperties: GateSecurityConfigProperties
) : GateLegalEntityApi {

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getChangeCompanyInputDataAsRole())")
override fun upsertLegalEntities(legalEntities: Collection<LegalEntityGateInputRequest>): ResponseEntity<Unit> {
if (legalEntities.size > apiConfigProperties.upsertLimit || legalEntities.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
Expand All @@ -47,28 +50,31 @@ class LegalEntityController(
return ResponseEntity(HttpStatus.OK)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getLegalEntityByExternalId(externalId: String): LegalEntityGateInputResponse {
return legalEntityService.getLegalEntityByExternalId(externalId)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getLegalEntitiesByExternalIds(
paginationRequest: PaginationRequest,
externalIds: Collection<String>
): PageResponse<LegalEntityGateInputResponse> {
return legalEntityService.getLegalEntities(page = paginationRequest.page, size = paginationRequest.size, externalIds = externalIds)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getLegalEntities(paginationRequest: PaginationRequest): PageResponse<LegalEntityGateInputResponse> {
return legalEntityService.getLegalEntities(page = paginationRequest.page, size = paginationRequest.size)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyOutputDataAsRole())")
override fun getLegalEntitiesOutput(
paginationRequest: PaginationRequest,
externalIds: Collection<String>?
): PageResponse<LegalEntityGateOutputResponse> {
return legalEntityService.getLegalEntitiesOutput(externalIds = externalIds, page = paginationRequest.page, size = paginationRequest.size)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getChangeCompanyOutputDataAsRole())")
override fun upsertLegalEntitiesOutput(legalEntities: Collection<LegalEntityGateOutputRequest>): ResponseEntity<Unit> {
if (legalEntities.size > apiConfigProperties.upsertLimit || legalEntities.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,23 +25,26 @@ import org.eclipse.tractusx.bpdm.common.dto.response.PageResponse
import org.eclipse.tractusx.bpdm.gate.api.GateSharingStateApi
import org.eclipse.tractusx.bpdm.gate.api.model.LsaType
import org.eclipse.tractusx.bpdm.gate.api.model.SharingStateDto
import org.eclipse.tractusx.bpdm.gate.config.GateSecurityConfigProperties
import org.eclipse.tractusx.bpdm.gate.service.SharingStateService
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.RestController

@RestController
class SharingStateController(
val sharingStateService: SharingStateService
val sharingStateService: SharingStateService,
val gateSecurityConfigProperties: GateSecurityConfigProperties
) : GateSharingStateApi {

private val logger = KotlinLogging.logger { }

@PreAuthorize("hasAuthority(gateSecurityConfigProperties.getReadCompanyOutputDataAsRole())")
override fun getSharingStates(paginationRequest: PaginationRequest, lsaType: LsaType?, externalIds: Collection<String>?): PageResponse<SharingStateDto> {

return sharingStateService.findSharingStates(paginationRequest, lsaType, externalIds)
}

@PreAuthorize("hasAuthority(gateSecurityConfigProperties.getChangeCompanyOutputDataAsRole())")
override fun upsertSharingState(request: SharingStateDto) {

logger.info { "upsertSharingState() called with $request" }
sharingStateService.upsertSharingState(request)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,23 @@ import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateInputResponse
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputRequest
import org.eclipse.tractusx.bpdm.gate.api.model.SiteGateOutputResponse
import org.eclipse.tractusx.bpdm.gate.config.ApiConfigProperties
import org.eclipse.tractusx.bpdm.gate.config.GateSecurityConfigProperties
import org.eclipse.tractusx.bpdm.gate.containsDuplicates
import org.eclipse.tractusx.bpdm.gate.service.SiteService
import org.springframework.http.HttpStatus
import org.springframework.http.ResponseEntity
import org.springframework.security.access.prepost.PreAuthorize
import org.springframework.web.bind.annotation.RestController

@RestController
class SiteController(
val siteService: SiteService,
val apiConfigProperties: ApiConfigProperties
val apiConfigProperties: ApiConfigProperties,
val gateSecurityConfigProperties: GateSecurityConfigProperties
) : GateSiteApi {


@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getChangeCompanyInputDataAsRole())")
override fun upsertSites(sites: Collection<SiteGateInputRequest>): ResponseEntity<Unit> {
if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
Expand All @@ -47,25 +52,30 @@ class SiteController(
return ResponseEntity(HttpStatus.OK)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getSiteByExternalId(externalId: String): SiteGateInputResponse {
return siteService.getSiteByExternalId(externalId)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getSitesByExternalIds(
paginationRequest: PaginationRequest,
externalIds: Collection<String>
): PageResponse<SiteGateInputResponse> {
return siteService.getSites(page = paginationRequest.page, size = paginationRequest.size, externalIds = externalIds)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyInputDataAsRole())")
override fun getSites(paginationRequest: PaginationRequest): PageResponse<SiteGateInputResponse> {
return siteService.getSites(page = paginationRequest.page, size = paginationRequest.size)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getReadCompanyOutputDataAsRole())")
override fun getSitesOutput(paginationRequest: PaginationRequest, externalIds: Collection<String>?): PageResponse<SiteGateOutputResponse> {
return siteService.getSitesOutput(externalIds = externalIds, page = paginationRequest.page, size = paginationRequest.size)
}

@PreAuthorize("hasAuthority(@gateSecurityConfigProperties.getChangeCompanyOutputDataAsRole())")
override fun upsertSitesOutput(sites: Collection<SiteGateOutputRequest>): ResponseEntity<Unit> {
if (sites.size > apiConfigProperties.upsertLimit || sites.map { it.externalId }.containsDuplicates()) {
return ResponseEntity(HttpStatus.BAD_REQUEST)
Expand Down
4 changes: 4 additions & 0 deletions bpdm-gate/src/main/resources/application-auth.properties
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,10 @@
# Enables and configures security for the application
bpdm.security.enabled=true
bpdm.security.cors-origins=*
bpdm.gate-security.readCompanyInputData=read_company_input_data
bpdm.gate-security.changeCompanyInputData=change_company_input_data
bpdm.gate-security.readCompanyOutputData=read_company_output_data
bpdm.gate-security.changeCompanyOutputData=change_company_output_data
#Generic OAuth configuration
bpdm.security.client-id=BPDM_GATE
bpdm.security.realm=master
Expand Down

0 comments on commit f141875

Please sign in to comment.