Skip to content

Commit

Permalink
fixup! Add SSLKEYLOGFILE support to k6
Browse files Browse the repository at this point in the history
  • Loading branch information
mstoykov committed Apr 8, 2022
1 parent 520ea79 commit 010e23d
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 5 deletions.
33 changes: 33 additions & 0 deletions cmd/integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.k6.io/k6/lib/testutils"
"go.k6.io/k6/lib/testutils/httpmultibin"
)

const (
Expand Down Expand Up @@ -229,6 +230,38 @@ func TestMetricsAndThresholds(t *testing.T) {
require.Equal(t, expected, teardownThresholds)
}

func TestSSLKEYLOGFILE(t *testing.T) {
t.Parallel()

// TODO don't use insecureSkipTLSVerify when/if tlsConfig is given to the runner from outside
tb := httpmultibin.NewHTTPMultiBin(t)
ts := newGlobalTestState(t)
ts.args = []string{"k6", "run", "-"}
ts.envVars = map[string]string{"SSLKEYLOGFILE": "./ssl.log"}
ts.stdIn = bytes.NewReader([]byte(tb.Replacer.Replace(`
import http from "k6/http"
export const options = {
hosts: {
"HTTPSBIN_DOMAIN": "HTTPSBIN_IP",
},
insecureSkipTLSVerify: true,
}
export default () => {
http.get("HTTPSBIN_URL/get");
}
`)))

newRootCommand(ts.globalState).execute()

assert.Empty(t, ts.stdErr.String())
assert.Empty(t, ts.loggerHook.Drain())
sslloglines, err := afero.ReadFile(ts.fs, filepath.Join(ts.cwd, "ssl.log"))
require.NoError(t, err)
// TODO maybe have multiple depending on the ciphers used as that seems to change it
require.Regexp(t, "^CLIENT_[A-Z_]+ [0-9a-f]+ [0-9a-f]+\n", string(sslloglines))
}

// TODO: add a hell of a lot more integration tests, including some that spin up
// a test HTTP server and actually check if k6 hits it

Expand Down
26 changes: 21 additions & 5 deletions cmd/test_load.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"fmt"
"io"
"os"
"path/filepath"
"sync"

"github.com/spf13/afero"
Expand All @@ -28,6 +29,7 @@ const (
// and configured k6 test.
type loadedTest struct {
sourceRootPath string // contains the raw string the user supplied
pwd string
source *loader.SourceData
fileSystems map[string]afero.Fs
runtimeOptions lib.RuntimeOptions
Expand All @@ -53,7 +55,7 @@ func loadAndConfigureTest(

sourceRootPath := args[0]
gs.logger.Debugf("Resolving and reading test '%s'...", sourceRootPath)
src, fileSystems, err := readSource(gs, sourceRootPath)
src, fileSystems, pwd, err := readSource(gs, sourceRootPath)
if err != nil {
return nil, err
}
Expand All @@ -71,6 +73,7 @@ func loadAndConfigureTest(

registry := metrics.NewRegistry()
test := &loadedTest{
pwd: pwd,
sourceRootPath: sourceRootPath,
source: src,
fileSystems: fileSystems,
Expand Down Expand Up @@ -112,7 +115,20 @@ func (lt *loadedTest) initializeFirstRunner(gs *globalState) error {
}
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)
keyfileName := filepath.Join(lt.pwd, lt.runtimeOptions.KeyWriter.String)
if ok, err := afero.Exists(fs, keyfileName); !ok {
// This whole parts is because afero has problems creating files with OpenFile.
// And while the latest version fixes those for MemMapFS, it doesn't for OsFs.
if err != nil {
return err
}

if _, err = fs.Create(keyfileName); err != nil { // this unfortunately defaults to 664, but nothing else works
return err
}
}

f, err := fs.OpenFile(keyfileName, os.O_WRONLY|os.O_CREATE|os.O_APPEND, 0o600)
if err != nil {
return err
}
Expand Down Expand Up @@ -158,15 +174,15 @@ func (lt *loadedTest) initializeFirstRunner(gs *globalState) error {

// readSource is a small wrapper around loader.ReadSource returning
// result of the load and filesystems map
func readSource(globalState *globalState, filename string) (*loader.SourceData, map[string]afero.Fs, error) {
func readSource(globalState *globalState, filename string) (*loader.SourceData, map[string]afero.Fs, string, error) {
pwd, err := globalState.getwd()
if err != nil {
return nil, nil, err
return nil, nil, "", err
}

filesystems := loader.CreateFilesystems(globalState.fs)
src, err := loader.ReadSource(globalState.logger, filename, pwd, filesystems, globalState.stdIn)
return src, filesystems, err
return src, filesystems, pwd, err
}

func detectTestType(data []byte) string {
Expand Down

0 comments on commit 010e23d

Please sign in to comment.