Skip to content

Commit

Permalink
Plugins interface
Browse files Browse the repository at this point in the history
  • Loading branch information
r0mant committed Oct 16, 2017
1 parent e0b9929 commit 668243d
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 44 deletions.
2 changes: 1 addition & 1 deletion e
Submodule e updated from 861b18 to a83b3d
70 changes: 30 additions & 40 deletions lib/plugins/plugins.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,65 +25,55 @@ import (
"github.com/gravitational/teleport"
)

var m = &sync.Mutex{}

var emptyRolesHandler = func() error {
return nil
// Plugins defines interface that external libraries can implement customizing
// default teleport behavior
type Plugins interface {
// EmptyRoles handler is called when a new trusted cluster with empty roles
// is being created
EmptyRolesHandler() error
// DefaultAllowedLogins returns default allowed logins for a new admin role
DefaultAllowedLogins() []string
// PrintVersion prints teleport version
PrintVersion()
}

// SetEmptyRolesHandler sets the callback which is called when a new trusted
// cluster with empty roles is being created
func SetEmptyRolesHandler(fn func() error) {
// SetPlugins sets the plugins interface
func SetPlugins(p Plugins) {
m.Lock()
defer m.Unlock()
emptyRolesHandler = fn
plugins = p
}

// EmptyRoles handler is called when a new trusted cluster with empty roles
// is being created
func EmptyRolesHandler() error {
// GetPlugins returns the plugins interface
func GetPlugins() Plugins {
m.Lock()
defer m.Unlock()
return emptyRolesHandler()
return plugins
}

var defaultAllowedLogins = func() []string {
return []string{teleport.TraitInternalRoleVariable}
}
type defaultPlugins struct{}

// SetDefaultAllowedLogins sets the function that returns default allowed
// logins for a new admin role
func SetDefaultAllowedLogins(fn func() []string) {
m.Lock()
defer m.Unlock()
defaultAllowedLogins = fn
// EmptyRolesHandler is called when a new trusted cluster with empty roles
// is created, no-op by default
func (p *defaultPlugins) EmptyRolesHandler() error {
return nil
}

// DefaultAllowedLogins returns default allowed logins for a new admin role
func DefaultAllowedLogins() []string {
m.Lock()
defer m.Unlock()
return defaultAllowedLogins()
// DefaultAllowedLogins returns allowed logins for a new admin role
func (p *defaultPlugins) DefaultAllowedLogins() []string {
return []string{teleport.TraitInternalRoleVariable}
}

var versionPrinter = func() {
// PrintVersion prints teleport version
func (p *defaultPlugins) PrintVersion() {
ver := fmt.Sprintf("Teleport v%s", teleport.Version)
if teleport.Gitref != "" {
ver = fmt.Sprintf("%s git:%s", ver, teleport.Gitref)
}
fmt.Println(ver)
}

// SetVersionPrinter sets the method that prints teleport version
func SetVersionPrinter(fn func()) {
m.Lock()
defer m.Unlock()
versionPrinter = fn
}

// VersionPrinter prints teleport version
func VersionPrinter() {
m.Lock()
defer m.Unlock()
versionPrinter()
}
var (
m = &sync.Mutex{}
plugins Plugins = &defaultPlugins{}
)
2 changes: 1 addition & 1 deletion lib/services/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -99,7 +99,7 @@ func NewAdminRole() Role {
},
},
}
role.SetLogins(Allow, plugins.DefaultAllowedLogins())
role.SetLogins(Allow, plugins.GetPlugins().DefaultAllowedLogins())
return role
}

Expand Down
2 changes: 1 addition & 1 deletion lib/services/trustedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ func (c *TrustedClusterV2) CheckAndSetDefaults() error {
}
// we are not mentioning Roles parameter because we are deprecating it
if len(c.Spec.Roles) == 0 && len(c.Spec.RoleMap) == 0 {
if err := plugins.EmptyRolesHandler(); err != nil {
if err := plugins.GetPlugins().EmptyRolesHandler(); err != nil {
return trace.Wrap(err)
}
// OSS teleport uses 'admin' by default:
Expand Down
2 changes: 1 addition & 1 deletion lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ func ReadOrMakeHostUUID(dataDir string) (string, error) {

// PrintVersion prints human readable version
func PrintVersion() {
plugins.VersionPrinter()
plugins.GetPlugins().PrintVersion()
}

// HumanTimeFormat formats time as recognized by humans
Expand Down

0 comments on commit 668243d

Please sign in to comment.