-
Notifications
You must be signed in to change notification settings - Fork 554
feat: Audit logs for resource deletion from devtron UI #2791
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
Merged
Merged
Changes from all commits
Commits
Show all changes
19 commits
Select commit
Hold shift + click to select a range
a53f098
wip: adding code for saving audit logs of resource deletion
iamayushm 003b1c8
Merge branch 'main' into audit_logs_for_k8s_resource
iamayushm 3ec5ab1
wire file ea mode
iamayushm 467a44f
adding deployment_app_type
iamayushm 8abe4fa
adding sql up script and fixing save history call condition
iamayushm ad72876
adding migration script
iamayushm fd102e9
adding sql script
iamayushm 7722c31
merging main
iamayushm 79bea38
removing string literals
iamayushm a5c9cb0
Merge branch 'main' into audit_logs_for_k8s_resource
iamayushm c44132d
migration no correction
iamayushm 11f2198
Merge branch 'main' into audit_logs_for_k8s_resource
iamayushm df6817f
adding error log
iamayushm 64287f3
Merge branch 'main' into audit_logs_for_k8s_resource
iamayushm 7fcfb4d
fixing error after merge
iamayushm dd034cc
sql script no correction
iamayushm b6c08b4
Merge branch 'main' into audit_logs_for_k8s_resource
iamayushm 84984a8
sql script no correction
iamayushm bf4619a
handling error
iamayushm File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
99 changes: 99 additions & 0 deletions
99
pkg/kubernetesResourceAuditLogs/kubernetesResourceHistoryService.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| package kubernetesResourceAuditLogs | ||
|
|
||
| import ( | ||
| "github.com/argoproj/argo-cd/v2/pkg/apiclient/application" | ||
| client "github.com/devtron-labs/devtron/api/helm-app" | ||
| application2 "github.com/devtron-labs/devtron/client/k8s/application" | ||
| "github.com/devtron-labs/devtron/internal/sql/repository/app" | ||
| repository2 "github.com/devtron-labs/devtron/pkg/cluster/repository" | ||
| "github.com/devtron-labs/devtron/pkg/kubernetesResourceAuditLogs/repository" | ||
| "github.com/devtron-labs/devtron/pkg/sql" | ||
| "go.uber.org/zap" | ||
| "time" | ||
| ) | ||
|
|
||
| const ( | ||
| delete string = "delete" | ||
| helm string = "helm" | ||
| GitOps string = "argo_cd" | ||
| ) | ||
|
|
||
| type K8sResourceHistoryService interface { | ||
| SaveArgoCdAppsResourceDeleteHistory(query *application.ApplicationResourceDeleteRequest, appId int, envId int, userId int32) error | ||
| SaveHelmAppsResourceHistory(appIdentifier *client.AppIdentifier, k8sRequestBean *application2.K8sRequestBean, userId int32, actionType string) error | ||
| } | ||
|
|
||
| type K8sResourceHistoryServiceImpl struct { | ||
| appRepository app.AppRepository | ||
| K8sResourceHistoryRepository repository.K8sResourceHistoryRepository | ||
| logger *zap.SugaredLogger | ||
| envRepository repository2.EnvironmentRepository | ||
| } | ||
|
|
||
| func Newk8sResourceHistoryServiceImpl(K8sResourceHistoryRepository repository.K8sResourceHistoryRepository, | ||
| logger *zap.SugaredLogger, appRepository app.AppRepository, envRepository repository2.EnvironmentRepository) *K8sResourceHistoryServiceImpl { | ||
| return &K8sResourceHistoryServiceImpl{ | ||
| K8sResourceHistoryRepository: K8sResourceHistoryRepository, | ||
| logger: logger, | ||
| appRepository: appRepository, | ||
| envRepository: envRepository, | ||
| } | ||
| } | ||
|
|
||
| func (impl K8sResourceHistoryServiceImpl) SaveArgoCdAppsResourceDeleteHistory(query *application.ApplicationResourceDeleteRequest, appId int, envId int, userId int32) error { | ||
|
|
||
| k8sResourceHistory := repository.K8sResourceHistory{ | ||
| AppId: appId, | ||
| AppName: *query.Name, | ||
| EnvId: envId, | ||
| Namespace: *query.Namespace, | ||
| ResourceName: *query.ResourceName, | ||
| Kind: *query.Kind, | ||
| Group: *query.Group, | ||
| ForceDelete: *query.Force, | ||
| AuditLog: sql.AuditLog{ | ||
| UpdatedBy: userId, | ||
| UpdatedOn: time.Now(), | ||
| }, | ||
| ActionType: delete, | ||
| DeploymentAppType: GitOps, | ||
| } | ||
|
|
||
| err := impl.K8sResourceHistoryRepository.SaveK8sResourceHistory(&k8sResourceHistory) | ||
|
|
||
| if err != nil { | ||
| return err | ||
| } | ||
|
|
||
| return nil | ||
|
|
||
| } | ||
|
|
||
| func (impl K8sResourceHistoryServiceImpl) SaveHelmAppsResourceHistory(appIdentifier *client.AppIdentifier, k8sRequestBean *application2.K8sRequestBean, userId int32, actionType string) error { | ||
|
|
||
| app, err := impl.appRepository.FindActiveByName(appIdentifier.ReleaseName) | ||
|
|
||
| env, err := impl.envRepository.FindOneByNamespaceAndClusterId(appIdentifier.Namespace, appIdentifier.ClusterId) | ||
|
|
||
| k8sResourceHistory := repository.K8sResourceHistory{ | ||
| AppId: app.Id, | ||
| AppName: appIdentifier.ReleaseName, | ||
| EnvId: env.Id, | ||
| Namespace: appIdentifier.Namespace, | ||
| ResourceName: k8sRequestBean.ResourceIdentifier.Name, | ||
| Kind: k8sRequestBean.ResourceIdentifier.GroupVersionKind.Kind, | ||
| Group: k8sRequestBean.ResourceIdentifier.GroupVersionKind.Group, | ||
| ForceDelete: false, | ||
| AuditLog: sql.AuditLog{ | ||
| UpdatedBy: userId, | ||
| UpdatedOn: time.Now(), | ||
| }, | ||
| ActionType: actionType, | ||
| DeploymentAppType: helm, | ||
| } | ||
|
|
||
| err = impl.K8sResourceHistoryRepository.SaveK8sResourceHistory(&k8sResourceHistory) | ||
|
|
||
| return err | ||
|
|
||
| } |
43 changes: 43 additions & 0 deletions
43
pkg/kubernetesResourceAuditLogs/repository/kubernetesResourceHistoryRepo.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,43 @@ | ||
| package repository | ||
|
|
||
| import ( | ||
| "github.com/devtron-labs/devtron/pkg/sql" | ||
| "github.com/go-pg/pg" | ||
| "go.uber.org/zap" | ||
| ) | ||
|
|
||
| type K8sResourceHistory struct { | ||
| tableName struct{} `sql:"kubernetes_resource_history" pg:",discard_unknown_columns"` | ||
| Id int `sql:"id,pk"` | ||
| AppId int `sql:"app_id"` | ||
| AppName string `sql:"app_name"` | ||
| EnvId int `sql:"env_id"` | ||
| Namespace string `sql:"namespace,omitempty"` | ||
| ResourceName string `sql:"resource_name,notnull"` | ||
| Kind string `sql:"kind,notnull"` | ||
| Group string `sql:"group"` | ||
| ForceDelete bool `sql:"force_delete, omitempty"` | ||
| ActionType string `sql:"action_type"` | ||
| DeploymentAppType string `sql:"deployment_app_type"` | ||
| sql.AuditLog | ||
| } | ||
|
|
||
| type K8sResourceHistoryRepository interface { | ||
| SaveK8sResourceHistory(history *K8sResourceHistory) error | ||
| } | ||
|
|
||
| type K8sResourceHistoryRepositoryImpl struct { | ||
| dbConnection *pg.DB | ||
| logger *zap.SugaredLogger | ||
| } | ||
|
|
||
| func NewK8sResourceHistoryRepositoryImpl(dbConnection *pg.DB, logger *zap.SugaredLogger) *K8sResourceHistoryRepositoryImpl { | ||
| return &K8sResourceHistoryRepositoryImpl{ | ||
| dbConnection: dbConnection, | ||
| logger: logger, | ||
| } | ||
| } | ||
|
|
||
| func (repo K8sResourceHistoryRepositoryImpl) SaveK8sResourceHistory(k8sResourceHistory *K8sResourceHistory) error { | ||
| return repo.dbConnection.Insert(k8sResourceHistory) | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| DROP SEQUENCE IF EXISTS "id_seq_k8s_resource_history_sequence"; | ||
| DROP TABLE IF EXISTS "kubernetes_resource_history"; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| CREATE SEQUENCE IF NOT EXISTS id_seq_k8s_resource_history_sequence; | ||
|
|
||
| -- Table Definition | ||
| CREATE TABLE IF NOT EXISTS "public"."kubernetes_resource_history" | ||
| ( | ||
| "id" integer NOT NULL DEFAULT nextval('id_seq_k8s_resource_history_sequence'::regclass), | ||
| "app_id" integer, | ||
| "app_name" VARCHAR(100), | ||
| "env_id" integer, | ||
| "namespace" VARCHAR(100) , | ||
| "resource_name" VARCHAR(100), | ||
| "kind" VARCHAR(100), | ||
| "group" VARCHAR(100), | ||
| "force_delete" boolean, | ||
| "action_type" VARCHAR(100), | ||
| "deployment_app_type" VARCHAR(100), | ||
| "created_on" timestamptz, | ||
| "created_by" int4, | ||
| "updated_on" timestamptz, | ||
| "updated_by" int4, | ||
| PRIMARY KEY ("id") | ||
| ); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -306,10 +306,18 @@ func (handler *K8sApplicationRestHandlerImpl) UpdateResource(w http.ResponseWrit | |
| } | ||
|
|
||
| func (handler *K8sApplicationRestHandlerImpl) DeleteResource(w http.ResponseWriter, r *http.Request) { | ||
|
|
||
| userId, err := handler.userService.GetLoggedInUser(r) | ||
|
|
||
|
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. handle error here and return 401 |
||
| if userId == 0 || err != nil { | ||
| common.WriteJsonResp(w, err, "Unauthorized User", http.StatusUnauthorized) | ||
| return | ||
| } | ||
|
|
||
| decoder := json.NewDecoder(r.Body) | ||
| token := r.Header.Get("token") | ||
| var request ResourceRequestBean | ||
| err := decoder.Decode(&request) | ||
| err = decoder.Decode(&request) | ||
| if err != nil { | ||
| handler.logger.Errorw("error in decoding request body", "err", err) | ||
| common.WriteJsonResp(w, err, nil, http.StatusBadRequest) | ||
|
|
@@ -349,7 +357,7 @@ func (handler *K8sApplicationRestHandlerImpl) DeleteResource(w http.ResponseWrit | |
| return | ||
| } | ||
|
|
||
| resource, err := handler.k8sApplicationService.DeleteResource(&request) | ||
| resource, err := handler.k8sApplicationService.DeleteResource(&request, userId) | ||
| if err != nil { | ||
| handler.logger.Errorw("error in deleting resource", "err", err) | ||
| common.WriteJsonResp(w, err, resource, http.StatusInternalServerError) | ||
|
|
||
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
handle error here