2019-06-06 19:26:04 +00:00
|
|
|
package agent
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
hclog "github.com/hashicorp/go-hclog"
|
|
|
|
log "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,
|
|
|
|
Logger: log.NewNullLogger(),
|
|
|
|
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.
|
2019-08-26 16:55:08 +00:00
|
|
|
mockCFAPI := cfAPI.MockServer(false)
|
|
|
|
defer mockCFAPI.Close()
|
|
|
|
|
|
|
|
// Configure a CA certificate like a Vault operator would in setting up CF.
|
|
|
|
if _, err := client.Logical().Write("auth/cf/config", map[string]interface{}{
|
|
|
|
"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.
|
2019-08-26 16:55:08 +00:00
|
|
|
if _, err := client.Logical().Write("auth/cf/roles/test-role", map[string]interface{}{
|
|
|
|
"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
|
|
|
|
|
|
|
ctx, cancelFunc := context.WithCancel(context.Background())
|
|
|
|
timer := time.AfterFunc(30*time.Second, func() {
|
|
|
|
cancelFunc()
|
|
|
|
})
|
|
|
|
defer timer.Stop()
|
|
|
|
|
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)
|
|
|
|
go ah.Run(ctx, am)
|
|
|
|
defer func() {
|
|
|
|
<-ah.DoneCh
|
|
|
|
}()
|
|
|
|
|
|
|
|
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,
|
|
|
|
})
|
|
|
|
go ss.Run(ctx, ah.OutputCh, []*sink.SinkConfig{config})
|
|
|
|
defer func() {
|
|
|
|
<-ss.DoneCh
|
|
|
|
}()
|
|
|
|
|
|
|
|
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")
|
|
|
|
}
|
|
|
|
}
|