test(e2e): Add test for LDAP (#4061)
* test(e2e): Add test for LDAP This adds the infrastructure and a test for an LDAP workflow * test(e2e): Refactor test packages This reduces the number of scenarios needed * CR * fixup! test(e2e): Add test for LDAP
This commit is contained in:
1 parent
a89573b35f
commit
093f889333
13 files changed
+451
-41
No files matched your search
@@ -0,0 +1,33 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package base_plus_test
|
||||
|
||||
import "github.com/kelseyhightower/envconfig"
|
||||
|
||||
type config struct {
|
||||
TargetAddress string `envconfig:"E2E_TARGET_ADDRESS" required:"true"` // e.g. 192.168.0.1
|
||||
TargetSshKeyPath string `envconfig:"E2E_SSH_KEY_PATH" required:"true"` // e.g. /Users/username/key.pem
|
||||
TargetSshUser string `envconfig:"E2E_SSH_USER" required:"true"` // e.g. ubuntu
|
||||
TargetPort string `envconfig:"E2E_TARGET_PORT" required:"true"` // e.g. 22
|
||||
PostgresDbName string `envconfig:"E2E_POSTGRES_DB_NAME" required:"true"`
|
||||
PostgresUser string `envconfig:"E2E_POSTGRES_USER" required:"true"`
|
||||
PostgresPassword string `envconfig:"E2E_POSTGRES_PASSWORD" required:"true"`
|
||||
LdapAddress string `envconfig:"E2E_LDAP_ADDR" required:"true"` // e.g. ldap://ldap
|
||||
LdapDomainDn string `envconfig:"E2E_LDAP_DOMAIN_DN" required:"true"` // e.g. dc=example,dc=org
|
||||
LdapAdminDn string `envconfig:"E2E_LDAP_ADMIN_DN" required:"true"` // e.g. cn=admin,dc=example,dc=org
|
||||
LdapAdminPassword string `envconfig:"E2E_LDAP_ADMIN_PASSWORD" required:"true"`
|
||||
LdapUserName string `envconfig:"E2E_LDAP_USER_NAME" required:"true"`
|
||||
LdapUserPassword string `envconfig:"E2E_LDAP_USER_PASSWORD" required:"true"`
|
||||
LdapGroupName string `envconfig:"E2E_LDAP_GROUP_NAME" required:"true"`
|
||||
}
|
||||
|
||||
func loadTestConfig() (*config, error) {
|
||||
var c config
|
||||
err := envconfig.Process("", &c)
|
||||
if err != nil {
|
||||
return nil, err
|
||||
}
|
||||
|
||||
return &c, nil
|
||||
}
|
||||
@@ -0,0 +1,363 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package base_plus_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"testing"
|
||||
"time"
|
||||
|
||||
"github.com/hashicorp/boundary/api/authmethods"
|
||||
"github.com/hashicorp/boundary/api/scopes"
|
||||
"github.com/hashicorp/boundary/internal/kms"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
|
||||
"github.com/stretchr/testify/assert"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestCliKeyDestruction uses the boundary CLI to test key destruction.
|
||||
func TestCliKeyDestruction(t *testing.T) {
|
||||
e2e.MaybeSkipTest(t)
|
||||
|
||||
ctx := context.Background()
|
||||
boundary.AuthenticateAdminCli(t, ctx)
|
||||
|
||||
t.Log("Creating scope...")
|
||||
output := e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "create",
|
||||
"-name", "testscope",
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
|
||||
var scopeReply scopes.ScopeCreateResult
|
||||
require.NoError(t, json.Unmarshal(output.Stdout, &scopeReply))
|
||||
t.Cleanup(func() {
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "delete",
|
||||
"-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "list-keys",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var keys scopes.KeyListResult
|
||||
err := json.Unmarshal(output.Stdout, &keys)
|
||||
require.NoError(t, err)
|
||||
// We expect to see all DEKs and the root key, so DEKs+1 in total.
|
||||
assert.Len(t, keys.Items, len(kms.ValidDekPurposes())+1, "Expected %d keys to be listed, but found %d. Keys: %v", len(kms.ValidDekPurposes())+1, len(keys.Items), keys.Items)
|
||||
for _, key := range keys.Items {
|
||||
assert.Len(t, key.Versions, 1)
|
||||
}
|
||||
|
||||
// Create OIDC auth method to create some encrypted data in the scope
|
||||
t.Log("Creating auth method...")
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"auth-methods", "create", "oidc",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-api-url-prefix", "http://example.com",
|
||||
"-client-id", "something",
|
||||
"-client-secret", "somethingelse",
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var amReply authmethods.AuthMethodCreateResult
|
||||
require.NoError(t, json.Unmarshal(output.Stdout, &amReply))
|
||||
t.Cleanup(func() {
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"auth-methods", "delete",
|
||||
"-id", amReply.Item.Id,
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
|
||||
t.Log("Rotating keys...")
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "rotate-keys",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "list-keys",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
err = json.Unmarshal(output.Stdout, &keys)
|
||||
require.NoError(t, err)
|
||||
// We expect to see all DEKs and the root key, so DEKs+1 in total.
|
||||
assert.Len(t, keys.Items, len(kms.ValidDekPurposes())+1, "Expected %d keys to be listed, but found %d. Keys: %v", len(kms.ValidDekPurposes())+1, len(keys.Items), keys.Items)
|
||||
var rootKeyVersionToDestroy *scopes.KeyVersion
|
||||
var databaseKeyVersionToDestroy *scopes.KeyVersion
|
||||
for _, key := range keys.Items {
|
||||
// Each key should have a new version after rotation
|
||||
assert.Len(t, key.Versions, 2)
|
||||
if key.Purpose == "rootKey" {
|
||||
rootKeyVersionToDestroy = key.Versions[1] // Key versions are ordered by version, descending
|
||||
}
|
||||
if key.Purpose == "database" {
|
||||
databaseKeyVersionToDestroy = key.Versions[1]
|
||||
}
|
||||
}
|
||||
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "list-key-version-destruction-jobs",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var jobs scopes.KeyVersionDestructionJobListResult
|
||||
err = json.Unmarshal(output.Stdout, &jobs)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, jobs.Items, 0)
|
||||
|
||||
t.Log("Destroying root key...")
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "destroy-key-version",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-key-version-id", rootKeyVersionToDestroy.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var destruction struct {
|
||||
Item *scopes.KeyVersionDestructionResult
|
||||
}
|
||||
err = json.Unmarshal(output.Stdout, &destruction)
|
||||
require.NoError(t, err)
|
||||
// Root key versions should be destroyed synchronously, as they don't require any re-encryption
|
||||
assert.Equal(t, "completed", destruction.Item.State)
|
||||
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "list-key-version-destruction-jobs",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
err = json.Unmarshal(output.Stdout, &jobs)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, jobs.Items, 0)
|
||||
|
||||
t.Log("Destroying database key...")
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "destroy-key-version",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-key-version-id", databaseKeyVersionToDestroy.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
err = json.Unmarshal(output.Stdout, &destruction)
|
||||
require.NoError(t, err)
|
||||
// Most data key versions should be destroyed asynchronously, as they require re-encryption
|
||||
// of existing data.
|
||||
assert.Equal(t, "pending", destruction.Item.State)
|
||||
|
||||
// The default scheduler job run interval is 1 minute. The runs become available 1
|
||||
// second after the last successful run. We need to re-encrypt data in 1 table,
|
||||
// and then remove the key. This job should take between 1 and 2 minutes to run,
|
||||
// depending on the timing of the first started run.
|
||||
cancelCtx, cancel := context.WithTimeout(ctx, 3*time.Minute)
|
||||
t.Cleanup(cancel)
|
||||
for {
|
||||
output = e2e.RunCommand(cancelCtx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "list-key-version-destruction-jobs",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
err = json.Unmarshal(output.Stdout, &jobs)
|
||||
require.NoError(t, err)
|
||||
if len(jobs.Items) == 0 {
|
||||
break
|
||||
}
|
||||
require.Len(t, jobs.Items, 1)
|
||||
assert.Equal(t, databaseKeyVersionToDestroy.Id, jobs.Items[0].KeyVersionId)
|
||||
select {
|
||||
case <-time.After(5 * time.Second):
|
||||
case <-cancelCtx.Done():
|
||||
t.Fatal("Test timed out waiting for destruction to finish")
|
||||
}
|
||||
}
|
||||
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"scopes", "list-keys",
|
||||
"-scope-id", scopeReply.Item.Id,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
err = json.Unmarshal(output.Stdout, &keys)
|
||||
require.NoError(t, err)
|
||||
// We expect to see all DEKs and the root key, so DEKs+1 in total.
|
||||
assert.Len(t, keys.Items, len(kms.ValidDekPurposes())+1, "Expected %d keys to be listed, but found %d. Keys: %v", len(kms.ValidDekPurposes())+1, len(keys.Items), keys.Items)
|
||||
for _, key := range keys.Items {
|
||||
switch key.Purpose {
|
||||
case "rootKey", "database":
|
||||
assert.Len(t, key.Versions, 1)
|
||||
default:
|
||||
assert.Len(t, key.Versions, 2)
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("Successfully destroyed a root key and data key")
|
||||
}
|
||||
|
||||
// TestApiKeyDestruction uses the boundary Go api to test key destruction.
|
||||
func TestApiKeyDestruction(t *testing.T) {
|
||||
e2e.MaybeSkipTest(t)
|
||||
|
||||
client, err := boundary.NewApiClient()
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
sc := scopes.NewClient(client)
|
||||
|
||||
t.Log("Creating scope...")
|
||||
scope, err := sc.Create(ctx, "global", scopes.WithName("testscope"))
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_, err := sc.Delete(context.Background(), scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
keys, err := sc.ListKeys(ctx, scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
// We expect to see all DEKs and the root key, so DEKs+1 in total.
|
||||
assert.Len(t, keys.Items, len(kms.ValidDekPurposes())+1, "Expected %d keys to be listed, but found %d. Keys: %v", len(kms.ValidDekPurposes())+1, len(keys.Items), keys.Items)
|
||||
// Assert that all keys have the same number of versions
|
||||
for _, key := range keys.Items {
|
||||
assert.Len(t, key.Versions, 1)
|
||||
}
|
||||
|
||||
// Create OIDC auth method to create some encrypted data in the scope
|
||||
t.Log("Creating auth method...")
|
||||
amc := authmethods.NewClient(client)
|
||||
am, err := amc.Create(ctx, "oidc", scope.Item.Id,
|
||||
authmethods.WithOidcAuthMethodApiUrlPrefix("http://example.com"),
|
||||
authmethods.WithOidcAuthMethodClientId("something"),
|
||||
authmethods.WithOidcAuthMethodClientSecret("somethingelse"),
|
||||
)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
_, err := amc.Delete(context.Background(), am.Item.Id)
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Log("Rotating keys...")
|
||||
_, err = sc.RotateKeys(ctx, scope.Item.Id, false)
|
||||
require.NoError(t, err)
|
||||
|
||||
keys, err = sc.ListKeys(ctx, scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
// We expect to see all DEKs and the root key, so DEKs+1 in total.
|
||||
assert.Len(t, keys.Items, len(kms.ValidDekPurposes())+1, "Expected %d keys to be listed, but found %d. Keys: %v", len(kms.ValidDekPurposes())+1, len(keys.Items), keys.Items)
|
||||
// Assert that all keys have the same number of versions,
|
||||
// one higher than before
|
||||
var rootKeyVersionToDestroy *scopes.KeyVersion
|
||||
var databaseKeyVersionToDestroy *scopes.KeyVersion
|
||||
for _, key := range keys.Items {
|
||||
// Each key should have a new version after rotation
|
||||
assert.Len(t, key.Versions, 2)
|
||||
if key.Purpose == "rootKey" {
|
||||
// Key versions are ordered by version, descending.
|
||||
// Pick the second newest version.
|
||||
rootKeyVersionToDestroy = key.Versions[1]
|
||||
}
|
||||
if key.Purpose == "database" {
|
||||
databaseKeyVersionToDestroy = key.Versions[1]
|
||||
}
|
||||
}
|
||||
|
||||
jobs, err := sc.ListKeyVersionDestructionJobs(ctx, scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, jobs.Items, 0)
|
||||
|
||||
t.Log("Destroying root key...")
|
||||
result, err := sc.DestroyKeyVersion(ctx, scope.Item.Id, rootKeyVersionToDestroy.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "completed", result.State)
|
||||
|
||||
jobs, err = sc.ListKeyVersionDestructionJobs(ctx, scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, jobs.Items, 0)
|
||||
|
||||
t.Log("Destroying database key...")
|
||||
result, err = sc.DestroyKeyVersion(ctx, scope.Item.Id, databaseKeyVersionToDestroy.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Equal(t, "pending", result.State)
|
||||
|
||||
jobs, err = sc.ListKeyVersionDestructionJobs(ctx, scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
assert.Len(t, jobs.Items, 1)
|
||||
|
||||
// The default scheduler job run interval is 1 minute. The runs become available 1
|
||||
// second after the last successful run. We need to re-encrypt data in 1 table,
|
||||
// and then remove the key. This job should take between 1 and 2 minutes to run,
|
||||
// depending on the timing of the first started run.
|
||||
ctx, cancel := context.WithTimeout(context.Background(), 3*time.Minute)
|
||||
t.Cleanup(cancel)
|
||||
for {
|
||||
jobs, err = sc.ListKeyVersionDestructionJobs(ctx, scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
if len(jobs.Items) == 0 {
|
||||
break
|
||||
}
|
||||
require.Len(t, jobs.Items, 1)
|
||||
assert.Equal(t, databaseKeyVersionToDestroy.Id, jobs.Items[0].KeyVersionId)
|
||||
select {
|
||||
case <-time.After(5 * time.Second):
|
||||
case <-ctx.Done():
|
||||
t.Fatal("Test timed out waiting for destruction to finish")
|
||||
}
|
||||
}
|
||||
|
||||
keys, err = sc.ListKeys(ctx, scope.Item.Id)
|
||||
require.NoError(t, err)
|
||||
// We expect to see all DEKs and the root key, so DEKs+1 in total.
|
||||
assert.Len(t, keys.Items, len(kms.ValidDekPurposes())+1, "Expected %d keys to be listed, but found %d. Keys: %v", len(kms.ValidDekPurposes())+1, len(keys.Items), keys.Items)
|
||||
for _, key := range keys.Items {
|
||||
switch key.Purpose {
|
||||
case "rootKey", "database":
|
||||
assert.Len(t, key.Versions, 1)
|
||||
default:
|
||||
assert.Len(t, key.Versions, 2)
|
||||
}
|
||||
}
|
||||
|
||||
t.Logf("Successfully destroyed a root key and data key")
|
||||
}
|
||||
@@ -0,0 +1,183 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package base_plus_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"encoding/json"
|
||||
"net/http"
|
||||
"testing"
|
||||
|
||||
"github.com/hashicorp/boundary/api/accounts"
|
||||
"github.com/hashicorp/boundary/api/authmethods"
|
||||
"github.com/hashicorp/boundary/api/managedgroups"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
|
||||
"github.com/stretchr/testify/require"
|
||||
)
|
||||
|
||||
// TestCliLdap uses the boundary cli to set up an LDAP auth method and confirm
|
||||
// that an LDAP user can authenticate to boundary. It also confirms that an LDAP
|
||||
// managed group can be added as a principal to a role.
|
||||
func TestCliLdap(t *testing.T) {
|
||||
e2e.MaybeSkipTest(t)
|
||||
c, err := loadTestConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
boundary.AuthenticateAdminCli(t, ctx)
|
||||
newOrgId := boundary.CreateNewOrgCli(t, ctx)
|
||||
t.Cleanup(func() {
|
||||
ctx := context.Background()
|
||||
boundary.AuthenticateAdminCli(t, ctx)
|
||||
output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", newOrgId))
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
|
||||
// Create an LDAP auth method
|
||||
output := e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"auth-methods", "create", "ldap",
|
||||
"-scope-id", newOrgId,
|
||||
"-name", "e2e LDAP",
|
||||
"-urls", c.LdapAddress,
|
||||
"-user-dn", c.LdapDomainDn,
|
||||
"-user-attr", "uid",
|
||||
"-group-dn", c.LdapDomainDn,
|
||||
"-bind-dn", c.LdapAdminDn,
|
||||
"-bind-password", "env://LDAP_PW",
|
||||
"-state", "active-public",
|
||||
"-enable-groups", "true",
|
||||
"-discover-dn", "true",
|
||||
"-format", "json",
|
||||
),
|
||||
e2e.WithEnv("LDAP_PW", c.LdapAdminPassword),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var newAuthMethodResult authmethods.AuthMethodCreateResult
|
||||
err = json.Unmarshal(output.Stdout, &newAuthMethodResult)
|
||||
require.NoError(t, err)
|
||||
ldapAuthMethodId := newAuthMethodResult.Item.Id
|
||||
t.Logf("Create Auth Method: %s", ldapAuthMethodId)
|
||||
|
||||
// Create an LDAP account
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"accounts", "create", "ldap",
|
||||
"-auth-method-id", ldapAuthMethodId,
|
||||
"-name", "einstein",
|
||||
"-login-name", c.LdapUserName,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var newAccountResult accounts.AccountCreateResult
|
||||
err = json.Unmarshal(output.Stdout, &newAccountResult)
|
||||
require.NoError(t, err)
|
||||
newAccountId := newAccountResult.Item.Id
|
||||
t.Logf("Created Account: %s", newAccountId)
|
||||
|
||||
// Create a user and attach the LDAP account
|
||||
newUserId := boundary.CreateNewUserCli(t, ctx, newOrgId)
|
||||
boundary.SetAccountToUserCli(t, ctx, newUserId, newAccountId)
|
||||
|
||||
// Try to log in with the wrong password
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"authenticate", "ldap",
|
||||
"-auth-method-id", ldapAuthMethodId,
|
||||
"-login-name", c.LdapUserName,
|
||||
"-password", "env://LDAP_PW",
|
||||
),
|
||||
e2e.WithEnv("LDAP_PW", c.LdapAdminPassword),
|
||||
)
|
||||
require.Error(t, output.Err, string(output.Stderr))
|
||||
|
||||
// Log in as the LDAP user
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"authenticate", "ldap",
|
||||
"-auth-method-id", ldapAuthMethodId,
|
||||
"-login-name", c.LdapUserName,
|
||||
"-password", "env://LDAP_PW",
|
||||
),
|
||||
e2e.WithEnv("LDAP_PW", c.LdapUserPassword),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
|
||||
// Confirm there is a permissions error when trying to read an auth method
|
||||
// as an LDAP user
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"auth-methods", "read",
|
||||
"-id", ldapAuthMethodId,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.Error(t, output.Err, string(output.Stderr))
|
||||
var response boundary.CliError
|
||||
err = json.Unmarshal(output.Stderr, &response)
|
||||
require.NoError(t, err)
|
||||
require.Equal(t, http.StatusForbidden, response.Status)
|
||||
|
||||
// Create an LDAP managed group
|
||||
boundary.AuthenticateAdminCli(t, ctx)
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"managed-groups", "create", "ldap",
|
||||
"-auth-method-id", ldapAuthMethodId,
|
||||
"-name", c.LdapGroupName,
|
||||
"-group-names", c.LdapGroupName,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
|
||||
var newManagedGroupResult managedgroups.ManagedGroupCreateResult
|
||||
err = json.Unmarshal(output.Stdout, &newManagedGroupResult)
|
||||
require.NoError(t, err)
|
||||
managedGroupId := newManagedGroupResult.Item.Id
|
||||
t.Logf("Created Managed Group: %s", managedGroupId)
|
||||
|
||||
// Confirm that LDAP user is in the managed group
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"managed-groups", "read",
|
||||
"-id", managedGroupId,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
var managedGroupReadResult managedgroups.ManagedGroupReadResult
|
||||
err = json.Unmarshal(output.Stdout, &managedGroupReadResult)
|
||||
require.NoError(t, err)
|
||||
require.Contains(t, managedGroupReadResult.Item.MemberIds, newAccountId)
|
||||
|
||||
// Add managed group as a principal to a role with permissions to read auth methods
|
||||
newRoleId := boundary.CreateNewRoleCli(t, ctx, newOrgId)
|
||||
boundary.AddPrincipalToRoleCli(t, ctx, newRoleId, managedGroupId)
|
||||
boundary.AddGrantToRoleCli(t, ctx, newRoleId, "ids=*;type=auth-method;actions=read")
|
||||
|
||||
// Log in as the LDAP user again
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"authenticate", "ldap",
|
||||
"-auth-method-id", ldapAuthMethodId,
|
||||
"-login-name", c.LdapUserName,
|
||||
"-password", "env://LDAP_PW",
|
||||
),
|
||||
e2e.WithEnv("LDAP_PW", c.LdapUserPassword),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
|
||||
// Read the auth method. Expect no error
|
||||
output = e2e.RunCommand(ctx, "boundary",
|
||||
e2e.WithArgs(
|
||||
"auth-methods", "read",
|
||||
"-id", ldapAuthMethodId,
|
||||
"-format", "json",
|
||||
),
|
||||
)
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
}
|
||||
@@ -0,0 +1,79 @@
|
||||
// Copyright (c) HashiCorp, Inc.
|
||||
// SPDX-License-Identifier: BUSL-1.1
|
||||
|
||||
package base_plus_test
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"context"
|
||||
"io"
|
||||
"os/exec"
|
||||
"testing"
|
||||
|
||||
"github.com/creack/pty"
|
||||
"github.com/stretchr/testify/require"
|
||||
|
||||
"github.com/hashicorp/boundary/internal/target"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e"
|
||||
"github.com/hashicorp/boundary/testing/internal/e2e/boundary"
|
||||
)
|
||||
|
||||
// TestCliTcpTargetConnectPostgres uses the boundary cli to connect to a
|
||||
// target using `connect postgres`
|
||||
func TestCliTcpTargetConnectPostgres(t *testing.T) {
|
||||
e2e.MaybeSkipTest(t)
|
||||
c, err := loadTestConfig()
|
||||
require.NoError(t, err)
|
||||
|
||||
ctx := context.Background()
|
||||
boundary.AuthenticateAdminCli(t, ctx)
|
||||
newOrgId := boundary.CreateNewOrgCli(t, ctx)
|
||||
t.Cleanup(func() {
|
||||
ctx := context.Background()
|
||||
boundary.AuthenticateAdminCli(t, ctx)
|
||||
output := e2e.RunCommand(ctx, "boundary", e2e.WithArgs("scopes", "delete", "-id", newOrgId))
|
||||
require.NoError(t, output.Err, string(output.Stderr))
|
||||
})
|
||||
newProjectId := boundary.CreateNewProjectCli(t, ctx, newOrgId)
|
||||
newTargetId := boundary.CreateNewTargetCli(
|
||||
t,
|
||||
ctx,
|
||||
newProjectId,
|
||||
c.TargetPort,
|
||||
target.WithAddress(c.TargetAddress),
|
||||
)
|
||||
newCredentialStoreId := boundary.CreateNewCredentialStoreStaticCli(t, ctx, newProjectId)
|
||||
newCredentialsId := boundary.CreateNewStaticCredentialPasswordCli(
|
||||
t,
|
||||
ctx,
|
||||
newCredentialStoreId,
|
||||
c.PostgresUser,
|
||||
c.PostgresPassword,
|
||||
)
|
||||
boundary.AddBrokeredCredentialSourceToTargetCli(t, ctx, newTargetId, newCredentialsId)
|
||||
|
||||
var cmd *exec.Cmd
|
||||
cmd = exec.CommandContext(ctx,
|
||||
"boundary",
|
||||
"connect", "postgres",
|
||||
"-target-id", newTargetId,
|
||||
"-dbname", c.PostgresDbName,
|
||||
)
|
||||
f, err := pty.Start(cmd)
|
||||
require.NoError(t, err)
|
||||
t.Cleanup(func() {
|
||||
err := f.Close()
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
f.Write([]byte("\\pset pager off\n")) // disables pagination in output
|
||||
f.Write([]byte("\\dt\n")) // list all tables
|
||||
f.Write([]byte("\\q\n")) // exit psql session
|
||||
f.Write([]byte{4}) // EOT (End of Transmission - marks end of file stream)
|
||||
|
||||
var buf bytes.Buffer
|
||||
io.Copy(&buf, f)
|
||||
require.Contains(t, buf.String(), "List of relations", "Session did not return expected output")
|
||||
require.Contains(t, buf.String(), c.PostgresDbName, "Session did not return expected output")
|
||||
t.Log("Successfully connected to target")
|
||||
}
|
||||
Reference in new issue
Block a user