Skip to content

Commit

Permalink
✨ : add CredentialsRestController
Browse files Browse the repository at this point in the history
  • Loading branch information
juwit committed Jul 9, 2020
1 parent ae437ae commit a1e23ab
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 6 deletions.
36 changes: 30 additions & 6 deletions src/main/java/io/gaia_app/credentials/Credentials.kt
Original file line number Diff line number Diff line change
@@ -1,17 +1,41 @@
package io.gaia_app.credentials

interface Credentials {
fun toEnv(): List<String>
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<String>
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"
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
package io.gaia_app.credentials

import org.springframework.data.mongodb.repository.MongoRepository

interface CredentialsRepository: MongoRepository<Credentials, String>
Original file line number Diff line number Diff line change
@@ -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<Credentials> 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);
}
}

0 comments on commit a1e23ab

Please sign in to comment.