2019-06-06 19:26:04 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
2019-08-26 16:55:08 +00:00
|
|
|
credCF "github.com/hashicorp/vault-plugin-auth-cf"
|
|
|
|
"github.com/hashicorp/vault-plugin-auth-cf/testing/certificates"
|
|
|
|
cfAPI "github.com/hashicorp/vault-plugin-auth-cf/testing/cf"
|
2019-06-06 19:26:04 +00:00
|
|
|
"github.com/hashicorp/vault/api"
|
|
|
|
"github.com/hashicorp/vault/command/agent/auth"
|
2019-08-26 16:55:08 +00:00
|
|
|
agentcf "github.com/hashicorp/vault/command/agent/auth/cf"
|
2019-06-06 19:26:04 +00:00
|
|
|
"github.com/hashicorp/vault/command/agent/sink"
|
|
|
|
"github.com/hashicorp/vault/command/agent/sink/file"
|
|
|
|
vaulthttp "github.com/hashicorp/vault/http"
|
|
|
|
"github.com/hashicorp/vault/sdk/helper/logging"
|
|
|
|
"github.com/hashicorp/vault/sdk/logical"
|
|
|
|
"github.com/hashicorp/vault/vault"
|
|
|
|
)
|
|
|
|
|
2019-08-26 16:55:08 +00:00
|
|
|
func TestCFEndToEnd(t *testing.T) {
|
2019-06-06 19:26:04 +00:00
|
|
|
logger := logging.NewVaultLogger(hclog.Trace)
|
|
|
|
|
|
|
|
coreConfig := &vault.CoreConfig{
|
|
|
|
DisableMlock: true,
|
|
|
|
DisableCache: true,
|
2020-05-21 20:07:50 +00:00
|
|
|
Logger: hclog.NewNullLogger(),
|
2019-06-06 19:26:04 +00:00
|
|
|
CredentialBackends: map[string]logical.Factory{
|
2019-08-26 16:55:08 +00:00
|
|
|
"cf": credCF.Factory,
|
2019-06-06 19:26:04 +00:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
2019-08-26 16:55:08 +00:00
|
|
|
if err := client.Sys().EnableAuthWithOptions("cf", &api.EnableAuthOptions{
|
|
|
|
Type: "cf",
|
2019-06-06 19:26:04 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
testIPAddress := "127.0.0.1"
|
|
|
|
|
2019-08-26 16:55:08 +00:00
|
|
|
// Generate some valid certs that look like the ones we get from CF.
|
|
|
|
testCFCerts, err := certificates.Generate(cfAPI.FoundServiceGUID, cfAPI.FoundOrgGUID, cfAPI.FoundSpaceGUID, cfAPI.FoundAppGUID, testIPAddress)
|
2019-06-06 19:26:04 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
defer func() {
|
2019-08-26 16:55:08 +00:00
|
|
|
if err := testCFCerts.Close(); err != nil {
|
2019-06-06 19:26:04 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
// Start a mock server representing their API.
|
2021-03-05 02:32:51 +00:00
|
|
|
mockCFAPI := cfAPI.MockServer(false, nil)
|
2019-08-26 16:55:08 +00:00
|
|
|
defer mockCFAPI.Close()
|
|
|
|
|
|
|
|
// Configure a CA certificate like a Vault operator would in setting up CF.
|
2022-03-23 21:47:43 +00:00
|
|
|
if _, err := client.Logical().WriteWithContext(context.Background(), "auth/cf/config", map[string]interface{}{
|
2019-08-26 16:55:08 +00:00
|
|
|
"identity_ca_certificates": testCFCerts.CACertificate,
|
2019-11-07 16:54:34 +00:00
|
|
|
"cf_api_addr": mockCFAPI.URL,
|
|
|
|
"cf_username": cfAPI.AuthUsername,
|
|
|
|
"cf_password": cfAPI.AuthPassword,
|
2019-06-06 19:26:04 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// Configure a role to be used for logging in, another thing a Vault operator would do.
|
2022-03-23 21:47:43 +00:00
|
|
|
if _, err := client.Logical().WriteWithContext(context.Background(), "auth/cf/roles/test-role", map[string]interface{}{
|
2019-08-26 16:55:08 +00:00
|
|
|
"bound_instance_ids": cfAPI.FoundServiceGUID,
|
|
|
|
"bound_organization_ids": cfAPI.FoundOrgGUID,
|
|
|
|
"bound_space_ids": cfAPI.FoundSpaceGUID,
|
|
|
|
"bound_application_ids": cfAPI.FoundAppGUID,
|
2019-06-06 19:26:04 +00:00
|
|
|
}); err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2019-08-26 16:55:08 +00:00
|
|
|
os.Setenv(credCF.EnvVarInstanceCertificate, testCFCerts.PathToInstanceCertificate)
|
|
|
|
os.Setenv(credCF.EnvVarInstanceKey, testCFCerts.PathToInstanceKey)
|
2019-06-06 19:26:04 +00:00
|
|
|
|
2020-09-30 01:03:09 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 30*time.Second)
|
2019-06-06 19:26:04 +00:00
|
|
|
|
2019-08-26 16:55:08 +00:00
|
|
|
am, err := agentcf.NewCFAuthMethod(&auth.AuthConfig{
|
|
|
|
MountPath: "auth/cf",
|
2019-06-06 19:26:04 +00:00
|
|
|
Config: map[string]interface{}{
|
|
|
|
"role": "test-role",
|
|
|
|
},
|
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
ahConfig := &auth.AuthHandlerConfig{
|
|
|
|
Logger: logger.Named("auth.handler"),
|
|
|
|
Client: client,
|
|
|
|
}
|
|
|
|
|
|
|
|
ah := auth.NewAuthHandler(ahConfig)
|
2020-09-30 01:03:09 +00:00
|
|
|
errCh := make(chan error)
|
|
|
|
go func() {
|
|
|
|
errCh <- ah.Run(ctx, am)
|
|
|
|
}()
|
2019-06-06 19:26:04 +00:00
|
|
|
defer func() {
|
2020-09-30 01:03:09 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 19:26:04 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
tmpFile, err := ioutil.TempFile("", "auth.tokensink.test.")
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
tokenSinkFileName := tmpFile.Name()
|
|
|
|
tmpFile.Close()
|
|
|
|
os.Remove(tokenSinkFileName)
|
|
|
|
t.Logf("output: %s", tokenSinkFileName)
|
|
|
|
|
|
|
|
config := &sink.SinkConfig{
|
|
|
|
Logger: logger.Named("sink.file"),
|
|
|
|
Config: map[string]interface{}{
|
|
|
|
"path": tokenSinkFileName,
|
|
|
|
},
|
|
|
|
WrapTTL: 10 * time.Second,
|
|
|
|
}
|
|
|
|
|
|
|
|
fs, err := file.NewFileSink(config)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
config.Sink = fs
|
|
|
|
|
|
|
|
ss := sink.NewSinkServer(&sink.SinkServerConfig{
|
|
|
|
Logger: logger.Named("sink.server"),
|
|
|
|
Client: client,
|
|
|
|
})
|
2020-09-30 01:03:09 +00:00
|
|
|
go func() {
|
|
|
|
errCh <- ss.Run(ctx, ah.OutputCh, []*sink.SinkConfig{config})
|
|
|
|
}()
|
2019-06-06 19:26:04 +00:00
|
|
|
defer func() {
|
2020-09-30 01:03:09 +00:00
|
|
|
select {
|
|
|
|
case <-ctx.Done():
|
|
|
|
case err := <-errCh:
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
}
|
2019-06-06 19:26:04 +00:00
|
|
|
}()
|
|
|
|
|
2020-09-30 01:03:09 +00:00
|
|
|
// This has to be after the other defers so it happens first. It allows
|
|
|
|
// successful test runs to immediately cancel all of the runner goroutines
|
|
|
|
// and unblock any of the blocking defer calls by the runner's DoneCh that
|
|
|
|
// comes before this and avoid successful tests from taking the entire
|
|
|
|
// timeout duration.
|
|
|
|
defer cancel()
|
|
|
|
|
2019-06-06 19:26:04 +00:00
|
|
|
if stat, err := os.Lstat(tokenSinkFileName); err == nil {
|
|
|
|
t.Fatalf("expected err but got %s", stat)
|
|
|
|
} else if !os.IsNotExist(err) {
|
|
|
|
t.Fatal("expected notexist err")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Wait 2 seconds for the env variables to be detected and an auth to be generated.
|
|
|
|
time.Sleep(time.Second * 2)
|
|
|
|
|
|
|
|
token, err := readToken(tokenSinkFileName)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if token.Token == "" {
|
|
|
|
t.Fatal("expected token but didn't receive it")
|
|
|
|
}
|
|
|
|
}
|