2022-08-08 21:50:12 +00:00
package commands
import (
2022-12-22 00:21:29 +00:00
"errors"
2022-08-08 21:50:12 +00:00
"fmt"
2022-12-22 00:21:29 +00:00
"io"
2022-10-17 10:51:59 +00:00
"os"
2022-12-22 06:34:20 +00:00
"path/filepath"
"strings"
2022-11-25 12:44:55 +00:00
"syscall"
2022-10-20 20:41:46 +00:00
2022-12-22 06:34:20 +00:00
"github.com/spf13/cobra"
2022-10-20 20:41:46 +00:00
"github.com/spf13/pflag"
2022-11-25 12:44:55 +00:00
"golang.org/x/term"
2022-10-20 20:41:46 +00:00
2022-12-22 06:34:20 +00:00
"github.com/authelia/authelia/v4/internal/configuration"
2023-01-07 00:19:41 +00:00
"github.com/authelia/authelia/v4/internal/random"
2022-08-08 21:50:12 +00:00
)
2022-10-05 05:05:23 +00:00
func recoverErr ( i any ) error {
2022-08-08 21:50:12 +00:00
switch v := i . ( type ) {
case nil :
return nil
case string :
return fmt . Errorf ( "recovered panic: %s" , v )
case error :
return fmt . Errorf ( "recovered panic: %w" , v )
default :
return fmt . Errorf ( "recovered panic with unknown type: %v" , v )
}
}
2022-10-17 10:51:59 +00:00
2022-12-22 00:21:29 +00:00
func flagsGetUserIdentifiersGenerateOptions ( flags * pflag . FlagSet ) ( users , services , sectors [ ] string , err error ) {
if users , err = flags . GetStringSlice ( cmdFlagNameUsers ) ; err != nil {
return nil , nil , nil , err
}
if services , err = flags . GetStringSlice ( cmdFlagNameServices ) ; err != nil {
return nil , nil , nil , err
}
if sectors , err = flags . GetStringSlice ( cmdFlagNameSectors ) ; err != nil {
return nil , nil , nil , err
}
return users , services , sectors , nil
}
2022-11-04 00:32:49 +00:00
//nolint:gocyclo
2022-10-20 20:41:46 +00:00
func flagsGetRandomCharacters ( flags * pflag . FlagSet , flagNameLength , flagNameCharSet , flagNameCharacters string ) ( r string , err error ) {
var (
n int
charset string
)
if n , err = flags . GetInt ( flagNameLength ) ; err != nil {
return "" , err
}
if n < 1 {
return "" , fmt . Errorf ( "flag --%s with value '%d' is invalid: must be at least 1" , flagNameLength , n )
}
useCharSet , useCharacters := flags . Changed ( flagNameCharSet ) , flags . Changed ( flagNameCharacters )
if useCharSet && useCharacters {
return "" , fmt . Errorf ( "flag --%s and flag --%s are mutually exclusive, only one may be used" , flagNameCharSet , flagNameCharacters )
}
switch {
case useCharSet , ! useCharSet && ! useCharacters :
var c string
if c , err = flags . GetString ( flagNameCharSet ) ; err != nil {
return "" , err
}
switch c {
case "ascii" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetASCII
2022-10-20 20:41:46 +00:00
case "alphanumeric" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphaNumeric
2022-11-04 00:32:49 +00:00
case "alphanumeric-lower" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphabeticLower + random . CharSetNumeric
2022-11-04 00:32:49 +00:00
case "alphanumeric-upper" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphabeticUpper + random . CharSetNumeric
2022-10-20 20:41:46 +00:00
case "alphabetic" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphabetic
2022-11-04 00:32:49 +00:00
case "alphabetic-lower" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphabeticLower
2022-11-04 00:32:49 +00:00
case "alphabetic-upper" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphabeticUpper
2022-10-20 20:41:46 +00:00
case "numeric-hex" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetNumericHex
2022-10-20 20:41:46 +00:00
case "numeric" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetNumeric
2022-11-04 00:32:49 +00:00
case "rfc3986" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetRFC3986Unreserved
2022-11-04 00:32:49 +00:00
case "rfc3986-lower" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphabeticLower + random . CharSetNumeric + random . CharSetSymbolicRFC3986Unreserved
2022-11-04 00:32:49 +00:00
case "rfc3986-upper" :
2023-01-07 00:19:41 +00:00
charset = random . CharSetAlphabeticUpper + random . CharSetNumeric + random . CharSetSymbolicRFC3986Unreserved
2022-10-20 20:41:46 +00:00
default :
2022-11-04 00:32:49 +00:00
return "" , fmt . Errorf ( "flag '--%s' with value '%s' is invalid, must be one of 'ascii', 'alphanumeric', 'alphabetic', 'numeric', 'numeric-hex', or 'rfc3986'" , flagNameCharSet , c )
2022-10-20 20:41:46 +00:00
}
case useCharacters :
if charset , err = flags . GetString ( flagNameCharacters ) ; err != nil {
return "" , err
}
}
2023-01-07 00:19:41 +00:00
rand := & random . Cryptographical { }
return rand . StringCustom ( n , charset ) , nil
2022-10-20 20:41:46 +00:00
}
2022-11-25 12:44:55 +00:00
2022-12-22 00:21:29 +00:00
func termReadConfirmation ( flags * pflag . FlagSet , name , prompt , confirmation string ) ( confirmed bool , err error ) {
if confirmed , _ = flags . GetBool ( name ) ; confirmed {
return confirmed , nil
}
2022-11-25 12:44:55 +00:00
2022-12-22 00:21:29 +00:00
terminal , fd , state , err := getTerminal ( prompt )
if err != nil {
return false , err
}
defer func ( fd int , oldState * term . State ) {
_ = term . Restore ( fd , oldState )
} ( fd , state )
var input string
if input , err = terminal . ReadLine ( ) ; err != nil {
return false , fmt . Errorf ( "failed to read from the terminal: %w" , err )
}
if input != confirmation {
return false , nil
2022-11-25 12:44:55 +00:00
}
2022-12-22 00:21:29 +00:00
return true , nil
2022-11-25 12:44:55 +00:00
}
2022-12-22 00:21:29 +00:00
func getTerminal ( prompt string ) ( terminal * term . Terminal , fd int , state * term . State , err error ) {
fd = int ( syscall . Stdin ) //nolint:unconvert,nolintlint
2022-11-25 12:44:55 +00:00
2022-12-22 00:21:29 +00:00
if ! term . IsTerminal ( fd ) {
return nil , - 1 , nil , ErrStdinIsNotTerminal
2022-11-25 12:44:55 +00:00
}
2022-12-22 00:21:29 +00:00
var width , height int
2022-11-25 12:44:55 +00:00
2022-12-22 00:21:29 +00:00
if width , height , err = term . GetSize ( int ( syscall . Stdout ) ) ; err != nil { //nolint:unconvert,nolintlint
return nil , - 1 , nil , fmt . Errorf ( "failed to get terminal size: %w" , err )
2022-11-25 12:44:55 +00:00
}
2022-12-22 00:21:29 +00:00
state , err = term . MakeRaw ( fd )
if err != nil {
return nil , - 1 , nil , fmt . Errorf ( "failed to get terminal state: %w" , err )
}
c := struct {
io . Reader
io . Writer
} {
os . Stdin ,
os . Stdout ,
}
terminal = term . NewTerminal ( c , prompt )
if err = terminal . SetSize ( width , height ) ; err != nil {
return nil , - 1 , nil , fmt . Errorf ( "failed to set terminal size: %w" , err )
}
return terminal , fd , state , nil
}
func termReadPasswordWithPrompt ( prompt , flag string ) ( password string , err error ) {
terminal , fd , state , err := getTerminal ( "" )
if err != nil {
if errors . Is ( err , ErrStdinIsNotTerminal ) {
switch len ( flag ) {
case 0 :
return "" , err
case 1 :
return "" , fmt . Errorf ( "you must either use an interactive terminal or use the -%s flag" , flag )
default :
return "" , fmt . Errorf ( "you must either use an interactive terminal or use the --%s flag" , flag )
}
}
return "" , err
}
defer func ( fd int , oldState * term . State ) {
_ = term . Restore ( fd , oldState )
} ( fd , state )
if password , err = terminal . ReadPassword ( prompt ) ; err != nil {
return "" , fmt . Errorf ( "failed to read the input from the terminal: %w" , err )
}
2022-11-25 12:44:55 +00:00
2022-12-22 00:21:29 +00:00
return password , nil
2022-11-25 12:44:55 +00:00
}
2022-12-22 06:34:20 +00:00
type XEnvCLIResult int
const (
XEnvCLIResultCLIExplicit XEnvCLIResult = iota
XEnvCLIResultCLIImplicit
XEnvCLIResultEnvironment
)
func loadXEnvCLIConfigValues ( cmd * cobra . Command ) ( configs [ ] string , filters [ ] configuration . FileFilter , err error ) {
var (
filterNames [ ] string
2023-04-11 10:52:04 +00:00
result XEnvCLIResult
2022-12-22 06:34:20 +00:00
)
2023-04-11 10:52:04 +00:00
if configs , result , err = loadXEnvCLIStringSliceValue ( cmd , cmdFlagEnvNameConfig , cmdFlagNameConfig ) ; err != nil {
2022-12-22 06:34:20 +00:00
return nil , nil , err
}
2023-04-11 10:52:04 +00:00
if configs , err = loadXNormalizedPaths ( configs , result ) ; err != nil {
2022-12-22 06:34:20 +00:00
return nil , nil , err
}
2022-12-23 00:03:50 +00:00
if filterNames , _ , err = loadXEnvCLIStringSliceValue ( cmd , cmdFlagEnvNameConfigFilters , cmdFlagNameConfigExpFilters ) ; err != nil {
2022-12-22 06:34:20 +00:00
return nil , nil , err
}
if filters , err = configuration . NewFileFilters ( filterNames ) ; err != nil {
return nil , nil , fmt . Errorf ( "error occurred loading configuration: flag '--%s' is invalid: %w" , cmdFlagNameConfigExpFilters , err )
}
return
}
2023-04-11 10:52:04 +00:00
func loadXNormalizedPaths ( paths [ ] string , result XEnvCLIResult ) ( [ ] string , error ) {
2022-12-22 06:34:20 +00:00
var (
configs , files , dirs [ ] string
err error
)
var stat os . FileInfo
for _ , path := range paths {
if path , err = filepath . Abs ( path ) ; err != nil {
return nil , fmt . Errorf ( "failed to determine absolute path for '%s': %w" , path , err )
}
switch stat , err = os . Stat ( path ) ; {
case err == nil && stat . IsDir ( ) :
configs = append ( configs , path )
dirs = append ( dirs , path )
case err == nil :
configs = append ( configs , path )
files = append ( files , path )
default :
if os . IsNotExist ( err ) {
2023-04-11 10:52:04 +00:00
switch result {
case XEnvCLIResultCLIImplicit :
continue
default :
configs = append ( configs , path )
files = append ( files , path )
continue
}
2022-12-22 06:34:20 +00:00
}
return nil , fmt . Errorf ( "error occurred stating file at path '%s': %w" , path , err )
}
}
for i , file := range files {
if file , err = filepath . Abs ( file ) ; err != nil {
return nil , fmt . Errorf ( "failed to determine absolute path for '%s': %w" , files [ i ] , err )
}
if len ( dirs ) != 0 {
filedir := filepath . Dir ( file )
for _ , dir := range dirs {
if filedir == dir {
return nil , fmt . Errorf ( "failed to load config directory '%s': the config file '%s' is in that directory which is not supported" , dir , file )
}
}
}
}
return configs , nil
}
func loadXEnvCLIStringSliceValue ( cmd * cobra . Command , envKey , flagName string ) ( value [ ] string , result XEnvCLIResult , err error ) {
if cmd . Flags ( ) . Changed ( flagName ) {
value , err = cmd . Flags ( ) . GetStringSlice ( flagName )
return value , XEnvCLIResultCLIExplicit , err
}
var (
env string
ok bool
)
if envKey != "" {
env , ok = os . LookupEnv ( envKey )
}
switch {
case ok && env != "" :
return strings . Split ( env , "," ) , XEnvCLIResultEnvironment , nil
default :
value , err = cmd . Flags ( ) . GetStringSlice ( flagName )
return value , XEnvCLIResultCLIImplicit , err
}
}
func newHelpTopic ( topic , short , body string ) ( cmd * cobra . Command ) {
cmd = & cobra . Command {
Use : topic ,
Short : short ,
}
cmd . SetHelpFunc ( func ( cmd * cobra . Command , args [ ] string ) {
_ = cmd . Parent ( ) . Help ( )
fmt . Println ( )
fmt . Printf ( "Help Topic: %s\n\n" , topic )
fmt . Print ( body )
fmt . Print ( "\n\n" )
} )
return cmd
}