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

Issue #340 - create application/project events for audit #440

Merged
merged 3 commits into from
Jul 24, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
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
5 changes: 5 additions & 0 deletions controller/appcontroller.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"time"

log "github.com/sirupsen/logrus"
"k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/errors"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
Expand Down Expand Up @@ -46,6 +47,7 @@ type ApplicationController struct {
namespace string
kubeClientset kubernetes.Interface
applicationClientset appclientset.Interface
auditLogger *argo.AuditLogger
appRefreshQueue workqueue.RateLimitingInterface
appOperationQueue workqueue.RateLimitingInterface
appInformer cache.SharedIndexInformer
Expand Down Expand Up @@ -88,6 +90,7 @@ func NewApplicationController(
statusRefreshTimeout: appResyncPeriod,
forceRefreshApps: make(map[string]bool),
forceRefreshAppsMutex: &sync.Mutex{},
auditLogger: argo.NewAuditLogger(namespace, kubeClientset, "appcontroller"),
}
}

Expand Down Expand Up @@ -283,6 +286,7 @@ func (ctrl *ApplicationController) finalizeApplicationDeletion(app *appv1.Applic
Type: appv1.ApplicationConditionDeletionError,
Message: err.Error(),
})
ctrl.auditLogger.LogAppEvent(app, argo.EventInfo{Reason: argo.EventReasonStatusRefreshed, Message: "Controller has failed to clean app resources"}, v1.EventTypeWarning)
} else {
log.Infof("Successfully deleted resources for application %s", app.Name)
}
Expand Down Expand Up @@ -396,6 +400,7 @@ func (ctrl *ApplicationController) setOperationState(app *appv1.Application, sta
// If operation is completed, clear the operation field to indicate no operation is
// in progress.
patch["operation"] = nil
ctrl.auditLogger.LogAppEvent(app, argo.EventInfo{Reason: argo.EventReasonResourceUpdated, Message: "Operation has been completed"}, v1.EventTypeNormal)
}
if reflect.DeepEqual(app.Status.OperationState, state) {
log.Infof("No operation updates necessary to '%s'. Skipping patch", app.Name)
Expand Down
52 changes: 44 additions & 8 deletions server/application/application.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,7 @@ import (
"github.com/argoproj/argo-cd/util/db"
"github.com/argoproj/argo-cd/util/grpc"
"github.com/argoproj/argo-cd/util/rbac"
"github.com/argoproj/argo-cd/util/session"
)

// Server provides a Application service
Expand All @@ -44,6 +45,7 @@ type Server struct {
appComparator controller.AppStateManager
enf *rbac.Enforcer
projectLock *util.KeyLock
auditLogger *argo.AuditLogger
}

// NewServer returns a new instance of the Application service
Expand All @@ -66,6 +68,7 @@ func NewServer(
appComparator: controller.NewAppStateManager(db, appclientset, repoClientset, namespace),
enf: enf,
projectLock: projectLock,
auditLogger: argo.NewAuditLogger(namespace, kubeclientset, "argo-server"),
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

argocd-server

}
}

Expand Down Expand Up @@ -128,6 +131,10 @@ func (s *Server) Create(ctx context.Context, q *ApplicationCreateRequest) (*appv
}
}
}

if err == nil {
s.logEvent(out, ctx, argo.EventReasonResourceCreated, "create")
}
return out, err
}

Expand Down Expand Up @@ -215,11 +222,20 @@ func (s *Server) ListResourceEvents(ctx context.Context, q *ApplicationResourceE
return nil, err
}

fieldSelector := fields.SelectorFromSet(map[string]string{
"involvedObject.name": q.ResourceName,
"involvedObject.uid": q.ResourceUID,
"involvedObject.namespace": namespace,
}).String()
var fieldSelector string
if q.ResourceName == "" && q.ResourceUID == "" {
fieldSelector = fields.SelectorFromSet(map[string]string{
"involvedObject.name": a.Name,
"involvedObject.uid": string(a.UID),
"involvedObject.namespace": namespace,
}).String()
} else {
fieldSelector = fields.SelectorFromSet(map[string]string{
"involvedObject.name": q.ResourceName,
"involvedObject.uid": q.ResourceUID,
"involvedObject.namespace": namespace,
}).String()
}

log.Infof("Querying for resource events with field selector: %s", fieldSelector)
opts := metav1.ListOptions{FieldSelector: fieldSelector}
Expand Down Expand Up @@ -300,6 +316,9 @@ func (s *Server) UpdateSpec(ctx context.Context, q *ApplicationUpdateSpecRequest
return nil, err
}
_, err = s.appclientset.ArgoprojV1alpha1().Applications(s.ns).Patch(*q.Name, types.MergePatchType, patch)
if err != nil {
s.logEvent(a, ctx, argo.EventReasonResourceUpdated, "update")
}
return &q.Spec, err
}

Expand Down Expand Up @@ -355,6 +374,7 @@ func (s *Server) Delete(ctx context.Context, q *ApplicationDeleteRequest) (*Appl
return nil, err
}

s.logEvent(a, ctx, argo.EventReasonResourceDeleted, "delete")
return &ApplicationResponse{}, nil
}

Expand Down Expand Up @@ -570,7 +590,7 @@ func (s *Server) Sync(ctx context.Context, syncReq *ApplicationSyncRequest) (*ap
if !s.enf.EnforceClaims(ctx.Value("claims"), "applications", "sync", appRBACName(*a)) {
return nil, grpc.ErrPermissionDenied
}
return s.setAppOperation(ctx, *syncReq.Name, func(app *appv1.Application) (*appv1.Operation, error) {
return s.setAppOperation(ctx, *syncReq.Name, "sync", func(app *appv1.Application) (*appv1.Operation, error) {
syncOp := appv1.SyncOperation{
Revision: syncReq.Revision,
Prune: syncReq.Prune,
Expand All @@ -591,7 +611,7 @@ func (s *Server) Rollback(ctx context.Context, rollbackReq *ApplicationRollbackR
if !s.enf.EnforceClaims(ctx.Value("claims"), "applications", "rollback", appRBACName(*a)) {
return nil, grpc.ErrPermissionDenied
}
return s.setAppOperation(ctx, *rollbackReq.Name, func(app *appv1.Application) (*appv1.Operation, error) {
return s.setAppOperation(ctx, *rollbackReq.Name, "rollback", func(app *appv1.Application) (*appv1.Operation, error) {
return &appv1.Operation{
Rollback: &appv1.RollbackOperation{
ID: rollbackReq.ID,
Expand All @@ -602,7 +622,7 @@ func (s *Server) Rollback(ctx context.Context, rollbackReq *ApplicationRollbackR
})
}

func (s *Server) setAppOperation(ctx context.Context, appName string, operationCreator func(app *appv1.Application) (*appv1.Operation, error)) (*appv1.Application, error) {
func (s *Server) setAppOperation(ctx context.Context, appName string, operationName string, operationCreator func(app *appv1.Application) (*appv1.Operation, error)) (*appv1.Application, error) {
for {
a, err := s.Get(ctx, &ApplicationQuery{Name: &appName})
if err != nil {
Expand All @@ -621,6 +641,9 @@ func (s *Server) setAppOperation(ctx context.Context, appName string, operationC
if err != nil && apierr.IsConflict(err) {
log.Warnf("Failed to set operation for app '%s' due to update conflict. Retrying again...", appName)
} else {
if err == nil {
s.logEvent(a, ctx, argo.EventReasonResourceUpdated, operationName)
}
return a, err
}
}
Expand Down Expand Up @@ -652,7 +675,20 @@ func (s *Server) TerminateOperation(ctx context.Context, termOpReq *OperationTer
a, err = s.appclientset.ArgoprojV1alpha1().Applications(s.ns).Get(*termOpReq.Name, metav1.GetOptions{})
if err != nil {
return nil, err
} else {
s.logEvent(a, ctx, argo.EventReasonResourceUpdated, "terminateop")
}
}
return nil, status.Errorf(codes.Internal, "Failed to terminate app. Too many conflicts")
}

func (s *Server) logEvent(a *appv1.Application, ctx context.Context, reason string, action string) {
username := session.Username(ctx)
var message string
if username != "" {
message = fmt.Sprintf("User %s executed action %s", username, action)
} else {
message = fmt.Sprintf("Unknown user executed action %s", action)
}
s.auditLogger.LogAppEvent(a, argo.EventInfo{Reason: reason, Action: action, Username: username, Message: message}, v1.EventTypeNormal)
}
43 changes: 38 additions & 5 deletions server/project/project.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,22 +14,27 @@ import (
"github.com/argoproj/argo-cd/util/git"
"github.com/argoproj/argo-cd/util/grpc"
"github.com/argoproj/argo-cd/util/rbac"
"github.com/argoproj/argo-cd/util/session"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"k8s.io/api/core/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes"
)

// Server provides a Project service
type Server struct {
ns string
enf *rbac.Enforcer
appclientset appclientset.Interface
auditLogger *argo.AuditLogger
projectLock *util.KeyLock
}

// NewServer returns a new instance of the Project service
func NewServer(ns string, appclientset appclientset.Interface, enf *rbac.Enforcer, projectLock *util.KeyLock) *Server {
return &Server{enf: enf, appclientset: appclientset, ns: ns, projectLock: projectLock}
func NewServer(ns string, kubeclientset kubernetes.Interface, appclientset appclientset.Interface, enf *rbac.Enforcer, projectLock *util.KeyLock) *Server {
auditLogger := argo.NewAuditLogger(ns, kubeclientset, "argo-server")
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This should be argocd-server.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done

return &Server{enf: enf, appclientset: appclientset, ns: ns, projectLock: projectLock, auditLogger: auditLogger}
}

// Create a new project.
Expand All @@ -44,7 +49,11 @@ func (s *Server) Create(ctx context.Context, q *ProjectCreateRequest) (*v1alpha1
if err != nil {
return nil, err
}
return s.appclientset.ArgoprojV1alpha1().AppProjects(s.ns).Create(q.Project)
res, err := s.appclientset.ArgoprojV1alpha1().AppProjects(s.ns).Create(q.Project)
if err == nil {
s.logEvent(res, ctx, argo.EventReasonResourceCreated, "create")
}
return res, err
}

// List returns list of projects
Expand Down Expand Up @@ -187,7 +196,11 @@ func (s *Server) Update(ctx context.Context, q *ProjectUpdateRequest) (*v1alpha1
codes.InvalidArgument, "following source repos are used by one or more application and cannot be removed: %s", strings.Join(removedSrcUsed, ";"))
}

return s.appclientset.ArgoprojV1alpha1().AppProjects(s.ns).Update(q.Project)
res, err := s.appclientset.ArgoprojV1alpha1().AppProjects(s.ns).Update(q.Project)
if err == nil {
s.logEvent(res, ctx, argo.EventReasonResourceUpdated, "update")
}
return res, err
}

// Delete deletes a project
Expand All @@ -199,6 +212,11 @@ func (s *Server) Delete(ctx context.Context, q *ProjectQuery) (*EmptyResponse, e
s.projectLock.Lock(q.Name)
defer s.projectLock.Unlock(q.Name)

p, err := s.appclientset.ArgoprojV1alpha1().AppProjects(s.ns).Get(q.Name, metav1.GetOptions{})
if err != nil {
return nil, err
}

appsList, err := s.appclientset.ArgoprojV1alpha1().Applications(s.ns).List(metav1.ListOptions{})
if err != nil {
return nil, err
Expand All @@ -207,5 +225,20 @@ func (s *Server) Delete(ctx context.Context, q *ProjectQuery) (*EmptyResponse, e
if len(apps) > 0 {
return nil, status.Errorf(codes.InvalidArgument, "project is referenced by %d applications", len(apps))
}
return &EmptyResponse{}, s.appclientset.ArgoprojV1alpha1().AppProjects(s.ns).Delete(q.Name, &metav1.DeleteOptions{})
err = s.appclientset.ArgoprojV1alpha1().AppProjects(s.ns).Delete(q.Name, &metav1.DeleteOptions{})
if err == nil {
s.logEvent(p, ctx, argo.EventReasonResourceDeleted, "delete")
}
return &EmptyResponse{}, err
}

func (s *Server) logEvent(p *v1alpha1.AppProject, ctx context.Context, reason string, action string) {
username := session.Username(ctx)
var message string
if username != "" {
message = fmt.Sprintf("User %s executed action %s", username, action)
} else {
message = fmt.Sprintf("Unknown user executed action %s", action)
}
s.auditLogger.LogAppProjEvent(p, argo.EventInfo{Reason: reason, Action: action, Username: username, Message: message}, v1.EventTypeNormal)
}
65 changes: 33 additions & 32 deletions server/project/project.pb.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 2 additions & 2 deletions server/project/project.pb.gw.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions server/project/project.proto
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ package project;
import "gogoproto/gogo.proto";
import "google/api/annotations.proto";
import "k8s.io/api/core/v1/generated.proto";
import "k8s.io/apimachinery/pkg/apis/meta/v1/generated.proto";
import "github.com/argoproj/argo-cd/pkg/apis/application/v1alpha1/generated.proto";


Expand Down Expand Up @@ -52,13 +53,13 @@ service ProjectService {
// Update updates a project
rpc Update(ProjectUpdateRequest) returns (github.com.argoproj.argo_cd.pkg.apis.application.v1alpha1.AppProject) {
option (google.api.http) = {
put: "/api/v1/repositories/{project.metadata.name}"
put: "/api/v1/projects/{project.metadata.name}"
body: "*"
};
}

// Delete deletes a project
rpc Delete(ProjectQuery) returns (EmptyResponse) {
option (google.api.http).delete = "/api/v1/repositories/{name}";
option (google.api.http).delete = "/api/v1/projects/{name}";
}
}
Loading