2023-03-15 16:00:52 +00:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
|
|
|
|
2018-02-09 15:40:56 +00:00
|
|
|
package command
|
|
|
|
|
|
|
|
import (
|
2021-10-26 23:48:48 +00:00
|
|
|
"context"
|
2018-02-09 15:40:56 +00:00
|
|
|
"sync"
|
|
|
|
"testing"
|
|
|
|
|
2018-04-03 00:46:59 +00:00
|
|
|
log "github.com/hashicorp/go-hclog"
|
2018-02-09 15:40:56 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
2021-10-26 23:48:48 +00:00
|
|
|
auth "github.com/hashicorp/vault/api/auth/approle"
|
2018-02-09 15:40:56 +00:00
|
|
|
credAppRole "github.com/hashicorp/vault/builtin/credential/approle"
|
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
2019-04-12 21:54:35 +00:00
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
2018-02-09 15:40:56 +00:00
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestAppRole_Integ_ConcurrentLogins(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
DisableMlock: true,
|
|
|
|
DisableCache: true,
|
2018-04-03 00:46:59 +00:00
|
|
|
Logger: log.NewNullLogger(),
|
2018-02-09 15:40:56 +00:00
|
|
|
CredentialBackends: map[string]logical.Factory{
|
|
|
|
"approle": credAppRole.Factory,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
cluster := vault.NewTestCluster(t, coreConfig, &vault.TestClusterOptions{
|
|
|
|
HandlerFunc: vaulthttp.Handler,
|
|
|
|
})
|
|
|
|
|
|
|
|
cluster.Start()
|
|
|
|
defer cluster.Cleanup()
|
|
|
|
|
|
|
|
cores := cluster.Cores
|
|
|
|
|
|
|
|
vault.TestWaitActive(t, cores[0].Core)
|
|
|
|
|
|
|
|
client := cores[0].Client
|
|
|
|
|
|
|
|
err = client.Sys().EnableAuthWithOptions("approle", &api.EnableAuthOptions{
|
|
|
|
Type: "approle",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-04-07 19:12:58 +00:00
|
|
|
_, err = client.Logical().Write("auth/approle/role/role1", map[string]interface{}{
|
2018-02-09 15:40:56 +00:00
|
|
|
"bind_secret_id": "true",
|
|
|
|
"period": "300",
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err := client.Logical().Write("auth/approle/role/role1/secret-id", nil)
|
2018-02-09 15:40:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
secretID := secret.Data["secret_id"].(string)
|
|
|
|
|
2022-04-07 19:12:58 +00:00
|
|
|
secret, err = client.Logical().Read("auth/approle/role/role1/role-id")
|
2018-02-09 15:40:56 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
roleID := secret.Data["role_id"].(string)
|
|
|
|
|
|
|
|
wg := &sync.WaitGroup{}
|
|
|
|
|
|
|
|
for i := 0; i < 100; i++ {
|
|
|
|
wg.Add(1)
|
|
|
|
go func() {
|
|
|
|
defer wg.Done()
|
2021-10-26 23:48:48 +00:00
|
|
|
appRoleAuth, err := auth.NewAppRoleAuth(roleID, &auth.SecretID{FromString: secretID})
|
|
|
|
if err != nil {
|
|
|
|
t.Error(err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
secret, err := client.Auth().Login(context.TODO(), appRoleAuth)
|
2018-02-09 15:40:56 +00:00
|
|
|
if err != nil {
|
2021-09-30 11:33:14 +00:00
|
|
|
t.Error(err)
|
|
|
|
return
|
2018-02-09 15:40:56 +00:00
|
|
|
}
|
|
|
|
if secret.Auth.ClientToken == "" {
|
2021-09-30 11:33:14 +00:00
|
|
|
t.Error("expected a successful login")
|
|
|
|
return
|
2018-02-09 15:40:56 +00:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
}
|
|
|
|
wg.Wait()
|
|
|
|
}
|