Skip to content

Commit 8f75f2f

Browse files
fix: validate AC remote config signature (#1893)
* test: ac signature bug * fix: validate AC remote config signature
1 parent f087cdf commit 8f75f2f

File tree

4 files changed

+35
-37
lines changed

4 files changed

+35
-37
lines changed

agent-control/src/agent_control/defaults.rs

Lines changed: 1 addition & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
1-
use crate::agent_type::agent_type_id::AgentTypeID;
21
use crate::data_store::StoreKey;
32
use crate::opamp::remote_config::signature::SIGNATURE_CUSTOM_CAPABILITY;
4-
use crate::sub_agent::identity::AgentIdentity;
53
use opamp_client::capabilities;
64
use opamp_client::opamp::proto::{AgentCapabilities, CustomCapabilities};
75
use opamp_client::operation::capabilities::Capabilities;
@@ -95,21 +93,12 @@ pub fn default_capabilities() -> Capabilities {
9593
)
9694
}
9795

98-
pub fn default_sub_agent_custom_capabilities() -> CustomCapabilities {
96+
pub fn default_custom_capabilities() -> CustomCapabilities {
9997
CustomCapabilities {
10098
capabilities: vec![SIGNATURE_CUSTOM_CAPABILITY.to_string()],
10199
}
102100
}
103101

104-
pub(crate) fn get_custom_capabilities(agent_type_id: &AgentTypeID) -> Option<CustomCapabilities> {
105-
if agent_type_id.eq(&AgentIdentity::new_agent_control_identity().agent_type_id) {
106-
// Agent_Control does not have custom capabilities for now
107-
return None;
108-
}
109-
110-
Some(default_sub_agent_custom_capabilities())
111-
}
112-
113102
pub const AGENT_TYPE_NAME_INFRA_AGENT: &str = "com.newrelic.infrastructure";
114103
pub const AGENT_TYPE_NAME_NRDOT: &str = "com.newrelic.opentelemetry.collector";
115104

agent-control/src/opamp/operations.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use super::{
55
};
66
use crate::agent_control::defaults::{
77
OPAMP_SERVICE_NAME, OPAMP_SERVICE_NAMESPACE, OPAMP_SUPERVISOR_KEY,
8-
PARENT_AGENT_ID_ATTRIBUTE_KEY, default_capabilities, get_custom_capabilities,
8+
PARENT_AGENT_ID_ATTRIBUTE_KEY, default_capabilities, default_custom_capabilities,
99
};
1010
use crate::sub_agent::identity::AgentIdentity;
1111
use crate::{
@@ -105,7 +105,7 @@ pub fn start_settings(
105105
StartSettings {
106106
instance_uid: instance_id.into(),
107107
capabilities: default_capabilities(),
108-
custom_capabilities: get_custom_capabilities(&agent_identity.agent_type_id),
108+
custom_capabilities: Some(default_custom_capabilities()),
109109
agent_description: AgentDescription {
110110
identifying_attributes,
111111
non_identifying_attributes,

agent-control/src/opamp/remote_config/validators/signature/validator.rs

Lines changed: 30 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,7 @@
1-
use crate::agent_control::defaults::get_custom_capabilities;
21
use crate::http::client::HttpClient;
32
use crate::http::config::HttpConfig;
43
use crate::http::config::ProxyConfig;
54
use crate::opamp::remote_config::OpampRemoteConfig;
6-
use crate::opamp::remote_config::signature::SIGNATURE_CUSTOM_CAPABILITY;
75
use crate::opamp::remote_config::validators::RemoteConfigValidator;
86
use crate::opamp::remote_config::validators::signature::public_key::PublicKey;
97
use crate::opamp::remote_config::validators::signature::public_key_fetcher::PublicKeyFetcher;
@@ -104,23 +102,14 @@ impl RemoteConfigValidator for SignatureValidator {
104102

105103
fn validate(
106104
&self,
107-
agent_identity: &AgentIdentity,
105+
_: &AgentIdentity,
108106
opamp_remote_config: &OpampRemoteConfig,
109107
) -> Result<(), Self::Err> {
110108
// Noop validation
111109
let Some(public_key_store) = &self.public_key_store else {
112110
return Ok(());
113111
};
114112

115-
// custom capabilities are got from the agent-type (currently hard-coded)
116-
// If the capability is not set, no validation is performed
117-
if !get_custom_capabilities(&agent_identity.agent_type_id).is_some_and(|c| {
118-
c.capabilities
119-
.contains(&SIGNATURE_CUSTOM_CAPABILITY.to_string())
120-
}) {
121-
return Ok(());
122-
}
123-
124113
let signature = opamp_remote_config
125114
.get_default_signature()
126115
.map_err(|e| SignatureValidatorError::VerifySignature(e.to_string()))?
@@ -170,10 +159,11 @@ pub mod tests {
170159
.unwrap();
171160

172161
let config = "value";
173-
174162
let encoded_signature = pub_key_server.sign(config.as_bytes());
163+
164+
// agent remote config
175165
let remote_config = OpampRemoteConfig::new(
176-
AgentID::AgentControl,
166+
AgentIdentity::default().id,
177167
Hash::from("test"),
178168
ConfigState::Applying,
179169
ConfigurationMap::new(HashMap::from([(
@@ -189,6 +179,26 @@ pub mod tests {
189179

190180
signature_validator
191181
.validate(&AgentIdentity::default(), &remote_config)
182+
.unwrap();
183+
184+
// agent-control remote config
185+
let remote_config = OpampRemoteConfig::new(
186+
AgentIdentity::new_agent_control_identity().id,
187+
Hash::from("test"),
188+
ConfigState::Applying,
189+
ConfigurationMap::new(HashMap::from([(
190+
DEFAULT_AGENT_CONFIG_IDENTIFIER.to_string(),
191+
config.to_string(),
192+
)])),
193+
)
194+
.with_signature(Signatures::new_default(
195+
encoded_signature.as_str(),
196+
ED25519,
197+
pub_key_server.key_id.as_str(),
198+
));
199+
200+
signature_validator
201+
.validate(&AgentIdentity::new_agent_control_identity(), &remote_config)
192202
.unwrap()
193203
}
194204

@@ -302,7 +312,7 @@ pub mod tests {
302312
}
303313

304314
#[test]
305-
pub fn test_signature_is_missing_for_agent_control_agent() {
315+
pub fn test_missing_signature_for_agent_control_agent() {
306316
let pub_key_server = FakePubKeyServer::new();
307317

308318
let signature_validator = SignatureValidator::new(
@@ -318,13 +328,12 @@ pub mod tests {
318328
AgentID::AgentControl,
319329
Hash::from("test"),
320330
ConfigState::Applying,
321-
ConfigurationMap::default(),
331+
ConfigurationMap::new(HashMap::from([("key".to_string(), "value".to_string())])),
322332
);
323-
// Signature custom capability is not set for agent-control agent, therefore signature is not checked
324-
assert!(
325-
signature_validator
326-
.validate(&AgentIdentity::new_agent_control_identity(), &rc)
327-
.is_ok()
333+
334+
assert_matches!(
335+
signature_validator.validate(&AgentIdentity::new_agent_control_identity(), &rc),
336+
Err(SignatureValidatorError::VerifySignature(_))
328337
);
329338
}
330339
}

agent-control/src/sub_agent/on_host/builder.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -165,7 +165,7 @@ mod tests {
165165
use crate::agent_control::agent_id::AgentID;
166166
use crate::agent_control::defaults::{
167167
OPAMP_SERVICE_NAME, OPAMP_SERVICE_NAMESPACE, OPAMP_SUPERVISOR_KEY,
168-
PARENT_AGENT_ID_ATTRIBUTE_KEY, default_capabilities, default_sub_agent_custom_capabilities,
168+
PARENT_AGENT_ID_ATTRIBUTE_KEY, default_capabilities, default_custom_capabilities,
169169
};
170170
use crate::agent_control::run::on_host::AGENT_CONTROL_MODE_ON_HOST;
171171
use crate::agent_type::agent_type_id::AgentTypeID;
@@ -466,7 +466,7 @@ mod tests {
466466
StartSettings {
467467
instance_uid: sub_agent_instance_id.into(),
468468
capabilities: default_capabilities(),
469-
custom_capabilities: Some(default_sub_agent_custom_capabilities()),
469+
custom_capabilities: Some(default_custom_capabilities()),
470470
agent_description: AgentDescription {
471471
identifying_attributes,
472472
non_identifying_attributes: HashMap::from([

0 commit comments

Comments
 (0)