mirror of
				https://github.com/optim-enterprises-bv/vault.git
				synced 2025-11-04 04:28:08 +00:00 
			
		
		
		
	* Native Login method, userpass and approle interfaces to implement it * Add AWS auth interface for Login, unexported struct fields for now * Add Kubernetes client login * Add changelog * Add a test for approle client login * Return errors from LoginOptions, use limited reader for secret ID * Fix auth comment length * Return actual type not interface, check for client token in tests * Require specification of secret ID location using SecretID struct as AppRole arg * Allow password from env, file, or plaintext * Add flexibility in how to fetch k8s service token, but still with default * Avoid passing strings that need to be validated by just having different login options * Try a couple real tests with approle and userpass login * Fix method name in comment * Add context to Login methods, remove comments about certain sources being inherently insecure * Perform read of secret ID at login time * Read password from file at login time * Pass context in integ tests * Read env var values in at login time, add extra tests * Update api version * Revert "Update api version" This reverts commit 1ef3949497dcf878c47e0e5ffcbc8cac1c3c1679. * Update api version in all go.mod files
		
			
				
	
	
		
			93 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
			
		
		
	
	
			93 lines
		
	
	
		
			2.0 KiB
		
	
	
	
		
			Go
		
	
	
	
	
	
package command
 | 
						|
 | 
						|
import (
 | 
						|
	"context"
 | 
						|
	"sync"
 | 
						|
	"testing"
 | 
						|
 | 
						|
	log "github.com/hashicorp/go-hclog"
 | 
						|
	"github.com/hashicorp/vault/api"
 | 
						|
	auth "github.com/hashicorp/vault/api/auth/approle"
 | 
						|
	credAppRole "github.com/hashicorp/vault/builtin/credential/approle"
 | 
						|
	vaulthttp "github.com/hashicorp/vault/http"
 | 
						|
	"github.com/hashicorp/vault/sdk/logical"
 | 
						|
	"github.com/hashicorp/vault/vault"
 | 
						|
)
 | 
						|
 | 
						|
func TestAppRole_Integ_ConcurrentLogins(t *testing.T) {
 | 
						|
	var err error
 | 
						|
	coreConfig := &vault.CoreConfig{
 | 
						|
		DisableMlock: true,
 | 
						|
		DisableCache: true,
 | 
						|
		Logger:       log.NewNullLogger(),
 | 
						|
		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)
 | 
						|
	}
 | 
						|
 | 
						|
	_, err = client.Logical().Write("auth/approle/role/role1", map[string]interface{}{
 | 
						|
		"bind_secret_id": "true",
 | 
						|
		"period":         "300",
 | 
						|
	})
 | 
						|
	if err != nil {
 | 
						|
		t.Fatal(err)
 | 
						|
	}
 | 
						|
 | 
						|
	secret, err := client.Logical().Write("auth/approle/role/role1/secret-id", nil)
 | 
						|
	if err != nil {
 | 
						|
		t.Fatal(err)
 | 
						|
	}
 | 
						|
	secretID := secret.Data["secret_id"].(string)
 | 
						|
 | 
						|
	secret, err = client.Logical().Read("auth/approle/role/role1/role-id")
 | 
						|
	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()
 | 
						|
			appRoleAuth, err := auth.NewAppRoleAuth(roleID, &auth.SecretID{FromString: secretID})
 | 
						|
			if err != nil {
 | 
						|
				t.Error(err)
 | 
						|
				return
 | 
						|
			}
 | 
						|
			secret, err := client.Auth().Login(context.TODO(), appRoleAuth)
 | 
						|
			if err != nil {
 | 
						|
				t.Error(err)
 | 
						|
				return
 | 
						|
			}
 | 
						|
			if secret.Auth.ClientToken == "" {
 | 
						|
				t.Error("expected a successful login")
 | 
						|
				return
 | 
						|
			}
 | 
						|
		}()
 | 
						|
 | 
						|
	}
 | 
						|
	wg.Wait()
 | 
						|
}
 |