Add names to init output (#428)
This commit is contained in:
1 parent
7fcbffbe88
commit
41a3c66ea1
5 files changed
+121
-95
No files matched your search
@@ -15,36 +15,39 @@ import (
|
||||
"github.com/hashicorp/vault/sdk/helper/base62"
|
||||
)
|
||||
|
||||
func (b *Server) CreateInitialAuthMethod(ctx context.Context) error {
|
||||
func (b *Server) CreateInitialAuthMethod(ctx context.Context) (*password.AuthMethod, *iam.User, error) {
|
||||
rw := db.New(b.Database)
|
||||
|
||||
kmsRepo, err := kms.NewRepository(rw, rw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms repository: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating kms repository: %w", err)
|
||||
}
|
||||
kmsCache, err := kms.NewKms(kmsRepo, kms.WithLogger(b.Logger.Named("kms")))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms cache: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating kms cache: %w", err)
|
||||
}
|
||||
if err := kmsCache.AddExternalWrappers(
|
||||
kms.WithRootWrapper(b.RootKms),
|
||||
); err != nil {
|
||||
return fmt.Errorf("error adding config keys to kms: %w", err)
|
||||
return nil, nil, fmt.Errorf("error adding config keys to kms: %w", err)
|
||||
}
|
||||
|
||||
// Create the dev auth method
|
||||
pwRepo, err := password.NewRepository(rw, rw, kmsCache)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating password repo: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating password repo: %w", err)
|
||||
}
|
||||
authMethod, err := password.NewAuthMethod(scope.Global.String())
|
||||
authMethod, err := password.NewAuthMethod(scope.Global.String(),
|
||||
password.WithName("Generated global scope initial auth method"),
|
||||
password.WithDescription("Provides initial administrative authentication into Boundary"),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating new in memory auth method: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating new in memory auth method: %w", err)
|
||||
}
|
||||
if b.DevAuthMethodId == "" {
|
||||
b.DevAuthMethodId, err = db.NewPublicId(password.AuthMethodPrefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating initial auth method id: %w", err)
|
||||
return nil, nil, fmt.Errorf("error generating initial auth method id: %w", err)
|
||||
}
|
||||
}
|
||||
|
||||
@@ -54,12 +57,10 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) error {
|
||||
cancel()
|
||||
}()
|
||||
|
||||
_, err = pwRepo.CreateAuthMethod(cancelCtx, authMethod,
|
||||
password.WithName("Generated global scope initial auth method"),
|
||||
password.WithDescription("Provides initial administrative authentication into Boundary"),
|
||||
am, err := pwRepo.CreateAuthMethod(cancelCtx, authMethod,
|
||||
password.WithPublicId(b.DevAuthMethodId))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error saving auth method to the db: %w", err)
|
||||
return nil, nil, fmt.Errorf("error saving auth method to the db: %w", err)
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated auth method id")
|
||||
b.Info["generated auth method id"] = b.DevAuthMethodId
|
||||
@@ -68,14 +69,14 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) error {
|
||||
if b.DevLoginName == "" {
|
||||
b.DevLoginName, err = base62.Random(10)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to generate login name: %w", err)
|
||||
return nil, nil, fmt.Errorf("unable to generate login name: %w", err)
|
||||
}
|
||||
b.DevLoginName = strings.ToLower(b.DevLoginName)
|
||||
}
|
||||
if b.DevPassword == "" {
|
||||
b.DevPassword, err = base62.Random(20)
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to generate password: %w", err)
|
||||
return nil, nil, fmt.Errorf("unable to generate password: %w", err)
|
||||
}
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated auth method password")
|
||||
@@ -83,25 +84,25 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) error {
|
||||
|
||||
acct, err := password.NewAccount(b.DevAuthMethodId, password.WithLoginName(b.DevLoginName))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating new in memory auth account: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating new in memory auth account: %w", err)
|
||||
}
|
||||
acct, err = pwRepo.CreateAccount(cancelCtx, scope.Global.String(), acct, password.WithPassword(b.DevPassword))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error saving auth account to the db: %w", err)
|
||||
return nil, nil, fmt.Errorf("error saving auth account to the db: %w", err)
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated auth method login name")
|
||||
b.Info["generated auth method login name"] = acct.GetLoginName()
|
||||
|
||||
iamRepo, err := iam.NewRepository(rw, rw, kmsCache, iam.WithRandomReader(b.SecureRandomReader))
|
||||
if err != nil {
|
||||
return fmt.Errorf("unable to create repo for org id: %w", err)
|
||||
return nil, nil, fmt.Errorf("unable to create repo for org id: %w", err)
|
||||
}
|
||||
|
||||
// Create a new user and associate it with the account
|
||||
if b.DevUserId == "" {
|
||||
b.DevUserId, err = db.NewPublicId(iam.UserPrefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating initial user id: %w", err)
|
||||
return nil, nil, fmt.Errorf("error generating initial user id: %w", err)
|
||||
}
|
||||
}
|
||||
opts := []iam.Option{
|
||||
@@ -111,13 +112,13 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) error {
|
||||
}
|
||||
u, err := iam.NewUser(scope.Global.String(), opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating in memory user: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating in memory user: %w", err)
|
||||
}
|
||||
if u, err = iamRepo.CreateUser(cancelCtx, u, opts...); err != nil {
|
||||
return fmt.Errorf("error creating initial admin user: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating initial admin user: %w", err)
|
||||
}
|
||||
if u, err = iamRepo.AssociateUserWithAccount(cancelCtx, u.GetPublicId(), acct.GetPublicId()); err != nil {
|
||||
return fmt.Errorf("error associating initial admin user with account: %w", err)
|
||||
return nil, nil, fmt.Errorf("error associating initial admin user with account: %w", err)
|
||||
}
|
||||
|
||||
// Create a role tying them together
|
||||
@@ -126,37 +127,37 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) error {
|
||||
iam.WithDescription(`Provides admin grants within the "global" scope to the initial user`),
|
||||
)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating in memory role for generated grants: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating in memory role for generated grants: %w", err)
|
||||
}
|
||||
defPermsRole, err := iamRepo.CreateRole(cancelCtx, pr)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating role for default generated grants: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating role for default generated grants: %w", err)
|
||||
}
|
||||
if _, err := iamRepo.AddRoleGrants(cancelCtx, defPermsRole.PublicId, defPermsRole.Version, []string{"id=*;actions=*"}); err != nil {
|
||||
return fmt.Errorf("error creating grant for default generated grants: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating grant for default generated grants: %w", err)
|
||||
}
|
||||
if _, err := iamRepo.AddPrincipalRoles(cancelCtx, defPermsRole.PublicId, defPermsRole.Version+1, []string{u.GetPublicId()}, nil); err != nil {
|
||||
return fmt.Errorf("error adding principal to role for default generated grants: %w", err)
|
||||
return nil, nil, fmt.Errorf("error adding principal to role for default generated grants: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return am, u, nil
|
||||
}
|
||||
|
||||
func (b *Server) CreateInitialScopes(ctx context.Context) error {
|
||||
func (b *Server) CreateInitialScopes(ctx context.Context) (*iam.Scope, *iam.Scope, error) {
|
||||
rw := db.New(b.Database)
|
||||
|
||||
kmsRepo, err := kms.NewRepository(rw, rw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms repository: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating kms repository: %w", err)
|
||||
}
|
||||
kmsCache, err := kms.NewKms(kmsRepo, kms.WithLogger(b.Logger.Named("kms")))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms cache: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating kms cache: %w", err)
|
||||
}
|
||||
if err := kmsCache.AddExternalWrappers(
|
||||
kms.WithRootWrapper(b.RootKms),
|
||||
); err != nil {
|
||||
return fmt.Errorf("error adding config keys to kms: %w", err)
|
||||
return nil, nil, fmt.Errorf("error adding config keys to kms: %w", err)
|
||||
}
|
||||
|
||||
cancelCtx, cancel := context.WithCancel(ctx)
|
||||
@@ -167,14 +168,14 @@ func (b *Server) CreateInitialScopes(ctx context.Context) error {
|
||||
|
||||
iamRepo, err := iam.NewRepository(rw, rw, kmsCache)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating scopes repository: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating scopes repository: %w", err)
|
||||
}
|
||||
|
||||
// Create the scopes
|
||||
if b.DevOrgId == "" {
|
||||
b.DevOrgId, err = db.NewPublicId(scope.Org.Prefix())
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating initial org id: %w", err)
|
||||
return nil, nil, fmt.Errorf("error generating initial org id: %w", err)
|
||||
}
|
||||
}
|
||||
opts := []iam.Option{
|
||||
@@ -185,11 +186,11 @@ func (b *Server) CreateInitialScopes(ctx context.Context) error {
|
||||
}
|
||||
orgScope, err := iam.NewOrg(opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating new in memory org scope: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating new in memory org scope: %w", err)
|
||||
}
|
||||
_, err = iamRepo.CreateScope(cancelCtx, orgScope, b.DevUserId, opts...)
|
||||
orgScope, err = iamRepo.CreateScope(cancelCtx, orgScope, b.DevUserId, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error saving org scope to the db: %w", err)
|
||||
return nil, nil, fmt.Errorf("error saving org scope to the db: %w", err)
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated org scope id")
|
||||
b.Info["generated org scope id"] = b.DevOrgId
|
||||
@@ -197,7 +198,7 @@ func (b *Server) CreateInitialScopes(ctx context.Context) error {
|
||||
if b.DevProjectId == "" {
|
||||
b.DevProjectId, err = db.NewPublicId(scope.Project.Prefix())
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating initial project id: %w", err)
|
||||
return nil, nil, fmt.Errorf("error generating initial project id: %w", err)
|
||||
}
|
||||
}
|
||||
opts = []iam.Option{
|
||||
@@ -208,33 +209,33 @@ func (b *Server) CreateInitialScopes(ctx context.Context) error {
|
||||
}
|
||||
projScope, err := iam.NewProject(b.DevOrgId, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating new in memory project scope: %w", err)
|
||||
return nil, nil, fmt.Errorf("error creating new in memory project scope: %w", err)
|
||||
}
|
||||
_, err = iamRepo.CreateScope(cancelCtx, projScope, b.DevUserId, opts...)
|
||||
projScope, err = iamRepo.CreateScope(cancelCtx, projScope, b.DevUserId, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error saving project scope to the db: %w", err)
|
||||
return nil, nil, fmt.Errorf("error saving project scope to the db: %w", err)
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated project scope id")
|
||||
b.Info["generated project scope id"] = b.DevProjectId
|
||||
|
||||
return nil
|
||||
return orgScope, projScope, nil
|
||||
}
|
||||
|
||||
func (b *Server) CreateInitialHostResources(ctx context.Context) error {
|
||||
func (b *Server) CreateInitialHostResources(ctx context.Context) (*static.HostCatalog, *static.HostSet, *static.Host, error) {
|
||||
rw := db.New(b.Database)
|
||||
|
||||
kmsRepo, err := kms.NewRepository(rw, rw)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms repository: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error creating kms repository: %w", err)
|
||||
}
|
||||
kmsCache, err := kms.NewKms(kmsRepo, kms.WithLogger(b.Logger.Named("kms")))
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating kms cache: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error creating kms cache: %w", err)
|
||||
}
|
||||
if err := kmsCache.AddExternalWrappers(
|
||||
kms.WithRootWrapper(b.RootKms),
|
||||
); err != nil {
|
||||
return fmt.Errorf("error adding config keys to kms: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error adding config keys to kms: %w", err)
|
||||
}
|
||||
|
||||
cancelCtx, cancel := context.WithCancel(ctx)
|
||||
@@ -245,14 +246,14 @@ func (b *Server) CreateInitialHostResources(ctx context.Context) error {
|
||||
|
||||
staticRepo, err := static.NewRepository(rw, rw, kmsCache)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating static repository: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error creating static repository: %w", err)
|
||||
}
|
||||
|
||||
// Host Catalog
|
||||
if b.DevHostCatalogId == "" {
|
||||
b.DevHostCatalogId, err = db.NewPublicId(static.HostCatalogPrefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating initial host catalog id: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error generating initial host catalog id: %w", err)
|
||||
}
|
||||
}
|
||||
opts := []static.Option{
|
||||
@@ -262,10 +263,10 @@ func (b *Server) CreateInitialHostResources(ctx context.Context) error {
|
||||
}
|
||||
hc, err := static.NewHostCatalog(b.DevProjectId, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating in memory host catalog: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error creating in memory host catalog: %w", err)
|
||||
}
|
||||
if hc, err = staticRepo.CreateCatalog(cancelCtx, hc, opts...); err != nil {
|
||||
return fmt.Errorf("error saving host catalog to the db: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error saving host catalog to the db: %w", err)
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated host catalog id")
|
||||
b.Info["generated host catalog id"] = b.DevHostCatalogId
|
||||
@@ -274,7 +275,7 @@ func (b *Server) CreateInitialHostResources(ctx context.Context) error {
|
||||
if b.DevHostId == "" {
|
||||
b.DevHostId, err = db.NewPublicId(static.HostPrefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating initial host id: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error generating initial host id: %w", err)
|
||||
}
|
||||
}
|
||||
if b.DevHostAddress == "" {
|
||||
@@ -288,10 +289,10 @@ func (b *Server) CreateInitialHostResources(ctx context.Context) error {
|
||||
}
|
||||
h, err := static.NewHost(hc.PublicId, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating in memory host: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error creating in memory host: %w", err)
|
||||
}
|
||||
if h, err = staticRepo.CreateHost(cancelCtx, b.DevProjectId, h, opts...); err != nil {
|
||||
return fmt.Errorf("error saving host to the db: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error saving host to the db: %w", err)
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated host id")
|
||||
b.Info["generated host id"] = b.DevHostId
|
||||
@@ -300,7 +301,7 @@ func (b *Server) CreateInitialHostResources(ctx context.Context) error {
|
||||
if b.DevHostSetId == "" {
|
||||
b.DevHostSetId, err = db.NewPublicId(static.HostSetPrefix)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error generating initial host set id: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error generating initial host set id: %w", err)
|
||||
}
|
||||
}
|
||||
opts = []static.Option{
|
||||
@@ -310,20 +311,20 @@ func (b *Server) CreateInitialHostResources(ctx context.Context) error {
|
||||
}
|
||||
hs, err := static.NewHostSet(hc.PublicId, opts...)
|
||||
if err != nil {
|
||||
return fmt.Errorf("error creating in memory host set: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error creating in memory host set: %w", err)
|
||||
}
|
||||
if hs, err = staticRepo.CreateSet(cancelCtx, b.DevProjectId, hs, opts...); err != nil {
|
||||
return fmt.Errorf("error saving host set to the db: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error saving host set to the db: %w", err)
|
||||
}
|
||||
b.InfoKeys = append(b.InfoKeys, "generated host set id")
|
||||
b.Info["generated host set id"] = b.DevHostSetId
|
||||
|
||||
// Associate members
|
||||
if _, err := staticRepo.AddSetMembers(cancelCtx, b.DevProjectId, b.DevHostSetId, hs.GetVersion(), []string{h.GetPublicId()}); err != nil {
|
||||
return fmt.Errorf("error associating host set to host in the db: %w", err)
|
||||
return nil, nil, nil, fmt.Errorf("error associating host set to host in the db: %w", err)
|
||||
}
|
||||
|
||||
return nil
|
||||
return hc, hs, h, nil
|
||||
}
|
||||
|
||||
func (b *Server) CreateInitialTarget(ctx context.Context) (target.Target, error) {
|
||||
|
||||
@@ -481,7 +481,7 @@ func (b *Server) CreateDevDatabase(dialect string, opt ...Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := b.CreateInitialAuthMethod(context.Background()); err != nil {
|
||||
if _, _, err := b.CreateInitialAuthMethod(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -492,7 +492,7 @@ func (b *Server) CreateDevDatabase(dialect string, opt ...Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := b.CreateInitialScopes(context.Background()); err != nil {
|
||||
if _, _, err := b.CreateInitialScopes(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
@@ -503,7 +503,7 @@ func (b *Server) CreateDevDatabase(dialect string, opt ...Option) error {
|
||||
return nil
|
||||
}
|
||||
|
||||
if err := b.CreateInitialHostResources(context.Background()); err != nil {
|
||||
if _, _, _, err := b.CreateInitialHostResources(context.Background()); err != nil {
|
||||
return err
|
||||
}
|
||||
|
||||
|
||||
Reference in new issue
Block a user