Skip to content

Commit

Permalink
aperture: initialize server with the configured authenticator
Browse files Browse the repository at this point in the history
  • Loading branch information
positiveblue committed Jun 14, 2023
1 parent 695cdee commit 4b0a926
Show file tree
Hide file tree
Showing 2 changed files with 74 additions and 1 deletion.
72 changes: 71 additions & 1 deletion aperture.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
flags "github.com/jessevdk/go-flags"
"github.com/lightninglabs/aperture/aperturedb"
"github.com/lightninglabs/aperture/auth"
"github.com/lightninglabs/aperture/lnc"
"github.com/lightninglabs/aperture/mint"
"github.com/lightninglabs/aperture/proxy"
"github.com/lightninglabs/lightning-node-connect/hashmailrpc"
Expand Down Expand Up @@ -164,6 +165,7 @@ type Aperture struct {

etcdClient *clientv3.Client
db *sql.DB
lncNodeConn *lnc.NodeConn
challenger *LndChallenger
httpsServer *http.Server
torHTTPServer *http.Server
Expand All @@ -183,6 +185,8 @@ func NewAperture(cfg *Config) *Aperture {
}

// Start sets up the proxy server and starts it.
//
//nolint:gocyclo
func (a *Aperture) Start(errChan chan error) error {
// Start the prometheus exporter.
err := StartPrometheusExporter(a.cfg.Prometheus)
Expand Down Expand Up @@ -215,6 +219,7 @@ func (a *Aperture) Start(errChan chan error) error {
var (
secretStore mint.SecretStore
onionStore tor.OnionStore
lncStore lnc.Store
)

// Connect to the chosen database backend.
Expand Down Expand Up @@ -256,6 +261,13 @@ func (a *Aperture) Start(errChan chan error) error {
)
onionStore = aperturedb.NewOnionStore(dbOnionTxer)

dbLNCTxer := aperturedb.NewTransactionExecutor(db,
func(tx *sql.Tx) aperturedb.LNCSessionsDB {
return db.WithTx(tx)
},
)
lncStore = aperturedb.NewLNCSessionsStore(dbLNCTxer)

case "sqlite":
db, err := aperturedb.NewSqliteStore(a.cfg.Sqlite)
if err != nil {
Expand All @@ -278,6 +290,13 @@ func (a *Aperture) Start(errChan chan error) error {
)
onionStore = aperturedb.NewOnionStore(dbOnionTxer)

dbLNCTxer := aperturedb.NewTransactionExecutor(db,
func(tx *sql.Tx) aperturedb.LNCSessionsDB {
return db.WithTx(tx)
},
)
lncStore = aperturedb.NewLNCSessionsStore(dbLNCTxer)

default:
return fmt.Errorf("unknown database backend: %s",
a.cfg.DatabaseBackend)
Expand All @@ -294,7 +313,50 @@ func (a *Aperture) Start(errChan chan error) error {
}, nil
}

if !a.cfg.Authenticator.Disable {
switch {
case a.cfg.LNC.Enable:
log.Infof("Using lnc's authenticator config")

authCfg := a.cfg.LNC

nodeConn := lnc.NewNodeConn(lncStore)
session, err := lnc.NewSession(
authCfg.Label, authCfg.Passphrase,
authCfg.MailboxAddress, authCfg.DevServer,
)
if err != nil {
return fmt.Errorf("unable to create lnc session: %w",
err)
}

err = nodeConn.OpenConn(session)
if err != nil {
return fmt.Errorf("unable to connect to lnd using "+
"lnc: %w", err)
}

client, err := nodeConn.Client()
if err != nil {
return err
}

a.challenger, err = NewLndChallenger(
client, genInvoiceReq, errChan,
)
if err != nil {
return err
}

err = a.challenger.Start()
if err != nil {
return err
}

a.lncNodeConn = nodeConn

case !a.cfg.Authenticator.Disable:
log.Infof("Using lnd's authenticator config")

authCfg := a.cfg.Authenticator
client, err := lndclient.NewBasicClient(
authCfg.LndHost, authCfg.TLSPath, authCfg.MacDir,
Expand All @@ -312,6 +374,7 @@ func (a *Aperture) Start(errChan chan error) error {
if err != nil {
return err
}

err = a.challenger.Start()
if err != nil {
return err
Expand Down Expand Up @@ -422,6 +485,13 @@ func (a *Aperture) Stop() error {
a.challenger.Stop()
}

if a.lncNodeConn != nil {
if err := a.lncNodeConn.Stop(); err != nil {
log.Errorf("Error closing LNC connection: %v", err)
returnErr = err
}
}

// Stop everything that was started alongside the proxy, for example the
// gRPC and REST servers.
if a.proxyCleanup != nil {
Expand Down
3 changes: 3 additions & 0 deletions hashmail_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -160,6 +160,9 @@ func setupAperture(t *testing.T) {
Authenticator: &AuthConfig{
Disable: true,
},
LNC: &LNC{
Enable: false,
},
DatabaseBackend: "etcd",
Etcd: &EtcdConfig{},
HashMail: &HashMailConfig{
Expand Down

0 comments on commit 4b0a926

Please sign in to comment.