2022-09-05 03:06:08 -04:00
/ *
Copyright ( c ) Edgeless Systems GmbH
SPDX - License - Identifier : AGPL - 3.0 - only
* /
2022-06-29 10:13:01 -04:00
package main
import (
"context"
"errors"
"flag"
2024-02-08 09:20:01 -05:00
"fmt"
"log/slog"
"os"
2022-06-29 10:13:01 -04:00
"path/filepath"
"strconv"
"time"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/constants"
"github.com/edgelesssys/constellation/v2/internal/crypto"
"github.com/edgelesssys/constellation/v2/internal/file"
2023-01-12 10:22:47 -05:00
"github.com/edgelesssys/constellation/v2/internal/kms/setup"
2023-03-02 09:08:31 -05:00
"github.com/edgelesssys/constellation/v2/internal/kms/uri"
2022-09-21 07:47:57 -04:00
"github.com/edgelesssys/constellation/v2/internal/logger"
2023-01-11 04:08:57 -05:00
"github.com/edgelesssys/constellation/v2/keyservice/internal/server"
2022-06-29 10:13:01 -04:00
"github.com/spf13/afero"
)
func main ( ) {
2023-01-20 12:51:06 -05:00
port := flag . String ( "port" , strconv . Itoa ( constants . KeyServicePort ) , "Port gRPC server listens on" )
2022-07-29 03:52:47 -04:00
masterSecretPath := flag . String ( "master-secret" , filepath . Join ( constants . ServiceBasePath , constants . ConstellationMasterSecretKey ) , "Path to the Constellation master secret" )
2022-10-18 07:15:54 -04:00
saltPath := flag . String ( "salt" , filepath . Join ( constants . ServiceBasePath , constants . ConstellationSaltKey ) , "Path to the Constellation salt" )
2022-07-01 10:17:06 -04:00
verbosity := flag . Int ( "v" , 0 , logger . CmdLineVerbosityDescription )
2022-06-29 10:13:01 -04:00
flag . Parse ( )
2024-02-08 09:20:01 -05:00
log := logger . NewTextLogger ( logger . VerbosityFromInt ( * verbosity ) )
2022-06-29 10:13:01 -04:00
2024-02-08 09:20:01 -05:00
log . With ( slog . String ( "version" , constants . BinaryVersion ( ) . String ( ) ) ) .
Info ( "Constellation Key Management Service" )
2022-06-29 10:13:01 -04:00
2022-07-29 03:52:47 -04:00
// read master secret and salt
file := file . NewHandler ( afero . NewOsFs ( ) )
masterKey , err := file . Read ( * masterSecretPath )
2022-06-29 10:13:01 -04:00
if err != nil {
2024-02-08 09:20:01 -05:00
log . With ( slog . Any ( "error" , err ) ) . Error ( "Failed to read master secret" )
os . Exit ( 1 )
2022-06-29 10:13:01 -04:00
}
2022-07-29 03:52:47 -04:00
if len ( masterKey ) < crypto . MasterSecretLengthMin {
2024-02-08 09:20:01 -05:00
log . With ( slog . Any ( "error" , errors . New ( "invalid key length" ) ) ) . Error ( fmt . Sprintf ( "Provided master secret is smaller than the required minimum of %d bytes" , crypto . MasterSecretLengthMin ) )
os . Exit ( 1 )
2022-07-29 03:52:47 -04:00
}
salt , err := file . Read ( * saltPath )
if err != nil {
2024-02-08 09:20:01 -05:00
log . With ( slog . Any ( "error" , err ) ) . Error ( "Failed to read salt" )
os . Exit ( 1 )
2022-07-29 03:52:47 -04:00
}
if len ( salt ) < crypto . RNGLengthDefault {
2024-02-08 09:20:01 -05:00
log . With ( slog . Any ( "error" , errors . New ( "invalid salt length" ) ) ) . Error ( fmt . Sprintf ( "Expected salt to be %d bytes, but got %d" , crypto . RNGLengthDefault , len ( salt ) ) )
os . Exit ( 1 )
2022-07-29 03:52:47 -04:00
}
2023-03-02 09:08:31 -05:00
masterSecret := uri . MasterSecret { Key : masterKey , Salt : salt }
2022-06-29 10:13:01 -04:00
2022-07-29 03:52:47 -04:00
// set up Key Management Service
2022-06-29 10:13:01 -04:00
ctx , cancel := context . WithTimeout ( context . Background ( ) , 1 * time . Minute )
defer cancel ( )
2023-03-02 09:08:31 -05:00
conKMS , err := setup . KMS ( ctx , uri . NoStoreURI , masterSecret . EncodeToURI ( ) )
2022-06-29 10:13:01 -04:00
if err != nil {
2024-02-08 09:20:01 -05:00
log . With ( slog . Any ( "error" , err ) ) . Error ( "Failed to setup KMS" )
os . Exit ( 1 )
2022-06-29 10:13:01 -04:00
}
2023-02-08 06:03:54 -05:00
defer conKMS . Close ( )
2022-06-29 10:13:01 -04:00
2024-02-08 09:20:01 -05:00
if err := server . New ( log . WithGroup ( "keyService" ) , conKMS ) . Run ( * port ) ; err != nil {
log . With ( slog . Any ( "error" , err ) ) . Error ( "Failed to run key-service server" )
os . Exit ( 1 )
2022-06-29 10:13:01 -04:00
}
}