2020-08-17 15:38:41 -04:00
|
|
|
// Copyright (c) HashiCorp, Inc.
|
2022-10-25 18:29:38 -05:00
|
|
|
// SPDX-License-Identifier: BUSL-1.1
|
2023-02-02 17:15:17 -08:00
|
|
|
|
2020-08-12 00:40:39 -04:00
|
|
|
package controller_test
|
|
|
|
|
|
|
|
|
|
import (
|
2021-04-08 20:54:32 -04:00
|
|
|
"encoding/json"
|
2020-08-12 00:40:39 -04:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/hashicorp/boundary/api/authmethods"
|
2021-04-08 20:54:32 -04:00
|
|
|
"github.com/hashicorp/boundary/api/authtokens"
|
2020-08-12 00:40:39 -04:00
|
|
|
"github.com/hashicorp/boundary/api/scopes"
|
2022-05-09 12:45:10 -07:00
|
|
|
"github.com/hashicorp/boundary/internal/daemon/controller"
|
2020-08-17 15:38:41 -04:00
|
|
|
"github.com/hashicorp/boundary/internal/types/scope"
|
2020-08-12 00:40:39 -04:00
|
|
|
"github.com/hashicorp/go-hclog"
|
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
)
|
|
|
|
|
|
|
|
|
|
func TestAuthenticationMulti(t *testing.T) {
|
|
|
|
|
assert, require := assert.New(t), require.New(t)
|
|
|
|
|
logger := hclog.New(&hclog.LoggerOptions{
|
|
|
|
|
Level: hclog.Trace,
|
|
|
|
|
})
|
|
|
|
|
|
|
|
|
|
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
|
2021-02-11 13:14:18 -05:00
|
|
|
Logger: logger.Named("c1"),
|
2020-08-12 00:40:39 -04:00
|
|
|
})
|
|
|
|
|
defer c1.Shutdown()
|
|
|
|
|
|
|
|
|
|
c2 := c1.AddClusterControllerMember(t, &controller.TestControllerOpts{
|
|
|
|
|
Logger: logger.Named("c2"),
|
|
|
|
|
})
|
|
|
|
|
defer c2.Shutdown()
|
|
|
|
|
|
2020-08-27 20:43:33 -04:00
|
|
|
auth := authmethods.NewClient(c1.Client())
|
2022-10-25 18:29:38 -05:00
|
|
|
token1Result, err := auth.Authenticate(c1.Context(), c1.Server().DevPasswordAuthMethodId, "login", map[string]any{"login_name": c1.Server().DevLoginName, "password": c1.Server().DevPassword})
|
2020-08-12 00:40:39 -04:00
|
|
|
require.Nil(err)
|
2021-04-08 20:54:32 -04:00
|
|
|
token1 := new(authtokens.AuthToken)
|
|
|
|
|
require.NoError(json.Unmarshal(token1Result.GetRawAttributes(), token1))
|
2020-08-12 00:40:39 -04:00
|
|
|
require.NotNil(token1)
|
|
|
|
|
|
2020-08-27 20:43:33 -04:00
|
|
|
auth = authmethods.NewClient(c2.Client())
|
2022-10-25 18:29:38 -05:00
|
|
|
token2Result, err := auth.Authenticate(c2.Context(), c2.Server().DevPasswordAuthMethodId, "login", map[string]any{"login_name": c2.Server().DevLoginName, "password": c2.Server().DevPassword})
|
2020-08-12 00:40:39 -04:00
|
|
|
require.Nil(err)
|
2021-04-08 20:54:32 -04:00
|
|
|
token2 := new(authtokens.AuthToken)
|
|
|
|
|
require.NoError(json.Unmarshal(token2Result.GetRawAttributes(), token2))
|
2020-08-12 00:40:39 -04:00
|
|
|
require.NotNil(token2)
|
|
|
|
|
|
|
|
|
|
assert.NotEqual(token1.Token, token2.Token)
|
|
|
|
|
|
|
|
|
|
c1.Client().SetToken(token1.Token)
|
|
|
|
|
c2.Client().SetToken(token1.Token) // Same token, as it should work on both
|
|
|
|
|
|
|
|
|
|
// Create a project, read from the other
|
2020-09-24 14:34:58 -07:00
|
|
|
org, err := scopes.NewClient(c1.Client()).Create(c1.Context(), scope.Global.String())
|
2020-08-12 00:40:39 -04:00
|
|
|
require.NoError(err)
|
2020-09-15 01:08:21 -04:00
|
|
|
require.NotNil(org.Item)
|
2020-08-12 00:40:39 -04:00
|
|
|
|
2020-09-24 14:34:58 -07:00
|
|
|
proj, err := scopes.NewClient(c2.Client()).Read(c2.Context(), org.Item.Id)
|
2020-08-12 00:40:39 -04:00
|
|
|
require.NoError(err)
|
2020-09-15 01:08:21 -04:00
|
|
|
require.NotNil(proj.Item)
|
2020-08-12 00:40:39 -04:00
|
|
|
}
|