Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add test for inbound policy index deletion #12143

Merged
merged 3 commits into from Mar 7, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions Cargo.lock
Expand Up @@ -1262,6 +1262,7 @@ dependencies = [
"futures",
"http",
"k8s-gateway-api",
"k8s-openapi",
"kube",
"kubert",
"linkerd-policy-controller-core",
Expand Down
1 change: 1 addition & 0 deletions policy-controller/k8s/index/Cargo.toml
Expand Up @@ -26,6 +26,7 @@ tracing = "0.1"

[dev-dependencies]
chrono = { version = "0.4", default-features = false }
k8s-openapi = { version = "0.20", features = ["schemars"] }
maplit = "1"
tokio-stream = "0.1"
tokio-test = "0.4"
Expand Down
@@ -1,4 +1,6 @@
use super::*;
use k8s_openapi::apimachinery::pkg::apis::meta::v1 as metav1;
use linkerd_policy_controller_core::{http_route, inbound};

#[test]
fn links_authorization_policy_with_mtls_name() {
Expand Down Expand Up @@ -264,6 +266,129 @@ fn links_authorization_policy_with_service_account() {
);
}

#[test]
fn authorization_policy_prevents_index_deletion() {
let test = TestConfig::default();

// Create policy resources: Server, HttpRoute, AuthorizationPolicy, and NetworkAuthentication.
let server = mk_server(
"ns-0",
"srv-8080",
Port::Number(8080.try_into().unwrap()),
None,
Some(("app", "app-0")),
Some(k8s::policy::server::ProxyProtocol::Http1),
);
test.index.write().apply(server.clone());

let authz = ClientAuthorization {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

this is really nitpicky but it was immediately clear to me that this is only used at the end as the expected configuration (i.e. expected InboundServer authz policy). Maybe moving it closer to the assert or naming it more explicitly might help.

Ultimately once you go through this test top-to-bottom once or twice it starts to add up anyway so I don't think it's a major problem and will just leave it up to you.

networks: vec!["10.0.0.0/8".parse::<IpNet>().unwrap().into()],
authentication: ClientAuthentication::TlsAuthenticated(vec![IdentityMatch::Exact(
"foo.ns-0.serviceaccount.identity.linkerd.cluster.example.com".to_string(),
)]),
};
let authz_policy = k8s::policy::AuthorizationPolicy {
metadata: k8s::ObjectMeta {
namespace: Some("ns-0".to_string()),
name: Some("authz-foo".to_string()),
..Default::default()
},
spec: k8s::policy::AuthorizationPolicySpec {
target_ref: LocalTargetRef {
group: Some("policy.linkerd.io".to_string()),
kind: "HTTPRoute".to_string(),
name: "route-foo".to_string(),
},
required_authentication_refs: vec![
NamespacedTargetRef {
group: Some("policy.linkerd.io".to_string()),
kind: "NetworkAuthentication".to_string(),
name: "net-foo".to_string(),
namespace: None,
},
NamespacedTargetRef {
group: None,
kind: "ServiceAccount".to_string(),
namespace: Some("ns-0".to_string()),
name: "foo".to_string(),
},
],
},
};
test.index.write().apply(authz_policy.clone());
let route = mk_http_route("ns-0", "route-foo", "srv-8080");
test.index.write().apply(route.clone());
let net_authn = mk_network_authentication(
"ns-0".to_string(),
"net-foo".to_string(),
vec![k8s::policy::network_authentication::Network {
cidr: "10.0.0.0/8".parse().unwrap(),
except: None,
}],
);
test.index.write().apply(net_authn.clone());

// Now we delete the server, and HTTPRoute.
<Index as kubert::index::IndexNamespacedResource<k8s::policy::Server>>::delete(
&mut test.index.write(),
"ns-0".to_string(),
"srv-8080".to_string(),
);
<Index as kubert::index::IndexNamespacedResource<k8s::policy::HttpRoute>>::delete(
&mut test.index.write(),
"ns-0".to_string(),
"route-foo".to_string(),
);

// Recreate the pod, server, and HTTPRoute.
test.index.write().apply(server);
test.index.write().apply(route);

let mut pod = mk_pod("ns-0", "pod-0", Some(("container-0", None)));
pod.labels_mut()
.insert("app".to_string(), "app-0".to_string());
test.index.write().apply(pod.clone());

let rx = test
.index
.write()
.pod_server_rx("ns-0", "pod-0", 8080.try_into().unwrap())
.expect("pod-0.ns-0 should exist");

// AuthorizationPolicy should apply.
assert_eq!(
*rx.borrow(),
InboundServer {
reference: ServerRef::Server("srv-8080".to_string()),
authorizations: Default::default(),
protocol: ProxyProtocol::Http1,
http_routes: hashmap!(HttpRouteRef::Linkerd(http_route::GroupKindName{
group: "policy.linkerd.io".into(),
kind: "HTTPRoute".into(),
name: "route-foo".into(),
}) => HttpRoute {
rules: vec![inbound::HttpRouteRule {
matches: vec![http_route::HttpRouteMatch {
path: Some(http_route::PathMatch::Prefix("/foo".to_string())),
headers: vec![],
query_params: vec![],
method: None,
}],
filters: vec![],
}],
authorizations: hashmap!(
AuthorizationRef::AuthorizationPolicy("authz-foo".to_string()) => authz.clone()
)
.into_iter()
.collect(),
..Default::default()
})
.into_iter()
.collect(),
},
);
}

fn mk_authorization_policy(
ns: impl ToString,
name: impl ToString,
Expand Down Expand Up @@ -339,3 +464,64 @@ fn mk_network_authentication(
},
}
}

fn mk_http_route(
ns: impl ToString,
name: impl ToString,
server: impl ToString,
) -> k8s::policy::HttpRoute {
k8s::policy::HttpRoute {
metadata: k8s::ObjectMeta {
namespace: Some(ns.to_string()),
name: Some(name.to_string()),
..Default::default()
},
spec: k8s::policy::HttpRouteSpec {
inner: k8s_gateway_api::CommonRouteSpec {
parent_refs: Some(vec![k8s_gateway_api::ParentReference {
group: Some("policy.linkerd.io".to_string()),
kind: Some("Server".to_string()),
namespace: Some(ns.to_string()),
name: server.to_string(),
section_name: None,
port: None,
}]),
},
rules: Some(vec![k8s::policy::httproute::HttpRouteRule {
matches: Some(vec![k8s::policy::httproute::HttpRouteMatch {
path: Some(k8s_gateway_api::HttpPathMatch::PathPrefix {
value: "/foo".to_string(),
}),
..Default::default()
}]),
filters: None,
backend_refs: None,
timeouts: None,
}]),
..Default::default()
},
status: Some(k8s::policy::httproute::HttpRouteStatus {
inner: k8s_gateway_api::RouteStatus {
parents: vec![k8s_gateway_api::RouteParentStatus {
conditions: vec![metav1::Condition {
type_: "Accepted".to_string(),
status: "True".to_string(),
message: String::new(),
reason: String::new(),
last_transition_time: metav1::Time(chrono::Utc::now()),
observed_generation: None,
}],
parent_ref: k8s_gateway_api::ParentReference {
group: Some("policy.linkerd.io".to_string()),
kind: Some("Server".to_string()),
namespace: Some(ns.to_string()),
name: server.to_string(),
section_name: None,
port: None,
},
controller_name: "linkerd.io/policy-controller".to_string(),
}],
},
}),
}
}