2022-02-09 14:01:19 +00:00
|
|
|
package start
|
|
|
|
|
|
|
|
import (
|
2022-02-14 16:22:30 +00:00
|
|
|
"context"
|
2022-06-24 12:38:22 +00:00
|
|
|
"crypto/tls"
|
2022-02-09 14:01:19 +00:00
|
|
|
_ "embed"
|
2022-02-14 16:22:30 +00:00
|
|
|
"fmt"
|
2023-05-11 07:24:44 +00:00
|
|
|
"math"
|
2022-02-14 16:22:30 +00:00
|
|
|
"net/http"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
2022-02-09 14:01:19 +00:00
|
|
|
|
2023-02-15 01:52:11 +00:00
|
|
|
clockpkg "github.com/benbjohnson/clock"
|
2023-07-12 17:46:50 +00:00
|
|
|
"github.com/common-nighthawk/go-figure"
|
|
|
|
"github.com/fatih/color"
|
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"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/logging"
|
|
|
|
"github.com/zitadel/oidc/v2/pkg/op"
|
2023-04-19 08:46:02 +00:00
|
|
|
"github.com/zitadel/saml/pkg/provider"
|
2022-02-14 16:22:30 +00:00
|
|
|
"golang.org/x/net/http2"
|
|
|
|
"golang.org/x/net/http2/h2c"
|
|
|
|
|
2023-07-12 17:46:50 +00:00
|
|
|
"github.com/zitadel/zitadel/cmd/build"
|
2022-06-27 10:32:34 +00:00
|
|
|
"github.com/zitadel/zitadel/cmd/key"
|
|
|
|
cmd_tls "github.com/zitadel/zitadel/cmd/tls"
|
2023-02-15 01:52:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/actions"
|
2022-04-26 23:01:45 +00:00
|
|
|
admin_es "github.com/zitadel/zitadel/internal/admin/repository/eventsourcing"
|
|
|
|
"github.com/zitadel/zitadel/internal/api"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/assets"
|
|
|
|
internal_authz "github.com/zitadel/zitadel/internal/api/authz"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/admin"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/auth"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/management"
|
2023-07-10 13:27:00 +00:00
|
|
|
oidc_v2 "github.com/zitadel/zitadel/internal/api/grpc/oidc/v2"
|
2023-08-11 14:19:14 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/org/v2"
|
2023-04-11 13:37:42 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/session/v2"
|
2023-05-11 09:23:40 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/settings/v2"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/system"
|
2023-04-11 13:37:42 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/grpc/user/v2"
|
2022-05-31 11:11:49 +00:00
|
|
|
http_util "github.com/zitadel/zitadel/internal/api/http"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/http/middleware"
|
2023-05-24 18:29:58 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/idp"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/oidc"
|
2023-05-05 08:25:02 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/robots_txt"
|
2023-02-15 01:52:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/saml"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/api/ui/console"
|
|
|
|
"github.com/zitadel/zitadel/internal/api/ui/login"
|
|
|
|
auth_es "github.com/zitadel/zitadel/internal/auth/repository/eventsourcing"
|
|
|
|
"github.com/zitadel/zitadel/internal/authz"
|
|
|
|
authz_repo "github.com/zitadel/zitadel/internal/authz/repository"
|
2023-06-20 16:23:28 +00:00
|
|
|
authz_es "github.com/zitadel/zitadel/internal/authz/repository/eventsourcing/eventstore"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/command"
|
2023-04-24 17:40:31 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/crypto"
|
2022-04-26 23:01:45 +00:00
|
|
|
cryptoDB "github.com/zitadel/zitadel/internal/crypto/database"
|
|
|
|
"github.com/zitadel/zitadel/internal/database"
|
2023-05-05 15:34:53 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/domain"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/eventstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/id"
|
2023-02-15 01:52:11 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/logstore"
|
|
|
|
"github.com/zitadel/zitadel/internal/logstore/emitters/access"
|
|
|
|
"github.com/zitadel/zitadel/internal/logstore/emitters/execution"
|
|
|
|
"github.com/zitadel/zitadel/internal/logstore/emitters/stdout"
|
2022-04-26 23:01:45 +00:00
|
|
|
"github.com/zitadel/zitadel/internal/notification"
|
|
|
|
"github.com/zitadel/zitadel/internal/query"
|
|
|
|
"github.com/zitadel/zitadel/internal/static"
|
|
|
|
"github.com/zitadel/zitadel/internal/webauthn"
|
|
|
|
"github.com/zitadel/zitadel/openapi"
|
2022-02-09 14:01:19 +00:00
|
|
|
)
|
|
|
|
|
2023-04-24 17:40:31 +00:00
|
|
|
func New(server chan<- *Server) *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-06-24 12:38:22 +00:00
|
|
|
err := cmd_tls.ModeFromFlag(cmd)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
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
|
|
|
|
2023-04-24 17:40:31 +00:00
|
|
|
return startZitadel(config, masterKey, server)
|
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
|
|
|
|
}
|
|
|
|
|
2023-04-24 17:40:31 +00:00
|
|
|
type Server struct {
|
|
|
|
Config *Config
|
|
|
|
DB *database.DB
|
|
|
|
KeyStorage crypto.KeyStorage
|
|
|
|
Keys *encryptionKeys
|
|
|
|
Eventstore *eventstore.Eventstore
|
|
|
|
Queries *query.Queries
|
|
|
|
AuthzRepo authz_repo.Repository
|
|
|
|
Storage static.Storage
|
|
|
|
Commands *command.Commands
|
|
|
|
LogStore *logstore.Service
|
|
|
|
Router *mux.Router
|
|
|
|
TLSConfig *tls.Config
|
|
|
|
Shutdown chan<- os.Signal
|
|
|
|
}
|
|
|
|
|
|
|
|
func startZitadel(config *Config, masterKey string, server chan<- *Server) error {
|
2023-07-12 17:46:50 +00:00
|
|
|
showBasicInformation(config)
|
|
|
|
|
2022-02-14 16:22:30 +00:00
|
|
|
ctx := context.Background()
|
|
|
|
|
2022-07-28 14:25:42 +00:00
|
|
|
dbClient, err := database.Connect(config.Database, false)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start client for projection: %w", err)
|
|
|
|
}
|
2022-03-14 06:55:09 +00:00
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
keyStorage, err := cryptoDB.NewKeyStorage(dbClient.DB, masterKey)
|
2022-03-14 06:55:09 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start key storage: %w", err)
|
|
|
|
}
|
|
|
|
keys, err := ensureEncryptionKeys(config.EncryptionKeys, keyStorage)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2022-12-15 09:40:13 +00:00
|
|
|
config.Eventstore.Client = dbClient
|
|
|
|
eventstoreClient, err := eventstore.Start(config.Eventstore)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start eventstore for queries: %w", err)
|
|
|
|
}
|
2022-02-16 15:49:17 +00:00
|
|
|
|
2023-05-05 15:34:53 +00:00
|
|
|
sessionTokenVerifier := internal_authz.SessionTokenVerifier(keys.OIDC)
|
|
|
|
|
|
|
|
queries, err := query.StartQueries(
|
|
|
|
ctx,
|
|
|
|
eventstoreClient,
|
|
|
|
dbClient,
|
|
|
|
config.Projections,
|
|
|
|
config.SystemDefaults,
|
|
|
|
keys.IDPConfig,
|
|
|
|
keys.OTP,
|
|
|
|
keys.OIDC,
|
|
|
|
keys.SAML,
|
|
|
|
config.InternalAuthZ.RolePermissionMappings,
|
|
|
|
sessionTokenVerifier,
|
2023-06-20 16:23:28 +00:00
|
|
|
func(q *query.Queries) domain.PermissionCheck {
|
|
|
|
return func(ctx context.Context, permission, orgID, resourceID string) (err error) {
|
|
|
|
return internal_authz.CheckPermission(ctx, &authz_es.UserMembershipRepo{Queries: q}, config.InternalAuthZ.RolePermissionMappings, permission, orgID, resourceID)
|
|
|
|
}
|
|
|
|
},
|
2023-05-05 15:34:53 +00:00
|
|
|
)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start queries: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-04-28 14:56:51 +00:00
|
|
|
authZRepo, err := authz.Start(queries, dbClient, keys.OIDC, config.ExternalSecure, config.Eventstore.AllowOrderByCreationDate)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error starting authz repo: %w", err)
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
permissionCheck := func(ctx context.Context, permission, orgID, resourceID string) (err error) {
|
|
|
|
return internal_authz.CheckPermission(ctx, authZRepo, config.InternalAuthZ.RolePermissionMappings, permission, orgID, resourceID)
|
|
|
|
}
|
2022-04-06 06:13:40 +00:00
|
|
|
|
2023-02-27 21:36:43 +00:00
|
|
|
storage, err := config.AssetStorage.NewStorage(dbClient.DB)
|
2022-04-06 06:13:40 +00:00
|
|
|
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,
|
|
|
|
webAuthNConfig,
|
2022-04-28 08:30:41 +00:00
|
|
|
config.ExternalDomain,
|
2022-04-25 09:16:36 +00:00
|
|
|
config.ExternalSecure,
|
|
|
|
config.ExternalPort,
|
|
|
|
keys.IDPConfig,
|
|
|
|
keys.OTP,
|
|
|
|
keys.SMTP,
|
|
|
|
keys.SMS,
|
|
|
|
keys.User,
|
|
|
|
keys.DomainVerification,
|
|
|
|
keys.OIDC,
|
2022-09-12 16:18:08 +00:00
|
|
|
keys.SAML,
|
|
|
|
&http.Client{},
|
2023-05-05 15:34:53 +00:00
|
|
|
permissionCheck,
|
|
|
|
sessionTokenVerifier,
|
2023-07-10 13:27:00 +00:00
|
|
|
config.OIDC.DefaultAccessTokenLifetime,
|
|
|
|
config.OIDC.DefaultRefreshTokenExpiration,
|
|
|
|
config.OIDC.DefaultRefreshTokenIdleExpiration,
|
2022-04-25 09:16:36 +00:00
|
|
|
)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("cannot start commands: %w", err)
|
|
|
|
}
|
|
|
|
|
2023-02-15 01:52:11 +00:00
|
|
|
clock := clockpkg.New()
|
|
|
|
actionsExecutionStdoutEmitter, err := logstore.NewEmitter(ctx, clock, config.LogStore.Execution.Stdout, stdout.NewStdoutEmitter())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
actionsExecutionDBEmitter, err := logstore.NewEmitter(ctx, clock, config.LogStore.Execution.Database, execution.NewDatabaseLogStorage(dbClient))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-07-06 06:38:13 +00:00
|
|
|
usageReporter := logstore.UsageReporterFunc(commands.ReportQuotaUsage)
|
2023-02-27 21:36:43 +00:00
|
|
|
actionsLogstoreSvc := logstore.New(queries, usageReporter, actionsExecutionDBEmitter, actionsExecutionStdoutEmitter)
|
2023-02-15 01:52:11 +00:00
|
|
|
actions.SetLogstoreService(actionsLogstoreSvc)
|
|
|
|
|
2023-07-06 17:31:08 +00:00
|
|
|
notification.Start(ctx, config.Projections.Customizations["notifications"], config.Projections.Customizations["notificationsquotas"], config.Projections.Customizations["telemetry"], *config.Telemetry, config.ExternalDomain, config.ExternalPort, config.ExternalSecure, commands, queries, eventstoreClient, assets.AssetAPIFromDomain(config.ExternalSecure, config.ExternalPort), config.SystemDefaults.Notifications.FileSystemPath, keys.User, keys.SMTP, keys.SMS)
|
2022-02-14 16:22:30 +00:00
|
|
|
|
|
|
|
router := mux.NewRouter()
|
2022-06-24 12:38:22 +00:00
|
|
|
tlsConfig, err := config.TLS.Config()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
err = startAPIs(
|
|
|
|
ctx,
|
|
|
|
clock,
|
|
|
|
router,
|
|
|
|
commands,
|
|
|
|
queries,
|
|
|
|
eventstoreClient,
|
|
|
|
dbClient,
|
|
|
|
config,
|
|
|
|
storage,
|
|
|
|
authZRepo,
|
|
|
|
keys,
|
|
|
|
queries,
|
|
|
|
usageReporter,
|
|
|
|
permissionCheck,
|
|
|
|
)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-04-24 17:40:31 +00:00
|
|
|
|
|
|
|
shutdown := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(shutdown, os.Interrupt, syscall.SIGTERM)
|
|
|
|
|
|
|
|
if server != nil {
|
|
|
|
server <- &Server{
|
|
|
|
Config: config,
|
|
|
|
DB: dbClient,
|
|
|
|
KeyStorage: keyStorage,
|
|
|
|
Keys: keys,
|
|
|
|
Eventstore: eventstoreClient,
|
|
|
|
Queries: queries,
|
|
|
|
AuthzRepo: authZRepo,
|
|
|
|
Storage: storage,
|
|
|
|
Commands: commands,
|
|
|
|
LogStore: actionsLogstoreSvc,
|
|
|
|
Router: router,
|
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
Shutdown: shutdown,
|
|
|
|
}
|
|
|
|
close(server)
|
|
|
|
}
|
|
|
|
|
|
|
|
return listen(ctx, router, config.Port, tlsConfig, shutdown)
|
2022-02-14 16:22:30 +00:00
|
|
|
}
|
|
|
|
|
2023-02-15 01:52:11 +00:00
|
|
|
func startAPIs(
|
|
|
|
ctx context.Context,
|
|
|
|
clock clockpkg.Clock,
|
|
|
|
router *mux.Router,
|
|
|
|
commands *command.Commands,
|
|
|
|
queries *query.Queries,
|
|
|
|
eventstore *eventstore.Eventstore,
|
2023-02-27 21:36:43 +00:00
|
|
|
dbClient *database.DB,
|
2023-02-15 01:52:11 +00:00
|
|
|
config *Config,
|
|
|
|
store static.Storage,
|
|
|
|
authZRepo authz_repo.Repository,
|
|
|
|
keys *encryptionKeys,
|
|
|
|
quotaQuerier logstore.QuotaQuerier,
|
|
|
|
usageReporter logstore.UsageReporter,
|
2023-05-05 15:34:53 +00:00
|
|
|
permissionCheck domain.PermissionCheck,
|
2023-02-15 01:52:11 +00:00
|
|
|
) error {
|
2022-02-14 16:22:30 +00:00
|
|
|
repo := struct {
|
|
|
|
authz_repo.Repository
|
|
|
|
*query.Queries
|
|
|
|
}{
|
|
|
|
authZRepo,
|
|
|
|
queries,
|
|
|
|
}
|
2022-06-24 12:38:22 +00:00
|
|
|
verifier := internal_authz.Start(repo, http_util.BuildHTTP(config.ExternalDomain, config.ExternalPort, config.ExternalSecure), config.SystemAPIUsers)
|
|
|
|
tlsConfig, err := config.TLS.Config()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-02-15 01:52:11 +00:00
|
|
|
|
|
|
|
accessStdoutEmitter, err := logstore.NewEmitter(ctx, clock, config.LogStore.Access.Stdout, stdout.NewStdoutEmitter())
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
accessDBEmitter, err := logstore.NewEmitter(ctx, clock, config.LogStore.Access.Database, access.NewDatabaseLogStorage(dbClient))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
accessSvc := logstore.New(quotaQuerier, usageReporter, accessDBEmitter, accessStdoutEmitter)
|
2023-05-11 07:24:44 +00:00
|
|
|
exhaustedCookieHandler := http_util.NewCookieHandler(
|
|
|
|
http_util.WithUnsecure(),
|
|
|
|
http_util.WithNonHttpOnly(),
|
|
|
|
http_util.WithMaxAge(int(math.Floor(config.Quotas.Access.ExhaustedCookieMaxAge.Seconds()))),
|
|
|
|
)
|
2023-05-15 06:51:02 +00:00
|
|
|
limitingAccessInterceptor := middleware.NewAccessInterceptor(accessSvc, exhaustedCookieHandler, config.Quotas.Access)
|
|
|
|
apis, err := api.New(ctx, config.Port, router, queries, verifier, config.InternalAuthZ, tlsConfig, config.HTTP2HostHeader, config.HTTP1HostHeader, limitingAccessInterceptor)
|
2023-04-11 13:37:42 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error creating api %w", err)
|
|
|
|
}
|
2023-04-28 14:56:51 +00:00
|
|
|
authRepo, err := auth_es.Start(ctx, config.Auth, config.SystemDefaults, commands, queries, dbClient, eventstore, keys.OIDC, keys.User, config.Eventstore.AllowOrderByCreationDate)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error starting auth repo: %w", err)
|
|
|
|
}
|
2023-04-28 14:56:51 +00:00
|
|
|
adminRepo, err := admin_es.Start(ctx, config.Admin, store, dbClient, eventstore, config.Eventstore.AllowOrderByCreationDate)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("error starting admin repo: %w", err)
|
|
|
|
}
|
2023-02-27 21:36:43 +00:00
|
|
|
if err := apis.RegisterServer(ctx, system.CreateServer(commands, queries, adminRepo, config.Database.DatabaseName(), config.DefaultInstance, config.ExternalDomain)); err != nil {
|
2022-02-14 16:22:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-03-17 09:14:06 +00:00
|
|
|
if err := apis.RegisterServer(ctx, admin.CreateServer(config.Database.DatabaseName(), commands, queries, config.SystemDefaults, adminRepo, config.ExternalSecure, keys.User, config.AuditLogRetention)); err != nil {
|
2022-02-14 16:22:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-06-07 08:04:51 +00:00
|
|
|
if err := apis.RegisterServer(ctx, management.CreateServer(commands, queries, config.SystemDefaults, keys.User, config.ExternalSecure, config.AuditLogRetention)); err != nil {
|
2022-04-21 10:37:39 +00:00
|
|
|
return err
|
|
|
|
}
|
2022-05-30 11:38:30 +00:00
|
|
|
if err := apis.RegisterServer(ctx, auth.CreateServer(commands, queries, authRepo, config.SystemDefaults, keys.User, config.ExternalSecure, config.AuditLogRetention)); err != nil {
|
2022-02-14 16:22:30 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-24 18:29:58 +00:00
|
|
|
if err := apis.RegisterService(ctx, user.CreateServer(commands, queries, keys.User, keys.IDPConfig, idp.CallbackURL(config.ExternalSecure))); err != nil {
|
2023-04-11 13:37:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-05-05 15:34:53 +00:00
|
|
|
if err := apis.RegisterService(ctx, session.CreateServer(commands, queries, permissionCheck)); err != nil {
|
2023-04-11 13:37:42 +00:00
|
|
|
return err
|
|
|
|
}
|
2023-07-10 13:27:00 +00:00
|
|
|
|
2023-05-11 09:23:40 +00:00
|
|
|
if err := apis.RegisterService(ctx, settings.CreateServer(commands, queries, config.ExternalSecure)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2023-08-11 14:19:14 +00:00
|
|
|
if err := apis.RegisterService(ctx, org.CreateServer(commands, queries, permissionCheck)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-05-02 15:26:54 +00:00
|
|
|
instanceInterceptor := middleware.InstanceInterceptor(queries, config.HTTP1HostHeader, login.IgnoreInstanceEndpoints...)
|
2022-08-16 05:04:36 +00:00
|
|
|
assetsCache := middleware.AssetsCacheInterceptor(config.AssetStorage.Cache.MaxAge, config.AssetStorage.Cache.SharedMaxAge)
|
2023-05-11 07:24:44 +00:00
|
|
|
apis.RegisterHandlerOnPrefix(assets.HandlerPrefix, assets.NewHandler(commands, verifier, config.InternalAuthZ, id.SonyFlakeGenerator(), store, queries, middleware.CallDurationHandler, instanceInterceptor.Handler, assetsCache.Handler, limitingAccessInterceptor.Handle))
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2023-05-24 18:29:58 +00:00
|
|
|
apis.RegisterHandlerOnPrefix(idp.HandlerPrefix, idp.NewHandler(commands, queries, keys.IDPConfig, config.ExternalSecure, instanceInterceptor.Handler))
|
|
|
|
|
feat: Configurable Unique Machine Identification (#3626)
* feat: Configurable Unique Machine Identification
This change fixes Segfault on AWS App Runner with v2 #3625
The change introduces two new dependencies:
* github.com/drone/envsubst for supporting AWS ECS, which has its metadata endpoint described by an environment variable
* github.com/jarcoal/jpath so that only relevant data from a metadata response is used to identify the machine.
The change ads new configuration (see `defaults.yaml`):
* `Machine.Identification` enables configuration of how machines are uniquely identified - I'm not sure about the top level category `Machine`, as I don't have anything else to add to it. Happy to hear suggestions for better naming or structure here.
* `Machine.Identifiation.PrivateId` turns on or off the existing private IP based identification. Default is on.
* `Machine.Identification.Hostname` turns on or off using the OS hostname to identify the machine. Great for most cloud environments, where this tends to be set to something that identifies the machine uniquely. Enabled by default.
* `Machine.Identification.Webhook` configures identification based on the response to an HTTP GET request. Request headers can be configured, a JSONPath can be set for processing the response (no JSON parsing is done if this is not set), and the URL is allowed to contain environment variables in the format `"${var}"`.
The new flow for getting a unique machine id is:
1. PrivateIP (if enabled)
2. Hostname (if enabled)
3. Webhook (if enabled, to configured URL)
4. Give up and error out.
It's important that init configures machine identity first. Otherwise we could try to get an ID before configuring it. To prevent this from causing difficult to debug issues, where for example the default configuration was used, I've ensured that
the application will generate an error if the module hasn't been configured and you try to get an ID.
Misc changes:
* Spelling and gramatical corrections to `init.go::New()` long description.
* Spelling corrections to `verify_zitadel.go::newZitadel()`.
* Updated `production.md` and `development.md` based on the new build process. I think the run instructions are also out of date, but I'll leave that for someone else.
* `id.SonyFlakeGenerator` is now a function, which sets `id.sonyFlakeGenerator`, this allows us to defer initialization until configuration has been read.
* Update internal/id/config.go
Co-authored-by: Alexei-Barnes <82444470+Alexei-Barnes@users.noreply.github.com>
* Fix authored by @livio-a for tests
Co-authored-by: Livio Amstutz <livio.a@gmail.com>
2022-05-24 14:57:57 +00:00
|
|
|
userAgentInterceptor, err := middleware.NewUserAgentHandler(config.UserAgentCookie, keys.UserAgentCookieKey, id.SonyFlakeGenerator(), config.ExternalSecure, login.EndpointResources)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-05-05 08:25:02 +00:00
|
|
|
// robots.txt handler
|
|
|
|
robotsTxtHandler, err := robots_txt.Start()
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start robots txt handler: %w", err)
|
|
|
|
}
|
|
|
|
apis.RegisterHandlerOnPrefix(robots_txt.HandlerPrefix, robotsTxtHandler)
|
|
|
|
|
2023-02-15 01:52:11 +00:00
|
|
|
// TODO: Record openapi access logs?
|
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)
|
|
|
|
}
|
2023-04-11 13:37:42 +00:00
|
|
|
apis.RegisterHandlerOnPrefix(openapi.HandlerPrefix, openAPIHandler)
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2023-05-11 07:24:44 +00:00
|
|
|
oidcProvider, err := oidc.NewProvider(config.OIDC, login.DefaultLoggedOutPath, config.ExternalSecure, commands, queries, authRepo, keys.OIDC, keys.OIDCKey, eventstore, dbClient, userAgentInterceptor, instanceInterceptor.Handler, limitingAccessInterceptor.Handle)
|
2022-06-07 08:04:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start oidc provider: %w", err)
|
|
|
|
}
|
2023-04-11 13:37:42 +00:00
|
|
|
apis.RegisterHandlerPrefixes(oidcProvider.HttpHandler(), "/.well-known/openid-configuration", "/oidc/v1", "/oauth/v2")
|
2022-06-07 08:04:51 +00:00
|
|
|
|
2023-05-11 07:24:44 +00:00
|
|
|
samlProvider, err := saml.NewProvider(config.SAML, config.ExternalSecure, commands, queries, authRepo, keys.OIDC, keys.SAML, eventstore, dbClient, instanceInterceptor.Handler, userAgentInterceptor, limitingAccessInterceptor.Handle)
|
2022-09-12 16:18:08 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start saml provider: %w", err)
|
|
|
|
}
|
2023-04-11 13:37:42 +00:00
|
|
|
apis.RegisterHandlerOnPrefix(saml.HandlerPrefix, samlProvider.HttpHandler())
|
2022-09-12 16:18:08 +00:00
|
|
|
|
2023-05-15 06:51:02 +00:00
|
|
|
c, err := console.Start(config.Console, config.ExternalSecure, oidcProvider.IssuerFromRequest, middleware.CallDurationHandler, instanceInterceptor.Handler, limitingAccessInterceptor, config.CustomerPortal)
|
2022-02-14 16:22:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("unable to start console: %w", err)
|
|
|
|
}
|
2023-04-11 13:37:42 +00:00
|
|
|
apis.RegisterHandlerOnPrefix(console.HandlerPrefix, c)
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2023-05-11 07:24:44 +00:00
|
|
|
l, err := login.CreateLogin(config.Login, commands, queries, authRepo, store, console.HandlerPrefix+"/", op.AuthCallbackURL(oidcProvider), provider.AuthCallbackURL(samlProvider), config.ExternalSecure, userAgentInterceptor, op.NewIssuerInterceptor(oidcProvider.IssuerFromRequest).Handler, provider.NewIssuerInterceptor(samlProvider.IssuerFromRequest).Handler, instanceInterceptor.Handler, assetsCache.Handler, limitingAccessInterceptor.Handle, 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)
|
|
|
|
}
|
2023-04-11 13:37:42 +00:00
|
|
|
apis.RegisterHandlerOnPrefix(login.HandlerPrefix, l.Handler())
|
2023-04-19 08:46:02 +00:00
|
|
|
apis.HandleFunc(login.EndpointDeviceAuth, login.RedirectDeviceAuthToPrefix)
|
2022-02-14 16:22:30 +00:00
|
|
|
|
2023-07-10 13:27:00 +00:00
|
|
|
// After OIDC provider so that the callback endpoint can be used
|
|
|
|
if err := apis.RegisterService(ctx, oidc_v2.CreateServer(commands, queries, oidcProvider, config.ExternalSecure)); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2023-04-11 13:37:42 +00:00
|
|
|
// handle grpc at last to be able to handle the root, because grpc and gateway require a lot of different prefixes
|
|
|
|
apis.RouteGRPC()
|
2022-02-14 16:22:30 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-04-24 17:40:31 +00:00
|
|
|
func listen(ctx context.Context, router *mux.Router, port uint16, tlsConfig *tls.Config, shutdown <-chan os.Signal) error {
|
2022-02-14 16:22:30 +00:00
|
|
|
http2Server := &http2.Server{}
|
2022-06-24 12:38:22 +00:00
|
|
|
http1Server := &http.Server{Handler: h2c.NewHandler(router, http2Server), TLSConfig: tlsConfig}
|
2023-05-02 16:24:24 +00:00
|
|
|
|
2023-05-11 08:58:35 +00:00
|
|
|
lc := listenConfig()
|
2023-05-02 16:24:24 +00:00
|
|
|
lis, err := lc.Listen(ctx, "tcp", fmt.Sprintf(":%d", port))
|
2022-02-14 16:22:30 +00:00
|
|
|
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())
|
2022-06-24 12:38:22 +00:00
|
|
|
if tlsConfig != nil {
|
|
|
|
//we don't need to pass the files here, because we already initialized the TLS config on the server
|
|
|
|
errCh <- http1Server.ServeTLS(lis, "", "")
|
|
|
|
} else {
|
|
|
|
errCh <- http1Server.Serve(lis)
|
|
|
|
}
|
2022-02-14 16:22:30 +00:00
|
|
|
}()
|
|
|
|
|
|
|
|
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
|
|
|
|
}
|
2023-07-12 17:46:50 +00:00
|
|
|
|
|
|
|
func showBasicInformation(startConfig *Config) {
|
2023-07-25 05:12:13 +00:00
|
|
|
fmt.Println(color.MagentaString(figure.NewFigure("ZITADEL", "", true).String()))
|
2023-07-12 17:46:50 +00:00
|
|
|
http := "http"
|
|
|
|
if startConfig.TLS.Enabled || startConfig.ExternalSecure {
|
|
|
|
http = "https"
|
|
|
|
}
|
|
|
|
|
|
|
|
consoleURL := fmt.Sprintf("%s://%s:%v/ui/console\n", http, startConfig.ExternalDomain, startConfig.ExternalPort)
|
|
|
|
healthCheckURL := fmt.Sprintf("%s://%s:%v/debug/healthz\n", http, startConfig.ExternalDomain, startConfig.ExternalPort)
|
|
|
|
|
|
|
|
insecure := !startConfig.TLS.Enabled && !startConfig.ExternalSecure
|
|
|
|
|
|
|
|
fmt.Printf(" ===============================================================\n\n")
|
|
|
|
fmt.Printf(" Version : %s\n", build.Version())
|
|
|
|
fmt.Printf(" TLS enabled : %v\n", startConfig.TLS.Enabled)
|
|
|
|
fmt.Printf(" External Secure : %v\n", startConfig.ExternalSecure)
|
|
|
|
fmt.Printf(" Console URL : %s", color.BlueString(consoleURL))
|
|
|
|
fmt.Printf(" Health Check URL : %s", color.BlueString(healthCheckURL))
|
|
|
|
if insecure {
|
|
|
|
fmt.Printf("\n %s: you're using plain http without TLS. Be aware this is \n", color.RedString("Warning"))
|
|
|
|
fmt.Printf(" not a secure setup and should only be used for test systems. \n")
|
|
|
|
fmt.Printf(" Visit: %s \n", color.CyanString("https://zitadel.com/docs/self-hosting/manage/tls_modes"))
|
|
|
|
}
|
|
|
|
fmt.Printf("\n ===============================================================\n\n")
|
|
|
|
}
|