Add account ID templating (#518)

* Remove default role and create via the same mechanism as other initial resources
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-10-01 10:51:54 -04:00
1 parent 19aecfefae
commit fa700dc002
24 files changed
+291 -142

No files matched your search

+53 -1
View File
@@ -15,6 +15,58 @@ import (
"github.com/hashicorp/vault/sdk/helper/base62"
)
func (b *Server) CreateInitialLoginRole(ctx context.Context) (*iam.Role, error) {
rw := db.New(b.Database)
kmsRepo, err := kms.NewRepository(rw, rw)
if err != nil {
return nil, fmt.Errorf("error creating kms repository: %w", err)
}
kmsCache, err := kms.NewKms(kmsRepo, kms.WithLogger(b.Logger.Named("kms")))
if err != nil {
return nil, fmt.Errorf("error creating kms cache: %w", err)
}
if err := kmsCache.AddExternalWrappers(
kms.WithRootWrapper(b.RootKms),
); err != nil {
return nil, fmt.Errorf("error adding config keys to kms: %w", err)
}
cancelCtx, cancel := context.WithCancel(ctx)
go func() {
<-b.ShutdownCh
cancel()
}()
iamRepo, err := iam.NewRepository(rw, rw, kmsCache, iam.WithRandomReader(b.SecureRandomReader))
if err != nil {
return nil, fmt.Errorf("unable to create repo for initial login role: %w", err)
}
pr, err := iam.NewRole(scope.Global.String(),
iam.WithName("Login and Default Grants"),
iam.WithDescription(`Role created for login capability and account self-management for users of the global scope at its creation time`),
)
if err != nil {
return nil, fmt.Errorf("error creating in memory role for generated grants: %w", err)
}
role, err := iamRepo.CreateRole(cancelCtx, pr)
if err != nil {
return nil, fmt.Errorf("error creating role for default generated grants: %w", err)
}
if _, err := iamRepo.AddRoleGrants(cancelCtx, role.PublicId, role.Version, []string{
"type=scope;actions=list",
"id=*;type=auth-method;actions=authenticate,list",
"id={{account.id}};actions=read,change-password",
}); err != nil {
return nil, fmt.Errorf("error creating grant for default generated grants: %w", err)
}
if _, err := iamRepo.AddPrincipalRoles(cancelCtx, role.PublicId, role.Version+1, []string{"u_anon"}, nil); err != nil {
return nil, fmt.Errorf("error adding principal to role for default generated grants: %w", err)
}
return role, nil
}
func (b *Server) CreateInitialAuthMethod(ctx context.Context) (*password.AuthMethod, *iam.User, error) {
rw := db.New(b.Database)
@@ -122,7 +174,7 @@ func (b *Server) CreateInitialAuthMethod(ctx context.Context) (*password.AuthMet
}
// Create a role tying them together
pr, err := iam.NewRole(scope.Global.String(),
iam.WithName("Generated global scope admin role"),
iam.WithName("Administration"),
iam.WithDescription(`Provides admin grants within the "global" scope to the initial user`),
)
if err != nil {
+4
View File
@@ -498,6 +498,10 @@ func (b *Server) CreateDevDatabase(dialect string, opt ...Option) error {
return err
}
if _, err := b.CreateInitialLoginRole(context.Background()); err != nil {
return err
}
if opts.withSkipAuthMethodCreation {
// now that we have passed all the error cases, reset c to be a noop so the
// defer doesn't do anything.
+30 -3
View File
@@ -6,7 +6,34 @@ import (
"github.com/hashicorp/boundary/internal/cmd/base"
)
type AuthMethodInfo struct {
type RoleInfo struct {
RoleId string `json:"scope_id"`
Name string `json:"name"`
}
func generateInitialRoleTableOutput(in *RoleInfo) string {
nonAttributeMap := map[string]interface{}{
"Role ID": in.RoleId,
"Name": in.Name,
}
maxLength := 0
for k := range nonAttributeMap {
if len(k) > maxLength {
maxLength = len(k)
}
}
ret := []string{
"",
"Initial login role information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
}
return base.WrapForHelpText(ret)
}
type AuthInfo struct {
AuthMethodId string `json:"auth_method_id"`
AuthMethodName string `json:"auth_method_name"`
LoginName string `json:"login_name"`
@@ -16,7 +43,7 @@ type AuthMethodInfo struct {
UserName string `json:"user_name"`
}
func generateInitialAuthMethodTableOutput(in *AuthMethodInfo) string {
func generateInitialAuthTableOutput(in *AuthInfo) string {
nonAttributeMap := map[string]interface{}{
"Scope ID": in.ScopeId,
"Auth Method ID": in.AuthMethodId,
@@ -36,7 +63,7 @@ func generateInitialAuthMethodTableOutput(in *AuthMethodInfo) string {
ret := []string{
"",
"Initial auth method information:",
"Initial auth information:",
base.WrapMap(2, maxLength+2, nonAttributeMap),
}
+44 -15
View File
@@ -31,15 +31,16 @@ type InitCommand struct {
configWrapper wrapping.Wrapper
flagConfig string
flagConfigKms string
flagLogLevel string
flagLogFormat string
flagMigrationUrl string
flagSkipAuthMethodCreation bool
flagSkipScopesCreation bool
flagSkipHostResourcesCreation bool
flagSkipTargetCreation bool
flagConfig string
flagConfigKms string
flagLogLevel string
flagLogFormat string
flagMigrationUrl string
flagSkipInitialLoginRoleCreation bool
flagSkipAuthMethodCreation bool
flagSkipScopesCreation bool
flagSkipHostResourcesCreation bool
flagSkipTargetCreation bool
}
func (c *InitCommand) Synopsis() string {
@@ -56,6 +57,7 @@ func (c *InitCommand) Help() string {
"",
" Unless told not to via flags, some initial resources will be created, in the following order and in the indicated scopes:",
"",
" Initial Login Role (global)",
" Password-Type Auth Method (global)",
" Org Scope (global)",
" Project Scope (org)",
@@ -113,6 +115,12 @@ func (c *InitCommand) Flags() *base.FlagSets {
f = set.NewFlagSet("Init Options")
f.BoolVar(&base.BoolVar{
Name: "skip-initial-login-role-creation",
Target: &c.flagSkipInitialLoginRoleCreation,
Usage: "If not set, a default role allowing necessary grants for logging in will not be created as part of initialization. If set, the recovery KMS will be needed to perform any actions.",
})
f.BoolVar(&base.BoolVar{
Name: "skip-auth-method-creation",
Target: &c.flagSkipAuthMethodCreation,
@@ -283,10 +291,6 @@ func (c *InitCommand) Run(args []string) (retCode int) {
c.UI.Info("Global-scope KMS keys successfully created.")
}
if c.flagSkipAuthMethodCreation {
return 0
}
var jsonMap map[string]interface{}
if base.Format(c.UI) == "json" {
jsonMap = make(map[string]interface{})
@@ -301,6 +305,31 @@ func (c *InitCommand) Run(args []string) (retCode int) {
}()
}
if c.flagSkipInitialLoginRoleCreation {
return 0
}
role, err := c.srv.CreateInitialLoginRole(c.Context)
if err != nil {
c.UI.Error(fmt.Errorf("Error creating initial global-scoped login role: %w", err).Error())
return 1
}
roleInfo := &RoleInfo{
RoleId: role.PublicId,
Name: role.Name,
}
switch base.Format(c.UI) {
case "table":
c.UI.Output(generateInitialRoleTableOutput(roleInfo))
case "json":
jsonMap["login_role"] = roleInfo
}
if c.flagSkipAuthMethodCreation {
return 0
}
// Use an easy name, at least
c.srv.DevLoginName = "admin"
am, user, err := c.srv.CreateInitialAuthMethod(c.Context)
@@ -309,7 +338,7 @@ func (c *InitCommand) Run(args []string) (retCode int) {
return 1
}
authMethodInfo := &AuthMethodInfo{
authMethodInfo := &AuthInfo{
AuthMethodId: c.srv.DevAuthMethodId,
AuthMethodName: am.Name,
LoginName: c.srv.DevLoginName,
@@ -320,7 +349,7 @@ func (c *InitCommand) Run(args []string) (retCode int) {
}
switch base.Format(c.UI) {
case "table":
c.UI.Output(generateInitialAuthMethodTableOutput(authMethodInfo))
c.UI.Output(generateInitialAuthTableOutput(authMethodInfo))
case "json":
jsonMap["auth_method"] = authMethodInfo
}
+1 -1
View File
@@ -182,7 +182,7 @@ func (c *Command) Run(args []string) int {
if len(grants) > 0 {
for _, grant := range grants {
_, err := perms.Parse("global", "", grant, false)
_, err := perms.Parse("global", grant)
if err != nil {
c.UI.Error(fmt.Errorf("Grant %q could not be parsed successfully: %w", grant, err).Error())
return 1