Skip to content

Commit

Permalink
Alter BuildTargets and Setup function signatures to accept a struct w…
Browse files Browse the repository at this point in the history
…ith more info (#138)

* rebase 1

* rebase 2

* add back context

* add randomness to signin

* updgrade deps

* rebase 3

* remove ldap ref

* switch position

* fix spacing

* replace func

* fix params

* make sure proper token is signed based on iam vs gce

* Update benchmarktests/target_auth_gcp.go

Co-authored-by: Luis (LT) Carbonell <lt.carbonell@hashicorp.com>

* Update benchmarktests/target_auth_gcp.go

Co-authored-by: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com>

* Update benchmarktests/target_auth_gcp.go

Co-authored-by: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com>

* cleanup config

* fix name

* add top level config struct

* remove gcp logic

* cleanup

* remove unrelated keys from toplevelconfig struct

* fix client reference

* fix rebase

---------

Co-authored-by: Luis (LT) Carbonell <lt.carbonell@hashicorp.com>
Co-authored-by: Jason O'Donnell <2160810+jasonodonnell@users.noreply.github.com>
  • Loading branch information
3 people committed Aug 7, 2023
1 parent d1e1641 commit 19b41d0
Show file tree
Hide file tree
Showing 36 changed files with 101 additions and 92 deletions.
15 changes: 11 additions & 4 deletions benchmarktests/benchmark_targets.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,13 +12,20 @@ import (
"os"
"sort"
"sync"
"time"

"github.com/hashicorp/go-hclog"
"github.com/hashicorp/hcl/v2"
"github.com/hashicorp/vault/api"
vegeta "github.com/tsenart/vegeta/v12/lib"
)

// Configuration that applies to all individual tests
type TopLevelTargetConfig struct {
Duration time.Duration
RandomMounts bool
}

const (
VaultBenchmarkEnvVarPrefix = "VAULT_BENCHMARK_"
)
Expand All @@ -30,7 +37,7 @@ type BenchmarkBuilder interface {
// Setup uses the passed in client and configuration to create the necessary test resources
// in Vault, and retrieve any necessary information needed to perform the test itself. Setup
// returns a test struct type which satisfies this BenchmarkBuilder interface.
Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error)
Setup(client *api.Client, mountName string, config *TopLevelTargetConfig) (BenchmarkBuilder, error)

// Cleanup uses the passed in client to clean up any created resources used as part of the test
Cleanup(client *api.Client) error
Expand Down Expand Up @@ -180,10 +187,10 @@ func (tm TargetMulti) DebugInfo(client *api.Client) {
}
}

func BuildTargets(tests []*BenchmarkTarget, client *api.Client, logger hclog.Logger, randomMounts bool) (*TargetMulti, error) {
func BuildTargets(client *api.Client, tests []*BenchmarkTarget, logger *hclog.Logger, config *TopLevelTargetConfig) (*TargetMulti, error) {
var tm TargetMulti
var err error
targetLogger = logger
targetLogger = *logger

// Check to make sure all weights add to 100
err = percentageValidate(tests)
Expand All @@ -198,7 +205,7 @@ func BuildTargets(tests []*BenchmarkTarget, client *api.Client, logger hclog.Log
if bvTest.MountName != "" {
mountName = bvTest.MountName
}
bvTest.Builder, err = bvTest.Builder.Setup(client, randomMounts, mountName)
bvTest.Builder, err = bvTest.Builder.Setup(client, mountName, config)
if err != nil {
// TODO:
// We should look to implement some mechanism to clean up the mount if we
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_auth_approle.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (a *ApproleAuth) GetTargetInfo() TargetInfo {
}
}

func (a *ApproleAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (a *ApproleAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
a.logger = targetLogger.Named(ApproleAuthTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_auth_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -147,12 +147,12 @@ func (a *AWSAuth) GetTargetInfo() TargetInfo {
}
}

func (a *AWSAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (a *AWSAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
a.logger = targetLogger.Named(AWSAuthTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_auth_cert.go
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,12 @@ func (c *CertAuth) GetTargetInfo() TargetInfo {
}
}

func (c *CertAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (c *CertAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
c.logger = targetLogger.Named(CertAuthTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
41 changes: 19 additions & 22 deletions benchmarktests/target_auth_gcp.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,14 +49,10 @@ type GCPAuth struct {
jwt string
header http.Header
timeout time.Duration
config *GCPTestConfig
config *GCPAuthTestConfig
logger hclog.Logger
}

type GCPTestConfig struct {
Config *GCPAuthTestConfig `hcl:"config,block"`
}

type GCPAuthTestConfig struct {
GCPAuthConfig *GCPAuthConfig `hcl:"auth,block"`
GCPTestRoleConfig *GCPTestRoleConfig `hcl:"role,block"`
Expand Down Expand Up @@ -95,18 +91,20 @@ type GCPTestRoleConfig struct {
}

func (g *GCPAuth) ParseConfig(body hcl.Body) error {
g.config = &GCPTestConfig{
Config: &GCPAuthTestConfig{
GCPAuthConfig: &GCPAuthConfig{},
GCPTestRoleConfig: &GCPTestRoleConfig{},
},
testConfig := &struct {
Config *GCPAuthTestConfig `hcl:"config,block"`
}{Config: &GCPAuthTestConfig{
GCPAuthConfig: &GCPAuthConfig{},
GCPTestRoleConfig: &GCPTestRoleConfig{},
},
}

diags := gohcl.DecodeBody(body, nil, g.config)
diags := gohcl.DecodeBody(body, nil, testConfig)
if diags.HasErrors() {
return fmt.Errorf("error decoding to struct: %v", diags)
}

g.config = testConfig.Config
return nil
}

Expand Down Expand Up @@ -135,13 +133,12 @@ func (g *GCPAuth) GetTargetInfo() TargetInfo {
}
}

func (g *GCPAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (g *GCPAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
config := g.config.Config
g.logger = targetLogger.Named(GCPAuthTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
return nil, fmt.Errorf("can't generate UUID for mount name: %v", err)
Expand All @@ -158,18 +155,18 @@ func (g *GCPAuth) Setup(client *api.Client, randomMountName bool, mountName stri
setupLogger := g.logger.Named(authPath)

// check if the provided argument should be read from file
creds := config.GCPAuthConfig.Credentials
creds := g.config.GCPAuthConfig.Credentials
if len(creds) > 0 && creds[0] == '@' {
contents, err := ioutil.ReadFile(creds[1:])
if err != nil {
return nil, fmt.Errorf("error reading file: %w", err)
}

config.GCPAuthConfig.Credentials = string(contents)
g.config.GCPAuthConfig.Credentials = string(contents)
}

setupLogger.Trace(parsingConfigLogMessage("gcp auth"))
GCPAuthConfig, err := structToMap(config.GCPAuthConfig)
GCPAuthConfig, err := structToMap(g.config.GCPAuthConfig)
if err != nil {
return nil, fmt.Errorf("error parsing gcp auth config from struct: %v", err)
}
Expand All @@ -182,19 +179,19 @@ func (g *GCPAuth) Setup(client *api.Client, randomMountName bool, mountName stri
}

setupLogger.Trace(parsingConfigLogMessage("role"))
GCPRoleConfig, err := structToMap(config.GCPTestRoleConfig)
GCPRoleConfig, err := structToMap(g.config.GCPTestRoleConfig)
if err != nil {
return nil, fmt.Errorf("error parsing role config from struct: %v", err)
}

// Write GCP Role
setupLogger.Trace(writingLogMessage("role"), "name", config.GCPTestRoleConfig.Name)
_, err = client.Logical().Write("auth/"+authPath+"/role/"+config.GCPTestRoleConfig.Name, GCPRoleConfig)
setupLogger.Trace(writingLogMessage("role"), "name", g.config.GCPTestRoleConfig.Name)
_, err = client.Logical().Write("auth/"+authPath+"/role/"+g.config.GCPTestRoleConfig.Name, GCPRoleConfig)
if err != nil {
return nil, fmt.Errorf("error writing gcp role: %v", err)
}

jwt, err := getSignedJwt(config)
jwt, err := getSignedJwt(g.config)
if err != nil {
return nil, fmt.Errorf("error fetching JWT: %v", err)
}
Expand All @@ -203,7 +200,7 @@ func (g *GCPAuth) Setup(client *api.Client, randomMountName bool, mountName stri
header: generateHeader(client),
pathPrefix: "/v1/" + filepath.Join("auth", authPath),
jwt: jwt,
roleName: config.GCPTestRoleConfig.Name,
roleName: g.config.GCPTestRoleConfig.Name,
timeout: g.timeout,
logger: g.logger,
}, nil
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_auth_jwt.go
Original file line number Diff line number Diff line change
Expand Up @@ -154,12 +154,12 @@ func (j *JWTAuth) GetTargetInfo() TargetInfo {
}
}

func (j *JWTAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (j *JWTAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
j.logger = targetLogger.Named(JWTAuthTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_auth_k8s.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ func readTokenFromFile(filepath string) (string, error) {
return string(jwt), nil
}

func (k *KubeAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (k *KubeAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
k.logger = targetLogger.Named(KubeAuthTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
return nil, fmt.Errorf("can't generate UUID for mount name: %v", err)
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_auth_ldap.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,12 +151,12 @@ func (l *LDAPAuth) GetTargetInfo() TargetInfo {
}
}

func (l *LDAPAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (l *LDAPAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
l.logger = targetLogger.Named(LDAPAuthTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_auth_userpass.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ func (u *UserpassAuth) GetTargetInfo() TargetInfo {
}
}

func (u *UserpassAuth) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (u *UserpassAuth) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
authPath := mountName
u.logger = targetLogger.Named(UserpassTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
authPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_aws.go
Original file line number Diff line number Diff line change
Expand Up @@ -127,12 +127,12 @@ func (a *AWSTest) GetTargetInfo() TargetInfo {
}
}

func (a *AWSTest) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (a *AWSTest) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
secretPath := mountName
a.logger = targetLogger.Named(AWSSecretTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
secretPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_cassandra.go
Original file line number Diff line number Diff line change
Expand Up @@ -153,12 +153,12 @@ func (c *CassandraSecret) GetTargetInfo() TargetInfo {
}
}

func (c *CassandraSecret) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (c *CassandraSecret) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
secretPath := mountName
c.logger = targetLogger.Named(CassandraSecretTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
secretPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_consul.go
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,12 @@ func (c *ConsulTest) GetTargetInfo() TargetInfo {
}
}

func (c *ConsulTest) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (c *ConsulTest) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
secretPath := mountName
c.logger = targetLogger.Named(ConsulSecretTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
secretPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_couchbase.go
Original file line number Diff line number Diff line change
Expand Up @@ -136,12 +136,12 @@ func (c *CouchbaseSecretTest) GetTargetInfo() TargetInfo {
}
}

func (c *CouchbaseSecretTest) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (c *CouchbaseSecretTest) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
secretPath := mountName
c.logger = targetLogger.Named(CouchbaseSecretTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
secretPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_dynamic_redis.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,12 +117,12 @@ func (r *RedisDynamicSecret) GetTargetInfo() TargetInfo {
}
}

func (r *RedisDynamicSecret) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (r *RedisDynamicSecret) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
secretPath := mountName
r.logger = targetLogger.Named(RedisDynamicSecretTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
secretPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_elasticsearch.go
Original file line number Diff line number Diff line change
Expand Up @@ -139,12 +139,12 @@ func (e *ElasticSearchTest) GetTargetInfo() TargetInfo {
}
}

func (e *ElasticSearchTest) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (e *ElasticSearchTest) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
secretPath := mountName
e.logger = targetLogger.Named(ElasticSearchSecretTestType)

if randomMountName {
if topLevelConfig.RandomMounts {
secretPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_kvv1.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,12 +122,12 @@ func (k *KVV1Test) Cleanup(client *api.Client) error {
return nil
}

func (k *KVV1Test) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (k *KVV1Test) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
mountPath := mountName
k.logger = targetLogger.Named("kvv1")

if randomMountName {
if topLevelConfig.RandomMounts {
mountPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
4 changes: 2 additions & 2 deletions benchmarktests/target_secret_kvv2.go
Original file line number Diff line number Diff line change
Expand Up @@ -122,7 +122,7 @@ func (k *KVV2Test) Cleanup(client *api.Client) error {
return nil
}

func (k *KVV2Test) Setup(client *api.Client, randomMountName bool, mountName string) (BenchmarkBuilder, error) {
func (k *KVV2Test) Setup(client *api.Client, mountName string, topLevelConfig *TopLevelTargetConfig) (BenchmarkBuilder, error) {
var err error
mountPath := mountName
switch k.action {
Expand All @@ -132,7 +132,7 @@ func (k *KVV2Test) Setup(client *api.Client, randomMountName bool, mountName str
k.logger = targetLogger.Named(KVV2ReadTestType)
}

if randomMountName {
if topLevelConfig.RandomMounts {
mountPath, err = uuid.GenerateUUID()
if err != nil {
log.Fatalf("can't create UUID")
Expand Down
Loading

0 comments on commit 19b41d0

Please sign in to comment.