Skip to content

Commit

Permalink
Merge branch 'develop' of https://github.com/bwsw/cs-vault-server int…
Browse files Browse the repository at this point in the history
…o CSVS-41

# Conflicts:
#	src/main/resources/application.conf
  • Loading branch information
MedvedevBW committed Oct 12, 2017
2 parents a3b127a + 0349c85 commit fa42813
Show file tree
Hide file tree
Showing 10 changed files with 76 additions and 76 deletions.
2 changes: 1 addition & 1 deletion src/main/resources/application.conf
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
app {
tokenPeriod = ${?VAULT_TOKEN_PERIOD}
tokenPeriod = "3562" //10 years
tokenPeriod = ${?VAULT_TOKEN_PERIOD}
accountsVaultBasicPath = "secret/cs/accounts/"
accountsVaultBasicPath = ${?VAULT_ACCOUNTS_BASIC_PATH}
vmsVaultBasicPath = "secret/cs/vms/"
Expand Down
2 changes: 1 addition & 1 deletion src/main/resources/log4j.properties
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
log4j.rootCategory = DEBUG, default
log4j.rootCategory = INFO, default

# Define the console appender
log4j.appender.default = org.apache.log4j.ConsoleAppender
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ import org.slf4j.LoggerFactory
/**
* Class is responsible for interaction with CloudStack server with help of CloudStackTaskCreator
*
* @param сloudStackTaskCreator allows for creating task for interaction with CloudStack
* @param cloudStackTaskCreator allows for creating task for interaction with CloudStack
* @param settings contains the settings for interaction with CloudStack
*/
class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
class CloudStackService(cloudStackTaskCreator: CloudStackTaskCreator,
settings: CloudStackService.Settings) {
private val logger = LoggerFactory.getLogger(this.getClass)
private val jsonSerializer = new JsonSerializer(true)
Expand All @@ -46,13 +46,13 @@ class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
* @return List with Tag
* @throws CloudStackCriticalException if account with specified id does not exist.
*/
def getUserTagsByAccountId(accountId: UUID): List[Tag] = {
logger.debug(s"getUserTagsByAccountId(accountId: $accountId)")
def getUserTagsByAccount(accountId: UUID): List[Tag] = {
logger.debug(s"getUserTagsByAccount(accountId: $accountId)")

val allUsersIdInAccount = getUserIdsByAccountId(accountId)
val allUsersIdInAccount = getUsersByAccount(accountId)

val tags = allUsersIdInAccount.flatMap { userId =>
getUserTagsByUserId(userId)
getUserTags(userId)
}

logger.debug(s"Tags were got for account: $accountId)")
Expand All @@ -67,8 +67,8 @@ class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
* @return List with Tag
* @throws CloudStackCriticalException if user with specified id does not exist.
*/
def getUserTagsByUserId(userId: UUID): List[Tag] = {
logger.debug(s"getUserTagsByUserId(userId: $userId)")
def getUserTags(userId: UUID): List[Tag] = {
logger.debug(s"getUserTags(userId: $userId)")

val tagResponse = getTagsJson(Tag.Type.User, userId)
val tags = jsonSerializer.deserialize[TagResponse](tagResponse).tagList.tags.getOrElse(List.empty[Tag])
Expand All @@ -85,8 +85,8 @@ class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
* @return List with Tag
* @throws CloudStackCriticalException if virtual machine with specified id does not exist.
*/
def getVmTagsById(vmId: UUID): List[Tag] = {
logger.debug(s"getVmTagsById(vmId: $vmId)")
def getVmTags(vmId: UUID): List[Tag] = {
logger.debug(s"getVmTags(vmId: $vmId)")

val tagResponse = getTagsJson(Tag.Type.UserVM, vmId)
val tags = jsonSerializer.deserialize[TagResponse](tagResponse).tagList.tags.getOrElse(List.empty[Tag])
Expand All @@ -105,17 +105,17 @@ class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
* @throws CloudStackCriticalException if virtual machine with specified id does not exist,
* or if account with specified name in virtual machine does not exist.
*/
def getAccountIdByVmId(vmId: UUID): UUID = {
logger.debug(s"getAccountIdByVmId(vmId: $vmId)")
def getVmOwnerAccount(vmId: UUID): UUID = {
logger.debug(s"getVmOwnerAccount(vmId: $vmId)")

val accountName = jsonSerializer.deserialize[VirtualMachinesResponse](
getEntityJson(vmId.toString, сloudStackTaskCreator.idParameter, Command.ListVirtualMachines)
getEntityJson(vmId.toString, cloudStackTaskCreator.idParameter, Command.ListVirtualMachines)
).virtualMashineList.virtualMashines.getOrElse(
throw new CloudStackCriticalException(new CloudStackEntityDoesNotExistException(s"Virtual machine with id: $vmId does not exist"))
).map(_.accountName).head

val accountId: UUID = jsonSerializer.deserialize[AccountResponse](
getEntityJson(accountName, сloudStackTaskCreator.nameParameter, Command.ListAccounts)
getEntityJson(accountName, cloudStackTaskCreator.nameParameter, Command.ListAccounts)
).accountList.accounts.getOrElse(
throw new CloudStackCriticalException(new CloudStackEntityDoesNotExistException(s"The vm: $vmId does not include account with name: $accountName"))
).map(_.id).head
Expand All @@ -132,11 +132,11 @@ class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
* @return UUID of account which include user with indicate id
* @throws CloudStackCriticalException if user with specified id does not exist.
*/
def getAccountIdByUserId(userId: UUID): UUID = {
logger.debug(s"getAccountIdByUserId(userId: $userId)")
def getAccountByUser(userId: UUID): UUID = {
logger.debug(s"getAccountByUser(userId: $userId)")

val accountId = jsonSerializer.deserialize[UserResponse](
getEntityJson(userId.toString, сloudStackTaskCreator.idParameter, Command.ListUsers)
getEntityJson(userId.toString, cloudStackTaskCreator.idParameter, Command.ListUsers)
).userList.users.getOrElse(
throw new CloudStackCriticalException(new CloudStackEntityDoesNotExistException(s"User with id: $userId does not exist"))
).map(_.accountid).head
Expand All @@ -153,13 +153,13 @@ class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
* @return List with UUID of users which are included in account
* @throws CloudStackCriticalException if account with specified id does not exist.
*/
def getUserIdsByAccountId(accountId: UUID): List[UUID] = {
logger.debug(s"getUserIdsForAccount(accountId: $accountId)")
def getUsersByAccount(accountId: UUID): List[UUID] = {
logger.debug(s"getUsersByAccount(accountId: $accountId)")
val jsonSerializer = new JsonSerializer(true)

val accountResponse = getEntityJson(
accountId.toString,
сloudStackTaskCreator.idParameter,
cloudStackTaskCreator.idParameter,
Command.ListAccounts
)

Expand All @@ -184,20 +184,20 @@ class CloudStackService(сloudStackTaskCreator: CloudStackTaskCreator,
*/
def setResourceTags(resourceId: UUID, resourceType: Tag.Type, tagList: List[Tag]): Unit = {
logger.debug(s"setResourceTags(resourceId: $resourceId, resourceType: $resourceType)")
def task = сloudStackTaskCreator.createSetResourceTagsTask(resourceId, resourceType, tagList)
def task = cloudStackTaskCreator.createSetResourceTagsTask(resourceId, resourceType, tagList)

TaskRunner.tryRunUntilSuccess[Unit](task, settings.cloudStackRetryDelay)
logger.debug(s"Tag was set to resource: $resourceId, $resourceType")
}

private def getEntityJson(parameterValue: String, parameterName: String, command: Command): String = {
def task = сloudStackTaskCreator.createGetEntityTask(parameterValue, parameterName, command)
def task = cloudStackTaskCreator.createGetEntityTask(parameterValue, parameterName, command)

TaskRunner.tryRunUntilSuccess[String](task, settings.cloudStackRetryDelay)
}

private def getTagsJson(resourceType: Tag.Type, resourceId: UUID): String = {
def task = сloudStackTaskCreator.createGetTagTask(resourceType, resourceId)
def task = cloudStackTaskCreator.createGetTagTask(resourceType, resourceId)

TaskRunner.tryRunUntilSuccess[String](task, settings.cloudStackRetryDelay)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ import br.com.autonomiccs.apacheCloudStack.client.beans.ApacheCloudStackUser
import br.com.autonomiccs.apacheCloudStack.exceptions.{ApacheCloudStackClientRequestRuntimeException, ApacheCloudStackClientRuntimeException}
import com.bwsw.cloudstack.vault.server.cloudstack.entities.{Command, Tag}
import com.bwsw.cloudstack.vault.server.cloudstack.util.exception.{CloudStackCriticalException, CloudStackEntityDoesNotExistException}
import com.bwsw.cloudstack.vault.server.util.HttpStatuses
import com.bwsw.cloudstack.vault.server.util.HttpStatus
import org.slf4j.LoggerFactory

import scala.annotation.tailrec
Expand Down Expand Up @@ -143,9 +143,9 @@ class CloudStackTaskCreator(settings: CloudStackTaskCreator.Settings) {
}
throw e
case Failure(e: ApacheCloudStackClientRequestRuntimeException)
if e.getStatusCode == HttpStatuses.CLOUD_STACK_ENTITY_DOES_NOT_EXIST =>
if e.getStatusCode == HttpStatus.CLOUD_STACK_ENTITY_DOES_NOT_EXIST =>
throw new CloudStackCriticalException(new CloudStackEntityDoesNotExistException(e.toString))
case Failure(e :Throwable) =>
case Failure(e: Throwable) =>
logger.error(s"Request execution thrown an critical exception: $e")
throw new CloudStackCriticalException(e)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -81,8 +81,8 @@ class CloudStackVaultController(vaultService: VaultService,
def handleUserCreate(userId: UUID): Unit = {
logger.debug(s"handleUserCreate(userId: $userId)")

val accountId = cloudStackService.getAccountIdByUserId(userId)
val usersIds = cloudStackService.getUserIdsByAccountId(accountId)
val accountId = cloudStackService.getAccountByUser(userId)
val usersIds = cloudStackService.getUsersByAccount(accountId)

val currentVaultTags = getCurrentVaultTagsOfUsers(usersIds)

Expand Down Expand Up @@ -119,7 +119,7 @@ class CloudStackVaultController(vaultService: VaultService,
def handleAccountCreate(accountId: UUID): Unit = {
logger.debug(s"handleAccountCreate(accountId: $accountId)")

val usersIds = cloudStackService.getUserIdsByAccountId(accountId)
val usersIds = cloudStackService.getUsersByAccount(accountId)

val currentVaultTags = getCurrentVaultTagsOfUsers(usersIds)

Expand Down Expand Up @@ -156,7 +156,7 @@ class CloudStackVaultController(vaultService: VaultService,
def handleVmCreate(vmId: UUID): Unit = {
logger.debug(s"handleVmCreate(vmId: $vmId)")

val accountId = cloudStackService.getAccountIdByVmId(vmId)
val accountId = cloudStackService.getVmOwnerAccount(vmId)

val policyList = List(
Policy.createVmReadPolicy(accountId, vmId, settings.vmSecretPath),
Expand Down Expand Up @@ -201,7 +201,7 @@ class CloudStackVaultController(vaultService: VaultService,
private def getCurrentVaultTagsOfUsers(usersIds: List[UUID]): Set[Tag] = {

val allUsersWithVaultTags = usersIds.map { userId =>
(userId, cloudStackService.getUserTagsByUserId(userId).filter { tag =>
(userId, cloudStackService.getUserTags(userId).filter { tag =>
tag.key.oneOf(Tag.Key.VaultRO, Tag.Key.VaultRW, Tag.Key.VaultHost, Tag.Key.VaultPrefix)
}.toSet)
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -48,7 +48,7 @@ object ConfigLiterals {
val cloudStackRetryDelay = s"$cloudStackDomain.retryDelay"
}

object HttpStatuses {
object HttpStatus {
val OK_STATUS = 200
val OK_STATUS_WITH_EMPTY_BODY = 204
val CLOUD_STACK_ENTITY_DOES_NOT_EXIST = 431
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ package com.bwsw.cloudstack.vault.server.vault.util

import com.bettercloud.vault.VaultException
import com.bettercloud.vault.rest.{Rest, RestException, RestResponse}
import com.bwsw.cloudstack.vault.server.util.{HttpStatuses, RequestPath}
import com.bwsw.cloudstack.vault.server.util.{HttpStatus, RequestPath}
import com.bwsw.cloudstack.vault.server.vault.util.exception.VaultCriticalException
import org.slf4j.LoggerFactory

Expand All @@ -46,7 +46,7 @@ class VaultRestRequestCreator(settings: VaultRestRequestCreator.Settings) {
def createTokenCreateRequest(tokenParameters: String):() => String = {
createRequest(
createRest(s"${RequestPath.vaultTokenCreate}", tokenParameters).post,
HttpStatuses.OK_STATUS :: Nil,
HttpStatus.OK_STATUS :: Nil,
"create token"
)
}
Expand All @@ -62,7 +62,7 @@ class VaultRestRequestCreator(settings: VaultRestRequestCreator.Settings) {
def createTokenRevokeRequest(jsonTokenId: String):() => String = {
createRequest(
createRest(s"${RequestPath.vaultTokenRevoke}", jsonTokenId).post,
HttpStatuses.OK_STATUS_WITH_EMPTY_BODY :: Nil,
HttpStatus.OK_STATUS_WITH_EMPTY_BODY :: Nil,
"revoke token"
)
}
Expand All @@ -79,7 +79,7 @@ class VaultRestRequestCreator(settings: VaultRestRequestCreator.Settings) {
def createPolicyCreateRequest(policyName: String, policyJson: String):() => String = {
createRequest(
createRest(s"${RequestPath.vaultPolicy}/$policyName", policyJson).put,
HttpStatuses.OK_STATUS_WITH_EMPTY_BODY :: Nil,
HttpStatus.OK_STATUS_WITH_EMPTY_BODY :: Nil,
"write policy"
)
}
Expand All @@ -95,7 +95,7 @@ class VaultRestRequestCreator(settings: VaultRestRequestCreator.Settings) {
def createPolicyDeleteRequest(policyName: String):() => String = {
createRequest(
createRest(s"${RequestPath.vaultPolicy}/$policyName", "").delete,
HttpStatuses.OK_STATUS_WITH_EMPTY_BODY :: Nil,
HttpStatus.OK_STATUS_WITH_EMPTY_BODY :: Nil,
"delete policy"
)
}
Expand All @@ -111,7 +111,7 @@ class VaultRestRequestCreator(settings: VaultRestRequestCreator.Settings) {
def createTokenLookupRequest(jsonTokenId: String):() => String = {
createRequest(
createRest(s"${RequestPath.vaultTokenLookup}", jsonTokenId).post,
HttpStatuses.OK_STATUS :: Nil,
HttpStatus.OK_STATUS :: Nil,
"get lookup token"
)
}
Expand All @@ -127,7 +127,7 @@ class VaultRestRequestCreator(settings: VaultRestRequestCreator.Settings) {
def createDeleteSecretRequest(pathToSecret: String):() => String = {
createRequest(
createRest(s"$pathToSecret", "").delete,
HttpStatuses.OK_STATUS_WITH_EMPTY_BODY :: Nil,
HttpStatus.OK_STATUS_WITH_EMPTY_BODY :: Nil,
"delete secret"
)
}
Expand All @@ -143,7 +143,7 @@ class VaultRestRequestCreator(settings: VaultRestRequestCreator.Settings) {
def createGetSubSecretPathsRequest(pathToRootSecret: String):() => String = {
createRequest(
createRest(s"$pathToRootSecret?list=true", "").get,
List(HttpStatuses.OK_STATUS, HttpStatuses.NOT_FOUND),
List(HttpStatus.OK_STATUS, HttpStatus.NOT_FOUND),
"getSubPaths"
)
}
Expand Down

0 comments on commit fa42813

Please sign in to comment.