Skip to content

Commit 7331410

Browse files
authored
Merge pull request #117 from datum-cloud/chore/tpp
chore: put traffic protection policy creation behind an env flag
2 parents 205c4f7 + a761102 commit 7331410

1 file changed

Lines changed: 65 additions & 49 deletions

File tree

lib/src/tunnels.rs

Lines changed: 65 additions & 49 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ pub struct TunnelService {
8787
datum: DatumCloudClient,
8888
listen: ListenNode,
8989
publish_tickets: bool,
90+
create_traffic_protection_policies: bool,
9091
}
9192

9293
// TODO(zachsmith1): Use connectors + ConnectorAdvertisements across all projects to
@@ -121,6 +122,7 @@ impl TunnelService {
121122
datum,
122123
listen,
123124
publish_tickets: publish_tickets_enabled(),
125+
create_traffic_protection_policies: create_traffic_protection_policies_enabled(),
124126
}
125127
}
126128

@@ -357,56 +359,64 @@ impl TunnelService {
357359
"created ConnectorAdvertisement"
358360
);
359361

360-
let tpps: Api<TrafficProtectionPolicy> =
361-
Api::namespaced(client.clone(), DEFAULT_PCP_NAMESPACE);
362-
debug!(
363-
%project_id,
364-
proxy = %proxy_name,
365-
"creating TrafficProtectionPolicy"
366-
);
367-
let tpp = TrafficProtectionPolicy {
368-
metadata: ObjectMeta {
369-
name: Some(proxy_name.clone()),
370-
..Default::default()
371-
},
372-
spec: TrafficProtectionPolicySpec {
373-
target_refs: vec![LocalPolicyTargetReferenceWithSectionName {
374-
group: "gateway.networking.k8s.io".to_string(),
375-
kind: "Gateway".to_string(),
376-
name: proxy_name.clone(),
377-
section_name: None,
378-
}],
379-
mode: Some(TrafficProtectionPolicyMode::Enforce),
380-
sampling_percentage: None,
381-
rule_sets: Some(vec![TrafficProtectionPolicyRuleSet {
382-
rule_set_type: TrafficProtectionPolicyRuleSetType::OWASPCoreRuleSet,
383-
owasp_core_rule_set: Some(OWASPCRS {
384-
paranoia_levels: Some(ParanoiaLevels {
385-
blocking: Some(1),
386-
detection: Some(1),
362+
if self.create_traffic_protection_policies {
363+
let tpps: Api<TrafficProtectionPolicy> =
364+
Api::namespaced(client.clone(), DEFAULT_PCP_NAMESPACE);
365+
debug!(
366+
%project_id,
367+
proxy = %proxy_name,
368+
"creating TrafficProtectionPolicy"
369+
);
370+
let tpp = TrafficProtectionPolicy {
371+
metadata: ObjectMeta {
372+
name: Some(proxy_name.clone()),
373+
..Default::default()
374+
},
375+
spec: TrafficProtectionPolicySpec {
376+
target_refs: vec![LocalPolicyTargetReferenceWithSectionName {
377+
group: "gateway.networking.k8s.io".to_string(),
378+
kind: "Gateway".to_string(),
379+
name: proxy_name.clone(),
380+
section_name: None,
381+
}],
382+
mode: Some(TrafficProtectionPolicyMode::Enforce),
383+
sampling_percentage: None,
384+
rule_sets: Some(vec![TrafficProtectionPolicyRuleSet {
385+
rule_set_type: TrafficProtectionPolicyRuleSetType::OWASPCoreRuleSet,
386+
owasp_core_rule_set: Some(OWASPCRS {
387+
paranoia_levels: Some(ParanoiaLevels {
388+
blocking: Some(1),
389+
detection: Some(1),
390+
}),
391+
score_thresholds: None,
392+
rule_exclusions: None,
387393
}),
388-
score_thresholds: None,
389-
rule_exclusions: None,
390-
}),
391-
}]),
392-
},
393-
status: None,
394-
};
395-
tpps.create(&PostParams::default(), &tpp)
396-
.await
397-
.std_context("Failed to create TrafficProtectionPolicy")
398-
.inspect_err(|err| {
399-
warn!(
400-
%project_id,
401-
proxy = %proxy_name,
402-
"TrafficProtectionPolicy create failed: {err:#}"
403-
);
404-
})?;
405-
debug!(
406-
%project_id,
407-
proxy = %proxy_name,
408-
"created TrafficProtectionPolicy"
409-
);
394+
}]),
395+
},
396+
status: None,
397+
};
398+
tpps.create(&PostParams::default(), &tpp)
399+
.await
400+
.std_context("Failed to create TrafficProtectionPolicy")
401+
.inspect_err(|err| {
402+
warn!(
403+
%project_id,
404+
proxy = %proxy_name,
405+
"TrafficProtectionPolicy create failed: {err:#}"
406+
);
407+
})?;
408+
debug!(
409+
%project_id,
410+
proxy = %proxy_name,
411+
"created TrafficProtectionPolicy"
412+
);
413+
} else {
414+
debug!(
415+
%project_id,
416+
proxy = %proxy_name,
417+
"skipping TrafficProtectionPolicy creation (env disabled)"
418+
);
419+
}
410420

411421
let proxy_state = proxy_state_from_summary(&proxy_name, &endpoint, label, true)?;
412422
if self.publish_tickets {
@@ -1120,3 +1130,9 @@ fn publish_tickets_enabled() -> bool {
11201130
.map(|value| matches!(value.as_str(), "1" | "true" | "TRUE" | "yes" | "YES"))
11211131
.unwrap_or(false)
11221132
}
1133+
1134+
fn create_traffic_protection_policies_enabled() -> bool {
1135+
std::env::var("DATUM_CONNECT_CREATE_TRAFFIC_PROTECTION_POLICIES")
1136+
.map(|value| matches!(value.as_str(), "1" | "true" | "TRUE" | "yes" | "YES"))
1137+
.unwrap_or(false)
1138+
}

0 commit comments

Comments
 (0)