Skip to content

Commit

Permalink
Merge pull request #31 from whdalsrnt/master
Browse files Browse the repository at this point in the history
feat: add user secret APIs
  • Loading branch information
whdalsrnt authored Dec 23, 2023
2 parents e14b6bf + 74c9279 commit b120e73
Show file tree
Hide file tree
Showing 11 changed files with 526 additions and 1 deletion.
1 change: 1 addition & 0 deletions src/spaceone/secret/info/__init__.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
from spaceone.secret.info.secret_info import *
from spaceone.secret.info.user_secret_info import *
from spaceone.secret.info.trusted_secret_info import *
from spaceone.secret.info.common_info import *
46 changes: 46 additions & 0 deletions src/spaceone/secret/info/user_secret_info.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
import logging
import functools
from spaceone.api.secret.v1 import user_secret_pb2
from spaceone.core.pygrpc.message_type import *
from spaceone.core import utils
from spaceone.secret.model.user_secret_model import UserSecret

__all__ = ["UserSecretInfo", "UserSecretsInfo", "UserSecretDataInfo"]
_LOGGER = logging.getLogger(__name__)


def UserSecretDataInfo(secret_data):
info = {
"encrypted": secret_data.get("encrypted", False),
"encrypt_options": change_struct_type(secret_data.get("encrypt_options", {})),
"data": change_struct_type(secret_data["data"]),
}

return user_secret_pb2.UserSecretDataInfo(**info)


def UserSecretInfo(secret_vo: UserSecret, minimal=False):
info = {
"user_secret_id": secret_vo.user_secret_id,
"name": secret_vo.name,
"schema_id": secret_vo.schema_id,
"provider": secret_vo.provider,
}

if minimal is False:
info.update(
{
"tags": change_struct_type(secret_vo.tags),
"user_id": secret_vo.user_id,
"domain_id": secret_vo.domain_id,
"created_at": utils.datetime_to_iso8601(secret_vo.created_at),
}
)

return user_secret_pb2.UserSecretInfo(**info)


def UserSecretsInfo(secret_vos, total_count, **kwargs):
results = list(map(functools.partial(UserSecretInfo, **kwargs), secret_vos))

return user_secret_pb2.UserSecretsInfo(results=results, total_count=total_count)
2 changes: 2 additions & 0 deletions src/spaceone/secret/interface/grpc/__init__.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
from spaceone.core.pygrpc.server import GRPCServer
from spaceone.secret.interface.grpc.secret import Secret
from spaceone.secret.interface.grpc.user_secret import UserSecret
from spaceone.secret.interface.grpc.trusted_secret import TrustedSecret

_all_ = ["app"]

app = GRPCServer()
app.add_service(Secret)
app.add_service(UserSecret)
app.add_service(TrustedSecret)
88 changes: 88 additions & 0 deletions src/spaceone/secret/interface/grpc/user_secret.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,88 @@
from spaceone.api.secret.v1 import user_secret_pb2, user_secret_pb2_grpc
from spaceone.core.pygrpc import BaseAPI


class UserSecret(BaseAPI, user_secret_pb2_grpc.UserSecretServicer):
pb2 = user_secret_pb2
pb2_grpc = user_secret_pb2_grpc

def create(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
return self.locator.get_info(
"UserSecretInfo", user_secret_service.create(params)
)

def update(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
return self.locator.get_info(
"UserSecretInfo", user_secret_service.update(params)
)

def delete(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
user_secret_service.delete(params)
return self.locator.get_info("EmptyInfo")

def update_data(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
user_secret_service.update_data(params)
return self.locator.get_info("EmptyInfo")

def get_data(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
user_secret_data = user_secret_service.get_data(params)
return self.locator.get_info("UserSecretDataInfo", user_secret_data)

def get(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
return self.locator.get_info(
"UserSecretInfo", user_secret_service.get(params)
)

def list(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
user_secret_vos, total_count = user_secret_service.list(params)
return self.locator.get_info(
"UserSecretsInfo",
user_secret_vos,
total_count,
minimal=self.get_minimal(params),
)

def stat(self, request, context):
params, metadata = self.parse_request(request, context)

with self.locator.get_service(
"UserSecretService", metadata
) as user_secret_service:
return self.locator.get_info(
"StatisticsInfo", user_secret_service.stat(params)
)
1 change: 1 addition & 0 deletions src/spaceone/secret/manager/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from spaceone.secret.manager.secret_manager import SecretManager
from spaceone.secret.manager.user_secret_manager import UserSecretManager
from spaceone.secret.manager.trusted_secret_manager import TrustedSecretManager
from spaceone.secret.manager.secret_connector_manager import SecretConnectorManager
from spaceone.secret.manager.identity_manager import IdentityManager
60 changes: 60 additions & 0 deletions src/spaceone/secret/manager/user_secret_manager.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,60 @@
import logging

from spaceone.core.manager import BaseManager
from spaceone.secret.model.user_secret_model import UserSecret

_LOGGER = logging.getLogger(__name__)


class UserSecretManager(BaseManager):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.user_secret_model: UserSecret = self.locator.get_model("UserSecret")

def create_user_secret(self, params):
def _rollback(user_secret_vo):
_LOGGER.info(
f"[ROLLBACK] Delete user_secret : {user_secret_vo.name} ({user_secret_vo.user_secret_id})"
)
user_secret_vo.delete()

user_secret_vo: UserSecret = self.user_secret_model.create(params)

self.transaction.add_rollback(_rollback, user_secret_vo)

return user_secret_vo

def update_user_secret_by_vo(self, params, user_secret_vo):
def _rollback(old_data):
_LOGGER.info(
f'[ROLLBACK] Revert Data : {old_data["name"]} ({old_data["user_secret_id"]})'
)
user_secret_vo.update(old_data)

self.transaction.add_rollback(_rollback, user_secret_vo.to_dict())

return user_secret_vo.update(params)

@staticmethod
def delete_user_secret_by_vo(user_secret_vo):
user_secret_vo.delete()

def get_user_secret(self, user_secret_id, domain_id, user_id=None):
conditions = {
"user_secret_id": user_secret_id,
"domain_id": domain_id,
}

if user_id:
conditions["user_id"] = user_id

return self.user_secret_model.get(**conditions)

def filter_user_secrets(self, **conditions):
return self.user_secret_model.filter(**conditions)

def list_user_secrets(self, query):
return self.user_secret_model.query(**query)

def stat_user_secrets(self, query):
return self.user_secret_model.stat(**query)
1 change: 1 addition & 0 deletions src/spaceone/secret/model/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from spaceone.secret.model.secret_model import Secret
from spaceone.secret.model.user_secret_model import UserSecret
from spaceone.secret.model.trusted_secret_model import TrustedSecret
2 changes: 1 addition & 1 deletion src/spaceone/secret/model/secret_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ class Secret(MongoModel):
)
project_id = StringField(max_length=40)
workspace_id = StringField(max_length=40)
domain_id = StringField(max_length=255)
domain_id = StringField(max_length=40)
created_at = DateTimeField(auto_now_add=True)

meta = {
Expand Down
37 changes: 37 additions & 0 deletions src/spaceone/secret/model/user_secret_model.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import logging
from mongoengine import *
from spaceone.core.model.mongo_model import MongoModel

_LOGGER = logging.getLogger(__name__)


class UserSecret(MongoModel):
user_secret_id = StringField(max_length=40, generate_id="user-secret", unique=True)
name = StringField(max_length=255)
schema_id = StringField(max_length=40, null=True, default=None)
provider = StringField(max_length=40, null=True, default=None)
tags = DictField()
encrypted = BooleanField(default=False)
encrypt_options = DictField()
user_id = StringField(max_length=255)
domain_id = StringField(max_length=40)
created_at = DateTimeField(auto_now_add=True)

meta = {
"updatable_fields": [
"name",
"schema_id",
"tags",
"encrypted",
"encrypt_options",
],
"minimal_fields": ["user_secret_id", "name", "schema_id", "provider"],
"ordering": ["name"],
"indexes": [
"name",
"schema_id",
"provider",
"user_id",
"domain_id",
],
}
1 change: 1 addition & 0 deletions src/spaceone/secret/service/__init__.py
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
from spaceone.secret.service.secret_service import SecretService
from spaceone.secret.service.user_secret_service import UserSecretService
from spaceone.secret.service.trusted_secret_service import TrustedSecretService
Loading

0 comments on commit b120e73

Please sign in to comment.