Skip to content

Commit c43f4a8

Browse files
committed
multi: move challenger and secrets files to new packages so they can be imported elsewhere.
This commit: - Moves challenger and secrets files to new packages so they can be imported elsewhere. - Alters NewLndChallenger so that it takes the lnd client in as a parameter. (This is useful in the case of using NewLndChallenger in the lnd watchtower project, to avoid having to import lndclient, which leads to` an import cycle.)
1 parent b952e4e commit c43f4a8

File tree

10 files changed

+135
-38
lines changed

10 files changed

+135
-38
lines changed

aperture.go

Lines changed: 17 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,9 +18,12 @@ import (
1818
gateway "github.com/grpc-ecosystem/grpc-gateway/v2/runtime"
1919
flags "github.com/jessevdk/go-flags"
2020
"github.com/lightninglabs/aperture/auth"
21+
"github.com/lightninglabs/aperture/challenger"
2122
"github.com/lightninglabs/aperture/mint"
2223
"github.com/lightninglabs/aperture/proxy"
24+
"github.com/lightninglabs/aperture/secrets"
2325
"github.com/lightninglabs/lightning-node-connect/hashmailrpc"
26+
"github.com/lightninglabs/lndclient"
2427
"github.com/lightningnetwork/lnd"
2528
"github.com/lightningnetwork/lnd/build"
2629
"github.com/lightningnetwork/lnd/cert"
@@ -42,14 +45,6 @@ import (
4245
)
4346

4447
const (
45-
// topLevelKey is the top level key for an etcd cluster where we'll
46-
// store all LSAT proxy related data.
47-
topLevelKey = "lsat/proxy"
48-
49-
// etcdKeyDelimeter is the delimeter we'll use for all etcd keys to
50-
// represent a path-like structure.
51-
etcdKeyDelimeter = "/"
52-
5348
// selfSignedCertOrganization is the static string that we encode in the
5449
// organization field of a certificate if we create it ourselves.
5550
selfSignedCertOrganization = "aperture autogenerated cert"
@@ -160,7 +155,7 @@ type Aperture struct {
160155
cfg *Config
161156

162157
etcdClient *clientv3.Client
163-
challenger *LndChallenger
158+
challenger *challenger.LndChallenger
164159
httpsServer *http.Server
165160
torHTTPServer *http.Server
166161
proxy *proxy.Proxy
@@ -229,8 +224,17 @@ func (a *Aperture) Start(errChan chan error) error {
229224
}
230225

231226
if !a.cfg.Authenticator.Disable {
232-
a.challenger, err = NewLndChallenger(
233-
a.cfg.Authenticator, genInvoiceReq, errChan,
227+
client, err := lndclient.NewBasicClient(
228+
a.cfg.Authenticator.LndHost, a.cfg.Authenticator.TLSPath,
229+
a.cfg.Authenticator.MacDir, a.cfg.Authenticator.Network,
230+
lndclient.MacFilename(challenger.InvoiceMacaroonName),
231+
)
232+
if err != nil {
233+
return err
234+
}
235+
236+
a.challenger, err = challenger.NewLndChallenger(
237+
genInvoiceReq, errChan, client,
234238
)
235239
if err != nil {
236240
return err
@@ -655,12 +659,12 @@ func initTorListener(cfg *Config, etcd *clientv3.Client) (*tor.Controller, error
655659
}
656660

657661
// createProxy creates the proxy with all the services it needs.
658-
func createProxy(cfg *Config, challenger *LndChallenger,
662+
func createProxy(cfg *Config, challenger *challenger.LndChallenger,
659663
etcdClient *clientv3.Client) (*proxy.Proxy, func(), error) {
660664

661665
minter := mint.New(&mint.Config{
662666
Challenger: challenger,
663-
Secrets: newSecretStore(etcdClient),
667+
Secrets: secrets.NewSecretStore(etcdClient),
664668
ServiceLimiter: newStaticServiceLimiter(cfg.Services),
665669
})
666670
authenticator := auth.NewLsatAuthenticator(minter, challenger)

challenger.go renamed to challenger/challenger.go

Lines changed: 4 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package aperture
1+
package challenger
22

33
import (
44
"context"
@@ -11,7 +11,6 @@ import (
1111

1212
"github.com/lightninglabs/aperture/auth"
1313
"github.com/lightninglabs/aperture/mint"
14-
"github.com/lightninglabs/lndclient"
1514
"github.com/lightningnetwork/lnd/lnrpc"
1615
"github.com/lightningnetwork/lnd/lntypes"
1716
"google.golang.org/grpc"
@@ -63,26 +62,18 @@ var _ auth.InvoiceChecker = (*LndChallenger)(nil)
6362
const (
6463
// invoiceMacaroonName is the name of the invoice macaroon belonging
6564
// to the target lnd node.
66-
invoiceMacaroonName = "invoice.macaroon"
65+
InvoiceMacaroonName = "invoice.macaroon"
6766
)
6867

6968
// NewLndChallenger creates a new challenger that uses the given connection
7069
// details to connect to an lnd backend to create payment challenges.
71-
func NewLndChallenger(cfg *AuthConfig, genInvoiceReq InvoiceRequestGenerator,
72-
errChan chan<- error) (*LndChallenger, error) {
70+
func NewLndChallenger(genInvoiceReq InvoiceRequestGenerator,
71+
errChan chan<- error, client InvoiceClient) (*LndChallenger, error) {
7372

7473
if genInvoiceReq == nil {
7574
return nil, fmt.Errorf("genInvoiceReq cannot be nil")
7675
}
7776

78-
client, err := lndclient.NewBasicClient(
79-
cfg.LndHost, cfg.TLSPath, cfg.MacDir, cfg.Network,
80-
lndclient.MacFilename(invoiceMacaroonName),
81-
)
82-
if err != nil {
83-
return nil, err
84-
}
85-
8677
invoicesMtx := &sync.Mutex{}
8778
return &LndChallenger{
8879
client: client,

challenger_test.go renamed to challenger/challenger_test.go

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package aperture
1+
package challenger
22

33
import (
44
"context"
@@ -130,7 +130,7 @@ func TestLndChallenger(t *testing.T) {
130130
// First of all, test that the NewLndChallenger doesn't allow a nil
131131
// invoice generator function.
132132
errChan := make(chan error)
133-
_, err := NewLndChallenger(nil, nil, errChan)
133+
_, err := NewLndChallenger(nil, errChan, nil)
134134
require.Error(t, err)
135135

136136
// Now mock the lnd backend and create a challenger instance that we can

challenger/log.go

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
package challenger
2+
3+
import (
4+
"github.com/btcsuite/btclog"
5+
"github.com/lightningnetwork/lnd/build"
6+
)
7+
8+
const Subsystem = "CHAL"
9+
10+
// log is a logger that is initialized with no output filters. This
11+
// means the package will not perform any logging by default until the caller
12+
// requests it.
13+
var log btclog.Logger
14+
15+
// The default amount of logging is none.
16+
func init() {
17+
UseLogger(build.NewSubLogger(Subsystem, nil))
18+
}
19+
20+
// DisableLog disables all library log output. Logging output is disabled
21+
// by default until UseLogger is called.
22+
func DisableLog() {
23+
UseLogger(btclog.Disabled)
24+
}
25+
26+
// UseLogger uses a specified Logger to output package logging info.
27+
// This should be used in preference to SetLogWriter if the caller is also
28+
// using btclog.
29+
func UseLogger(logger btclog.Logger) {
30+
log = logger
31+
}

log.go

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package aperture
33
import (
44
"github.com/btcsuite/btclog"
55
"github.com/lightninglabs/aperture/auth"
6+
"github.com/lightninglabs/aperture/challenger"
67
"github.com/lightninglabs/aperture/lsat"
78
"github.com/lightninglabs/aperture/proxy"
89
"github.com/lightninglabs/lndclient"
@@ -27,6 +28,7 @@ func SetupLoggers(root *build.RotatingLogWriter, intercept signal.Interceptor) {
2728

2829
lnd.SetSubLogger(root, Subsystem, log)
2930
lnd.AddSubLogger(root, auth.Subsystem, intercept, auth.UseLogger)
31+
lnd.AddSubLogger(root, challenger.Subsystem, intercept, challenger.UseLogger)
3032
lnd.AddSubLogger(root, lsat.Subsystem, intercept, lsat.UseLogger)
3133
lnd.AddSubLogger(root, proxy.Subsystem, intercept, proxy.UseLogger)
3234
lnd.AddSubLogger(root, "LNDC", intercept, lndclient.UseLogger)

onion_store.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"context"
55
"strings"
66

7+
"github.com/lightninglabs/aperture/secrets"
78
"github.com/lightningnetwork/lnd/tor"
89
clientv3 "go.etcd.io/etcd/client/v3"
910
)
@@ -20,7 +21,7 @@ const (
2021

2122
// onionPath is the full path to an onion service's private key.
2223
var onionPath = strings.Join(
23-
[]string{topLevelKey, onionDir, onionV3Dir}, etcdKeyDelimeter,
24+
[]string{secrets.TopLevelKey, onionDir, onionV3Dir}, secrets.EtcdKeyDelimeter,
2425
)
2526

2627
// onionStore is an etcd-based implementation of tor.OnionStore.

onion_store_test.go

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import (
44
"bytes"
55
"testing"
66

7+
"github.com/lightninglabs/aperture/secrets"
78
"github.com/lightningnetwork/lnd/tor"
89
)
910

@@ -35,7 +36,7 @@ func assertPrivateKeyExists(t *testing.T, store *onionStore,
3536
// TestOnionStore ensures the different operations of the onionStore behave
3637
// as expected.
3738
func TestOnionStore(t *testing.T) {
38-
etcdClient, serverCleanup := etcdSetup(t)
39+
etcdClient, serverCleanup := secrets.EtcdSetup(t)
3940
defer etcdClient.Close()
4041
defer serverCleanup()
4142

secrets.go renamed to secrets/secrets.go

Lines changed: 15 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package aperture
1+
package secrets
22

33
import (
44
"context"
@@ -13,6 +13,16 @@ import (
1313
clientv3 "go.etcd.io/etcd/client/v3"
1414
)
1515

16+
const (
17+
// TopLevelKey is the top level key for an etcd cluster where we'll
18+
// store all LSAT proxy related data.
19+
TopLevelKey = "lsat/proxy"
20+
21+
// EtcdKeyDelimeter is the delimeter we'll use for all etcd keys to
22+
// represent a path-like structure.
23+
EtcdKeyDelimeter = "/"
24+
)
25+
1626
var (
1727
// secretsPrefix is the key we'll use to prefix all LSAT identifiers
1828
// with when storing secrets in an etcd cluster.
@@ -27,8 +37,8 @@ var (
2737
// lsat/proxy/secrets/bff4ee83
2838
func idKey(id [sha256.Size]byte) string {
2939
return strings.Join(
30-
[]string{topLevelKey, secretsPrefix, hex.EncodeToString(id[:])},
31-
etcdKeyDelimeter,
40+
[]string{TopLevelKey, secretsPrefix, hex.EncodeToString(id[:])},
41+
EtcdKeyDelimeter,
3242
)
3343
}
3444

@@ -40,9 +50,9 @@ type secretStore struct {
4050
// A compile-time constraint to ensure secretStore implements mint.SecretStore.
4151
var _ mint.SecretStore = (*secretStore)(nil)
4252

43-
// newSecretStore instantiates a new LSAT secrets store backed by an etcd
53+
// NewSecretStore instantiates a new LSAT secrets store backed by an etcd
4454
// cluster.
45-
func newSecretStore(client *clientv3.Client) *secretStore {
55+
func NewSecretStore(client *clientv3.Client) *secretStore {
4656
return &secretStore{Client: client}
4757
}
4858

secrets_test.go renamed to secrets/secrets_test.go

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package aperture
1+
package secrets
22

33
import (
44
"bytes"
@@ -87,12 +87,12 @@ func assertSecretExists(t *testing.T, store *secretStore, id [sha256.Size]byte,
8787
// TestSecretStore ensures the different operations of the secretStore behave as
8888
// expected.
8989
func TestSecretStore(t *testing.T) {
90-
etcdClient, serverCleanup := etcdSetup(t)
90+
etcdClient, serverCleanup := EtcdSetup(t)
9191
defer etcdClient.Close()
9292
defer serverCleanup()
9393

9494
ctx := context.Background()
95-
store := newSecretStore(etcdClient)
95+
store := NewSecretStore(etcdClient)
9696

9797
// Create a test ID and ensure a secret doesn't exist for it yet as we
9898
// haven't created one.

secrets/test_util.go

Lines changed: 57 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,57 @@
1+
package secrets
2+
3+
import (
4+
"io/ioutil"
5+
"net/url"
6+
"os"
7+
"testing"
8+
"time"
9+
10+
clientv3 "go.etcd.io/etcd/client/v3"
11+
"go.etcd.io/etcd/server/v3/embed"
12+
)
13+
14+
// EtcdSetup is a helper that instantiates a new etcd cluster along with a
15+
// client connection to it. A cleanup closure is also returned to free any
16+
// allocated resources required by etcd.
17+
func EtcdSetup(t *testing.T) (*clientv3.Client, func()) {
18+
t.Helper()
19+
20+
tempDir, err := ioutil.TempDir("", "etcd")
21+
if err != nil {
22+
t.Fatalf("unable to create temp dir: %v", err)
23+
}
24+
25+
cfg := embed.NewConfig()
26+
cfg.Dir = tempDir
27+
cfg.Logger = "zap"
28+
cfg.LCUrls = []url.URL{{Host: "127.0.0.1:9125"}}
29+
cfg.LPUrls = []url.URL{{Host: "127.0.0.1:9126"}}
30+
31+
etcd, err := embed.StartEtcd(cfg)
32+
if err != nil {
33+
os.RemoveAll(tempDir)
34+
t.Fatalf("unable to start etcd: %v", err)
35+
}
36+
37+
select {
38+
case <-etcd.Server.ReadyNotify():
39+
case <-time.After(5 * time.Second):
40+
os.RemoveAll(tempDir)
41+
etcd.Server.Stop() // trigger a shutdown
42+
t.Fatal("server took too long to start")
43+
}
44+
45+
client, err := clientv3.New(clientv3.Config{
46+
Endpoints: []string{cfg.LCUrls[0].Host},
47+
DialTimeout: 5 * time.Second,
48+
})
49+
if err != nil {
50+
t.Fatalf("unable to connect to etcd: %v", err)
51+
}
52+
53+
return client, func() {
54+
etcd.Close()
55+
os.RemoveAll(tempDir)
56+
}
57+
}

0 commit comments

Comments
 (0)