2022-05-08 17:15:40 +00:00
|
|
|
/*
|
2024-06-08 08:29:35 +00:00
|
|
|
Copyright CAcert Inc.
|
2022-05-08 17:15:40 +00:00
|
|
|
SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
|
|
|
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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
// The CAcert board voting software.
|
|
|
|
package main
|
|
|
|
|
2022-05-09 19:09:24 +00:00
|
|
|
import (
|
2022-05-22 09:02:37 +00:00
|
|
|
"crypto/tls"
|
|
|
|
"crypto/x509"
|
2022-05-15 18:10:49 +00:00
|
|
|
"database/sql"
|
2022-05-09 19:09:24 +00:00
|
|
|
"flag"
|
2022-05-15 18:10:49 +00:00
|
|
|
"fmt"
|
2022-10-15 17:58:58 +00:00
|
|
|
"io"
|
2022-05-09 19:09:24 +00:00
|
|
|
"log"
|
|
|
|
"net/http"
|
2022-05-26 13:27:25 +00:00
|
|
|
"net/url"
|
2022-05-09 19:09:24 +00:00
|
|
|
"os"
|
2022-05-26 13:27:25 +00:00
|
|
|
"strings"
|
2022-05-22 19:15:54 +00:00
|
|
|
"time"
|
2022-05-09 19:09:24 +00:00
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
"github.com/alexedwards/scs/sqlite3store"
|
|
|
|
"github.com/alexedwards/scs/v2"
|
2022-05-15 18:10:49 +00:00
|
|
|
"github.com/jmoiron/sqlx"
|
|
|
|
_ "github.com/mattn/go-sqlite3"
|
2022-05-09 19:09:24 +00:00
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
u "git.cacert.org/cacert-boardvoting/internal/app"
|
|
|
|
|
2022-05-15 18:10:49 +00:00
|
|
|
"git.cacert.org/cacert-boardvoting/internal"
|
2022-05-09 19:09:24 +00:00
|
|
|
)
|
2022-05-08 17:15:40 +00:00
|
|
|
|
2022-05-22 19:47:27 +00:00
|
|
|
const sessionHours = 12
|
|
|
|
|
2022-05-08 17:15:40 +00:00
|
|
|
var (
|
|
|
|
version = "undefined"
|
|
|
|
commit = "undefined"
|
|
|
|
date = "undefined"
|
|
|
|
)
|
|
|
|
|
|
|
|
func main() {
|
2022-05-09 19:09:24 +00:00
|
|
|
configFile := flag.String("config", "config.yaml", "Configuration file name")
|
|
|
|
flag.Parse()
|
|
|
|
|
|
|
|
infoLog := log.New(os.Stdout, "INFO\t", log.Ldate|log.Ltime)
|
|
|
|
errorLog := log.New(os.Stderr, "ERROR\t", log.Ldate|log.Ltime)
|
|
|
|
|
|
|
|
infoLog.Printf("CAcert Board Voting version %s, commit %s built at %s", version, commit, date)
|
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
config, err := parseConfig(*configFile)
|
2022-05-15 18:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
errorLog.Fatal(err)
|
|
|
|
}
|
2022-05-09 19:09:24 +00:00
|
|
|
|
2022-05-15 18:10:49 +00:00
|
|
|
db, err := openDB(config.DatabaseFile)
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Fatal(err)
|
2022-05-09 19:09:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
defer func(db io.Closer) {
|
2022-05-15 18:10:49 +00:00
|
|
|
_ = db.Close()
|
|
|
|
}(db)
|
2022-05-09 19:09:24 +00:00
|
|
|
|
2022-05-15 18:10:49 +00:00
|
|
|
if err != nil {
|
|
|
|
errorLog.Fatalf("could not setup decision model: %v", err)
|
|
|
|
}
|
2022-05-09 19:09:24 +00:00
|
|
|
|
2022-05-22 19:15:54 +00:00
|
|
|
sessionManager := scs.New()
|
|
|
|
sessionManager.Store = sqlite3store.New(db.DB)
|
2022-05-22 19:47:27 +00:00
|
|
|
sessionManager.Lifetime = sessionHours * time.Hour
|
2022-05-26 14:27:44 +00:00
|
|
|
sessionManager.Cookie.SameSite = http.SameSiteStrictMode
|
|
|
|
sessionManager.Cookie.Secure = true
|
2022-05-22 19:15:54 +00:00
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
application, err := u.New(errorLog, infoLog, db, config.MailConfig, sessionManager)
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Fatalf("could not setup application: %v", err)
|
2022-05-09 19:09:24 +00:00
|
|
|
}
|
2022-05-21 12:09:19 +00:00
|
|
|
|
2022-05-26 13:27:25 +00:00
|
|
|
err = internal.InitializeDb(db.DB, infoLog)
|
|
|
|
if err != nil {
|
|
|
|
errorLog.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
defer func(application io.Closer) {
|
|
|
|
_ = application.Close()
|
|
|
|
}(application)
|
2022-05-29 13:36:27 +00:00
|
|
|
|
2022-05-22 09:02:37 +00:00
|
|
|
infoLog.Printf("Starting server on %s", config.HTTPAddress)
|
|
|
|
|
|
|
|
errChan := make(chan error, 1)
|
|
|
|
|
2022-05-22 13:00:50 +00:00
|
|
|
infoLog.Printf("TLS config setup, starting TLS server on %s", config.HTTPSAddress)
|
2022-05-22 09:02:37 +00:00
|
|
|
|
2022-05-22 13:00:50 +00:00
|
|
|
go setupHTTPRedirect(config, errChan)
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
err = startHTTPSServer(config, errorLog, application.Routes(), func() { _ = application.Close() })
|
2022-05-22 09:02:37 +00:00
|
|
|
if err != nil {
|
2022-05-22 13:00:50 +00:00
|
|
|
errorLog.Fatalf("ListenAndServeTLS (HTTPS) failed: %v", err)
|
2022-05-22 09:02:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 13:00:50 +00:00
|
|
|
if err := <-errChan; err != nil {
|
|
|
|
errorLog.Fatalf("ListenAndServe (HTTP) failed: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
func startHTTPSServer(config *Config, errorLog *log.Logger, routes http.Handler, shutdownFunc func()) error {
|
2022-05-22 13:00:50 +00:00
|
|
|
tlsConfig, err := setupTLSConfig(config)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("could not setup TLS configuration: %w", err)
|
|
|
|
}
|
2022-05-22 09:02:37 +00:00
|
|
|
|
2022-05-09 19:09:24 +00:00
|
|
|
srv := &http.Server{
|
2022-05-22 09:02:37 +00:00
|
|
|
Addr: config.HTTPSAddress,
|
|
|
|
TLSConfig: tlsConfig,
|
2022-10-15 17:58:58 +00:00
|
|
|
ErrorLog: errorLog,
|
|
|
|
Handler: routes,
|
2022-05-15 18:10:49 +00:00
|
|
|
IdleTimeout: config.Timeouts.Idle,
|
|
|
|
ReadHeaderTimeout: config.Timeouts.ReadHeader,
|
|
|
|
ReadTimeout: config.Timeouts.Read,
|
|
|
|
WriteTimeout: config.Timeouts.Write,
|
2022-05-09 19:09:24 +00:00
|
|
|
}
|
|
|
|
|
2022-10-15 17:58:58 +00:00
|
|
|
srv.RegisterOnShutdown(shutdownFunc)
|
|
|
|
|
2022-05-22 09:02:37 +00:00
|
|
|
err = srv.ListenAndServeTLS(config.ServerCert, config.ServerKey)
|
|
|
|
if err != nil {
|
2022-05-22 13:00:50 +00:00
|
|
|
return fmt.Errorf("")
|
2022-05-22 09:02:37 +00:00
|
|
|
}
|
|
|
|
|
2022-05-22 13:00:50 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func setupHTTPRedirect(config *Config, errChan chan error) {
|
|
|
|
redirect := &http.Server{
|
2022-05-26 13:27:25 +00:00
|
|
|
Addr: config.HTTPAddress,
|
|
|
|
Handler: http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
2022-05-26 14:47:57 +00:00
|
|
|
redirectURL := url.URL{
|
2022-05-26 13:27:25 +00:00
|
|
|
Scheme: "https://",
|
|
|
|
Host: strings.Join(
|
|
|
|
[]string{
|
|
|
|
strings.Split(r.URL.Host, ":")[0],
|
|
|
|
strings.Split(config.HTTPSAddress, ":")[1],
|
|
|
|
},
|
|
|
|
":",
|
|
|
|
),
|
|
|
|
Path: r.URL.Path,
|
|
|
|
}
|
|
|
|
|
2022-05-26 14:47:57 +00:00
|
|
|
http.Redirect(w, r, redirectURL.String(), http.StatusMovedPermanently)
|
2022-05-26 13:27:25 +00:00
|
|
|
}),
|
2022-05-22 13:00:50 +00:00
|
|
|
IdleTimeout: config.Timeouts.Idle,
|
|
|
|
ReadHeaderTimeout: config.Timeouts.ReadHeader,
|
|
|
|
ReadTimeout: config.Timeouts.Read,
|
|
|
|
WriteTimeout: config.Timeouts.Write,
|
2022-05-22 09:02:37 +00:00
|
|
|
}
|
2022-05-22 13:00:50 +00:00
|
|
|
|
|
|
|
if err := redirect.ListenAndServe(); err != nil {
|
|
|
|
errChan <- err
|
|
|
|
}
|
|
|
|
|
|
|
|
close(errChan)
|
2022-05-22 09:02:37 +00:00
|
|
|
}
|
2022-05-09 19:09:24 +00:00
|
|
|
|
2022-05-22 09:02:37 +00:00
|
|
|
func setupTLSConfig(config *Config) (*tls.Config, error) {
|
2022-09-26 09:58:36 +00:00
|
|
|
caCert, err := os.ReadFile(config.ClientCACertificates)
|
2022-05-22 09:02:37 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not read client certificate CAs %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
caCertPool := x509.NewCertPool()
|
|
|
|
if !caCertPool.AppendCertsFromPEM(caCert) {
|
|
|
|
return nil, fmt.Errorf(
|
|
|
|
"could not initialize client CA certificate pool from %s",
|
|
|
|
config.ClientCACertificates,
|
|
|
|
)
|
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
|
2022-05-22 09:02:37 +00:00
|
|
|
return &tls.Config{
|
|
|
|
MinVersion: tls.VersionTLS12,
|
|
|
|
ClientCAs: caCertPool,
|
|
|
|
ClientAuth: tls.VerifyClientCertIfGiven,
|
|
|
|
}, nil
|
2022-05-08 17:15:40 +00:00
|
|
|
}
|
2022-05-15 18:10:49 +00:00
|
|
|
|
|
|
|
func openDB(dbFile string) (*sqlx.DB, error) {
|
|
|
|
db, err := sql.Open("sqlite3", dbFile)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not open database file %s: %w", dbFile, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = db.Ping(); err != nil {
|
|
|
|
return nil, fmt.Errorf("could not ping database: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return sqlx.NewDb(db, "sqlite3"), nil
|
|
|
|
}
|