Remove the default org (#270)

This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-08-17 15:38:41 -04:00
1 parent 01059e6ca8
commit 282177afc2
24 files changed
+190 -431

No files matched your search

+4 -3
View File
@@ -10,6 +10,7 @@ import (
"github.com/stretchr/testify/require"
"github.com/hashicorp/boundary/internal/cmd/config"
"github.com/hashicorp/boundary/internal/iam"
"github.com/hashicorp/go-retryablehttp"
)
@@ -69,11 +70,12 @@ func TestHandler_CORS(t *testing.T) {
}
tc := NewTestController(t, &TestControllerOpts{
Config: cfg,
DefaultOrgId: "o_1234567890",
DisableAuthorizationFailures: true,
})
defer tc.Shutdown()
org := iam.TestOrg(t, tc.DbConn())
cases := []struct {
name string
method string
@@ -183,7 +185,6 @@ func TestHandler_CORS(t *testing.T) {
client := tc.Client()
err := client.SetAddr(tc.ApiAddrs()[c.listenerNum-1])
require.NoError(t, err)
client.SetScopeId("o_1234567890")
client.SetToken("fo_o_bar")
// Create the request
@@ -193,7 +194,7 @@ func TestHandler_CORS(t *testing.T) {
var body interface{}
scopeId := "global"
if c.provideScopeId {
scopeId = "o_1234567890"
scopeId = org.GetPublicId()
if c.method == http.MethodPost {
body = map[string]interface{}{}
}
+4 -2
View File
@@ -8,20 +8,22 @@ import (
"strings"
"testing"
"github.com/hashicorp/boundary/internal/iam"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestAuthenticationHandler(t *testing.T) {
c := NewTestController(t, &TestControllerOpts{
DefaultOrgId: "o_1234567890",
DisableAuthorizationFailures: true,
DefaultLoginName: "admin",
DefaultPassword: "password123",
})
defer c.Shutdown()
resp, err := http.Post(fmt.Sprintf("%s/v1/scopes/o_1234567890/auth-methods/paum_1234567890:authenticate", c.ApiAddrs()[0]), "application/json",
org := iam.TestOrg(t, c.DbConn())
resp, err := http.Post(fmt.Sprintf("%s/v1/scopes/%s/auth-methods/paum_1234567890:authenticate", c.ApiAddrs()[0], org.GetPublicId()), "application/json",
strings.NewReader(`{"token_type": null, "credentials": {"login_name":"admin", "password": "password123"}}`))
require.NoError(t, err)
assert.Equal(t, http.StatusOK, resp.StatusCode, "Got response: %v", resp)
+1 -13
View File
@@ -21,18 +21,6 @@ func handleUiWithAssets(c *Controller) http.Handler {
nextHandler = http.FileServer(ui.AssetFile())
}
rootHandler := http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
switch r.URL.Path {
case "/":
irw := newIndexResponseWriter(c.conf.DefaultOrgId)
nextHandler.ServeHTTP(irw, r)
irw.writeToWriter(w)
default:
nextHandler.ServeHTTP(w, r)
}
})
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
if r.Method != http.MethodGet {
w.WriteHeader(http.StatusMethodNotAllowed)
@@ -66,6 +54,6 @@ func handleUiWithAssets(c *Controller) http.Handler {
}
// Fall through to the next handler
rootHandler.ServeHTTP(w, r)
nextHandler.ServeHTTP(w, r)
})
}
@@ -1,50 +0,0 @@
package controller
import (
"bytes"
"fmt"
"net/http"
"strings"
)
const magicValue = `{{DEFAULT_ORG_ID}}`
type indexResponseWriter struct {
statusCode int
header http.Header
body *bytes.Buffer
defaultOrgId string
}
// newindexResponseWriter returns an initialized indexResponseWriter
func newIndexResponseWriter(defaultOrgId string) *indexResponseWriter {
return &indexResponseWriter{
header: make(http.Header),
body: new(bytes.Buffer),
defaultOrgId: defaultOrgId,
}
}
func (i *indexResponseWriter) Header() http.Header {
return i.header
}
func (i *indexResponseWriter) Write(buf []byte) (int, error) {
return i.body.Write(buf)
}
func (i *indexResponseWriter) WriteHeader(code int) {
i.statusCode = code
}
func (i *indexResponseWriter) writeToWriter(w http.ResponseWriter) {
for k, v := range i.header {
for _, h := range v {
w.Header().Add(k, h)
}
}
newBody := []byte(strings.Replace(i.body.String(), magicValue, i.defaultOrgId, 1))
w.Header().Set("content-length", fmt.Sprintf("%d", len(newBody)))
w.WriteHeader(i.statusCode)
w.Write(newBody)
}
+6 -6
View File
@@ -7,6 +7,7 @@ import (
"github.com/hashicorp/boundary/api/authmethods"
"github.com/hashicorp/boundary/api/scopes"
"github.com/hashicorp/boundary/internal/servers/controller"
"github.com/hashicorp/boundary/internal/types/scope"
"github.com/hashicorp/go-hclog"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
@@ -17,13 +18,11 @@ func TestAuthenticationMulti(t *testing.T) {
amId := "paum_1234567890"
user := "user"
password := "passpass"
orgId := "o_1234567890"
logger := hclog.New(&hclog.LoggerOptions{
Level: hclog.Trace,
})
c1 := controller.NewTestController(t, &controller.TestControllerOpts{
DefaultOrgId: orgId,
DefaultAuthMethodId: amId,
DefaultLoginName: user,
DefaultPassword: password,
@@ -52,16 +51,17 @@ func TestAuthenticationMulti(t *testing.T) {
assert.NotEqual(token1.Token, token2.Token)
c1.Client().SetToken(token1.Token)
c1.Client().SetScopeId(scope.Global.String())
c2.Client().SetToken(token1.Token) // Same token, as it should work on both
c2.Client().SetScopeId(scope.Global.String())
// Create a project, read from the other
proj, apiErr, err := scopes.NewScopesClient(c1.Client()).Create(c1.Context(), orgId)
org, apiErr, err := scopes.NewScopesClient(c1.Client()).Create(c1.Context(), scope.Global.String())
require.NoError(err)
require.Nil(apiErr)
require.NotNil(proj)
projId := proj.Id
require.NotNil(org)
proj, apiErr, err = scopes.NewScopesClient(c2.Client()).Read(c2.Context(), projId)
proj, apiErr, err := scopes.NewScopesClient(c2.Client()).Read(c2.Context(), org.Id)
require.NoError(err)
require.Nil(apiErr)
require.NotNil(proj)
+9 -15
View File
@@ -12,6 +12,7 @@ import (
"github.com/hashicorp/go-hclog"
wrapping "github.com/hashicorp/go-kms-wrapping"
"github.com/hashicorp/vault/sdk/helper/base62"
"github.com/jinzhu/gorm"
)
// TestController wraps a base.Server and Controller to provide a
@@ -62,6 +63,10 @@ func (tc *TestController) ClusterAddrs() []string {
return tc.addrs("cluster")
}
func (tc *TestController) DbConn() *gorm.DB {
return tc.b.Database
}
func (tc *TestController) addrs(purpose string) []string {
var prefix string
switch purpose {
@@ -110,9 +115,10 @@ func (tc *TestController) buildClient() {
if err := client.SetAddr(apiAddrs[0]); err != nil {
tc.t.Fatal(fmt.Errorf("error setting client address: %w", err))
}
if tc.b.DefaultOrgId != "" {
client.SetScopeId(tc.b.DefaultOrgId)
}
// Because this is using the real lib it can pick up from stored locations
// like the system keychain. Explicitly clear the token to ensure we
// understand the client state at the start of each test.
client.SetToken("")
tc.client = client
}
@@ -147,10 +153,6 @@ type TestControllerOpts struct {
// Config; if not provided a dev one will be created
Config *config.Config
// DefaultOrgId is the default org ID to use, if set. Can also be provided
// in the normal config.
DefaultOrgId string
// DefaultAuthMethodId is the default auth method ID to use, if set.
DefaultAuthMethodId string
@@ -219,13 +221,6 @@ func NewTestController(t *testing.T, opts *TestControllerOpts) *TestController {
opts.Config.Controller.Name = opts.Name
}
// Set default org ID, preferring one passed in from opts over config
if opts.Config.DefaultOrgId != "" {
tc.b.DefaultOrgId = opts.Config.DefaultOrgId
}
if opts.DefaultOrgId != "" {
tc.b.DefaultOrgId = opts.DefaultOrgId
}
if opts.DefaultAuthMethodId != "" {
tc.b.DevAuthMethodId = opts.DefaultAuthMethodId
}
@@ -319,7 +314,6 @@ func (tc *TestController) AddClusterControllerMember(t *testing.T, opts *TestCon
nextOpts := &TestControllerOpts{
DatabaseUrl: tc.c.conf.DatabaseUrl,
DefaultAuthMethodId: tc.c.conf.DevAuthMethodId,
DefaultOrgId: tc.c.conf.DefaultOrgId,
ControllerKMS: tc.c.conf.ControllerKMS,
WorkerAuthKMS: tc.c.conf.WorkerAuthKMS,
Name: opts.Name,