From a1e23abf3c9a8a56f103f4f2f9b1f392222f1bbd Mon Sep 17 00:00:00 2001 From: Julien WITTOUCK Date: Fri, 19 Jun 2020 14:39:58 +0200 Subject: [PATCH] :sparkles: : add CredentialsRestController --- .../io/gaia_app/credentials/Credentials.kt | 36 +++++++++++++++---- .../credentials/CredentialsRepository.kt | 5 +++ .../CredentialsRestController.java | 33 +++++++++++++++++ 3 files changed, 68 insertions(+), 6 deletions(-) create mode 100644 src/main/java/io/gaia_app/credentials/CredentialsRepository.kt create mode 100644 src/main/java/io/gaia_app/credentials/CredentialsRestController.java diff --git a/src/main/java/io/gaia_app/credentials/Credentials.kt b/src/main/java/io/gaia_app/credentials/Credentials.kt index e9fdfbc22..603f5b41c 100644 --- a/src/main/java/io/gaia_app/credentials/Credentials.kt +++ b/src/main/java/io/gaia_app/credentials/Credentials.kt @@ -1,17 +1,41 @@ package io.gaia_app.credentials -interface Credentials { - fun toEnv(): List +import com.fasterxml.jackson.annotation.JsonSubTypes +import com.fasterxml.jackson.annotation.JsonSubTypes.Type +import com.fasterxml.jackson.annotation.JsonTypeInfo +import org.springframework.data.annotation.Id + + +import com.fasterxml.jackson.annotation.JsonTypeInfo.As.PROPERTY +import com.fasterxml.jackson.annotation.JsonTypeInfo.Id.NAME + + +@JsonTypeInfo(use = NAME, include = PROPERTY, property = "provider") +@JsonSubTypes( + Type(value = AWSCredentials::class, name = "AWS"), + Type(value = GCPCredentials::class, name = "GCP"), + Type(value = AzureCredentials::class, name = "Azure") +) +abstract class Credentials { + + @Id + lateinit var id: String + + abstract fun toEnv(): List + abstract fun provider(): String } -data class AWSCredentials(val access_key:String, val secret_key:String):Credentials { - override fun toEnv() = listOf("""AWS_ACCESS_KEY_ID=${access_key}""", """AWS_SECRET_ACCESS_KEY=${secret_key}""") +data class AWSCredentials(val accessKey:String, val secretKey:String):Credentials() { + override fun toEnv() = listOf("""AWS_ACCESS_KEY_ID=${accessKey}""", """AWS_SECRET_ACCESS_KEY=${secretKey}""") + override fun provider() = "AWS" } -data class GCPCredentials(val serviceAccountJSONContents:String):Credentials { +data class GCPCredentials(val serviceAccountJSONContents:String):Credentials() { override fun toEnv() = listOf("""GOOGLE_CREDENTIALS=${serviceAccountJSONContents}""") + override fun provider() = "GCP" } -data class AzureCredentials(val clientId:String, val clientSecret:String):Credentials { +data class AzureCredentials(val clientId:String, val clientSecret:String):Credentials() { override fun toEnv() = listOf("""ARM_CLIENT_ID=${clientId}""", """ARM_CLIENT_SECRET=${clientSecret}""") + override fun provider() = "Azure" } diff --git a/src/main/java/io/gaia_app/credentials/CredentialsRepository.kt b/src/main/java/io/gaia_app/credentials/CredentialsRepository.kt new file mode 100644 index 000000000..ead50b76c --- /dev/null +++ b/src/main/java/io/gaia_app/credentials/CredentialsRepository.kt @@ -0,0 +1,5 @@ +package io.gaia_app.credentials + +import org.springframework.data.mongodb.repository.MongoRepository + +interface CredentialsRepository: MongoRepository diff --git a/src/main/java/io/gaia_app/credentials/CredentialsRestController.java b/src/main/java/io/gaia_app/credentials/CredentialsRestController.java new file mode 100644 index 000000000..92bd53784 --- /dev/null +++ b/src/main/java/io/gaia_app/credentials/CredentialsRestController.java @@ -0,0 +1,33 @@ +package io.gaia_app.credentials; + +import org.springframework.security.access.annotation.Secured; +import org.springframework.web.bind.annotation.*; + +import java.util.List; + +@RestController +@RequestMapping("/api/credentials") +@Secured({"ROLE_USER","ROLE_ADMIN"}) +public class CredentialsRestController { + + private CredentialsRepository credentialsRepository; + + public CredentialsRestController(CredentialsRepository credentialsRepository) { + this.credentialsRepository = credentialsRepository; + } + + @GetMapping + public List getAllCredentials(){ + return this.credentialsRepository.findAll(); + } + + @PostMapping + public Credentials createCredentials(@RequestBody Credentials credentials){ + return this.credentialsRepository.save(credentials); + } + + @PutMapping("/{id}") + public Credentials updateCredentials(@RequestBody Credentials credentials, @PathVariable String id){ + return this.credentialsRepository.save(credentials); + } +}