Skip to content

Commit

Permalink
Merge branch 'main' into sys/build-and-push-workflow
Browse files Browse the repository at this point in the history
  • Loading branch information
olekfur authored Jan 16, 2024
2 parents 17945de + ffd55f0 commit 0133f10
Show file tree
Hide file tree
Showing 8 changed files with 86 additions and 17 deletions.
16 changes: 16 additions & 0 deletions .dockerignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
# flyctl launch added from .gitignore
# Default ignored files
shelf
.idea/workspace.xml
# Editor-based HTTP Client requests
httpRequests
# Datasource local storage ignored files
dataSources
dataSources.local.xml

**/build
**/.idea
**/*.iml
**/.gradle
out
**/http-client.private.env.json
12 changes: 12 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
FROM eclipse-temurin:21 as build
COPY . .
RUN ./gradlew build -x test

FROM eclipse-temurin:21
COPY --from=build /build/libs/*.jar backend.jar
COPY .sikkerhet .sikkerhet
ADD https://github.com/getsops/sops/releases/download/v3.8.1/sops-v3.8.1.linux.arm64 /usr/local/bin/sops
RUN chmod +x /usr/local/bin/sops

EXPOSE 8080
ENTRYPOINT ["java", "-jar", "backend.jar"]
16 changes: 15 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,4 +6,18 @@ The JSON schema validation is done using the [json-kotlin-schema](https://github
This library has some limitations.
It does not fully support the latest JSON Schema draft.
It covers our need regarding the JSON Schema validation.
If the version of the schema is updated, ensure that the library supports it.
If the version of the schema is updated, ensure that the library supports it.

### Docker

To build the docker image, run:

```sh
docker image build -t kv-ros-backend .
```

To run the docker image, run:

```sh
docker run -it -p 8080:8080 -e SOPS_AGE_PUBLIC_KEY=${SOPS_AGE_PUBLIC_KEY} -e SOPS_AGE_KEY=${SOPS_AGE_PRIVATE_KEY} kv-ros-backend
```
33 changes: 23 additions & 10 deletions src/main/kotlin/no/kvros/ros/GithubConnector.kt
Original file line number Diff line number Diff line change
@@ -1,19 +1,24 @@
package no.kvros.ros

import no.kvros.infra.connector.WebClientConnector
import no.kvros.ros.models.ROSDownloadUrl
import no.kvros.ros.models.ROSDownloadUrlDTO
import no.kvros.ros.models.ShaResponseDTO
import org.springframework.stereotype.Component
import org.springframework.web.reactive.function.client.WebClient.ResponseSpec
import org.springframework.web.reactive.function.client.bodyToMono
import reactor.core.publisher.Mono
import java.util.UUID
import java.util.*

data class GithubWritePayload(
val message: String,
val content: String,
// val sha: String - denne må brukes når vi skal oppdatere noe -> kommer snart
val sha: String? = null
) {
fun toContentBody(): String = "{\"message\":\"$message\", \"content\":\"$content\"}"
fun toContentBody(): String =
when (sha) {
null -> "{\"message\":\"$message\", \"content\":\"$content\"}"
else -> "{\"message\":\"$message\", \"content\":\"$content\", \"sha\":\"$sha\"}"
}
}

@Component
Expand All @@ -33,16 +38,20 @@ class GithubConnector : WebClientConnector("https://api.github.com/repos") {
repository: String,
pathToRoser: String,
accessToken: String,
): List<ROSDownloadUrl>? = getGithubResponse("/$owner/$repository/contents/$pathToRoser", accessToken).toROSDownloadUrls()
): List<ROSDownloadUrlDTO>? =
getGithubResponse("/$owner/$repository/contents/$pathToRoser", accessToken).rosDownloadUrls()

internal fun getRosSha(
owner: String,
repository: String,
accessToken: String,
writePayload: GithubWritePayload,
path: String,
): String {
return ""
pathToROS: String,
): String? {
val shaForExistingROS: ShaResponseDTO =
getGithubResponse("/$owner/$repository/contents/$pathToROS", accessToken).shaReponseDTO()
?: return null

return shaForExistingROS.sha
}

internal fun writeToGithub(
Expand All @@ -67,7 +76,11 @@ class GithubConnector : WebClientConnector("https://api.github.com/repos") {

private fun ResponseSpec.toROS(): String? = this.bodyToMono<String>().block()

private fun ResponseSpec.toROSDownloadUrls(): List<ROSDownloadUrl>? = this.bodyToMono<List<ROSDownloadUrl>>().block()
private fun ResponseSpec.rosDownloadUrls(): List<ROSDownloadUrlDTO>? =
this.bodyToMono<List<ROSDownloadUrlDTO>>().block()

private fun ResponseSpec.shaReponseDTO(): ShaResponseDTO? =
this.bodyToMono<List<ShaResponseDTO>>().block()?.firstOrNull()

private fun getGithubResponse(
uri: String,
Expand Down
11 changes: 7 additions & 4 deletions src/main/kotlin/no/kvros/ros/ROSService.kt
Original file line number Diff line number Diff line change
Expand Up @@ -40,17 +40,20 @@ class ROSService(
SopsEncryptorForYaml.encrypt(publicKey, content.ros)
?: return ResponseEntity.internalServerError().body("Kryptering feilet")

val shaForExisingROS = githubConnector.getRosSha(owner, repository, accessToken, path)

return ResponseEntity.ok(
githubConnector.writeToGithub(
owner = owner,
repository = repository,
path = path,
accessToken = accessToken,
writePayload =
GithubWritePayload(
message = "Yeehaw dette er en ny ros",
content = Base64.getEncoder().encodeToString(encryptedData.toByteArray()),
),
GithubWritePayload(
message = if (shaForExisingROS == null) "Yeehaw new ROS" else "Yeehaw oppdatert ROS",
content = Base64.getEncoder().encodeToString(encryptedData.toByteArray()),
sha = shaForExisingROS
),
),
)
}
Expand Down
2 changes: 1 addition & 1 deletion src/main/kotlin/no/kvros/ros/models/ROSDownlaodUrls.kt
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,6 @@ package no.kvros.ros.models
import com.fasterxml.jackson.annotation.JsonIgnoreProperties

@JsonIgnoreProperties(ignoreUnknown = true)
data class ROSDownloadUrl(
data class ROSDownloadUrlDTO(
val download_url: String,
)
11 changes: 11 additions & 0 deletions src/main/kotlin/no/kvros/ros/models/ShaReponse.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package no.kvros.ros.models

import com.fasterxml.jackson.annotation.JsonIgnoreProperties


@JsonIgnoreProperties(ignoreUnknown = true)
data class ShaResponseDTO(
val name: String,
val path: String,
val sha: String,
)
2 changes: 1 addition & 1 deletion src/test/kotlin/no/kvros/ros/GithubConnectorTest.kt
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,12 @@ class GithubConnectorTest {
githubConnector.writeToGithub(
owner = "bekk",
repository = "kv-ros-backend",
path = "/",
accessToken = "accessToken",
writePayload = GithubWritePayload(
message = "Commit-melding for noe fra kotlin",
content = Base64.getEncoder().encodeToString("Dette er min melding".toByteArray()),
),
rosFilePath = "rosetiros.txt"
)
}
}

0 comments on commit 0133f10

Please sign in to comment.