forked from gravitational/teleport
/
testhelpers.go
100 lines (84 loc) · 3.04 KB
/
testhelpers.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
/*
Copyright 2021 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package keystore
import (
"fmt"
"os"
"os/exec"
"strings"
"sync"
"testing"
"github.com/google/uuid"
"github.com/stretchr/testify/require"
)
var (
cachedConfig *Config
cacheMutex sync.Mutex
)
// SetupSoftHSMToken is for use in tests only and creates a test SOFTHSM2
// token. This should be used for all tests which need to use SoftHSM because
// the library can only be initialized once and SOFTHSM2_PATH and SOFTHSM2_CONF
// cannot be changed. New tokens added after the library has been initialized
// will not be found by the library.
//
// A new token will be used for each `go test` invocation, but it's difficult
// to create a separate token for each test because because new tokens
// added after the library has been initialized will not be found by the
// library. It's also difficult to clean up the token because tests for all
// packages are run in parallel there is not a good time to safely
// delete the token or the entire token directory. Each test should clean up
// all keys that it creates because SoftHSM2 gets really slow when there are
// many keys for a given token.
func SetupSoftHSMTest(t *testing.T) Config {
path := os.Getenv("SOFTHSM2_PATH")
require.NotEqual(t, path, "")
cacheMutex.Lock()
defer cacheMutex.Unlock()
if cachedConfig != nil {
return *cachedConfig
}
if os.Getenv("SOFTHSM2_CONF") == "" {
// create tokendir
tokenDir, err := os.MkdirTemp("", "tokens")
require.NoError(t, err)
// create config file
configFile, err := os.CreateTemp("", "softhsm2.conf")
require.NoError(t, err)
// write config file
_, err = configFile.WriteString(fmt.Sprintf(
"directories.tokendir = %s\nobjectstore.backend = file\nlog.level = DEBUG\n",
tokenDir))
require.NoError(t, err)
require.NoError(t, configFile.Close())
// set env
os.Setenv("SOFTHSM2_CONF", configFile.Name())
}
// create test token (max length is 32 chars)
tokenLabel := strings.Replace(uuid.NewString(), "-", "", -1)
cmd := exec.Command("softhsm2-util", "--init-token", "--free", "--label", tokenLabel, "--so-pin", "password", "--pin", "password")
t.Logf("Running command: %q", cmd)
if err := cmd.Run(); err != nil {
if exitErr, ok := err.(*exec.ExitError); ok {
require.NoError(t, exitErr, "error creating test softhsm token: %s", string(exitErr.Stderr))
}
require.NoError(t, err, "error attempting to run softhsm2-util")
}
cachedConfig = &Config{
PKCS11: PKCS11Config{
Path: path,
TokenLabel: tokenLabel,
Pin: "password",
},
}
return *cachedConfig
}