Skip to content

Commit 4df8081

Browse files
committed
tailscale, langs: exclude disco key from config
Remove the disco key from the exposed configurations, as it's generated automatically each time a `Device` starts. Signed-off-by: Nathan Perry <nathan@tailscale.com> Change-Id: I2bea17085984cac6965c0d5d8eb8db266a6a6964
1 parent 86ba3dd commit 4df8081

11 files changed

Lines changed: 54 additions & 88 deletions

File tree

src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ const AUTHKEY_VAR: &str = "TS_AUTH_KEY";
1414
/// Config for connecting to Tailscale.
1515
pub struct Config {
1616
/// The cryptographic keys representing this node's identity.
17-
pub key_state: NodeState,
17+
pub key_state: PersistState,
1818

1919
// TODO(npry): let clients also define an app name once the sdk-level name moves
2020
// to a dedicated field
@@ -43,7 +43,7 @@ impl Config {
4343
/// the key file.
4444
pub async fn default_with_key_file(p: impl AsRef<Path>) -> Result<Self, crate::Error> {
4545
Ok(Config {
46-
key_state: load_key_file(p, Default::default()).await?.into(),
46+
key_state: load_key_file(p, Default::default()).await?,
4747
..Default::default()
4848
})
4949
}

src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -174,7 +174,7 @@ impl Device {
174174
check_magic_env()?;
175175

176176
let rt =
177-
ts_runtime::Runtime::spawn(config.into(), auth_key, config.key_state.clone()).await?;
177+
ts_runtime::Runtime::spawn(config.into(), auth_key, (&config.key_state).into()).await?;
178178
let channel = rt.channel().await?;
179179

180180
Ok(Self {

ts_cli_util/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ impl CommonArgs {
5757

5858
let (client, stream) = ts_control::AsyncControlClient::connect(
5959
&(&config).into(),
60-
&config.key_state,
60+
&(&config.key_state).into(),
6161
self.auth_key.as_deref(),
6262
)
6363
.await?;

ts_devtools/src/bin/derp_ping.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -40,13 +40,13 @@ async fn main() -> ts_cli_util::Result<()> {
4040

4141
tracing::info!(?region_id, "starting derp transport");
4242

43-
let derp =
44-
ts_transport_derp::Client::connect(&derp_servers, &config.key_state.node_keys).await?;
43+
let derp = ts_transport_derp::Client::connect(&derp_servers, &config.key_state.node_key.into())
44+
.await?;
4545
let derp = Arc::new(derp);
4646

4747
let peer = args
4848
.send_to_self
49-
.then_some(config.key_state.node_keys.public)
49+
.then_some(config.key_state.node_key.public_key())
5050
.or(args.peer);
5151

5252
if let Some(peer) = peer {

ts_elixir/native/ts_elixir/src/config.rs

Lines changed: 9 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
use std::collections::HashMap;
22

33
use rustler::{Atom, NifResult, Term};
4-
use tailscale::keys::{DiscoPrivateKey, MachinePrivateKey, NetworkLockPrivateKey, NodePrivateKey};
54

65
mod atoms {
76
rustler::atoms! {
@@ -61,22 +60,20 @@ pub fn config_from_erl(
6160
pub struct Keystate {
6261
pub machine: Vec<u8>,
6362
pub node: Vec<u8>,
64-
pub disco: Vec<u8>,
6563
pub network_lock: Vec<u8>,
6664
}
6765

68-
impl From<tailscale::keys::NodeState> for Keystate {
69-
fn from(value: tailscale::keys::NodeState) -> Self {
66+
impl From<tailscale::keys::PersistState> for Keystate {
67+
fn from(value: tailscale::keys::PersistState) -> Self {
7068
Self {
71-
machine: value.machine_keys.private.to_bytes().into(),
72-
node: value.node_keys.private.to_bytes().into(),
73-
disco: value.disco_keys.private.to_bytes().into(),
74-
network_lock: value.network_lock_keys.private.to_bytes().into(),
69+
machine: value.machine_key.to_bytes().into(),
70+
node: value.node_key.to_bytes().into(),
71+
network_lock: value.network_lock_key.to_bytes().into(),
7572
}
7673
}
7774
}
7875

79-
impl TryFrom<Keystate> for tailscale::keys::NodeState {
76+
impl TryFrom<Keystate> for tailscale::keys::PersistState {
8077
type Error = ();
8178

8279
fn try_from(value: Keystate) -> Result<Self, ()> {
@@ -88,10 +85,9 @@ impl TryFrom<Keystate> for tailscale::keys::NodeState {
8885
}
8986

9087
Ok(Self {
91-
machine_keys: key::<MachinePrivateKey>(value.machine)?.into(),
92-
node_keys: key::<NodePrivateKey>(value.node)?.into(),
93-
disco_keys: key::<DiscoPrivateKey>(value.disco)?.into(),
94-
network_lock_keys: key::<NetworkLockPrivateKey>(value.network_lock)?.into(),
88+
machine_key: key(value.machine)?,
89+
node_key: key(value.node)?,
90+
network_lock_key: key(value.network_lock)?,
9591
})
9692
}
9793
}

ts_elixir/native/ts_elixir/src/lib.rs

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -128,11 +128,7 @@ fn connect<'env>(
128128
fn load_key_file(env: rustler::Env, path: &str) -> impl Encoder {
129129
let result = TOKIO_RUNTIME
130130
.block_on(tailscale::config::load_key_file(path, Default::default()))
131-
.map(|keys| {
132-
let keys: tailscale::keys::NodeState = keys.into();
133-
let result: Keystate = keys.into();
134-
result
135-
})
131+
.map(Keystate::from)
136132
.map_err(Into::into);
137133

138134
erl_result(env, result)

ts_ffi/examples/udp_ping.c

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -34,7 +34,7 @@ int main(int argc, char** argv) {
3434
* See the TCP example for a usage of `ts_init_from_key_file`, which collapses this init to a
3535
* single line, using a default config and key state from the selected file.
3636
*/
37-
struct ts_node_key_state key_state = {0};
37+
struct ts_persisted_key_state key_state = {0};
3838
assert(ts_load_key_file(argv[1], false, &key_state) >= 0);
3939

4040
struct ts_config config = {0};

ts_ffi/src/config.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
use std::ffi::c_char;
22

3-
use crate::{keys::node_key_state, util};
3+
use crate::{keys::persisted_key_state, util};
44

55
/// Tailscale configuration.
66
///
@@ -38,7 +38,7 @@ pub struct config<'a> {
3838
/// The key state to use.
3939
///
4040
/// If `NULL`, ephemeral key state is generated.
41-
pub key_state: Option<&'a mut node_key_state>,
41+
pub key_state: Option<&'a mut persisted_key_state>,
4242
}
4343

4444
impl config<'_> {

ts_ffi/src/keys.rs

Lines changed: 17 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -31,12 +31,12 @@ macro_rules! impl_to_from {
3131

3232
impl From<$key> for key {
3333
fn from(value: $key) -> Self {
34-
key(conv::<[u8; 32]>(value))
34+
key(value.into())
3535
}
3636
}
3737
impl From<&$key> for key {
3838
fn from(value: &$key) -> Self {
39-
key(conv::<[u8; 32]>(*value))
39+
key((*value).into())
4040
}
4141
}
4242
};
@@ -56,48 +56,37 @@ impl_to_from!(
5656
/// Tailscale key state for running a device.
5757
#[derive(Debug, Default)]
5858
#[repr(C)]
59-
pub struct node_key_state {
59+
pub struct persisted_key_state {
6060
/// Private key for the node (device) identity.
6161
pub node_private_key: key,
6262
/// Private key for the machine.
6363
pub machine_private_key: key,
6464
/// Private key for tailnet lock.
6565
pub network_lock_private_key: key,
66-
/// Private key for Tailscale discovery protocol (disco).
67-
pub disco_private_key: key,
6866
}
6967

70-
fn conv<T>(u: impl Into<T>) -> T {
71-
u.into()
72-
}
73-
74-
impl From<node_key_state> for ts_keys::NodeState {
75-
fn from(value: node_key_state) -> Self {
68+
impl From<persisted_key_state> for ts_keys::PersistState {
69+
fn from(value: persisted_key_state) -> Self {
7670
(&value).into()
7771
}
7872
}
7973

80-
impl From<&node_key_state> for ts_keys::NodeState {
81-
fn from(value: &node_key_state) -> Self {
82-
ts_keys::NodeState {
83-
disco_keys: conv::<ts_keys::DiscoPrivateKey>(&value.disco_private_key).into(),
84-
machine_keys: conv::<ts_keys::MachinePrivateKey>(&value.machine_private_key).into(),
85-
network_lock_keys: conv::<ts_keys::NetworkLockPrivateKey>(
86-
&value.network_lock_private_key,
87-
)
88-
.into(),
89-
node_keys: conv::<ts_keys::NodePrivateKey>(&value.node_private_key).into(),
74+
impl From<&persisted_key_state> for ts_keys::PersistState {
75+
fn from(value: &persisted_key_state) -> Self {
76+
ts_keys::PersistState {
77+
machine_key: (&value.machine_private_key).into(),
78+
network_lock_key: (&value.network_lock_private_key).into(),
79+
node_key: (&value.node_private_key).into(),
9080
}
9181
}
9282
}
9383

94-
impl From<ts_keys::NodeState> for node_key_state {
95-
fn from(value: ts_keys::NodeState) -> Self {
84+
impl From<ts_keys::PersistState> for persisted_key_state {
85+
fn from(value: ts_keys::PersistState) -> Self {
9686
Self {
97-
machine_private_key: value.machine_keys.private.into(),
98-
network_lock_private_key: value.network_lock_keys.private.into(),
99-
disco_private_key: value.disco_keys.private.into(),
100-
node_private_key: value.node_keys.private.into(),
87+
machine_private_key: value.machine_key.into(),
88+
network_lock_private_key: value.network_lock_key.into(),
89+
node_private_key: value.node_key.into(),
10190
}
10291
}
10392
}
@@ -117,7 +106,7 @@ impl From<ts_keys::NodeState> for node_key_state {
117106
pub unsafe extern "C" fn ts_load_key_file(
118107
path: *const c_char,
119108
overwrite_if_invalid: bool,
120-
key_state: &mut node_key_state,
109+
key_state: &mut persisted_key_state,
121110
) -> ffi::c_int {
122111
let s = unsafe { CStr::from_ptr(path) };
123112
let s = match s.to_str() {
@@ -138,7 +127,6 @@ pub unsafe extern "C" fn ts_load_key_file(
138127

139128
match TOKIO_RUNTIME.block_on(tailscale::config::load_key_file(s, mode)) {
140129
Ok(state) => {
141-
let state: tailscale::keys::NodeState = state.into();
142130
*key_state = state.into();
143131
tracing::info!(?key_state, "loaded key state");
144132

ts_ffi/src/lib.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ pub unsafe extern "C" fn ts_init_from_key_file(
135135
key_file: *const c_char,
136136
auth_token: *const c_char,
137137
) -> Option<Box<device>> {
138-
let mut state = keys::node_key_state::default();
138+
let mut state = keys::persisted_key_state::default();
139139

140140
// SAFETY: CStr invariants maintained by function precondition
141141
if unsafe { keys::ts_load_key_file(key_file, false, &mut state) } < 0 {

0 commit comments

Comments
 (0)