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

@ -25,19 +25,29 @@ type Reporter struct {
client client.Client client client.Client
clientM sync.Mutex clientM sync.Mutex
logOffset int logOffset int
logRows []*runnerv1.LogRow logRows []*runnerv1.LogRow
state *runnerv1.TaskState logReplacer *strings.Replacer
stateM sync.RWMutex 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{ return &Reporter{
ctx: ctx, ctx: ctx,
cancel: cancel, cancel: cancel,
client: client, client: client,
logReplacer: strings.NewReplacer(oldnew...),
state: &runnerv1.TaskState{ 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 { func (r *Reporter) parseLogRow(entry *log.Entry) *runnerv1.LogRow {
content := strings.TrimSuffix(entry.Message, "\r\n")
content = r.logReplacer.Replace(content)
return &runnerv1.LogRow{ return &runnerv1.LogRow{
Time: timestamppb.New(entry.Time), 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) defer globalTaskMap.Delete(task.Id)
lastWords := "" lastWords := ""
reporter := NewReporter(ctx, cancel, t.client, task.Id) reporter := NewReporter(ctx, cancel, t.client, task)
defer func() { defer func() {
_ = reporter.Close(lastWords) _ = reporter.Close(lastWords)
}() }()