Skip to content

Commit 201a569

Browse files
Merge pull request #378 from code0-tech/#354-reliable-action-config
static mode didnt lookup token
2 parents 3c682b5 + aca7e8c commit 201a569

2 files changed

Lines changed: 83 additions & 2 deletions

File tree

src/configuration/service.rs

Lines changed: 57 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,13 @@ impl ServiceConfiguration {
131131

132132
pub fn get_action_configuration(
133133
&self,
134+
token: &String,
134135
action_identifier: &String,
135136
) -> Vec<ModuleConfigurations> {
136137
match self
137138
.actions
138139
.iter()
139-
.find(|x| &x.service_name == action_identifier)
140+
.find(|x| &x.token == token && &x.service_name == action_identifier)
140141
{
141142
Some(a) => a.config.clone(),
142143
None => vec![],
@@ -187,6 +188,7 @@ impl ServiceConfiguration {
187188
mod tests {
188189
use super::{
189190
RuntimeServiceConfiguration, SerializableActionServiceConfiguration,
191+
SerializableModuleConfiguration, SerializableModuleProjectConfiguration,
190192
SerializableServiceConfiguration, ServiceConfiguration,
191193
};
192194

@@ -253,6 +255,7 @@ mod tests {
253255
&String::from("action-identifier")
254256
));
255257
assert!(!config.has_action(&String::from("action-token"), &String::from("action-other")));
258+
assert!(!config.has_action(&String::from("example"), &String::from("example")));
256259
}
257260

258261
#[test]
@@ -274,4 +277,57 @@ mod tests {
274277
));
275278
assert!(!config.has_service(&String::from("action-token"), &String::from("taurus-x")));
276279
}
280+
281+
#[test]
282+
fn get_action_configuration_requires_matching_token_and_identifier() {
283+
let config: ServiceConfiguration = SerializableServiceConfiguration {
284+
actions: vec![
285+
SerializableActionServiceConfiguration {
286+
token: String::from("old-token"),
287+
identifier: String::from("shared-action"),
288+
configs: vec![SerializableModuleProjectConfiguration {
289+
project_id: 1,
290+
configs: vec![SerializableModuleConfiguration {
291+
identifier: String::from("endpoint"),
292+
value: serde_json::json!("old.example"),
293+
}],
294+
}],
295+
},
296+
SerializableActionServiceConfiguration {
297+
token: String::from("new-token"),
298+
identifier: String::from("shared-action"),
299+
configs: vec![SerializableModuleProjectConfiguration {
300+
project_id: 2,
301+
configs: vec![SerializableModuleConfiguration {
302+
identifier: String::from("endpoint"),
303+
value: serde_json::json!("new.example"),
304+
}],
305+
}],
306+
},
307+
],
308+
runtimes: vec![],
309+
}
310+
.into();
311+
312+
let configs = config
313+
.get_action_configuration(&String::from("new-token"), &String::from("shared-action"));
314+
315+
assert_eq!(configs.len(), 1);
316+
assert_eq!(configs[0].module_identifier, "shared-action");
317+
assert_eq!(configs[0].module_configurations[0].project_id, 2);
318+
}
319+
320+
#[test]
321+
fn get_action_configuration_returns_empty_for_identifier_with_wrong_token() {
322+
let config = fixture();
323+
324+
assert!(
325+
config
326+
.get_action_configuration(
327+
&String::from("wrong-token"),
328+
&String::from("action-identifier")
329+
)
330+
.is_empty()
331+
);
332+
}
277333
}

src/server/action_transfer_service_server_impl.rs

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -229,6 +229,15 @@ fn pending_reply_keys(
229229
keys
230230
}
231231

232+
async fn send_stream_error(
233+
tx: &tokio::sync::mpsc::Sender<Result<ActionTransferResponse, tonic::Status>>,
234+
status: tonic::Status,
235+
) {
236+
if tx.send(Err(status)).await.is_err() {
237+
log::debug!("Action transfer response stream closed before error could be sent");
238+
}
239+
}
240+
232241
fn insert_pending_reply(
233242
pending: &mut HashMap<String, PendingReply>,
234243
reply_subject: Subject,
@@ -636,6 +645,11 @@ impl ActionTransferService for AquilaActionTransferServiceServer {
636645
Some(ref m) => m.identifier.clone(),
637646
None => {
638647
log::warn!("Rejected action logon reason=missing_module");
648+
send_stream_error(
649+
&tx,
650+
Status::aborted("Please provide a module configuration."),
651+
)
652+
.await;
639653
break;
640654
}
641655
};
@@ -663,6 +677,7 @@ impl ActionTransferService for AquilaActionTransferServiceServer {
663677
status.code(),
664678
status.message()
665679
);
680+
send_stream_error(&tx, status).await;
666681
break;
667682
}
668683
};
@@ -677,6 +692,11 @@ impl ActionTransferService for AquilaActionTransferServiceServer {
677692
}
678693
_ => {
679694
log::error!("Action stream protocol violation expected=logon");
695+
send_stream_error(
696+
&tx,
697+
Status::failed_precondition("first action stream message must be logon"),
698+
)
699+
.await;
680700
break;
681701
}
682702
}
@@ -702,7 +722,7 @@ impl ActionTransferService for AquilaActionTransferServiceServer {
702722

703723
if is_static {
704724
let lock = actions.lock().await;
705-
let configs = lock.get_action_configuration(&identifier);
725+
let configs = lock.get_action_configuration(&token, &identifier);
706726
for conf in configs {
707727
if let Err(err) = cfg_tx.send(conf) {
708728
log::warn!("No action configuration receivers available: {:?}", err);
@@ -716,6 +736,11 @@ impl ActionTransferService for AquilaActionTransferServiceServer {
716736
"Action stream protocol violation identifier={} reason=duplicate_logon",
717737
identifier
718738
);
739+
send_stream_error(
740+
&tx,
741+
Status::failed_precondition("action stream logon was already accepted"),
742+
)
743+
.await;
719744
break;
720745
}
721746
tucana::aquila::action_transfer_request::Data::Event(event) => {

0 commit comments

Comments
 (0)