Skip to content

Commit

Permalink
[Amir] auditor interface takes in user_email to persist to db
Browse files Browse the repository at this point in the history
  • Loading branch information
anagri committed Sep 27, 2018
1 parent afda932 commit fa91802
Show file tree
Hide file tree
Showing 7 changed files with 22 additions and 14 deletions.
5 changes: 3 additions & 2 deletions proctord/audit/auditor.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,18 +29,19 @@ func (auditor *auditor) AuditJobsExecution(ctx context.Context) {
jobSubmissionStatus := ctx.Value(utility.JobSubmissionStatusContextKey).(string)

if jobSubmissionStatus != utility.JobSubmissionSuccess {
err := auditor.store.JobsExecutionAuditLog(jobSubmissionStatus, utility.JobFailed, "", "", "", map[string]string{})
err := auditor.store.JobsExecutionAuditLog(jobSubmissionStatus, utility.JobFailed, "", "", "", "", map[string]string{})
if err != nil {
logger.Error("Error auditing jobs execution", err)
}
return
}
jobName := ctx.Value(utility.JobNameContextKey).(string)
userEmail := ctx.Value(utility.UserEmailContextKey).(string)
JobNameSubmittedForExecution := ctx.Value(utility.JobNameSubmittedForExecutionContextKey).(string)
imageName := ctx.Value(utility.ImageNameContextKey).(string)
jobArgs := ctx.Value(utility.JobArgsContextKey).(map[string]string)

err := auditor.store.JobsExecutionAuditLog(jobSubmissionStatus, utility.JobWaiting, jobName, JobNameSubmittedForExecution, imageName, jobArgs)
err := auditor.store.JobsExecutionAuditLog(jobSubmissionStatus, utility.JobWaiting, jobName, userEmail, JobNameSubmittedForExecution, imageName, jobArgs)
if err != nil {
logger.Error("Error auditing jobs execution", err)
}
Expand Down
8 changes: 5 additions & 3 deletions proctord/audit/auditor_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,15 +19,17 @@ func TestExecutionAuditor(t *testing.T) {
executedJobName := "proctor-123"
imageName := "any/image:name"
jobArgs := map[string]string{"key": "value"}
userEmail := "mrproctor@go-jek.com"

ctx := context.WithValue(context.Background(), utility.JobSubmissionStatusContextKey, utility.JobSubmissionSuccess)
ctx = context.WithValue(ctx, utility.JobNameContextKey, jobName)
ctx = context.WithValue(ctx, utility.JobNameSubmittedForExecutionContextKey, executedJobName)
ctx = context.WithValue(ctx, utility.ImageNameContextKey, imageName)
ctx = context.WithValue(ctx, utility.JobArgsContextKey, jobArgs)
ctx = context.WithValue(ctx, utility.UserEmailContextKey, userEmail)

done := make(chan bool, 2)
mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionSuccess, utility.JobWaiting, jobName, executedJobName, imageName, jobArgs).Return(nil).Once()
mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionSuccess, utility.JobWaiting, jobName, userEmail, executedJobName, imageName, jobArgs).Return(nil).Once()
mockKubeClient.On("JobExecutionStatus", executedJobName).Return("SUCCEEDED", nil).Once()
mockStore.On("UpdateJobsExecutionAuditLog", executedJobName, "SUCCEEDED").Return(nil).Run(func(args mock.Arguments) {}).Once().Run(func(args mock.Arguments) { done <- true })

Expand All @@ -45,7 +47,7 @@ func TestExecutionAuditorClientError(t *testing.T) {

ctx := context.WithValue(context.Background(), utility.JobSubmissionStatusContextKey, utility.JobSubmissionClientError)

mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionClientError, utility.JobFailed, "", "", "", map[string]string{}).Return(nil).Once()
mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionClientError, utility.JobFailed, "", "", "", "", map[string]string{}).Return(nil).Once()

testAuditor.AuditJobsExecution(ctx)

Expand All @@ -60,7 +62,7 @@ func TestExecutionAuditorServerError(t *testing.T) {

ctx := context.WithValue(context.Background(), utility.JobSubmissionStatusContextKey, utility.JobSubmissionServerError)

mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionServerError, utility.JobFailed, "", "", "", map[string]string{}).Return(nil).Once()
mockStore.On("JobsExecutionAuditLog", utility.JobSubmissionServerError, utility.JobFailed, "", "", "", "", map[string]string{}).Return(nil).Once()

testAuditor.AuditJobsExecution(ctx)

Expand Down
1 change: 1 addition & 0 deletions proctord/storage/postgres/schema.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import "database/sql"

type JobsExecutionAuditLog struct {
JobName string `db:"job_name"`
UserEmail string `db:"user_email"`
ImageName string `db:"image_name"`
JobNameSubmittedForExecution sql.NullString `db:"job_name_submitted_for_execution"`
JobArgs string `db:"job_args"`
Expand Down
7 changes: 4 additions & 3 deletions proctord/storage/store.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ import (
)

type Store interface {
JobsExecutionAuditLog(string, string, string, string, string, map[string]string) error
JobsExecutionAuditLog(string, string, string, string, string, string, map[string]string) error
UpdateJobsExecutionAuditLog(string, string) error
GetJobExecutionStatus(string) (string, error)
}
Expand All @@ -24,7 +24,7 @@ func New(postgresClient postgres.Client) Store {
}
}

func (store *store) JobsExecutionAuditLog(jobSubmissionStatus, jobExecutionStatus, jobName, JobNameSubmittedForExecution, imageName string, jobArgs map[string]string) error {
func (store *store) JobsExecutionAuditLog(jobSubmissionStatus, jobExecutionStatus, jobName, userEmail, JobNameSubmittedForExecution, imageName string, jobArgs map[string]string) error {
var encodedJobArgs bytes.Buffer
enc := gob.NewEncoder(&encodedJobArgs)
err := enc.Encode(jobArgs)
Expand All @@ -34,13 +34,14 @@ func (store *store) JobsExecutionAuditLog(jobSubmissionStatus, jobExecutionStatu

jobsExecutionAuditLog := postgres.JobsExecutionAuditLog{
JobName: jobName,
UserEmail: userEmail,
ImageName: imageName,
JobNameSubmittedForExecution: postgres.StringToSQLString(JobNameSubmittedForExecution),
JobArgs: base64.StdEncoding.EncodeToString(encodedJobArgs.Bytes()),
JobSubmissionStatus: jobSubmissionStatus,
JobExecutionStatus: jobExecutionStatus,
}
return store.postgresClient.NamedExec("INSERT INTO jobs_execution_audit_log (job_name, image_name, job_name_submitted_for_execution, job_args, job_submission_status, job_execution_status) VALUES (:job_name, :image_name, :job_name_submitted_for_execution, :job_args, :job_submission_status, :job_execution_status)", &jobsExecutionAuditLog)
return store.postgresClient.NamedExec("INSERT INTO jobs_execution_audit_log (job_name, user_email, image_name, job_name_submitted_for_execution, job_args, job_submission_status, job_execution_status) VALUES (:job_name, :user_email, :image_name, :job_name_submitted_for_execution, :job_args, :job_submission_status, :job_execution_status)", &jobsExecutionAuditLog)
}

func (store *store) UpdateJobsExecutionAuditLog(JobNameSubmittedForExecution, status string) error {
Expand Down
4 changes: 2 additions & 2 deletions proctord/storage/store_mock.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@ type MockStore struct {
mock.Mock
}

func (m *MockStore) JobsExecutionAuditLog(jobSubmissionStatus, jobExecutionStatus, jobName, jobExecutedName, imageName string, jobArgs map[string]string) error {
args := m.Called(jobSubmissionStatus, jobExecutionStatus, jobName, jobExecutedName, imageName, jobArgs)
func (m *MockStore) JobsExecutionAuditLog(jobSubmissionStatus, jobExecutionStatus, jobName, userEmail, jobExecutedName, imageName string, jobArgs map[string]string) error {
args := m.Called(jobSubmissionStatus, jobExecutionStatus, jobName, userEmail, jobExecutedName, imageName, jobArgs)
return args.Error(0)
}

Expand Down
10 changes: 6 additions & 4 deletions proctord/storage/store_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ func TestJobsExecutionAuditLog(t *testing.T) {

jobName := "any-job"
imageName := "any-image"
userEmail := "mrproctor@go-jek.com"
JobNameSubmittedForExecution := "any-submission"
jobArgs := map[string]string{"key": "value"}
jobSubmissionStatus := "any-status"
Expand All @@ -31,6 +32,7 @@ func TestJobsExecutionAuditLog(t *testing.T) {

data := postgres.JobsExecutionAuditLog{
JobName: jobName,
UserEmail: userEmail,
ImageName: imageName,
JobNameSubmittedForExecution: postgres.StringToSQLString(JobNameSubmittedForExecution),
JobArgs: base64.StdEncoding.EncodeToString(encodedJobArgs.Bytes()),
Expand All @@ -39,12 +41,12 @@ func TestJobsExecutionAuditLog(t *testing.T) {
}

mockPostgresClient.On("NamedExec",
"INSERT INTO jobs_execution_audit_log (job_name, image_name, job_name_submitted_for_execution, job_args, job_submission_status, job_execution_status) VALUES (:job_name, :image_name, :job_name_submitted_for_execution, :job_args, :job_submission_status, :job_execution_status)",
"INSERT INTO jobs_execution_audit_log (job_name, user_email, image_name, job_name_submitted_for_execution, job_args, job_submission_status, job_execution_status) VALUES (:job_name, :user_email, :image_name, :job_name_submitted_for_execution, :job_args, :job_submission_status, :job_execution_status)",
&data).
Return(nil).
Once()

err = testStore.JobsExecutionAuditLog(jobSubmissionStatus, jobExecutionStatus, jobName, JobNameSubmittedForExecution, imageName, jobArgs)
err = testStore.JobsExecutionAuditLog(jobSubmissionStatus, jobExecutionStatus, jobName, userEmail, JobNameSubmittedForExecution, imageName, jobArgs)

assert.NoError(t, err)
mockPostgresClient.AssertExpectations(t)
Expand All @@ -64,12 +66,12 @@ func TestJobsExecutionAuditLogPostgresClientFailure(t *testing.T) {
}

mockPostgresClient.On("NamedExec",
"INSERT INTO jobs_execution_audit_log (job_name, image_name, job_name_submitted_for_execution, job_args, job_submission_status, job_execution_status) VALUES (:job_name, :image_name, :job_name_submitted_for_execution, :job_args, :job_submission_status, :job_execution_status)",
"INSERT INTO jobs_execution_audit_log (job_name, user_email, image_name, job_name_submitted_for_execution, job_args, job_submission_status, job_execution_status) VALUES (:job_name, :user_email, :image_name, :job_name_submitted_for_execution, :job_args, :job_submission_status, :job_execution_status)",
&data).
Return(errors.New("error")).
Once()

err = testStore.JobsExecutionAuditLog("", "", "", "", "", map[string]string{})
err = testStore.JobsExecutionAuditLog("", "", "", "", "", "", map[string]string{})

assert.Error(t, err)
mockPostgresClient.AssertExpectations(t)
Expand Down
1 change: 1 addition & 0 deletions proctord/utility/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ const JobFailed = "FAILED"
const JobWaiting = "WAITING"

const JobNameContextKey = "job_name"
const UserEmailContextKey = "user_email"
const JobArgsContextKey = "job_args"
const ImageNameContextKey = "image_name"
const JobNameSubmittedForExecutionContextKey = "job_name_submitted_for_execution"
Expand Down

0 comments on commit fa91802

Please sign in to comment.