feat: replace ciphertext in log

This commit is contained in:
Jason Song 2022-11-02 12:12:45 +08:00
parent 222b5100b6
commit d650bae0b6
2 changed files with 23 additions and 11 deletions

View File

@ -27,17 +27,27 @@ type Reporter struct {
logOffset int
logRows []*runnerv1.LogRow
logReplacer *strings.Replacer
state *runnerv1.TaskState
stateM sync.RWMutex
}
func NewReporter(ctx context.Context, cancel context.CancelFunc, client client.Client, taskID int64) *Reporter {
func NewReporter(ctx context.Context, cancel context.CancelFunc, client client.Client, task *runnerv1.Task) *Reporter {
var oldnew []string
if v := task.Context.Fields["token"].GetStringValue(); v != "" {
oldnew = append(oldnew, v, "***")
}
for _, v := range task.Secrets {
oldnew = append(oldnew, v, "***")
}
return &Reporter{
ctx: ctx,
cancel: cancel,
client: client,
logReplacer: strings.NewReplacer(oldnew...),
state: &runnerv1.TaskState{
Id: taskID,
Id: task.Id,
},
}
}
@ -266,8 +276,10 @@ func (r *Reporter) parseResult(result interface{}) (runnerv1.Result, bool) {
}
func (r *Reporter) parseLogRow(entry *log.Entry) *runnerv1.LogRow {
content := strings.TrimSuffix(entry.Message, "\r\n")
content = r.logReplacer.Replace(content)
return &runnerv1.LogRow{
Time: timestamppb.New(entry.Time),
Content: strings.TrimSuffix(entry.Message, "\r\n"),
Content: content,
}
}

View File

@ -131,7 +131,7 @@ func (t *Task) Run(ctx context.Context, task *runnerv1.Task) error {
defer globalTaskMap.Delete(task.Id)
lastWords := ""
reporter := NewReporter(ctx, cancel, t.client, task.Id)
reporter := NewReporter(ctx, cancel, t.client, task)
defer func() {
_ = reporter.Close(lastWords)
}()