Skip to content

Commit

Permalink
Merge pull request #1279 from gravitational/rjones/enterprise-admin
Browse files Browse the repository at this point in the history
Add root to the list of logins for an Enterprise role.
  • Loading branch information
russjones authored Sep 8, 2017
2 parents 6d345b5 + f9ad90f commit 49fb9c7
Show file tree
Hide file tree
Showing 8 changed files with 56 additions and 9 deletions.
3 changes: 3 additions & 0 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -194,6 +194,9 @@ const (
TraitInternalRoleVariable = "{{internal.logins}}"
)

// Root is *nix system administrator account name.
const Root = "root"

// DefaultRole is the name of the default admin role for all local users if
// another role is not explicitly assigned (Enterprise only).
const AdminRoleName = "admin"
Expand Down
5 changes: 5 additions & 0 deletions lib/auth/auth_with_roles.go
Original file line number Diff line number Diff line change
Expand Up @@ -744,6 +744,11 @@ func (a *AuthWithRoles) GetRoles() ([]services.Role, error) {
return a.authServer.GetRoles()
}

// CreateRole creates a role.
func (a *AuthWithRoles) CreateRole(role services.Role, ttl time.Duration) error {
return trace.BadParameter("not implemented")
}

// UpsertRole creates or updates role
func (a *AuthWithRoles) UpsertRole(role services.Role, ttl time.Duration) error {
if err := a.action(defaults.Namespace, services.KindRole, services.VerbCreate); err != nil {
Expand Down
5 changes: 5 additions & 0 deletions lib/auth/clt.go
Original file line number Diff line number Diff line change
Expand Up @@ -1309,6 +1309,11 @@ func (c *Client) GetRoles() ([]services.Role, error) {
return roles, nil
}

// CreateRole creates a role.
func (c *Client) CreateRole(role services.Role, ttl time.Duration) error {
return trace.BadParameter("not implemented")
}

// UpsertRole creates or updates role
func (c *Client) UpsertRole(role services.Role, ttl time.Duration) error {
data, err := services.GetRoleMarshaler().MarshalRole(role)
Expand Down
11 changes: 7 additions & 4 deletions lib/auth/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (
"time"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib"
"github.com/gravitational/teleport/lib/backend"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/services"
Expand Down Expand Up @@ -192,12 +193,14 @@ func Init(cfg InitConfig) (*AuthServer, *Identity, error) {
log.Infof("[INIT] Created Namespace: %q", defaults.Namespace)

// always create a default admin role
defaultRole := services.NewAdminRole()
err = asrv.UpsertRole(defaultRole, backend.Forever)
if err != nil {
defaultRole := services.NewAdminRole(lib.IsEnterprise())
err = asrv.CreateRole(defaultRole, backend.Forever)
if err != nil && !trace.IsAlreadyExists(err) {
return nil, nil, trace.Wrap(err)
}
log.Infof("[INIT] Created default Role: %q", defaultRole.GetName())
if !trace.IsAlreadyExists(err) {
log.Infof("[INIT] Created default admin role: %q", defaultRole.GetName())
}

// generate a user certificate authority if it doesn't exist
if _, err := asrv.GetCertAuthority(services.CertAuthID{DomainName: cfg.ClusterName.GetClusterName(), Type: services.UserCA}, false); err != nil {
Expand Down
2 changes: 1 addition & 1 deletion lib/auth/tun_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ func (s *TunSuite) SetUpTest(c *C) {
c.Assert(err, IsNil)

// create the default role
c.Assert(s.a.UpsertRole(services.NewAdminRole(), backend.Forever), IsNil)
c.Assert(s.a.UpsertRole(services.NewAdminRole(false), backend.Forever), IsNil)

// set up host private key and certificate
c.Assert(s.a.UpsertCertAuthority(
Expand Down
20 changes: 20 additions & 0 deletions lib/services/local/access.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,26 @@ func (s *AccessService) GetRoles() ([]services.Role, error) {
return out, nil
}

// CreateRole creates a role on the backend.
func (s *AccessService) CreateRole(role services.Role, ttl time.Duration) error {
data, err := services.GetRoleMarshaler().MarshalRole(role)
if err != nil {
return trace.Wrap(err)
}

// TODO(klizhentas): Picking smaller of the two ttls
backendTTL := backend.TTL(s.Clock(), role.Expiry())
if backendTTL < ttl {
ttl = backendTTL
}

err = s.CreateVal([]string{"roles", role.GetName()}, "params", []byte(data), ttl)
if err != nil {
return trace.Wrap(err)
}
return nil
}

// UpsertRole updates parameters about role
func (s *AccessService) UpsertRole(role services.Role, ttl time.Duration) error {
data, err := services.GetRoleMarshaler().MarshalRole(role)
Expand Down
17 changes: 14 additions & 3 deletions lib/services/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,8 +79,8 @@ func RoleNameForCertAuthority(name string) string {

// NewAdminRole is the default admin role for all local users if another role
// is not explicitly assigned (Enterprise only).
func NewAdminRole() Role {
return &RoleV3{
func NewAdminRole(isEnterprise bool) Role {
role := &RoleV3{
Kind: KindRole,
Version: V3,
Metadata: Metadata{
Expand All @@ -93,12 +93,20 @@ func NewAdminRole() Role {
},
Allow: RoleConditions{
Namespaces: []string{defaults.Namespace},
Logins: []string{teleport.TraitInternalRoleVariable},
NodeLabels: map[string]string{Wildcard: Wildcard},
Rules: CopyRulesSlice(AdminUserRules),
},
},
}

// the default role also has "root" for enterprise users
allowedLogins := []string{teleport.TraitInternalRoleVariable}
if isEnterprise {
allowedLogins = append(allowedLogins, teleport.Root)
}
role.SetLogins(Allow, allowedLogins)

return role
}

// NewImplicitRole is the default implicit role that gets added to all
Expand Down Expand Up @@ -181,6 +189,9 @@ type Access interface {
// GetRoles returns a list of roles
GetRoles() ([]Role, error)

// CreateRole creates a role
CreateRole(role Role, ttl time.Duration) error

// UpsertRole creates or updates role
UpsertRole(role Role, ttl time.Duration) error

Expand Down
2 changes: 1 addition & 1 deletion lib/web/apiserver_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ func (s *WebSuite) SetUpTest(c *C) {
c.Assert(err, IsNil)

// create the default role
c.Assert(s.authServer.UpsertRole(services.NewAdminRole(), backend.Forever), IsNil)
c.Assert(s.authServer.UpsertRole(services.NewAdminRole(false), backend.Forever), IsNil)

// configure cluster authentication preferences
cap, err := services.NewAuthPreference(services.AuthPreferenceSpecV2{
Expand Down

0 comments on commit 49fb9c7

Please sign in to comment.