Create dev org in database (#41)

These changes create a dev org in the database on startup.

They also plumb through a dev-org-id flag that allows specifying a chosen org ID in dev mode.

The logic will allow us to let people specify a default org ID in the config file too, although currently that's not enabled.

Several other improvements were made as well:

*    Consolidation of some code from across the server commands
*    Plumbing shutdownch through so we can use it for canceling contexts during startup
This commit is contained in:
Jeff Mitchell authored and GitHub committed 2020-05-11 17:03:23 -04:00
1 parent 670abaed88
commit 062ce6079f
7 files changed
+150 -63

No files matched your search

+72 -4
View File
@@ -25,7 +25,9 @@ import (
"github.com/hashicorp/vault/sdk/logical"
"github.com/hashicorp/watchtower/globals"
"github.com/hashicorp/watchtower/internal/db"
"github.com/hashicorp/watchtower/internal/iam"
"github.com/hashicorp/watchtower/version"
"github.com/jinzhu/gorm"
"github.com/mitchellh/cli"
"golang.org/x/net/http/httpproxy"
"google.golang.org/grpc/grpclog"
@@ -34,6 +36,8 @@ import (
)
type Server struct {
*Command
InfoKeys []string
Info map[string]string
@@ -57,12 +61,17 @@ type Server struct {
Listeners []*ServerListener
DefaultOrgId string
DevDatabaseUrl string
DevDatabaseCleanupFunc func() error
Database *gorm.DB
}
func NewServer() *Server {
func NewServer(cmd *Command) *Server {
return &Server{
Command: cmd,
InfoKeys: make([]string, 0, 20),
Info: make(map[string]string),
SecureRandomReader: rand.Reader,
@@ -355,11 +364,11 @@ func (b *Server) RunShutdownFuncs(ui cli.Ui) {
}
}
func (b *Server) CreateDevDatabase() error {
c, url, err := db.InitDbInDocker("postgres")
func (b *Server) CreateDevDatabase(dialect string) error {
c, url, err := db.InitDbInDocker(dialect)
if err != nil {
c()
return fmt.Errorf("unable to start dev database: %w", err)
return fmt.Errorf("unable to start dev database with dialect %s: %w", dialect, err)
}
b.DevDatabaseCleanupFunc = c
@@ -367,10 +376,69 @@ func (b *Server) CreateDevDatabase() error {
b.InfoKeys = append(b.InfoKeys, "dev database url")
b.Info["dev database url"] = b.DevDatabaseUrl
dbase, err := gorm.Open(dialect, url)
if err != nil {
c()
return fmt.Errorf("unable to create db object with dialect %s: %w", dialect, err)
}
b.Database = dbase
rw := &db.Db{Tx: b.Database}
repo, err := iam.NewRepository(rw, rw, b.ControllerKMS)
if err != nil {
c()
return fmt.Errorf("unable to create repo for org id: %w", err)
}
ctx, cancel := context.WithCancel(context.Background())
go func() {
<-b.ShutdownCh
cancel()
}()
var scope *iam.Scope
if b.DefaultOrgId != "" {
scope, err = repo.LookupScope(ctx, iam.WithPublicId(b.DefaultOrgId))
if err != nil {
c()
return fmt.Errorf("error looking up existing scope with org ID %q: %w", b.DefaultOrgId, err)
}
if scope != nil {
goto INFO
}
}
scope, err = iam.NewOrganization(iam.WithPublicId(b.DefaultOrgId))
if err != nil {
c()
return fmt.Errorf("error creating new org scope: %w", err)
}
scope, err = repo.CreateScope(ctx, scope)
if err != nil {
c()
return fmt.Errorf("error persisting new org scope: %w", err)
}
if b.DefaultOrgId != "" {
if scope.GetPublicId() != b.DefaultOrgId {
c()
return fmt.Errorf("expected org ID %q, got %q after persisting", b.DefaultOrgId, scope.GetPublicId())
}
} else {
b.DefaultOrgId = scope.GetPublicId()
}
INFO:
b.InfoKeys = append(b.InfoKeys, "dev org id")
b.Info["dev org id"] = b.DefaultOrgId
return nil
}
func (b *Server) DestroyDevDatabase() error {
if b.Database != nil {
b.Database.Close()
}
if b.DevDatabaseCleanupFunc != nil {
return b.DevDatabaseCleanupFunc()
}