-
Notifications
You must be signed in to change notification settings - Fork 18
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
feat!: construct Processor with Config and Builder #454
Conversation
7f8f64a
to
7eef535
Compare
99f8fd2
to
435edf1
Compare
@@ -118,6 +118,19 @@ impl TryFrom<(String, String)> for Authorizer { | |||
} | |||
} | |||
|
|||
// A SessionManager can be converted to a string using JSON and then encoded with base58. | |||
// To load the SessionManager from a string, use `SessionManager::from_str`. | |||
impl FromStr for SessionManager { |
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.
Maybe we should rename SessionManager to a better name, Session Manager is not a Menager, it's actualy a wrapper of generated key and it's session.
DelegationToken ? DelegatedKey? and can be abbr. as DTOKEN / DKEY
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.
How about SessionKeypair
? Better to do it in a separated PR.
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.
It's wrap of key, not wrap of session. The field session is used as a proof of the sk.
So, SessionizeKey
if you want to undeline it can be verified by session. (I dont think it's a good idea, but it's ok)
Or, Delegatedkey
if you want to underline how and what it's desined for.
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.
DelegatedKey
sounds more understandable. 😆
@@ -132,31 +169,6 @@ impl Processor { | |||
} | |||
|
|||
impl Processor { | |||
/// Generate Signature for Authorization | |||
pub fn generate_signature(secret_key: &SecretKey) -> String { |
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.
This change is related to issue #447.
The implementation incorrect, but we should not remove it for now, it may cause security issues.
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.
maybe simply sign with DKEY/DTOKEN(SessionManager)
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.
Agree that. Fixing now.
/// Dump session_manager to string, allowing user to save it in a config file. | ||
/// It can be restored using `SessionManager::from_str`. | ||
pub fn dump(&self) -> Result<String> { | ||
let s = serde_json::to_string(&self).map_err(|_| Error::SerializeError)?; |
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.
Why not bincode? It will be encoded into b58 finally, so readable
is not necessary property.
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.
Readable would be better for debug. You could simply decode b58 to see the content, because it's serialized json string after decoding.
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.
Ok, I agree
|
||
let key = get_value(args.ecdsa_key, c.ecdsa_key); |
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.
"Accept a secret key directly, generated delegatedKey, then drop it."
-- I don't think the origin impl is that bad. It's fine if the sk is not holding by Client.
-- Just use & drop.
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.
We can support both way for Client initialization, by sk directly, and dumped DKEY(SessionManager)
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 generation procedure is in init command.
Here is just a behavior that is easily misunderstood, it replace the key of config with the command line arguments, so that I removed this behavior.
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.
Get!
@@ -83,11 +85,10 @@ pub struct Client { | |||
impl Client { | |||
/// Creat a new client instance. | |||
pub fn new_client( | |||
session_manager: SessionManager, | |||
stuns: String, | |||
config: String, |
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.
what is this config.....
ProcessorConfig ?
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.
Sure it's ProcessorConfig
.
The original Config
was moved to native module and should only be used in native mode.
The Client need indeed enhancement. Here I just temporarily make it work.
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.
Ok
node/src/browser/client.rs
Outdated
let measure = PeriodicMeasure::new(ms); | ||
|
||
let processor = Arc::new( | ||
ProcessorBuilder::from_config(config)? |
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.
How to generate the Config?
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.
Ok, I got it from unittest
let config = serde_yaml::to_string(&ProcessorConfig {
ice_servers: "stun://stun.l.google.com:19302".to_string(),
external_address: None,
session_manager: sm.dump().unwrap(),
stabilize_timeout: 200,
})
- How to generate session manager (DKEY).
- I think JSON is better than YAML here, for browser case. We can support both YAML and JSON.
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.
- Support serialized config is great, but I think It's also necessary to support Config Struct.
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.
- To generate the SessionManager, use the exported SessionManagerBuilder.
- JSON is also available for YAML loader.
- I'm trying to make the method as less as possible.
core/src/swarm.rs
Outdated
pub fn measure(mut self, implement: MeasureImpl) -> Self { | ||
self.measure = Some(implement); | ||
pub fn measure(mut self, implement: Option<MeasureImpl>) -> Self { | ||
self.measure = implement; |
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.
Avoid use Option as input param of function unless there is no other way to impl it.
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.
BTW, you should add document here, we should add document for any new or modified code
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.
Agree to avoid using Option as input param (especially in build pattern). Fixing now.
@RyanKung Two commits are appended. PR description updated. |
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.
Looks great
Please check if the PR fulfills these requirements
🔵 What kind of change does this PR introduce? (Bug fix, feature, docs update, ...)
In order to reduce the differences in
Processor
initialization between native and browser, this PR providesProcessorBuilder
andProcessorConfig
.ProcessorConfig
should be a yaml string from user.And
ProcessorBuilder::from_config
will accept that string to initializeProcessorBuilder
.Then the programming context may set storage/measure/message_callback by its respective methods.
Finally use
ProcessorBuilder::build
method to construct aProcessor
.This PR also defined session_manager serialization method
SessionManager::dump
for saving it in ProcessorConfig.This PR also fixed the unsafe signature of rpcserver endpoint by using
SessionManager
to sign each request. See also #447.🟤 What is the current behavior? (You can also link to an open issue here)
User use variant ways to construct Swarm and Stabilization then make them
into
Processor in tests, rings bin and browser Client.🟢 What is the new behavior (if this is a feature change)?
Use ProcessorConfig for configuration, use ProcessorBuilder for runtime setup.
☢️ Does this PR introduce a breaking change? (What changes might users need to make in their application due to this PR?)
Yes, it changed the initialization parameters of browser Client.
ℹ️ Other information
Closes #447