2020-02-07 14:17:58 +08:00
|
|
|
package runner
|
2019-01-13 12:45:25 +08:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2020-02-12 01:10:35 +08:00
|
|
|
"context"
|
2019-01-13 12:45:25 +08:00
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"strings"
|
2020-02-12 15:38:30 +08:00
|
|
|
"sync"
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-02-12 01:10:35 +08:00
|
|
|
"github.com/nektos/act/pkg/common"
|
|
|
|
|
2019-01-13 12:45:25 +08:00
|
|
|
"github.com/sirupsen/logrus"
|
2021-01-12 14:39:43 +08:00
|
|
|
"golang.org/x/term"
|
2019-01-13 12:45:25 +08:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2021-01-12 14:39:43 +08:00
|
|
|
// nocolor = 0
|
2020-02-12 01:10:35 +08:00
|
|
|
red = 31
|
|
|
|
green = 32
|
|
|
|
yellow = 33
|
|
|
|
blue = 34
|
|
|
|
magenta = 35
|
|
|
|
cyan = 36
|
|
|
|
gray = 37
|
2019-01-13 12:45:25 +08:00
|
|
|
)
|
|
|
|
|
2020-02-12 01:10:35 +08:00
|
|
|
var colors []int
|
|
|
|
var nextColor int
|
2020-02-12 15:38:30 +08:00
|
|
|
var mux sync.Mutex
|
2020-02-12 01:10:35 +08:00
|
|
|
|
|
|
|
func init() {
|
|
|
|
nextColor = 0
|
|
|
|
colors = []int{
|
|
|
|
blue, yellow, green, magenta, red, gray, cyan,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-12 03:06:05 +08:00
|
|
|
type masksContextKey string
|
|
|
|
|
|
|
|
const masksContextKeyVal = masksContextKey("logrus.FieldLogger")
|
|
|
|
|
|
|
|
// Logger returns the appropriate logger for current context
|
|
|
|
func Masks(ctx context.Context) *[]string {
|
|
|
|
val := ctx.Value(masksContextKeyVal)
|
|
|
|
if val != nil {
|
|
|
|
if masks, ok := val.(*[]string); ok {
|
|
|
|
return masks
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return &[]string{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// WithLogger adds a value to the context for the logger
|
|
|
|
func WithMasks(ctx context.Context, masks *[]string) context.Context {
|
|
|
|
return context.WithValue(ctx, masksContextKeyVal, masks)
|
|
|
|
}
|
|
|
|
|
2022-12-09 18:25:32 +08:00
|
|
|
type JobLoggerFactory interface {
|
|
|
|
WithJobLogger() *logrus.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
type jobLoggerFactoryContextKey string
|
|
|
|
|
|
|
|
var jobLoggerFactoryContextKeyVal = (jobLoggerFactoryContextKey)("jobloggerkey")
|
|
|
|
|
|
|
|
func WithJobLoggerFactory(ctx context.Context, factory JobLoggerFactory) context.Context {
|
|
|
|
return context.WithValue(ctx, jobLoggerFactoryContextKeyVal, factory)
|
|
|
|
}
|
|
|
|
|
2020-02-12 01:10:35 +08:00
|
|
|
// WithJobLogger attaches a new logger to context that is aware of steps
|
2022-09-27 23:49:22 +08:00
|
|
|
func WithJobLogger(ctx context.Context, jobID string, jobName string, config *Config, masks *[]string, matrix map[string]interface{}) context.Context {
|
2022-12-09 18:25:32 +08:00
|
|
|
ctx = WithMasks(ctx, masks)
|
|
|
|
|
|
|
|
var logger *logrus.Logger
|
|
|
|
if jobLoggerFactory, ok := ctx.Value(jobLoggerFactoryContextKeyVal).(JobLoggerFactory); ok && jobLoggerFactory != nil {
|
|
|
|
logger = jobLoggerFactory.WithJobLogger()
|
2022-03-14 23:33:11 +08:00
|
|
|
} else {
|
2022-12-09 18:25:32 +08:00
|
|
|
var formatter logrus.Formatter
|
|
|
|
if config.JSONLogger {
|
|
|
|
formatter = &logrus.JSONFormatter{}
|
|
|
|
} else {
|
|
|
|
mux.Lock()
|
|
|
|
defer mux.Unlock()
|
|
|
|
nextColor++
|
|
|
|
formatter = &jobLogFormatter{
|
2023-07-20 05:45:44 +08:00
|
|
|
color: colors[nextColor%len(colors)],
|
|
|
|
logPrefixJobID: config.LogPrefixJobID,
|
2022-12-09 18:25:32 +08:00
|
|
|
}
|
2022-03-14 23:33:11 +08:00
|
|
|
}
|
2022-03-02 16:29:34 +08:00
|
|
|
|
2022-12-09 18:25:32 +08:00
|
|
|
logger = logrus.New()
|
|
|
|
logger.SetOutput(os.Stdout)
|
|
|
|
logger.SetLevel(logrus.GetLevel())
|
|
|
|
logger.SetFormatter(formatter)
|
|
|
|
}
|
2020-02-12 01:10:35 +08:00
|
|
|
|
2022-12-09 18:25:32 +08:00
|
|
|
logger.SetFormatter(&maskedFormatter{
|
|
|
|
Formatter: logger.Formatter,
|
|
|
|
masker: valueMasker(config.InsecureSecrets, config.Secrets),
|
|
|
|
})
|
2022-06-17 23:55:21 +08:00
|
|
|
rtn := logger.WithFields(logrus.Fields{
|
|
|
|
"job": jobName,
|
|
|
|
"jobID": jobID,
|
|
|
|
"dryrun": common.Dryrun(ctx),
|
2022-09-27 23:49:22 +08:00
|
|
|
"matrix": matrix,
|
2022-06-17 23:55:21 +08:00
|
|
|
}).WithContext(ctx)
|
2020-02-12 01:10:35 +08:00
|
|
|
|
|
|
|
return common.WithLogger(ctx, rtn)
|
2019-01-13 12:45:25 +08:00
|
|
|
}
|
|
|
|
|
2022-05-12 03:06:05 +08:00
|
|
|
func WithCompositeLogger(ctx context.Context, masks *[]string) context.Context {
|
|
|
|
ctx = WithMasks(ctx, masks)
|
|
|
|
return common.WithLogger(ctx, common.Logger(ctx).WithFields(logrus.Fields{}).WithContext(ctx))
|
|
|
|
}
|
|
|
|
|
2022-07-28 03:56:41 +08:00
|
|
|
func WithCompositeStepLogger(ctx context.Context, stepID string) context.Context {
|
|
|
|
val := common.Logger(ctx)
|
|
|
|
stepIDs := make([]string, 0)
|
|
|
|
|
|
|
|
if logger, ok := val.(*logrus.Entry); ok {
|
|
|
|
if oldStepIDs, ok := logger.Data["stepID"].([]string); ok {
|
|
|
|
stepIDs = append(stepIDs, oldStepIDs...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
stepIDs = append(stepIDs, stepID)
|
|
|
|
|
|
|
|
return common.WithLogger(ctx, common.Logger(ctx).WithFields(logrus.Fields{
|
|
|
|
"stepID": stepIDs,
|
|
|
|
}).WithContext(ctx))
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
func withStepLogger(ctx context.Context, stepID string, stepName string, stageName string) context.Context {
|
|
|
|
rtn := common.Logger(ctx).WithFields(logrus.Fields{
|
|
|
|
"step": stepName,
|
2022-07-28 03:56:41 +08:00
|
|
|
"stepID": []string{stepID},
|
2022-06-17 23:55:21 +08:00
|
|
|
"stage": stageName,
|
|
|
|
})
|
2022-03-15 01:38:30 +08:00
|
|
|
return common.WithLogger(ctx, rtn)
|
|
|
|
}
|
|
|
|
|
2022-03-14 23:33:11 +08:00
|
|
|
type entryProcessor func(entry *logrus.Entry) *logrus.Entry
|
2020-02-07 14:17:58 +08:00
|
|
|
|
2022-05-12 03:06:05 +08:00
|
|
|
func valueMasker(insecureSecrets bool, secrets map[string]string) entryProcessor {
|
2022-03-14 23:33:11 +08:00
|
|
|
return func(entry *logrus.Entry) *logrus.Entry {
|
|
|
|
if insecureSecrets {
|
|
|
|
return entry
|
|
|
|
}
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2022-05-12 03:06:05 +08:00
|
|
|
masks := Masks(entry.Context)
|
|
|
|
|
2022-03-14 23:33:11 +08:00
|
|
|
for _, v := range secrets {
|
2021-06-07 21:54:12 +08:00
|
|
|
if v != "" {
|
|
|
|
entry.Message = strings.ReplaceAll(entry.Message, v, "***")
|
|
|
|
}
|
2021-01-12 14:28:45 +08:00
|
|
|
}
|
2022-03-02 16:29:34 +08:00
|
|
|
|
2022-03-14 23:33:11 +08:00
|
|
|
for _, v := range *masks {
|
2022-03-02 16:29:34 +08:00
|
|
|
if v != "" {
|
|
|
|
entry.Message = strings.ReplaceAll(entry.Message, v, "***")
|
|
|
|
}
|
|
|
|
}
|
2022-03-14 23:33:11 +08:00
|
|
|
|
|
|
|
return entry
|
2020-05-14 06:22:31 +08:00
|
|
|
}
|
2022-03-14 23:33:11 +08:00
|
|
|
}
|
|
|
|
|
2022-12-09 18:25:32 +08:00
|
|
|
type maskedFormatter struct {
|
|
|
|
logrus.Formatter
|
2022-03-14 23:33:11 +08:00
|
|
|
masker entryProcessor
|
|
|
|
}
|
|
|
|
|
2022-12-09 18:25:32 +08:00
|
|
|
func (f *maskedFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
|
|
return f.Formatter.Format(f.masker(entry))
|
|
|
|
}
|
|
|
|
|
|
|
|
type jobLogFormatter struct {
|
2023-07-20 05:45:44 +08:00
|
|
|
color int
|
|
|
|
logPrefixJobID bool
|
2022-12-09 18:25:32 +08:00
|
|
|
}
|
|
|
|
|
2022-03-14 23:33:11 +08:00
|
|
|
func (f *jobLogFormatter) Format(entry *logrus.Entry) ([]byte, error) {
|
|
|
|
b := &bytes.Buffer{}
|
|
|
|
|
2019-01-13 12:45:25 +08:00
|
|
|
if f.isColored(entry) {
|
|
|
|
f.printColored(b, entry)
|
|
|
|
} else {
|
|
|
|
f.print(b, entry)
|
|
|
|
}
|
|
|
|
|
|
|
|
b.WriteByte('\n')
|
|
|
|
return b.Bytes(), nil
|
|
|
|
}
|
|
|
|
|
2022-03-14 23:33:11 +08:00
|
|
|
func (f *jobLogFormatter) printColored(b *bytes.Buffer, entry *logrus.Entry) {
|
2019-01-13 12:45:25 +08:00
|
|
|
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
2023-07-20 05:45:44 +08:00
|
|
|
|
|
|
|
var job any
|
|
|
|
if f.logPrefixJobID {
|
|
|
|
job = entry.Data["jobID"]
|
|
|
|
} else {
|
|
|
|
job = entry.Data["job"]
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
debugFlag := ""
|
|
|
|
if entry.Level == logrus.DebugLevel {
|
|
|
|
debugFlag = "[DEBUG] "
|
|
|
|
}
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-02-12 15:38:30 +08:00
|
|
|
if entry.Data["raw_output"] == true {
|
|
|
|
fmt.Fprintf(b, "\x1b[%dm|\x1b[0m %s", f.color, entry.Message)
|
|
|
|
} else if entry.Data["dryrun"] == true {
|
2023-07-20 05:45:44 +08:00
|
|
|
fmt.Fprintf(b, "\x1b[1m\x1b[%dm\x1b[7m*DRYRUN*\x1b[0m \x1b[%dm[%s] \x1b[0m%s%s", gray, f.color, job, debugFlag, entry.Message)
|
2019-01-13 12:45:25 +08:00
|
|
|
} else {
|
2023-07-20 05:45:44 +08:00
|
|
|
fmt.Fprintf(b, "\x1b[%dm[%s] \x1b[0m%s%s", f.color, job, debugFlag, entry.Message)
|
2019-01-13 12:45:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 23:33:11 +08:00
|
|
|
func (f *jobLogFormatter) print(b *bytes.Buffer, entry *logrus.Entry) {
|
2019-01-13 12:45:25 +08:00
|
|
|
entry.Message = strings.TrimSuffix(entry.Message, "\n")
|
2023-07-20 05:45:44 +08:00
|
|
|
|
|
|
|
var job any
|
|
|
|
if f.logPrefixJobID {
|
|
|
|
job = entry.Data["jobID"]
|
|
|
|
} else {
|
|
|
|
job = entry.Data["job"]
|
|
|
|
}
|
|
|
|
|
2022-06-17 23:55:21 +08:00
|
|
|
debugFlag := ""
|
|
|
|
if entry.Level == logrus.DebugLevel {
|
|
|
|
debugFlag = "[DEBUG] "
|
|
|
|
}
|
2019-01-13 12:45:25 +08:00
|
|
|
|
2020-02-12 15:38:30 +08:00
|
|
|
if entry.Data["raw_output"] == true {
|
2023-07-20 05:45:44 +08:00
|
|
|
fmt.Fprintf(b, "[%s] | %s", job, entry.Message)
|
2020-02-12 15:38:30 +08:00
|
|
|
} else if entry.Data["dryrun"] == true {
|
2023-07-20 05:45:44 +08:00
|
|
|
fmt.Fprintf(b, "*DRYRUN* [%s] %s%s", job, debugFlag, entry.Message)
|
2019-01-13 12:45:25 +08:00
|
|
|
} else {
|
2023-07-20 05:45:44 +08:00
|
|
|
fmt.Fprintf(b, "[%s] %s%s", job, debugFlag, entry.Message)
|
2019-01-13 12:45:25 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-14 23:33:11 +08:00
|
|
|
func (f *jobLogFormatter) isColored(entry *logrus.Entry) bool {
|
2019-01-13 12:45:25 +08:00
|
|
|
isColored := checkIfTerminal(entry.Logger.Out)
|
|
|
|
|
|
|
|
if force, ok := os.LookupEnv("CLICOLOR_FORCE"); ok && force != "0" {
|
|
|
|
isColored = true
|
|
|
|
} else if ok && force == "0" {
|
|
|
|
isColored = false
|
|
|
|
} else if os.Getenv("CLICOLOR") == "0" {
|
|
|
|
isColored = false
|
|
|
|
}
|
|
|
|
|
|
|
|
return isColored
|
|
|
|
}
|
|
|
|
|
|
|
|
func checkIfTerminal(w io.Writer) bool {
|
|
|
|
switch v := w.(type) {
|
|
|
|
case *os.File:
|
2021-01-12 14:39:43 +08:00
|
|
|
return term.IsTerminal(int(v.Fd()))
|
2019-01-13 12:45:25 +08:00
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|