2023-02-10 03:00:14 +00:00
package cmd
import (
2023-02-11 02:29:08 +00:00
"errors"
2023-02-10 03:00:14 +00:00
"fmt"
"github.com/rs/zerolog/log"
"github.com/urfave/cli/v2"
2023-02-11 02:29:08 +00:00
"codeberg.org/codeberg/pages/server/cache"
"codeberg.org/codeberg/pages/server/certificates"
2023-02-10 03:00:14 +00:00
"codeberg.org/codeberg/pages/server/database"
)
2023-02-11 02:29:08 +00:00
var ErrAcmeMissConfig = errors . New ( "ACME client has wrong config" )
2023-02-10 03:00:14 +00:00
func openCertDB ( ctx * cli . Context ) ( certDB database . CertDB , closeFn func ( ) , err error ) {
2023-02-11 02:04:57 +00:00
certDB , err = database . NewXormDB ( ctx . String ( "db-type" ) , ctx . String ( "db-conn" ) )
if err != nil {
return nil , nil , fmt . Errorf ( "could not connect to database: %w" , err )
2023-02-10 03:00:14 +00:00
}
closeFn = func ( ) {
if err := certDB . Close ( ) ; err != nil {
log . Error ( ) . Err ( err )
}
}
return certDB , closeFn , nil
}
2023-02-11 02:29:08 +00:00
func createAcmeClient ( ctx * cli . Context , enableHTTPServer bool , challengeCache cache . SetGetKey ) ( * certificates . AcmeClient , error ) {
acmeAPI := ctx . String ( "acme-api-endpoint" )
acmeMail := ctx . String ( "acme-email" )
acmeEabHmac := ctx . String ( "acme-eab-hmac" )
acmeEabKID := ctx . String ( "acme-eab-kid" )
acmeAcceptTerms := ctx . Bool ( "acme-accept-terms" )
dnsProvider := ctx . String ( "dns-provider" )
acmeUseRateLimits := ctx . Bool ( "acme-use-rate-limits" )
acmeAccountConf := ctx . String ( "acme-account-config" )
// check config
if ( ! acmeAcceptTerms || dnsProvider == "" ) && acmeAPI != "https://acme.mock.directory" {
return nil , fmt . Errorf ( "%w: you must set $ACME_ACCEPT_TERMS and $DNS_PROVIDER, unless $ACME_API is set to https://acme.mock.directory" , ErrAcmeMissConfig )
}
2023-02-13 20:14:45 +00:00
if acmeEabHmac != "" && acmeEabKID == "" {
return nil , fmt . Errorf ( "%w: ACME_EAB_HMAC also needs ACME_EAB_KID to be set" , ErrAcmeMissConfig )
} else if acmeEabHmac == "" && acmeEabKID != "" {
return nil , fmt . Errorf ( "%w: ACME_EAB_KID also needs ACME_EAB_HMAC to be set" , ErrAcmeMissConfig )
}
2023-02-11 02:29:08 +00:00
return certificates . NewAcmeClient (
acmeAccountConf ,
acmeAPI ,
acmeMail ,
acmeEabHmac ,
acmeEabKID ,
dnsProvider ,
acmeAcceptTerms ,
enableHTTPServer ,
acmeUseRateLimits ,
challengeCache ,
)
}