Skip to content
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

Tech - Ajout de keycloak et sécurisation des APIs (UPDATE ENV VAR) #3501

Merged
merged 8 commits into from
Aug 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ init-local-sig:
./infra/local/postgis_insert_layers.sh && ./infra/init/geoserver_init_layers.sh

run-back: run-stubbed-apis
docker compose up -d --quiet-pull --wait db
docker compose up -d --quiet-pull --wait db keycloak
cd backend && ./gradlew bootRun --args='--spring.profiles.active=local --spring.config.additional-location=$(INFRA_FOLDER)'

run-back-with-monitorenv: run-monitorenv
Expand Down Expand Up @@ -154,15 +154,15 @@ docker-compose-up:
docker compose -f ./infra/docker/docker-compose.cypress.yml up --quiet-pull flyway
docker compose -f ./infra/docker/docker-compose.cypress.yml up -d --quiet-pull app
@printf 'Waiting for backend app to be ready'
@until curl --output /dev/null --silent --fail "http://localhost:8880/bff/v1/healthcheck"; do printf '.' && sleep 1; done
@until curl --output /dev/null --silent --fail "http://localhost:8880/api/v1/healthcheck"; do printf '.' && sleep 1; done

docker-compose-puppeteer-up: docker-env
docker compose -f ./infra/docker/docker-compose.puppeteer.yml up -d monitorenv-app
docker compose -f ./infra/docker/docker-compose.puppeteer.yml up -d monitorfish-app
@printf 'Waiting for MonitorEnv app to be ready'
@until curl --output /dev/null --silent --fail "http://localhost:9880/bff/v1/healthcheck"; do printf '.' && sleep 1; done
@until curl --output /dev/null --silent --fail "http://localhost:9880/api/v1/healthcheck"; do printf '.' && sleep 1; done
@printf 'Waiting for MonitorFish app to be ready'
@until curl --output /dev/null --silent --fail "http://localhost:8880/bff/v1/healthcheck"; do printf '.' && sleep 1; done
@until curl --output /dev/null --silent --fail "http://localhost:8880/api/v1/healthcheck"; do printf '.' && sleep 1; done

# ----------------------------------------------------------
# CI: Pipeline Commands
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,9 @@ import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/bff/v1/control_objectives")
@RequestMapping("/bff/v1/admin/control_objectives")
@Tag(name = "APIs for Control objectives")
class ControlObjectiveController(
class ControlObjectiveAdminController(
private val getControlObjectivesOfYear: GetControlObjectivesOfYear,
private val getControlObjectiveYearEntries: GetControlObjectiveYearEntries,
private val addControlObjectiveYear: AddControlObjectiveYear,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.bff

import fr.gouv.cnsp.monitorfish.domain.use_cases.fleet_segment.*
import fr.gouv.cnsp.monitorfish.infrastructure.api.input.CreateOrUpdateFleetSegmentDataInput
import fr.gouv.cnsp.monitorfish.infrastructure.api.outputs.FleetSegmentDataOutput
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.websocket.server.PathParam
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/bff/v1/admin/fleet_segments")
@Tag(name = "APIs for administration of Fleet segments")
class FleetSegmentAdminController(
private val updateFleetSegment: UpdateFleetSegment,
private val deleteFleetSegment: DeleteFleetSegment,
private val createFleetSegment: CreateFleetSegment,
private val getFleetSegmentYearEntries: GetFleetSegmentYearEntries,
private val addFleetSegmentYear: AddFleetSegmentYear,
) {

@PutMapping(value = [""], consumes = ["application/json"])
@Operation(summary = "Update a fleet segment")
fun updateFleetSegment(
@Parameter(description = "Year")
@RequestParam(name = "year")
year: Int,
@Parameter(description = "Segment")
@RequestParam(name = "segment")
segment: String,
@RequestBody
createOrUpdateFleetSegmentData: CreateOrUpdateFleetSegmentDataInput,
): FleetSegmentDataOutput {
val updatedFleetSegment = updateFleetSegment.execute(
segment = segment,
fields = createOrUpdateFleetSegmentData.toCreateOrUpdateFleetSegmentFields(),
year = year,
)

return FleetSegmentDataOutput.fromFleetSegment(updatedFleetSegment)
}

@DeleteMapping(value = [""])
@Operation(summary = "Delete a fleet segment")
fun deleteFleetSegment(
@Parameter(description = "Year")
@RequestParam(name = "year")
year: Int,
@Parameter(description = "Segment")
@RequestParam(name = "segment")
segment: String,
): List<FleetSegmentDataOutput> {
return deleteFleetSegment.execute(segment, year).map {
FleetSegmentDataOutput.fromFleetSegment(it)
}
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping(value = [""])
@Operation(summary = "Create a fleet segment")
fun createFleetSegment(
@RequestBody
newFleetSegmentData: CreateOrUpdateFleetSegmentDataInput,
): FleetSegmentDataOutput {
val createdFleetSegment = createFleetSegment.execute(newFleetSegmentData.toCreateOrUpdateFleetSegmentFields())

return FleetSegmentDataOutput.fromFleetSegment(createdFleetSegment)
}

@GetMapping("/years")
@Operation(summary = "Get fleet segment year entries")
fun getFleetSegmentYearEntries(): List<Int> {
return getFleetSegmentYearEntries.execute()
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/{year}")
@Operation(summary = "Add a fleet segment year")
fun addFleetSegmentYear(
@PathParam("Year")
@PathVariable(name = "year")
year: Int,
) {
return addFleetSegmentYear.execute(year)
}
}
louptheron marked this conversation as resolved.
Show resolved Hide resolved
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
package fr.gouv.cnsp.monitorfish.infrastructure.api.bff

import fr.gouv.cnsp.monitorfish.domain.use_cases.fleet_segment.*
import fr.gouv.cnsp.monitorfish.infrastructure.api.input.CreateOrUpdateFleetSegmentDataInput
import fr.gouv.cnsp.monitorfish.domain.use_cases.fleet_segment.ComputeFleetSegments
import fr.gouv.cnsp.monitorfish.domain.use_cases.fleet_segment.GetAllFleetSegmentsByYear
import fr.gouv.cnsp.monitorfish.infrastructure.api.outputs.FleetSegmentDataOutput
import io.swagger.v3.oas.annotations.Operation
import io.swagger.v3.oas.annotations.Parameter
import io.swagger.v3.oas.annotations.tags.Tag
import jakarta.websocket.server.PathParam
import org.springframework.http.HttpStatus
import org.springframework.web.bind.annotation.*

@RestController
@RequestMapping("/bff/v1/fleet_segments")
@Tag(name = "APIs for Fleet segments")
class FleetSegmentController(
private val getAllFleetSegmentsByYearByYear: GetAllFleetSegmentsByYear,
private val updateFleetSegment: UpdateFleetSegment,
private val deleteFleetSegment: DeleteFleetSegment,
private val createFleetSegment: CreateFleetSegment,
private val getFleetSegmentYearEntries: GetFleetSegmentYearEntries,
private val addFleetSegmentYear: AddFleetSegmentYear,
private val computeFleetSegments: ComputeFleetSegments,
) {

Expand All @@ -35,71 +29,6 @@ class FleetSegmentController(
}
}

@PutMapping(value = [""], consumes = ["application/json"])
@Operation(summary = "Update a fleet segment")
fun updateFleetSegment(
@Parameter(description = "Year")
@RequestParam(name = "year")
year: Int,
@Parameter(description = "Segment")
@RequestParam(name = "segment")
segment: String,
@RequestBody
createOrUpdateFleetSegmentData: CreateOrUpdateFleetSegmentDataInput,
): FleetSegmentDataOutput {
val updatedFleetSegment = updateFleetSegment.execute(
segment = segment,
fields = createOrUpdateFleetSegmentData.toCreateOrUpdateFleetSegmentFields(),
year = year,
)

return FleetSegmentDataOutput.fromFleetSegment(updatedFleetSegment)
}

@DeleteMapping(value = [""])
@Operation(summary = "Delete a fleet segment")
fun deleteFleetSegment(
@Parameter(description = "Year")
@RequestParam(name = "year")
year: Int,
@Parameter(description = "Segment")
@RequestParam(name = "segment")
segment: String,
): List<FleetSegmentDataOutput> {
return deleteFleetSegment.execute(segment, year).map {
FleetSegmentDataOutput.fromFleetSegment(it)
}
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping(value = [""])
@Operation(summary = "Create a fleet segment")
fun createFleetSegment(
@RequestBody
newFleetSegmentData: CreateOrUpdateFleetSegmentDataInput,
): FleetSegmentDataOutput {
val createdFleetSegment = createFleetSegment.execute(newFleetSegmentData.toCreateOrUpdateFleetSegmentFields())

return FleetSegmentDataOutput.fromFleetSegment(createdFleetSegment)
}

@GetMapping("/years")
@Operation(summary = "Get fleet segment year entries")
fun getFleetSegmentYearEntries(): List<Int> {
return getFleetSegmentYearEntries.execute()
}

@ResponseStatus(HttpStatus.CREATED)
@PostMapping("/{year}")
@Operation(summary = "Add a fleet segment year")
fun addFleetSegmentYear(
@PathParam("Year")
@PathVariable(name = "year")
year: Int,
) {
return addFleetSegmentYear.execute(year)
}

@GetMapping("/compute")
@Operation(summary = "compute fleet segments for the current year")
fun computeFleetSegments(
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import org.springframework.test.web.servlet.result.MockMvcResultMatchers.status

@Import(SentryConfig::class)
@AutoConfigureMockMvc(addFilters = false)
@WebMvcTest(value = [(ControlObjectiveController::class)])
class ControlObjectiveControllerITests {
@WebMvcTest(value = [(ControlObjectiveAdminController::class)])
class ControlObjectiveAdminControllerITests {

@Autowired
private lateinit var api: MockMvc
Expand Down Expand Up @@ -53,7 +53,7 @@ class ControlObjectiveControllerITests {
fun `Should return Created When an update of a control objective is done`() {
// When
api.perform(
put("/bff/v1/control_objectives/123")
put("/bff/v1/admin/control_objectives/123")
.content(
objectMapper.writeValueAsString(UpdateControlObjectiveDataInput(targetNumberOfControlsAtSea = 123)),
)
Expand All @@ -66,7 +66,7 @@ class ControlObjectiveControllerITests {
@Test
fun `Should return Ok When a delete of a control objective is done`() {
// When
api.perform(delete("/bff/v1/control_objectives/123"))
api.perform(delete("/bff/v1/admin/control_objectives/123"))
// Then
.andExpect(status().isOk)
}
Expand All @@ -75,7 +75,7 @@ class ControlObjectiveControllerITests {
fun `Should return the id When a adding a control objective`() {
// When
api.perform(
post("/bff/v1/control_objectives")
post("/bff/v1/admin/control_objectives")
.content(
objectMapper.writeValueAsString(
AddControlObjectiveDataInput(segment = "SEGMENT", facade = "FACADE", year = 2021),
Expand Down Expand Up @@ -123,7 +123,7 @@ class ControlObjectiveControllerITests {
)

// When
api.perform(get("/bff/v1/control_objectives/2021"))
api.perform(get("/bff/v1/admin/control_objectives/2021"))
// Then
.andExpect(status().isOk)
.andExpect(jsonPath("$.length()", equalTo(3)))
Expand All @@ -135,7 +135,7 @@ class ControlObjectiveControllerITests {
given(this.getControlObjectiveYearEntries.execute()).willReturn(listOf(2021, 2022))

// When
api.perform(get("/bff/v1/control_objectives/years"))
api.perform(get("/bff/v1/admin/control_objectives/years"))
// Then
.andExpect(status().isOk)
.andExpect(jsonPath("$.length()", equalTo(2)))
Expand All @@ -145,7 +145,7 @@ class ControlObjectiveControllerITests {
@Test
fun `Should add a new control objective year`() {
// When
api.perform(post("/bff/v1/control_objectives/years"))
api.perform(post("/bff/v1/admin/control_objectives/years"))
// Then
.andExpect(status().isCreated)
}
Expand Down
Loading
Loading