Skip to content

Commit

Permalink
Add RuntimeState and keylogger to it
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 7, 2022
1 parent ece4633 commit 71b9c50
Show file tree
Hide file tree
Showing 5 changed files with 62 additions and 3 deletions.
5 changes: 5 additions & 0 deletions cmd/run.go
Original file line number Diff line number Diff line change
Expand Up @@ -268,6 +268,11 @@ func (c *cmdRun) run(cmd *cobra.Command, args []string) error {
logger.Debug("Waiting for engine processes to finish...")
engineWait()
logger.Debug("Everything has finished, exiting k6!")
if test.keywriter != nil {
if err := test.keywriter.Close(); err != nil {
logger.WithError(err).Warn("error while closing the SSLKEYLOGFILE")
}
}
if interrupt != nil {
return interrupt
}
Expand Down
6 changes: 6 additions & 0 deletions cmd/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,6 +117,12 @@ func getRuntimeOptions(flags *pflag.FlagSet, environment map[string]string) (lib
}
}

if envVar, ok := environment["SSLKEYLOGFILE"]; ok {
if !opts.KeyWriter.Valid {
opts.KeyWriter = null.StringFrom(envVar)
}
}

if opts.IncludeSystemEnvVars.Bool { // If enabled, gather the actual system environment variables
opts.Env = environment
}
Expand Down
34 changes: 31 additions & 3 deletions cmd/test_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,9 @@ import (
"archive/tar"
"bytes"
"fmt"
"io"
"os"
"sync"

"github.com/spf13/afero"
"github.com/spf13/cobra"
Expand Down Expand Up @@ -31,6 +34,7 @@ type loadedTest struct {
metricsRegistry *metrics.Registry
builtInMetrics *metrics.BuiltinMetrics
initRunner lib.Runner // TODO: rename to something more appropriate
keywriter io.Closer

// Only set if cliConfigGetter is supplied to loadAndConfigureTest() or if
// consolidateDeriveAndValidateConfig() is manually called.
Expand Down Expand Up @@ -103,9 +107,22 @@ func (lt *loadedTest) initializeFirstRunner(gs *globalState) error {
switch testType {
case testTypeJS:
logger.Debug("Trying to load as a JS test...")
runner, err := js.New(
gs.logger, lt.source, lt.fileSystems, lt.runtimeOptions, lt.builtInMetrics, lt.metricsRegistry,
)
state := &lib.RuntimeState{
Logger: gs.logger,
RuntimeOptions: &lt.runtimeOptions,
BuiltinMetrics: lt.builtInMetrics,
Registry: lt.metricsRegistry,
}
if lt.runtimeOptions.KeyWriter.Valid {
fs := lt.fileSystems["file"]
f, err := fs.OpenFile(lt.runtimeOptions.KeyWriter.String, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
return err
}
lt.keywriter = f
state.KeyLogger = &syncWriter{w: f}
}
runner, err := js.NewFromState(state, lt.source, lt.fileSystems)
// TODO: should we use common.UnwrapGojaInterruptedError() here?
if err != nil {
return fmt.Errorf("could not load JS test '%s': %w", testPath, err)
Expand Down Expand Up @@ -202,3 +219,14 @@ func (lt *loadedTest) consolidateDeriveAndValidateConfig(

return nil
}

type syncWriter struct {
w io.Writer
m sync.Mutex
}

func (cw *syncWriter) Write(b []byte) (int, error) {
cw.m.Lock()
defer cw.m.Unlock()
return cw.w.Write(b)
}
19 changes: 19 additions & 0 deletions js/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,6 +79,24 @@ type Runner struct {

console *console
setupData []byte

keylogger io.Writer
}

func NewFromState(
rs *lib.RuntimeState, src *loader.SourceData, filesystems map[string]afero.Fs,
) (*Runner, error) {
bundle, err := NewBundle(rs.Logger, src, filesystems, *rs.RuntimeOptions, rs.Registry)
if err != nil {
return nil, err
}

r, err := NewFromBundle(rs.Logger, bundle, rs.BuiltinMetrics, rs.Registry)
if err != nil {
return nil, err
}
r.keylogger = rs.KeyLogger
return r, nil
}

// New returns a new Runner for the provide source
Expand Down Expand Up @@ -207,6 +225,7 @@ func (r *Runner) newVU(idLocal, idGlobal uint64, samplesOut chan<- metrics.Sampl
MaxVersion: uint16(tlsVersions.Max),
Certificates: certs,
Renegotiation: tls.RenegotiateFreelyAsClient,
KeyLogWriter: r.keylogger,
}
// Follow NameToCertificate in https://pkg.go.dev/crypto/[email protected]#Config, leave this field nil
// when it is empty
Expand Down
1 change: 1 addition & 0 deletions lib/runtime_options.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,7 @@ type RuntimeOptions struct {
NoThresholds null.Bool `json:"noThresholds"`
NoSummary null.Bool `json:"noSummary"`
SummaryExport null.String `json:"summaryExport"`
KeyWriter null.String `json:"-"`
}

// ValidateCompatibilityMode checks if the provided val is a valid compatibility mode
Expand Down

0 comments on commit 71b9c50

Please sign in to comment.