Skip to content

Commit 0deba12

Browse files
committed
multi: move challenger and secrets files to new packages
Moves challenger and secrets files to new packages so they can be imported elsewhere.
1 parent b952e4e commit 0deba12

File tree

10 files changed

+146
-78
lines changed

10 files changed

+146
-78
lines changed

aperture.go

Lines changed: 14 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -18,8 +18,10 @@ 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"
2426
"github.com/lightningnetwork/lnd"
2527
"github.com/lightningnetwork/lnd/build"
@@ -42,14 +44,6 @@ import (
4244
)
4345

4446
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-
5347
// selfSignedCertOrganization is the static string that we encode in the
5448
// organization field of a certificate if we create it ourselves.
5549
selfSignedCertOrganization = "aperture autogenerated cert"
@@ -160,7 +154,7 @@ type Aperture struct {
160154
cfg *Config
161155

162156
etcdClient *clientv3.Client
163-
challenger *LndChallenger
157+
challenger *challenger.LndChallenger
164158
httpsServer *http.Server
165159
torHTTPServer *http.Server
166160
proxy *proxy.Proxy
@@ -229,8 +223,15 @@ func (a *Aperture) Start(errChan chan error) error {
229223
}
230224

231225
if !a.cfg.Authenticator.Disable {
232-
a.challenger, err = NewLndChallenger(
233-
a.cfg.Authenticator, genInvoiceReq, errChan,
226+
challengerAuth := &challenger.AuthConfig{
227+
LndHost: a.cfg.Authenticator.LndHost,
228+
TLSPath: a.cfg.Authenticator.TLSPath,
229+
MacDir: a.cfg.Authenticator.MacDir,
230+
Network: a.cfg.Authenticator.Network,
231+
Disable: a.cfg.Authenticator.Disable,
232+
}
233+
a.challenger, err = challenger.NewLndChallenger(
234+
challengerAuth, genInvoiceReq, errChan,
234235
)
235236
if err != nil {
236237
return err
@@ -655,12 +656,12 @@ func initTorListener(cfg *Config, etcd *clientv3.Client) (*tor.Controller, error
655656
}
656657

657658
// createProxy creates the proxy with all the services it needs.
658-
func createProxy(cfg *Config, challenger *LndChallenger,
659+
func createProxy(cfg *Config, challenger *challenger.LndChallenger,
659660
etcdClient *clientv3.Client) (*proxy.Proxy, func(), error) {
660661

661662
minter := mint.New(&mint.Config{
662663
Challenger: challenger,
663-
Secrets: newSecretStore(etcdClient),
664+
Secrets: secrets.NewStore(etcdClient),
664665
ServiceLimiter: newStaticServiceLimiter(cfg.Services),
665666
})
666667
authenticator := auth.NewLsatAuthenticator(minter, challenger)

challenger.go renamed to challenger/challenger.go

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

33
import (
44
"context"
@@ -55,15 +55,28 @@ type LndChallenger struct {
5555
wg sync.WaitGroup
5656
}
5757

58+
type AuthConfig struct {
59+
// LndHost is the hostname of the LND instance to connect to.
60+
LndHost string `long:"lndhost" description:"Hostname of the LND instance to connect to"`
61+
62+
TLSPath string `long:"tlspath" description:"Path to LND instance's tls certificate"`
63+
64+
MacDir string `long:"macdir" description:"Directory containing LND instance's macaroons"`
65+
66+
Network string `long:"network" description:"The network LND is connected to." choice:"regtest" choice:"simnet" choice:"testnet" choice:"mainnet"`
67+
68+
Disable bool `long:"disable" description:"Whether to disable LND auth."`
69+
}
70+
5871
// A compile time flag to ensure the LndChallenger satisfies the
5972
// mint.Challenger and auth.InvoiceChecker interface.
6073
var _ mint.Challenger = (*LndChallenger)(nil)
6174
var _ auth.InvoiceChecker = (*LndChallenger)(nil)
6275

6376
const (
64-
// invoiceMacaroonName is the name of the invoice macaroon belonging
77+
// InvoiceMacaroonName is the name of the invoice macaroon belonging
6578
// to the target lnd node.
66-
invoiceMacaroonName = "invoice.macaroon"
79+
InvoiceMacaroonName = "invoice.macaroon"
6780
)
6881

6982
// NewLndChallenger creates a new challenger that uses the given connection
@@ -77,7 +90,7 @@ func NewLndChallenger(cfg *AuthConfig, genInvoiceReq InvoiceRequestGenerator,
7790

7891
client, err := lndclient.NewBasicClient(
7992
cfg.LndHost, cfg.TLSPath, cfg.MacDir, cfg.Network,
80-
lndclient.MacFilename(invoiceMacaroonName),
93+
lndclient.MacFilename(InvoiceMacaroonName),
8194
)
8295
if err != nil {
8396
return nil, err

challenger_test.go renamed to challenger/challenger_test.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
package aperture
1+
package challenger
22

33
import (
44
"context"

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: 4 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,9 @@ 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(
32+
root, challenger.Subsystem, intercept, challenger.UseLogger,
33+
)
3034
lnd.AddSubLogger(root, lsat.Subsystem, intercept, lsat.UseLogger)
3135
lnd.AddSubLogger(root, proxy.Subsystem, intercept, proxy.UseLogger)
3236
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+
// NewStore instantiates a new LSAT secrets store backed by an etcd
4454
// cluster.
45-
func newSecretStore(client *clientv3.Client) *secretStore {
55+
func NewStore(client *clientv3.Client) *secretStore {
4656
return &secretStore{Client: client}
4757
}
4858

secrets_test.go renamed to secrets/secrets_test.go

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

33
import (
44
"bytes"
55
"context"
66
"crypto/sha256"
7-
"net/url"
8-
"os"
97
"testing"
10-
"time"
118

129
"github.com/lightninglabs/aperture/lsat"
1310
"github.com/lightninglabs/aperture/mint"
14-
clientv3 "go.etcd.io/etcd/client/v3"
15-
"go.etcd.io/etcd/server/v3/embed"
1611
)
1712

18-
// etcdSetup is a helper that instantiates a new etcd cluster along with a
19-
// client connection to it. A cleanup closure is also returned to free any
20-
// allocated resources required by etcd.
21-
func etcdSetup(t *testing.T) (*clientv3.Client, func()) {
22-
t.Helper()
23-
24-
tempDir, err := os.MkdirTemp("", "etcd")
25-
if err != nil {
26-
t.Fatalf("unable to create temp dir: %v", err)
27-
}
28-
29-
cfg := embed.NewConfig()
30-
cfg.Dir = tempDir
31-
cfg.Logger = "zap"
32-
cfg.LCUrls = []url.URL{{Host: "127.0.0.1:9125"}}
33-
cfg.LPUrls = []url.URL{{Host: "127.0.0.1:9126"}}
34-
35-
etcd, err := embed.StartEtcd(cfg)
36-
if err != nil {
37-
os.RemoveAll(tempDir)
38-
t.Fatalf("unable to start etcd: %v", err)
39-
}
40-
41-
select {
42-
case <-etcd.Server.ReadyNotify():
43-
case <-time.After(5 * time.Second):
44-
os.RemoveAll(tempDir)
45-
etcd.Server.Stop() // trigger a shutdown
46-
t.Fatal("server took too long to start")
47-
}
48-
49-
client, err := clientv3.New(clientv3.Config{
50-
Endpoints: []string{cfg.LCUrls[0].Host},
51-
DialTimeout: 5 * time.Second,
52-
})
53-
if err != nil {
54-
t.Fatalf("unable to connect to etcd: %v", err)
55-
}
56-
57-
return client, func() {
58-
etcd.Close()
59-
os.RemoveAll(tempDir)
60-
}
61-
}
62-
6313
// assertSecretExists is a helper to determine if a secret for the given
6414
// identifier exists in the store. If it exists, its value is compared against
6515
// the expected secret.
@@ -87,12 +37,12 @@ func assertSecretExists(t *testing.T, store *secretStore, id [sha256.Size]byte,
8737
// TestSecretStore ensures the different operations of the secretStore behave as
8838
// expected.
8939
func TestSecretStore(t *testing.T) {
90-
etcdClient, serverCleanup := etcdSetup(t)
40+
etcdClient, serverCleanup := EtcdSetup(t)
9141
defer etcdClient.Close()
9242
defer serverCleanup()
9343

9444
ctx := context.Background()
95-
store := newSecretStore(etcdClient)
45+
store := NewStore(etcdClient)
9646

9747
// Create a test ID and ensure a secret doesn't exist for it yet as we
9848
// 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)