Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #994 프로젝트 사용자 권한 관리 시스템 설계/개발 ( keycloak 연동 k8s 권한 관리 ) #224

Merged
merged 8 commits into from
Feb 26, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions hack/endpoint-codegen.go
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//go:build ignore

package main

import (
Expand Down
139 changes: 139 additions & 0 deletions internal/delivery/http/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -374,6 +374,8 @@ func (p ProjectHandler) UpdateProject(w http.ResponseWriter, r *http.Request) {
}

func (p ProjectHandler) DeleteProject(w http.ResponseWriter, r *http.Request) {
//ToDo: to donggyu. implement cleanup logic for k8s & keycloak

//TODO implement me
}

Expand Down Expand Up @@ -497,6 +499,17 @@ func (p ProjectHandler) AddProjectMember(w http.ResponseWriter, r *http.Request)
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}

now := time.Now()
for _, pmr := range projectMemberReq.ProjectMemberRequests {
pu, err := p.usecase.GetProjectUser(pmr.ProjectUserId)
Expand Down Expand Up @@ -533,6 +546,15 @@ func (p ProjectHandler) AddProjectMember(w http.ResponseWriter, r *http.Request)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}

// tasks for keycloak & k8s
for stackId := range stackIds {
if err := p.usecase.AssignKeycloakClientRoleToMember(organizationId, projectId, stackId, pmId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}
}

out := domain.CommonProjectResponse{Result: "OK"}
Expand Down Expand Up @@ -768,6 +790,26 @@ func (p ProjectHandler) RemoveProjectMember(w http.ResponseWriter, r *http.Reque
"C_INVALID_PROJECT_MEMBER_ID", ""))
return
}

// tasks for keycloak & k8s
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}
for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, projectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

if err := p.usecase.RemoveProjectMember(projectMemberId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
Expand Down Expand Up @@ -812,8 +854,29 @@ func (p ProjectHandler) RemoveProjectMembers(w http.ResponseWriter, r *http.Requ
return
}

// tasks for keycloak & k8s
pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}

// TODO: change multi row delete
for _, pm := range projectMemberReq.ProjectMember {
// tasks for keycloak & k8s
for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, pm.ProjectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

if err := p.usecase.RemoveProjectMember(pm.ProjectMemberId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
Expand Down Expand Up @@ -879,6 +942,25 @@ func (p ProjectHandler) UpdateProjectMemberRole(w http.ResponseWriter, r *http.R
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}
// tasks for keycloak & k8s. Unassign old role
for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, projectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

pm.ProjectRoleId = pmrReq.ProjectRoleId
pm.ProjectUser = nil
pm.ProjectRole = nil
Expand All @@ -888,6 +970,14 @@ func (p ProjectHandler) UpdateProjectMemberRole(w http.ResponseWriter, r *http.R
ErrorJSON(w, r, err)
return
}
// tasks for keycloak & k8s. Assign new role
for stackId := range stackIds {
if err := p.usecase.AssignKeycloakClientRoleToMember(organizationId, projectId, stackId, projectMemberId); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

ResponseJSON(w, r, http.StatusOK, domain.CommonProjectResponse{Result: "OK"})
}
Expand Down Expand Up @@ -929,6 +1019,17 @@ func (p ProjectHandler) UpdateProjectMembersRole(w http.ResponseWriter, r *http.
return
}

pns, err := p.usecase.GetProjectNamespaces(organizationId, projectId)
if err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
stackIds := make(map[string]struct{})
for _, pn := range pns {
stackIds[pn.StackId] = struct{}{}
}

for _, pmr := range projectMemberReq.ProjectMemberRoleRequests {
pm, err := p.usecase.GetProjectMember(pmr.ProjectMemberId)
if err != nil {
Expand All @@ -942,6 +1043,14 @@ func (p ProjectHandler) UpdateProjectMembersRole(w http.ResponseWriter, r *http.
return
}

for stackId := range stackIds {
if err := p.usecase.UnassignKeycloakClientRoleToMember(organizationId, projectId, stackId, pm.ID); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}

pm.ProjectRoleId = pmr.ProjectRoleId
pm.ProjectUser = nil
pm.ProjectRole = nil
Expand All @@ -951,6 +1060,14 @@ func (p ProjectHandler) UpdateProjectMembersRole(w http.ResponseWriter, r *http.
ErrorJSON(w, r, err)
return
}

for stackId := range stackIds {
if err := p.usecase.AssignKeycloakClientRoleToMember(organizationId, projectId, stackId, pm.ID); err != nil {
log.Error(err)
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
}
}

ResponseJSON(w, r, http.StatusOK, domain.CommonProjectResponse{Result: "OK"})
Expand Down Expand Up @@ -1000,6 +1117,16 @@ func (p ProjectHandler) CreateProjectNamespace(w http.ResponseWriter, r *http.Re
CreatedAt: now,
}

// tasks for keycloak & k8s
if err := p.usecase.EnsureRequiredSetupForCluster(organizationId, projectId, projectNamespaceReq.StackId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
if err := p.usecase.CreateK8SNSRoleBinding(organizationId, projectId, projectNamespaceReq.StackId, projectNamespaceReq.Namespace); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}

if err := p.usecase.CreateProjectNamespace(organizationId, pn); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
Expand Down Expand Up @@ -1267,6 +1394,18 @@ func (p ProjectHandler) UpdateProjectNamespace(w http.ResponseWriter, r *http.Re
// @Router /organizations/{organizationId}/projects/{projectId}/namespaces/{projectNamespace}/stacks/{stackId} [delete]
// @Security JWT
func (p ProjectHandler) DeleteProjectNamespace(w http.ResponseWriter, r *http.Request) {

//ToDo: from donggyu. uncomment lines below after implementing usecase.DeleteProjectNamespace.
// tasks for keycloak & k8s
/*if err := p.usecase.DeleteK8SNSRoleBinding(organizationId, projectId, stackId, projectNamespace); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}
if err := p.usecase.MayRemoveRequiredSetupForCluster(organizationId, projectId, stackId); err != nil {
ErrorJSON(w, r, httpErrors.NewInternalServerError(err, "", ""))
return
}*/

//TODO implement me

//vars := mux.Vars(r)
Expand Down
Loading
Loading