Skip to content

Commit

Permalink
Use plugins mechanism instead of oss/e flag
Browse files Browse the repository at this point in the history
  • Loading branch information
r0mant committed Oct 14, 2017
1 parent 7ff5fdb commit 5dbda4f
Show file tree
Hide file tree
Showing 16 changed files with 118 additions and 91 deletions.
10 changes: 0 additions & 10 deletions constants.go
Original file line number Diff line number Diff line change
Expand Up @@ -204,13 +204,3 @@ const AdminRoleName = "admin"
// DefaultImplicitRole is implicit role that gets added to all service.RoleSet
// objects.
const DefaultImplicitRole = "default-implicit-role"

// DistroType allows to declare what kind of distribution of Teleport
// is running
type DistroType string

// Possible values for DistroType:
const (
DistroTypeOSS DistroType = "community"
DistroTypeEnterprise DistroType = "enterprise"
)
2 changes: 1 addition & 1 deletion e
Submodule e updated from a64ce9 to 861b18
2 changes: 1 addition & 1 deletion lib/auth/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -188,7 +188,7 @@ func Init(cfg InitConfig) (*AuthServer, *Identity, error) {
log.Infof("[INIT] Created Namespace: %q", defaults.Namespace)

// always create a default admin role
defaultRole := services.NewAdminRole(lib.IsEnterprise())
defaultRole := services.NewAdminRole()
err = asrv.CreateRole(defaultRole, backend.Forever)
if err != nil && !trace.IsAlreadyExists(err) {
return nil, nil, trace.Wrap(err)
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(false), backend.Forever), IsNil)
c.Assert(s.a.UpsertRole(services.NewAdminRole(), backend.Forever), IsNil)

// set up host private key and certificate
c.Assert(s.a.UpsertCertAuthority(
Expand Down
89 changes: 89 additions & 0 deletions lib/plugins/plugins.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,89 @@
/*
Copyright 2017 Gravitational, Inc.
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

// package plugins allows external packages override certain behavioral
// aspects of teleport
package plugins

import (
"fmt"
"sync"

"github.com/gravitational/teleport"
)

var m = &sync.Mutex{}

var emptyRolesHandler = func() error {
return nil
}

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

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

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

// 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
}

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

var versionPrinter = func() {
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()
}
20 changes: 0 additions & 20 deletions lib/runtimeflags.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,16 +31,9 @@ package lib

import (
"sync"

"github.com/gravitational/teleport"
)

var (
// currentDistroType contains the type of teleport binary: enterprise or
// open source this flag does not enable any enterprise features, but it
// makes the default experience of the OSS users nicer.
currentDistroType teleport.DistroType = teleport.DistroTypeOSS

// insecureDevMode is set to 'true' when teleport is started with a hidden
// --insecure flag. This mode is only useful for learning Teleport and following
// quick starts: it disables HTTPS certificate validation
Expand All @@ -50,19 +43,6 @@ var (
flagLock sync.Mutex
)

func SetDistroType(t teleport.DistroType) {
flagLock.Lock()
defer flagLock.Unlock()
currentDistroType = t
}

// IsEnterprise returns 'true' if Teleport is packaged with enterprise runime
func IsEnterprise() bool {
flagLock.Lock()
defer flagLock.Unlock()
return currentDistroType == teleport.DistroTypeEnterprise
}

// SetInsecureDevMode turns the 'insecure' mode on. In this mode Teleport accpets
// self-signed HTTPS certificates (for development only!)
func SetInsecureDevMode(m bool) {
Expand Down
12 changes: 3 additions & 9 deletions lib/services/role.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ import (

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/plugins"
"github.com/gravitational/teleport/lib/utils"
"github.com/gravitational/teleport/lib/utils/parse"

Expand Down Expand Up @@ -79,7 +80,7 @@ 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(isEnterprise bool) Role {
func NewAdminRole() Role {
role := &RoleV3{
Kind: KindRole,
Version: V3,
Expand All @@ -98,14 +99,7 @@ func NewAdminRole(isEnterprise bool) Role {
},
},
}

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

role.SetLogins(Allow, plugins.DefaultAllowedLogins())
return role
}

Expand Down
10 changes: 5 additions & 5 deletions lib/services/trustedcluster.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,8 +22,8 @@ import (
"time"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/plugins"
"github.com/gravitational/teleport/lib/utils"

"github.com/gravitational/trace"
Expand Down Expand Up @@ -251,8 +251,8 @@ 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 lib.IsEnterprise() {
return trace.BadParameter("missing 'role_map' parameter")
if err := plugins.EmptyRolesHandler(); err != nil {
return trace.Wrap(err)
}
// OSS teleport uses 'admin' by default:
c.Spec.RoleMap = RoleMap{
Expand Down Expand Up @@ -427,9 +427,9 @@ const RoleMapSchema = `{
"items": {
"type": "object",
"additionalProperties": false,
"properties": {
"properties": {
"local": {
"type": "array",
"type": "array",
"items": {
"type": "string"
}
Expand Down
18 changes: 4 additions & 14 deletions lib/utils/utils.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package utils

import (
"fmt"
"io"
"io/ioutil"
"net"
Expand All @@ -28,6 +27,7 @@ import (
"time"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib/plugins"
"github.com/gravitational/trace"
"github.com/pborman/uuid"
"golang.org/x/crypto/ssh"
Expand Down Expand Up @@ -166,19 +166,9 @@ func ReadOrMakeHostUUID(dataDir string) (string, error) {
return id, nil
}

// PrintVersion prints human readable version.
// - distro: name of the distribution. Empty string for OSS or "enterprise"
func PrintVersion(distro teleport.DistroType) {
if distro == teleport.DistroTypeEnterprise {
distro = " " + distro
} else {
distro = ""
}
ver := fmt.Sprintf("Teleport%s v%s", distro, teleport.Version)
if teleport.Gitref != "" {
ver = fmt.Sprintf("%s git:%s", ver, teleport.Gitref)
}
fmt.Println(ver)
// PrintVersion prints human readable version
func PrintVersion() {
plugins.VersionPrinter()
}

// HumanTimeFormat formats time as recognized by humans
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 @@ -192,7 +192,7 @@ func (s *WebSuite) SetUpTest(c *C) {
c.Assert(err, IsNil)

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

// configure cluster authentication preferences
cap, err := services.NewAuthPreference(services.AuthPreferenceSpecV2{
Expand Down
7 changes: 2 additions & 5 deletions tool/tctl/common/tctl.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,6 @@ import (
"os"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib"
"github.com/gravitational/teleport/lib/auth"
"github.com/gravitational/teleport/lib/config"
"github.com/gravitational/teleport/lib/defaults"
Expand Down Expand Up @@ -61,11 +60,9 @@ type CLICommand interface {
// "distributions" like OSS or Enterprise
//
// distribution: name of the Teleport distribution
func Run(distro teleport.DistroType, commands []CLICommand) {
func Run(commands []CLICommand) {
utils.InitLogger(utils.LoggingForCLI, logrus.WarnLevel)

lib.SetDistroType(distro)

// app is the command line parser
app := utils.InitCLIParser("tctl", GlobalHelpString)

Expand Down Expand Up @@ -101,7 +98,7 @@ func Run(distro teleport.DistroType, commands []CLICommand) {

// "version" command?
if selectedCmd == ver.FullCommand() {
utils.PrintVersion(distro)
utils.PrintVersion()
return
}

Expand Down
3 changes: 1 addition & 2 deletions tool/tctl/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,6 @@ limitations under the License.
package main

import (
"github.com/gravitational/teleport"
"github.com/gravitational/teleport/tool/tctl/common"
)

Expand All @@ -29,5 +28,5 @@ func main() {
&common.AuthCommand{},
&common.ResourceCommand{},
}
common.Run(teleport.DistroTypeOSS, commands)
common.Run(commands)
}
9 changes: 2 additions & 7 deletions tool/teleport/common/teleport.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ import (
"strings"

"github.com/gravitational/teleport"
"github.com/gravitational/teleport/lib"
"github.com/gravitational/teleport/lib/config"
"github.com/gravitational/teleport/lib/defaults"
"github.com/gravitational/teleport/lib/service"
Expand All @@ -42,14 +41,10 @@ import (

// same as main() but has a testing switch
// - cmdlineArgs are passed from main()
// - distro can be "" (OSS version) or "enterprise"
// - testRun is 'true' when running under an integration test
func Run(cmdlineArgs []string, distro teleport.DistroType, testRun bool) (executedCommand string, conf *service.Config) {
func Run(cmdlineArgs []string, testRun bool) (executedCommand string, conf *service.Config) {
var err error

// initialize the teleport library with the proper distro flag
lib.SetDistroType(distro)

// configure trace's errors to produce full stack traces
isDebug, _ := strconv.ParseBool(os.Getenv(teleport.VerboseLogsEnvVar))
if isDebug {
Expand Down Expand Up @@ -185,7 +180,7 @@ func Run(cmdlineArgs []string, distro teleport.DistroType, testRun bool) (execut
case dump.FullCommand():
onConfigDump()
case ver.FullCommand():
utils.PrintVersion(distro)
utils.PrintVersion()
}
if err != nil {
utils.FatalError(err)
Expand Down
Loading

0 comments on commit 5dbda4f

Please sign in to comment.