-
Notifications
You must be signed in to change notification settings - Fork 454
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
Add ability to pass a connection protector for private networking #210
Merged
Merged
Changes from all commits
Commits
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 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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 |
---|---|---|
@@ -0,0 +1,90 @@ | ||
/* eslint-env mocha */ | ||
'use strict' | ||
|
||
const chai = require('chai') | ||
chai.use(require('dirty-chai')) | ||
const expect = chai.expect | ||
const PeerInfo = require('peer-info') | ||
const PeerId = require('peer-id') | ||
const waterfall = require('async/waterfall') | ||
const WS = require('libp2p-websockets') | ||
const defaultsDeep = require('@nodeutils/defaults-deep') | ||
|
||
const Libp2p = require('../src') | ||
|
||
describe('private network', () => { | ||
let config | ||
|
||
before((done) => { | ||
waterfall([ | ||
(cb) => PeerId.create({ bits: 512 }, cb), | ||
(peerId, cb) => PeerInfo.create(peerId, cb), | ||
(peerInfo, cb) => { | ||
config = { | ||
peerInfo, | ||
modules: { | ||
transport: [ WS ] | ||
} | ||
} | ||
cb() | ||
} | ||
], () => done()) | ||
}) | ||
|
||
describe('enforced network protection', () => { | ||
before(() => { | ||
process.env.LIBP2P_FORCE_PNET = 1 | ||
}) | ||
|
||
after(() => { | ||
delete process.env.LIBP2P_FORCE_PNET | ||
}) | ||
|
||
it('should throw an error without a provided protector', () => { | ||
expect(() => { | ||
return new Libp2p(config) | ||
}).to.throw('Private network is enforced, but no protector was provided') | ||
}) | ||
|
||
it('should create a libp2p node with a provided protector', () => { | ||
let node | ||
let protector = { | ||
psk: '123', | ||
tag: '/psk/1.0.0', | ||
protect: () => { } | ||
} | ||
|
||
expect(() => { | ||
let options = defaultsDeep(config, { | ||
modules: { | ||
connProtector: protector | ||
} | ||
}) | ||
|
||
node = new Libp2p(options) | ||
return node | ||
}).to.not.throw() | ||
expect(node._switch.protector).to.deep.equal(protector) | ||
}) | ||
|
||
it('should throw an error if the protector does not have a protect method', () => { | ||
expect(() => { | ||
let options = defaultsDeep(config, { | ||
modules: { | ||
connProtector: { } | ||
} | ||
}) | ||
|
||
return new Libp2p(options) | ||
}).to.throw() | ||
}) | ||
}) | ||
|
||
describe('network protection not enforced', () => { | ||
it('should not throw an error with no provided protector', () => { | ||
expect(() => { | ||
return new Libp2p(config) | ||
}).to.not.throw() | ||
}) | ||
}) | ||
}) |
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.
Do we want to have multiple Protectors or just just have it inside and enable it as a config value?
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.
The implementation is based off of go which just supports a single, configurable protector. Users will be able to supply any protector they want, so it's extendable in the future as more protectors are created. In theory we could turn it into a list of protectors but I can't think of a use case where you'd want to do that as multiple levels of encryption on top of secio is really expensive.
If we produce more "intelligent" protectors in the future, those would be easy to swap out.