2022-02-09 14:01:19 +00:00
|
|
|
package start
|
|
|
|
|
|
|
|
import (
|
2022-02-14 16:22:30 +00:00
|
|
|
"context"
|
|
|
|
"database/sql"
|
2022-02-09 14:01:19 +00:00
|
|
|
_ "embed"
|
2022-02-14 16:22:30 +00:00
|
|
|
"fmt"
|
|
|
|
"net"
|
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2022-02-09 14:01:19 +00:00
|
|
|
|
|
|
|
"github.com/caos/logging"
|
2022-04-25 08:01:17 +00:00
|
|
|
"github.com/caos/oidc/v2/pkg/op"
|
2022-02-14 16:22:30 +00:00
|
|
|
"github.com/gorilla/mux"
|
2022-02-09 14:01:19 +00:00
|
|
|
"github.com/spf13/cobra"
|
2022-02-14 16:22:30 +00:00
|
|
|
"github.com/spf13/viper"
|
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
|
|
|
|
2022-04-04 08:10:57 +00:00
|
|
|
"github.com/caos/zitadel/cmd/admin/key"
|
2022-02-14 16:22:30 +00:00
|
|
|
admin_es "github.com/caos/zitadel/internal/admin/repository/eventsourcing"
|
|
|
|
"github.com/caos/zitadel/internal/api"
|
|
|
|
"github.com/caos/zitadel/internal/api/assets"
|
|
|
|
internal_authz "github.com/caos/zitadel/internal/api/authz"
|
|
|
|
"github.com/caos/zitadel/internal/api/grpc/admin"
|
|
|
|
"github.com/caos/zitadel/internal/api/grpc/auth"
|
|
|
|
"github.com/caos/zitadel/internal/api/grpc/management"
|
2022-04-25 08:01:17 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/grpc/system"
|
2022-02-14 16:22:30 +00:00
|
|
|
"github.com/caos/zitadel/internal/api/http/middleware"
|
|
|
|
"github.com/caos/zitadel/internal/api/oidc"
|
|
|
|
"github.com/caos/zitadel/internal/api/ui/console"
|
|
|
|
"github.com/caos/zitadel/internal/api/ui/login"
|
|
|
|
auth_es "github.com/caos/zitadel/internal/auth/repository/eventsourcing"
|
|
|
|
"github.com/caos/zitadel/internal/authz"
|
|
|
|
authz_repo "github.com/caos/zitadel/internal/authz/repository"
|
|
|
|
"github.com/caos/zitadel/internal/command"
|
2022-03-14 06:55:09 +00:00
|
|
|
cryptoDB "github.com/caos/zitadel/internal/crypto/database"
|
2022-02-14 16:22:30 +00:00
|
|
|
"github.com/caos/zitadel/internal/database"
|
|
|
|
"github.com/caos/zitadel/internal/eventstore"
|
|
|
|
"github.com/caos/zitadel/internal/id"
|
|
|
|
"github.com/caos/zitadel/internal/notification"
|
|
|
|
"github.com/caos/zitadel/internal/query"
|
|
|
|
"github.com/caos/zitadel/internal/static"
|
|
|
|
"github.com/caos/zitadel/internal/webauthn"
|
|
|
|
"github.com/caos/zitadel/openapi"
|
2022-02-09 14:01:19 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func New() *cobra.Command {
|
2022-02-14 16:22:30 +00:00
|
|
|
start := &cobra.Command{
|
2022-02-09 14:01:19 +00:00
|
|
|
Use: "start",
|
|
|
|
Short: "starts ZITADEL instance",
|
|
|
|
Long: `starts ZITADEL.
|
|
|
|
Requirements:
|
|
|
|
- cockroachdb`,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-03-28 08:05:09 +00:00
|
|
|
config := MustNewConfig(viper.GetViper())
|
2022-04-04 08:10:57 +00:00
|
|
|
masterKey, err := key.MasterKey(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-03-28 08:05:09 +00:00
|
|
|
|
2022-03-14 06:55:09 +00:00
|
|
|
return startZitadel(config, masterKey)
|
2022-02-09 14:01:19 +00:00
|
|
|
},
|
|
|
|
}
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
startFlags(start)
|
2022-03-14 06:55:09 +00:00
|
|
|
|
2022-02-14 16:22:30 +00:00
|
|
|
return start
|
|
|
|
}
|
|
|
|
|
2022-03-28 08:05:09 +00:00
|
|
|
func startZitadel(config *Config, masterKey string) error {
|
2022-02-14 16:22:30 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
|
|
|
dbClient, err := database.Connect(config.Database)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start client for projection: %w", err)
|
|
|
|
}
|
2022-03-14 06:55:09 +00:00
|
|
|
|
|
|
|
keyStorage, err := cryptoDB.NewKeyStorage(dbClient, masterKey)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start key storage: %w", err)
|
|
|
|
}
|
|
|
|
keys, err := ensureEncryptionKeys(config.EncryptionKeys, keyStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-02-14 16:22:30 +00:00
|
|
|
eventstoreClient, err := eventstore.Start(dbClient)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start eventstore for queries: %w", err)
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
queries, err := query.StartQueries(ctx, eventstoreClient, dbClient, config.Projections, keys.OIDC, config.InternalAuthZ.RolePermissionMappings)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start queries: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-03-14 06:55:09 +00:00
|
|
|
authZRepo, err := authz.Start(config.AuthZ, config.SystemDefaults, queries, dbClient, keys.OIDC)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error starting authz repo: %w", err)
|
|
|
|
}
|
2022-04-06 06:13:40 +00:00
|
|
|
|
|
|
|
storage, err := config.AssetStorage.NewStorage(dbClient)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start asset storage client: %w", err)
|
|
|
|
}
|
2022-04-25 08:01:17 +00:00
|
|
|
webAuthNConfig := &webauthn.Config{
|
|
|
|
DisplayName: config.WebAuthNName,
|
|
|
|
ExternalSecure: config.ExternalSecure,
|
2022-02-14 16:22:30 +00:00
|
|
|
}
|
2022-04-25 09:16:36 +00:00
|
|
|
commands, err := command.StartCommands(
|
|
|
|
eventstoreClient,
|
|
|
|
config.SystemDefaults,
|
|
|
|
config.InternalAuthZ.RolePermissionMappings,
|
|
|
|
storage,
|
|
|
|
authZRepo,
|
|
|
|
webAuthNConfig,
|
|
|
|
config.ExternalSecure,
|
|
|
|
config.ExternalPort,
|
|
|
|
keys.IDPConfig,
|
|
|
|
keys.OTP,
|
|
|
|
keys.SMTP,
|
|
|
|
keys.SMS,
|
|
|
|
keys.User,
|
|
|
|
keys.DomainVerification,
|
|
|
|
keys.OIDC,
|
|
|
|
)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start commands: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:16:36 +00:00
|
|
|
notification.Start(config.Notification, config.ExternalPort, config.ExternalSecure, commands, queries, dbClient, assets.HandlerPrefix, keys.User, keys.SMTP, keys.SMS)
|
2022-02-14 16:22:30 +00:00
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2022-04-25 08:01:17 +00:00
|
|
|
err = startAPIs(ctx, router, commands, queries, eventstoreClient, dbClient, config, storage, authZRepo, keys)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return listen(ctx, router, config.Port)
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
func startAPIs(ctx context.Context, router *mux.Router, commands *command.Commands, queries *query.Queries, eventstore *eventstore.Eventstore, dbClient *sql.DB, config *Config, store static.Storage, authZRepo authz_repo.Repository, keys *encryptionKeys) error {
|
2022-02-14 16:22:30 +00:00
|
|
|
repo := struct {
|
|
|
|
authz_repo.Repository
|
|
|
|
*query.Queries
|
|
|
|
}{
|
|
|
|
authZRepo,
|
|
|
|
queries,
|
|
|
|
}
|
|
|
|
verifier := internal_authz.Start(repo)
|
|
|
|
|
2022-04-21 10:37:39 +00:00
|
|
|
authenticatedAPIs := api.New(config.Port, router, &repo, config.InternalAuthZ, config.ExternalSecure, config.HTTP2HostHeader)
|
2022-03-14 06:55:09 +00:00
|
|
|
authRepo, err := auth_es.Start(config.Auth, config.SystemDefaults, commands, queries, dbClient, assets.HandlerPrefix, keys.OIDC, keys.User)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error starting auth repo: %w", err)
|
|
|
|
}
|
|
|
|
adminRepo, err := admin_es.Start(config.Admin, store, dbClient, login.HandlerPrefix)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error starting admin repo: %w", err)
|
|
|
|
}
|
2022-04-25 09:16:36 +00:00
|
|
|
if err := authenticatedAPIs.RegisterServer(ctx, system.CreateServer(commands, queries, adminRepo, config.DefaultInstance, config.ExternalSecure)); err != nil {
|
2022-02-14 16:22:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-25 09:16:36 +00:00
|
|
|
if err := authenticatedAPIs.RegisterServer(ctx, admin.CreateServer(commands, queries, adminRepo, assets.HandlerPrefix, keys.User)); err != nil {
|
2022-02-14 16:22:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-25 09:16:36 +00:00
|
|
|
if err := authenticatedAPIs.RegisterServer(ctx, management.CreateServer(commands, queries, config.SystemDefaults, assets.HandlerPrefix, keys.User, config.ExternalSecure, oidc.HandlerPrefix)); err != nil {
|
2022-04-21 10:37:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-04-25 09:16:36 +00:00
|
|
|
if err := authenticatedAPIs.RegisterServer(ctx, auth.CreateServer(commands, queries, authRepo, config.SystemDefaults, assets.HandlerPrefix, keys.User, config.ExternalSecure)); err != nil {
|
2022-02-14 16:22:30 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-06 06:13:40 +00:00
|
|
|
instanceInterceptor := middleware.InstanceInterceptor(queries, config.HTTP1HostHeader)
|
2022-04-21 10:37:39 +00:00
|
|
|
authenticatedAPIs.RegisterHandler(assets.HandlerPrefix, assets.NewHandler(commands, verifier, config.InternalAuthZ, id.SonyFlakeGenerator, store, queries, instanceInterceptor.Handler))
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
userAgentInterceptor, err := middleware.NewUserAgentHandler(config.UserAgentCookie, keys.UserAgentCookieKey, id.SonyFlakeGenerator, config.ExternalSecure)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
oidcProvider, err := oidc.NewProvider(ctx, config.OIDC, login.DefaultLoggedOutPath, config.ExternalSecure, commands, queries, authRepo, keys.OIDC, keys.OIDCKey, eventstore, dbClient, userAgentInterceptor, instanceInterceptor.Handler)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start oidc provider: %w", err)
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
authenticatedAPIs.RegisterHandler(oidc.HandlerPrefix, oidcProvider.HttpHandler())
|
2022-02-14 16:22:30 +00:00
|
|
|
|
|
|
|
openAPIHandler, err := openapi.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start openapi handler: %w", err)
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
authenticatedAPIs.RegisterHandler(openapi.HandlerPrefix, openAPIHandler)
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2022-04-25 08:01:17 +00:00
|
|
|
c, err := console.Start(config.Console, config.ExternalSecure, oidcProvider.IssuerFromRequest, instanceInterceptor.Handler)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start console: %w", err)
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
authenticatedAPIs.RegisterHandler(console.HandlerPrefix, c)
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2022-04-25 09:16:36 +00:00
|
|
|
l, err := login.CreateLogin(config.Login, commands, queries, authRepo, store, console.HandlerPrefix+"/", op.AuthCallbackURL(oidcProvider), config.ExternalSecure, userAgentInterceptor, op.NewIssuerInterceptor(oidcProvider.IssuerFromRequest).Handler, instanceInterceptor.Handler, keys.User, keys.IDPConfig, keys.CSRFCookieKey)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start login: %w", err)
|
|
|
|
}
|
2022-04-21 10:37:39 +00:00
|
|
|
authenticatedAPIs.RegisterHandler(login.HandlerPrefix, l.Handler())
|
2022-02-14 16:22:30 +00:00
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func listen(ctx context.Context, router *mux.Router, port uint16) error {
|
|
|
|
http2Server := &http2.Server{}
|
|
|
|
http1Server := &http.Server{Handler: h2c.NewHandler(router, http2Server)}
|
|
|
|
lis, err := net.Listen("tcp", fmt.Sprintf(":%d", port))
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("tcp listener on %d failed: %w", port, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
errCh := make(chan error)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
logging.Infof("server is listening on %s", lis.Addr().String())
|
|
|
|
errCh <- http1Server.Serve(lis)
|
|
|
|
}()
|
|
|
|
|
|
|
|
shutdown := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
select {
|
|
|
|
case err := <-errCh:
|
|
|
|
return fmt.Errorf("error starting server: %w", err)
|
|
|
|
case <-shutdown:
|
|
|
|
ctx, cancel := context.WithTimeout(ctx, 5*time.Second)
|
|
|
|
defer cancel()
|
|
|
|
return shutdownServer(ctx, http1Server)
|
|
|
|
case <-ctx.Done():
|
|
|
|
return shutdownServer(ctx, http1Server)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func shutdownServer(ctx context.Context, server *http.Server) error {
|
|
|
|
err := server.Shutdown(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not shutdown gracefully: %w", err)
|
|
|
|
}
|
|
|
|
logging.New().Info("server shutdown gracefully")
|
|
|
|
return nil
|
|
|
|
}
|