Skip to content

Commit

Permalink
Updated python bindings and fixed tests.
Browse files Browse the repository at this point in the history
  • Loading branch information
Randy808 committed May 6, 2024
1 parent 8113b2b commit cd1da65
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 46 deletions.
7 changes: 3 additions & 4 deletions libs/gl-client-py/glclient/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -64,11 +64,10 @@ def is_running(self) -> bool:

class Scheduler(object):

def __init__(self, node_id: bytes, network: str, creds: Optional[Credentials] = None):
self.node_id = node_id
def __init__(self, network: str, creds: Optional[Credentials] = None):
self.network = network
self.creds = creds if creds is not None else native.Credentials()
self.inner = native.Scheduler(node_id, network, self.creds)
self.inner = native.Scheduler(network, self.creds)

def schedule(self) -> schedpb.NodeInfoResponse:
res = self.inner.schedule()
Expand Down Expand Up @@ -99,7 +98,7 @@ def node(self) -> "Node":
res = self.inner.node()
info = schedpb.NodeInfoResponse.FromString(bytes(res))
return Node(
node_id=self.node_id,
node_id=self.creds.node_id(),
grpc_uri=info.grpc_uri,
creds=self.creds,
)
Expand Down
3 changes: 2 additions & 1 deletion libs/gl-client-py/glclient/glclient.pyi
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ class Credentials:
def from_path(path: str) -> Credentials: ...
@staticmethod
def from_parts(cert: bytes, key: bytes, ca: bytes, rune: str) -> Credentials: ...
def node_id(self) -> bytes: ...
def upgrade(self, scheduler: Scheduler, signer: Signer) -> Credentials: ...
def to_bytes(self) -> bytes: ...

Expand All @@ -49,7 +50,7 @@ class Signer:


class Scheduler:
def __init__(self, node_id: bytes, network: str, creds: Optional[Credentials]): ...
def __init__(self, network: str, creds: Optional[Credentials]): ...
def register(self, signer: Signer, invite_code: Optional[str]) -> bytes: ...
def recover(self, signer: Signer) -> bytes: ...
def authenticate(self, creds: Credentials): ...
Expand Down
4 changes: 4 additions & 0 deletions libs/gl-client-py/src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -169,6 +169,10 @@ impl Credentials {
pub fn ensure_nobody(&self) -> Result<()> {
self.inner.ensure_nobody()
}

pub fn node_id(&self) -> Result<Vec<u8>> {
Ok(self.inner.node_id()?)
}
}

type Result<T, E = ErrorWrapper> = std::result::Result<T, E>;
Expand Down
2 changes: 1 addition & 1 deletion libs/gl-client-py/tests/fixtures.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ def sclient(signer, creds):
registration and recovery, but no mTLS certificate yet.
"""
network = "regtest"
return Scheduler(signer.node_id(), network=network, creds=creds)
return Scheduler(network=network, creds=creds)


@pytest.fixture
Expand Down
2 changes: 1 addition & 1 deletion libs/gl-client-py/tests/test_creds.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@ def test_upgrade_credentials(scheduler, sclient, signer):

c = Credentials.from_bytes(creds).upgrade(
Scheduler(
node_id=signer.node_id(), network="regtest", creds=screds
network="regtest", creds=screds
).inner,
signer.inner,
)
Expand Down
2 changes: 1 addition & 1 deletion libs/gl-client-py/tests/test_scheduler.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ def test_connect(scheduler, creds):
"""Test that we can connect to the scheduler."""
sig = Signer(b"\x00" * 32, network="regtest", creds=creds)
node_id = sig.node_id()
s = Scheduler(node_id, network="regtest", creds=creds)
s = Scheduler(network="regtest", creds=creds)
with pytest.raises(ValueError):
s.recover(sig)

Expand Down
45 changes: 8 additions & 37 deletions libs/gl-client/src/scheduler.rs
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ where
/// let node_id = vec![0, 1, 2, 3];
/// let network = Network::Regtest;
/// let creds = Nobody::new();
/// let scheduler = Scheduler::new(node_id, network, creds).await.unwrap();
/// let scheduler = Scheduler::new(network, creds).await.unwrap();
/// # }
/// ```
pub async fn new(network: Network, creds: Creds) -> Result<Scheduler<Creds>> {
Expand All @@ -65,7 +65,7 @@ where
/// let network = Network::Regtest;
/// let creds = Nobody::new();
/// let uri = "https://example.com".to_string();
/// let scheduler = Scheduler::with(node_id, network, creds, uri).await.unwrap();
/// let scheduler = Scheduler::with(network, creds, uri).await.unwrap();
/// # }
/// ```
pub async fn with(
Expand Down Expand Up @@ -114,7 +114,7 @@ impl<Creds> Scheduler<Creds> {
/// let node_id = vec![0, 1, 2, 3];
/// let network = Network::Regtest;
/// let creds = Nobody::new();
/// let scheduler = Scheduler::new(node_id.clone(), network, creds.clone()).await.unwrap();
/// let scheduler = Scheduler::new(network, creds.clone()).await.unwrap();
/// let secret = vec![0, 0, 0, 0];
/// let signer = Signer::new(secret, network, creds).unwrap(); // Create or obtain a signer instance
/// let registration_response = scheduler.register(&signer, None).await.unwrap();
Expand Down Expand Up @@ -242,7 +242,7 @@ impl<Creds> Scheduler<Creds> {
/// let node_id = vec![0, 1, 2, 3];
/// let network = Network::Regtest;
/// let creds = Nobody::new();
/// let scheduler = Scheduler::new(node_id.clone(), network, creds.clone()).await.unwrap();
/// let scheduler = Scheduler::new(network, creds.clone()).await.unwrap();
/// let secret = vec![0, 0, 0, 0];
/// let signer = Signer::new(secret, network, creds).unwrap(); // Create or obtain a signer instance
/// let recovery_response = scheduler.recover(&signer).await.unwrap();
Expand Down Expand Up @@ -341,7 +341,7 @@ impl<Creds> Scheduler<Creds> {
/// let node_id = vec![0, 1, 2, 3];
/// let network = Network::Regtest;
/// let creds = Nobody::new();
/// let scheduler_unauthed = Scheduler::new(node_id.clone(), network, creds.clone()).await.unwrap();
/// let scheduler_unauthed = Scheduler::new(network, creds.clone()).await.unwrap();
/// let secret = vec![0, 0, 0, 0];
/// let signer = Signer::new(secret, network, creds).unwrap(); // Create or obtain a signer instance
/// let registration_response = scheduler_unauthed.register(&signer, None).await.unwrap();
Expand Down Expand Up @@ -391,7 +391,7 @@ where
/// let node_id = vec![0, 1, 2, 3];
/// let network = Network::Regtest;
/// let creds = Device::from_path("my/path/to/credentials.glc");
/// let scheduler = Scheduler::new(node_id.clone(), network, creds.clone()).await.unwrap();
/// let scheduler = Scheduler::new(network, creds.clone()).await.unwrap();
/// let info = scheduler.schedule().await.unwrap();
/// let node_client: Client = Node::new(node_id, creds).unwrap().connect(info.grpc_uri).await.unwrap();
/// # }
Expand Down Expand Up @@ -421,7 +421,7 @@ where
/// let node_id = vec![0, 1, 2, 3];
/// let network = Network::Regtest;
/// let creds = Device::from_path("my/path/to/credentials.glc");
/// let scheduler = Scheduler::new(node_id.clone(), network, creds.clone()).await.unwrap();
/// let scheduler = Scheduler::new(network, creds.clone()).await.unwrap();
/// let node_client: Client = scheduler.node().await.unwrap();
/// # }
/// ```
Expand Down Expand Up @@ -518,33 +518,4 @@ where
.await?;
Ok(res.into_inner())
}
}

#[cfg(test)]
mod tests {
use crate::credentials::Device;

use super::*;

#[tokio::test]
async fn test_node_returns_error_on_node_id_mismatch() {
let cert_node_id: [u8; 32] = rand::random();
let device_cert = tls::generate_self_signed_device_cert(
&hex::encode(cert_node_id.clone()),
"default".into(),
vec!["localhost".into()],
);

let device_crt = device_cert.serialize_pem().unwrap();
let device_key = device_cert.serialize_private_key_pem();

let creds = Device::with(device_crt, device_key, String::new(), "");

let scheduler_node_id: [u8; 32] = rand::random();
let sched = Scheduler::new(scheduler_node_id.to_vec(), Network::Bitcoin, creds)
.await
.unwrap();

assert!(sched.node::<node::ClnClient>().await.is_err_and(|e| e.to_string().contains("The node_id defined on the Credential's certificate does not match the node_id the scheduler was initialized with")));
}
}
}
2 changes: 1 addition & 1 deletion libs/gl-testing/gltesting/clients.py
Original file line number Diff line number Diff line change
Expand Up @@ -100,7 +100,7 @@ def creds(self) -> glclient.Credentials:

def scheduler(self, authenticate: bool = False) -> glclient.Scheduler:
"""Return a scheduler stub configured with our identity if configured."""
scheduler = glclient.Scheduler(self.node_id, network=NETWORK, creds=self.creds())
scheduler = glclient.Scheduler(network=NETWORK, creds=self.creds())
if authenticate:
scheduler.authenticate(creds=self.creds())
return scheduler
Expand Down

0 comments on commit cd1da65

Please sign in to comment.