-
Notifications
You must be signed in to change notification settings - Fork 47
Implement RawTLS and RawMacaroons parameters in LndServicesConfig #35
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Closed
Closed
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cf291c0
Create RawMacaroons type and functions
jalavosus e3a383c
Refactor LndServicesConfig with RawMacaroons param
jalavosus 448e6be
Implement RawTLS param in LndServicesConfig
jalavosus f270025
gomod
jalavosus 98be17e
Add .idea folder to gitignore
jalavosus 132d19b
Refactor RawMacaroons config parameter to CustomMacaroon
jalavosus bddf7f0
Fix go.mod for PR
jalavosus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -5,6 +5,9 @@ | |
| *.so | ||
| *.dylib | ||
|
|
||
| # IDE/Editor folders | ||
| .idea/ | ||
|
|
||
| # Test binary, built with `go test -c` | ||
| *.test | ||
|
|
||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -2,6 +2,8 @@ package lndclient | |
|
|
||
| import ( | ||
| "context" | ||
| "crypto/tls" | ||
| "crypto/x509" | ||
| "errors" | ||
| "fmt" | ||
| "net" | ||
|
|
@@ -71,16 +73,26 @@ type LndServicesConfig struct { | |
| Network Network | ||
|
|
||
| // MacaroonDir is the directory where all lnd macaroons can be found. | ||
| // Either this or CustomMacaroonPath can be specified but not both. | ||
| // One of this, CustomMacaroonPath, or CustomMacaroon can be specified, | ||
| // but only one. | ||
| MacaroonDir string | ||
|
|
||
| // CustomMacaroonPath is the full path to a custom macaroon file. Either | ||
| // this or MacaroonDir can be specified but not both. | ||
| // CustomMacaroonPath is the full path to a custom macaroon file. | ||
| // One of this, MacaroonDir, or CustomMacaroon can be specified, | ||
| // but only one. | ||
| CustomMacaroonPath string | ||
|
|
||
| // CustomMacaroon contains the raw data of any/all provided lnd macaroons. | ||
| // One of this, MacaroonDir, or CustomMacaroonPath can be specified, | ||
| // but only one. | ||
| CustomMacaroon []byte | ||
|
|
||
| // TLSPath is the path to lnd's TLS certificate file. | ||
| TLSPath string | ||
|
|
||
| // Raw byte data of lnd's TLS certificate file. | ||
| RawTLS []byte | ||
|
|
||
| // CheckVersion is the minimum version the connected lnd node needs to | ||
| // be in order to be compatible. The node will be checked against this | ||
| // when connecting. If no version is supplied, the default minimum | ||
|
|
@@ -150,43 +162,21 @@ func NewLndServices(cfg *LndServicesConfig) (*GrpcLndServices, error) { | |
| // We don't allow setting both the macaroon directory and the custom | ||
| // macaroon path. If both are empty, that's fine, the default behavior | ||
| // is to use lnd's default directory to try to locate the macaroons. | ||
| if cfg.MacaroonDir != "" && cfg.CustomMacaroonPath != "" { | ||
| return nil, fmt.Errorf("must set either MacaroonDir or " + | ||
| if cfg.CustomMacaroon == nil && (cfg.MacaroonDir != "" && cfg.CustomMacaroonPath != "") { | ||
| return nil, fmt.Errorf("if CustomMacaroon is not provided, " + | ||
| "must set either MacaroonDir or " + | ||
| "CustomMacaroonPath but not both") | ||
| } | ||
|
|
||
| // Based on the network, if the macaroon directory isn't set, then | ||
| // we'll use the expected default locations. | ||
| macaroonDir := cfg.MacaroonDir | ||
| if macaroonDir == "" { | ||
| switch cfg.Network { | ||
| case NetworkTestnet: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "testnet", | ||
| ) | ||
|
|
||
| case NetworkMainnet: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "mainnet", | ||
| ) | ||
|
|
||
| case NetworkSimnet: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "simnet", | ||
| ) | ||
|
|
||
| case NetworkRegtest: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "regtest", | ||
| ) | ||
| var ( | ||
| macaroonDir string | ||
| loadMacDirErr error | ||
| ) | ||
|
|
||
| default: | ||
| return nil, fmt.Errorf("unsupported network: %v", | ||
| cfg.Network) | ||
| if cfg.CustomMacaroon == nil { | ||
| macaroonDir, loadMacDirErr = loadMacaroonsFromDirectory(cfg) | ||
| if loadMacDirErr != nil { | ||
| return nil, loadMacDirErr | ||
| } | ||
| } | ||
|
|
||
|
|
@@ -210,12 +200,21 @@ func NewLndServices(cfg *LndServicesConfig) (*GrpcLndServices, error) { | |
| // macaroon. We don't use the pouch yet because if not all subservers | ||
| // are enabled, then not all macaroons might be there and the user would | ||
| // get a more cryptic error message. | ||
| readonlyMac, err := loadMacaroon( | ||
| macaroonDir, defaultReadonlyFilename, cfg.CustomMacaroonPath, | ||
| ) | ||
| if err != nil { | ||
| return nil, err | ||
| var readonlyMac serializedMacaroon | ||
| if cfg.CustomMacaroon == nil { | ||
| var loadMacErr error | ||
|
|
||
| readonlyMac, loadMacErr = loadMacaroon( | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. should move this into the above if |
||
| macaroonDir, defaultReadonlyFilename, cfg.CustomMacaroonPath, | ||
| ) | ||
|
|
||
| if loadMacErr != nil { | ||
| return nil, loadMacErr | ||
| } | ||
| } else { | ||
| readonlyMac = serializeBytesToMacaroon(cfg.CustomMacaroon) | ||
| } | ||
|
|
||
| nodeAlias, nodeKey, version, err := checkLndCompatibility( | ||
| conn, chainParams, readonlyMac, cfg.Network, cfg.CheckVersion, | ||
| ) | ||
|
|
@@ -225,9 +224,9 @@ func NewLndServices(cfg *LndServicesConfig) (*GrpcLndServices, error) { | |
|
|
||
| // Now that we've ensured our macaroon directory is set properly, we | ||
| // can retrieve our full macaroon pouch from the directory. | ||
| macaroons, err := newMacaroonPouch(macaroonDir, cfg.CustomMacaroonPath) | ||
| if err != nil { | ||
| return nil, fmt.Errorf("unable to obtain macaroons: %v", err) | ||
| macaroons, loadMacPouchErr := newMacaroonPouch(macaroonDir, cfg.CustomMacaroonPath, cfg.CustomMacaroon) | ||
| if loadMacPouchErr != nil { | ||
| return nil, fmt.Errorf("unable to obtain macaroons: %v", loadMacPouchErr) | ||
| } | ||
|
|
||
| // With the macaroons loaded and the version checked, we can now create | ||
|
|
@@ -365,6 +364,48 @@ func (s *GrpcLndServices) waitForChainSync(ctx context.Context) error { | |
| return <-update | ||
| } | ||
|
|
||
| // If loading macaroons from a specific directory, | ||
| // loadMacaroonsFromDirectory creates a fully qualified | ||
| // path to the macaroons directory based on the network. | ||
| func loadMacaroonsFromDirectory(cfg *LndServicesConfig) (string, error) { | ||
| // Based on the network, if the macaroon directory isn't set, then | ||
| // we'll use the expected default locations. | ||
| macaroonDir := cfg.MacaroonDir | ||
| if macaroonDir == "" { | ||
| switch cfg.Network { | ||
| case NetworkTestnet: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "testnet", | ||
| ) | ||
|
|
||
| case NetworkMainnet: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "mainnet", | ||
| ) | ||
|
|
||
| case NetworkSimnet: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "simnet", | ||
| ) | ||
|
|
||
| case NetworkRegtest: | ||
| macaroonDir = filepath.Join( | ||
| defaultLndDir, defaultDataDir, | ||
| defaultChainSubDir, "bitcoin", "regtest", | ||
| ) | ||
|
|
||
| default: | ||
| return "", fmt.Errorf("unsupported network: %v", | ||
| cfg.Network) | ||
| } | ||
| } | ||
|
|
||
| return macaroonDir, nil | ||
| } | ||
|
|
||
| // checkLndCompatibility makes sure the connected lnd instance is running on the | ||
| // correct network, has the version RPC implemented, is the correct minimal | ||
| // version and supports all required build tags/subservers. | ||
|
|
@@ -533,17 +574,21 @@ var ( | |
| ) | ||
|
|
||
| func getClientConn(cfg *LndServicesConfig) (*grpc.ClientConn, error) { | ||
| var ( | ||
| creds credentials.TransportCredentials | ||
| loadCredsError error | ||
| ) | ||
|
|
||
| // Load the specified TLS certificate and build transport credentials | ||
| // with it. | ||
| tlsPath := cfg.TLSPath | ||
| if tlsPath == "" { | ||
| tlsPath = defaultTLSCertPath | ||
| switch { | ||
| case cfg.RawTLS != nil: | ||
| creds, loadCredsError = loadRawTls(cfg) | ||
| default: | ||
| creds, loadCredsError = loadTlsFromFile(cfg) | ||
| } | ||
|
|
||
| creds, err := credentials.NewClientTLSFromFile(tlsPath, "") | ||
| if err != nil { | ||
| return nil, err | ||
| if loadCredsError != nil { | ||
| return nil, fmt.Errorf("unable to load tls credentials: %v", | ||
| loadCredsError) | ||
| } | ||
|
|
||
| // Create a dial options array. | ||
|
|
@@ -564,3 +609,37 @@ func getClientConn(cfg *LndServicesConfig) (*grpc.ClientConn, error) { | |
|
|
||
| return conn, nil | ||
| } | ||
|
|
||
| func loadTlsFromFile(cfg *LndServicesConfig) (credentials.TransportCredentials, error) { | ||
| // Load the specified TLS certificate and build transport credentials | ||
| // with it. | ||
| tlsPath := cfg.TLSPath | ||
| if tlsPath == "" { | ||
| tlsPath = defaultTLSCertPath | ||
| } | ||
|
|
||
| creds, err := credentials.NewClientTLSFromFile(tlsPath, "") | ||
| if err != nil { | ||
| return nil, err | ||
| } | ||
|
|
||
| return creds, nil | ||
| } | ||
|
|
||
| func loadRawTls(cfg *LndServicesConfig) (credentials.TransportCredentials, error) { | ||
| tlsBytes := cfg.RawTLS | ||
|
|
||
| certPool := x509.NewCertPool() | ||
|
|
||
| if !certPool.AppendCertsFromPEM(tlsBytes) { | ||
| return nil, fmt.Errorf("could not append raw tls cert to " + | ||
| "x509 certpool") | ||
| } | ||
|
|
||
| creds := credentials.NewTLS(&tls.Config{ | ||
| InsecureSkipVerify: false, | ||
| RootCAs: certPool, | ||
| }) | ||
|
|
||
| return creds, nil | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
style nit: reverse this check, check non-nil path instead