2021-09-11 11:35:15 +00:00
|
|
|
/*
|
2023-05-13 11:27:19 +00:00
|
|
|
Copyright 2020-2023 CAcert Inc.
|
|
|
|
SPDX-License-Identifier: Apache-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
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
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-07-18 18:37:04 +00:00
|
|
|
https://www.apache.org/licenses/LICENSE-2.0
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
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.
|
2021-09-11 11:35:15 +00:00
|
|
|
*/
|
|
|
|
|
|
|
|
package main
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
|
|
|
"encoding/base64"
|
2023-07-29 18:32:02 +00:00
|
|
|
"errors"
|
2021-09-11 11:35:15 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
|
|
|
"net/url"
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync/atomic"
|
|
|
|
"time"
|
|
|
|
|
2023-07-29 19:15:11 +00:00
|
|
|
"code.cacert.org/cacert/oidc-idp/internal/handlers"
|
|
|
|
"code.cacert.org/cacert/oidc-idp/internal/services"
|
|
|
|
"code.cacert.org/cacert/oidc-idp/ui"
|
2021-09-11 11:35:15 +00:00
|
|
|
"github.com/go-openapi/runtime/client"
|
|
|
|
"github.com/gorilla/csrf"
|
2021-09-19 17:54:00 +00:00
|
|
|
"github.com/knadh/koanf"
|
2021-09-11 11:35:15 +00:00
|
|
|
hydra "github.com/ory/hydra-client-go/client"
|
|
|
|
log "github.com/sirupsen/logrus"
|
|
|
|
)
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
const (
|
2023-07-29 18:32:02 +00:00
|
|
|
IdleTimeout = 30 * time.Second
|
|
|
|
ShutdownTimeout = 30 * time.Second
|
|
|
|
ReadTimeOut = 20 * time.Second
|
|
|
|
WriteTimeOut = 20 * time.Second
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
DefaultCSRFMaxAge = 600
|
2023-07-29 18:32:02 +00:00
|
|
|
|
|
|
|
httpsDefaultPort = 443
|
2023-05-13 11:27:19 +00:00
|
|
|
)
|
|
|
|
|
2023-07-24 16:10:28 +00:00
|
|
|
var (
|
|
|
|
version = "local"
|
|
|
|
commit = "unknown"
|
|
|
|
date = "unknown"
|
|
|
|
)
|
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
func main() {
|
|
|
|
logger := log.New()
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2023-07-29 17:47:51 +00:00
|
|
|
config, err := services.ConfigureApplication(logger, "IDP", services.DefaultConfig)
|
2021-09-11 11:35:15 +00:00
|
|
|
if err != nil {
|
2023-07-29 18:43:59 +00:00
|
|
|
logger.WithError(err).Fatal("error loading configuration")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 18:32:02 +00:00
|
|
|
if level := config.String("log.level"); level != "" {
|
|
|
|
logLevel, err := log.ParseLevel(level)
|
|
|
|
if err != nil {
|
|
|
|
logger.WithError(err).Fatal("could not parse log level")
|
|
|
|
}
|
|
|
|
|
|
|
|
logger.SetLevel(logLevel)
|
|
|
|
}
|
|
|
|
|
|
|
|
if config.Bool("log.json") {
|
|
|
|
logger.SetFormatter(&log.JSONFormatter{})
|
|
|
|
}
|
|
|
|
|
2023-07-24 16:10:28 +00:00
|
|
|
logger.WithFields(log.Fields{
|
|
|
|
"version": version, "commit": commit, "date": date,
|
|
|
|
}).Info("Starting CAcert OpenID Connect Identity Provider")
|
2021-09-11 11:35:15 +00:00
|
|
|
logger.Infoln("Server is starting")
|
2023-05-13 11:27:19 +00:00
|
|
|
bundle, catalog := services.InitI18n(logger, config.Strings("i18n.languages"))
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
if err = services.AddMessages(catalog); err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
logger.WithError(err).Fatal("could not add messages for i18n")
|
2023-05-13 11:27:19 +00:00
|
|
|
}
|
2021-09-11 11:35:15 +00:00
|
|
|
|
2023-07-29 18:40:36 +00:00
|
|
|
clientTransport, err := configureAdminClient(config)
|
2021-09-11 11:35:15 +00:00
|
|
|
if err != nil {
|
2023-07-29 18:40:36 +00:00
|
|
|
logger.WithError(err).Fatal("could not configure Hydra admin client")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
adminClient := hydra.New(clientTransport, nil)
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
loginHandler := handlers.NewLoginHandler(logger, bundle, catalog, adminClient.Admin)
|
|
|
|
consentHandler := handlers.NewConsentHandler(logger, bundle, catalog, adminClient.Admin)
|
|
|
|
logoutHandler := handlers.NewLogoutHandler(logger, adminClient.Admin)
|
|
|
|
|
2023-07-24 19:09:35 +00:00
|
|
|
logoutSuccessHandler := handlers.NewLogoutSuccessHandler(logger, bundle, catalog)
|
2023-07-29 19:56:19 +00:00
|
|
|
errorHandler := handlers.NewErrorHandler(logger, bundle, catalog)
|
2022-08-22 16:50:59 +00:00
|
|
|
staticFiles := http.FileServer(http.FS(ui.Static))
|
2021-09-11 11:35:15 +00:00
|
|
|
|
|
|
|
router := http.NewServeMux()
|
|
|
|
router.Handle("/login", loginHandler)
|
|
|
|
router.Handle("/consent", consentHandler)
|
|
|
|
router.Handle("/logout", logoutHandler)
|
|
|
|
router.Handle("/error", errorHandler)
|
|
|
|
router.Handle("/logout-successful", logoutSuccessHandler)
|
|
|
|
router.Handle("/health", handlers.NewHealthHandler())
|
|
|
|
router.Handle("/images/", staticFiles)
|
|
|
|
router.Handle("/css/", staticFiles)
|
|
|
|
router.Handle("/js/", staticFiles)
|
|
|
|
|
|
|
|
csrfKey, err := base64.StdEncoding.DecodeString(config.MustString("security.csrf.key"))
|
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
logger.WithError(err).Fatal("could not parse CSRF key bytes")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
nextRequestID := func() string {
|
2021-09-11 11:35:15 +00:00
|
|
|
return fmt.Sprintf("%d", time.Now().UnixNano())
|
|
|
|
}
|
|
|
|
|
2023-05-13 11:27:19 +00:00
|
|
|
tracing := handlers.Tracing(nextRequestID)
|
2021-09-11 11:35:15 +00:00
|
|
|
logging := handlers.Logging(logger)
|
|
|
|
hsts := handlers.EnableHSTS()
|
|
|
|
csrfProtect := csrf.Protect(
|
|
|
|
csrfKey,
|
|
|
|
csrf.Secure(true),
|
|
|
|
csrf.SameSite(csrf.SameSiteStrictMode),
|
2023-05-13 11:27:19 +00:00
|
|
|
csrf.MaxAge(DefaultCSRFMaxAge))
|
|
|
|
|
2023-07-29 18:32:02 +00:00
|
|
|
errorMiddleware, err := handlers.ErrorHandling(logger, ui.Templates, bundle, catalog)
|
2021-09-11 11:35:15 +00:00
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
logger.WithError(err).Fatal("could not initialize request error handling")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
2021-09-19 17:54:00 +00:00
|
|
|
handlerChain := tracing(logging(hsts(errorMiddleware(csrfProtect(router)))))
|
|
|
|
|
2023-07-29 18:43:59 +00:00
|
|
|
startServer(logger, config, handlerChain)
|
2021-09-19 17:54:00 +00:00
|
|
|
}
|
|
|
|
|
2023-07-29 18:40:36 +00:00
|
|
|
func configureAdminClient(config *koanf.Koanf) (*client.Runtime, error) {
|
|
|
|
adminURL, err := url.Parse(config.MustString("admin.url"))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("error parsing admin URL: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClientConfig := &tls.Config{MinVersion: tls.VersionTLS12}
|
|
|
|
|
|
|
|
if config.Exists("api-client.rootCAs") {
|
|
|
|
rootCAFile := config.MustString("api-client.rootCAs")
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
|
|
|
|
pemBytes, err := os.ReadFile(rootCAFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read CA certificate file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool.AppendCertsFromPEM(pemBytes)
|
|
|
|
tlsClientConfig.RootCAs = caCertPool
|
|
|
|
}
|
|
|
|
|
|
|
|
tlsClientTransport := &http.Transport{TLSClientConfig: tlsClientConfig}
|
|
|
|
httpClient := &http.Client{Transport: tlsClientTransport}
|
|
|
|
clientTransport := client.NewWithClient(
|
|
|
|
adminURL.Host,
|
|
|
|
adminURL.Path,
|
|
|
|
[]string{adminURL.Scheme},
|
|
|
|
httpClient,
|
|
|
|
)
|
|
|
|
|
|
|
|
return clientTransport, nil
|
|
|
|
}
|
|
|
|
|
2023-07-29 18:43:59 +00:00
|
|
|
func startServer(logger *log.Logger, config *koanf.Koanf, handlerChain http.Handler) {
|
2021-09-19 17:54:00 +00:00
|
|
|
clientCertificateCAFile := config.MustString("security.client.ca-file")
|
2023-07-24 19:32:28 +00:00
|
|
|
serverBindAddress := config.String("server.bind_address")
|
2021-09-19 17:54:00 +00:00
|
|
|
serverName := config.String("server.name")
|
|
|
|
serverPort := config.Int("server.port")
|
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
clientCertPool := x509.NewCertPool()
|
2023-05-13 11:27:19 +00:00
|
|
|
|
|
|
|
pemBytes, err := os.ReadFile(clientCertificateCAFile)
|
2021-09-11 11:35:15 +00:00
|
|
|
if err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
logger.WithError(err).Fatal("could not load client CA certificates")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
clientCertPool.AppendCertsFromPEM(pemBytes)
|
|
|
|
|
|
|
|
tlsConfig := &tls.Config{
|
2021-09-19 17:54:00 +00:00
|
|
|
ServerName: serverName,
|
2021-09-11 11:35:15 +00:00
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
ClientAuth: tls.VerifyClientCertIfGiven,
|
|
|
|
ClientCAs: clientCertPool,
|
|
|
|
}
|
|
|
|
server := &http.Server{
|
2023-07-24 19:32:28 +00:00
|
|
|
Addr: fmt.Sprintf("%s:%d", serverBindAddress, serverPort),
|
2021-09-19 17:54:00 +00:00
|
|
|
Handler: handlerChain,
|
2023-07-29 18:32:02 +00:00
|
|
|
ReadTimeout: ReadTimeOut,
|
|
|
|
WriteTimeout: WriteTimeOut,
|
|
|
|
IdleTimeout: IdleTimeout,
|
2021-09-11 11:35:15 +00:00
|
|
|
TLSConfig: tlsConfig,
|
|
|
|
}
|
|
|
|
|
|
|
|
done := make(chan bool)
|
|
|
|
quit := make(chan os.Signal, 1)
|
|
|
|
signal.Notify(quit, os.Interrupt)
|
|
|
|
|
|
|
|
go func() {
|
|
|
|
<-quit
|
|
|
|
logger.Infoln("Server is shutting down...")
|
|
|
|
atomic.StoreInt32(&handlers.Healthy, 0)
|
|
|
|
|
2023-07-29 18:43:59 +00:00
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), ShutdownTimeout)
|
2021-09-11 11:35:15 +00:00
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
server.SetKeepAlivesEnabled(false)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
if err := server.Shutdown(ctx); err != nil {
|
2023-07-29 18:32:02 +00:00
|
|
|
logger.WithError(err).Fatal("Could not gracefully shutdown the server")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
close(done)
|
|
|
|
}()
|
|
|
|
|
2023-07-24 19:32:28 +00:00
|
|
|
logger.WithFields(log.Fields{
|
2023-07-29 18:32:02 +00:00
|
|
|
"address": server.Addr, "url": publicAddress(serverName, serverPort),
|
2023-07-24 19:32:28 +00:00
|
|
|
}).Info("Server is ready to handle requests")
|
2021-09-11 11:35:15 +00:00
|
|
|
atomic.StoreInt32(&handlers.Healthy, 1)
|
2023-05-13 11:27:19 +00:00
|
|
|
|
2021-09-11 11:35:15 +00:00
|
|
|
if err := server.ListenAndServeTLS(
|
|
|
|
config.String("server.certificate"), config.String("server.key"),
|
2023-07-29 18:32:02 +00:00
|
|
|
); err != nil && !errors.Is(err, http.ErrServerClosed) {
|
|
|
|
logger.WithError(err).WithField(
|
|
|
|
"server_addr", server.Addr,
|
|
|
|
).Fatal("Could not listen on configured server address")
|
2021-09-11 11:35:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
<-done
|
|
|
|
logger.Infoln("Server stopped")
|
|
|
|
}
|
2023-07-29 18:32:02 +00:00
|
|
|
|
|
|
|
func publicAddress(serverName string, serverPort int) string {
|
|
|
|
if serverPort != httpsDefaultPort {
|
|
|
|
return fmt.Sprintf("https://%s:%d/", serverName, serverPort)
|
|
|
|
}
|
|
|
|
|
|
|
|
return fmt.Sprintf("https://%s/", serverName)
|
|
|
|
}
|