Skip to content

Commit 0798ab2

Browse files
committed
fix external IPs tests, simplify pool lookup by name logic
1 parent 2ba2ffe commit 0798ab2

File tree

3 files changed

+22
-25
lines changed

3 files changed

+22
-25
lines changed

nexus/db-queries/src/db/datastore/external_ip.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,7 +64,7 @@ impl DataStore {
6464
&opctx,
6565
authz::Action::CreateChild, // TODO: wtf
6666
&name,
67-
Some(current_silo.id()),
67+
current_silo.id(),
6868
None, // TODO: include current project ID
6969
)
7070
.await?

nexus/db-queries/src/db/datastore/ip_pool.rs

Lines changed: 10 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -116,40 +116,32 @@ impl DataStore {
116116
opctx: &OpContext,
117117
action: authz::Action,
118118
name: &Name,
119-
silo_id: Option<Uuid>,
119+
silo_id: Uuid,
120+
// TODO: project_id should always be defined, this is temporary
120121
project_id: Option<Uuid>,
121122
) -> LookupResult<IpPool> {
122123
let (.., authz_pool, pool) = LookupPath::new(opctx, &self)
123124
.ip_pool_name(&name)
124125
.fetch_for(action)
125126
.await?;
126127

127-
if pool.silo_id == Some(*INTERNAL_SILO_ID) {
128-
return Err(authz_pool.not_found());
129-
}
128+
// You can't look up a pool by name if it conflicts with your current
129+
// scope, i.e., if it has a silo or project and those are different from
130+
// your current silo or project
130131

131-
// You can't look up the internal pool by name, and you can't look up
132-
// a pool by name if it conflicts with your current scope, i.e., if it
133-
// has a silo or project and those are different from your current silo
134-
// or project
135132
if let Some(pool_silo_id) = pool.silo_id {
136-
if pool_silo_id == *INTERNAL_SILO_ID {
133+
if pool_silo_id != silo_id {
137134
return Err(authz_pool.not_found());
138135
}
139-
140-
if let Some(current_silo_id) = silo_id {
141-
if current_silo_id != pool_silo_id {
142-
return Err(authz_pool.not_found());
143-
}
144-
}
145136
}
146137

147-
if let Some(pool_project_id) = pool.project_id {
148-
if let Some(current_project_id) = project_id {
149-
if current_project_id != pool_project_id {
138+
match (pool.project_id, project_id) {
139+
(Some(pool_project_id), Some(current_project_id)) => {
140+
if pool_project_id != current_project_id {
150141
return Err(authz_pool.not_found());
151142
}
152143
}
144+
_ => {}
153145
}
154146

155147
Ok(pool)

nexus/db-queries/src/db/queries/external_ip.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -816,7 +816,6 @@ mod tests {
816816
use crate::context::OpContext;
817817
use crate::db::datastore::DataStore;
818818
use crate::db::datastore::SERVICE_IP_POOL_NAME;
819-
use crate::db::fixed_data::silo::INTERNAL_SILO_ID;
820819
use crate::db::identity::Resource;
821820
use crate::db::model::IpKind;
822821
use crate::db::model::IpPool;
@@ -862,14 +861,20 @@ mod tests {
862861
Self { logctx, opctx, db, db_datastore }
863862
}
864863

865-
async fn create_ip_pool(&self, name: &str, range: IpRange) {
864+
async fn create_ip_pool(
865+
&self,
866+
name: &str,
867+
range: IpRange,
868+
is_default: bool,
869+
) {
870+
let silo_id = self.opctx.authn.silo_required().unwrap().id();
866871
let pool = IpPool::new(
867872
&IdentityMetadataCreateParams {
868873
name: String::from(name).parse().unwrap(),
869874
description: format!("ip pool {}", name),
870875
},
871-
Some(*INTERNAL_SILO_ID),
872-
true,
876+
Some(silo_id),
877+
is_default,
873878
);
874879

875880
use crate::db::schema::ip_pool::dsl as ip_pool_dsl;
@@ -1709,7 +1714,7 @@ mod tests {
17091714
Ipv4Addr::new(10, 0, 0, 6),
17101715
))
17111716
.unwrap();
1712-
context.create_ip_pool("p1", second_range).await;
1717+
context.create_ip_pool("p1", second_range, /*default*/ false).await;
17131718

17141719
// Allocating an address on an instance in the second pool should be
17151720
// respected, even though there are IPs available in the first.
@@ -1752,7 +1757,7 @@ mod tests {
17521757
let last_address = Ipv4Addr::new(10, 0, 0, 6);
17531758
let second_range =
17541759
IpRange::try_from((first_address, last_address)).unwrap();
1755-
context.create_ip_pool("p1", second_range).await;
1760+
context.create_ip_pool("p1", second_range, /* default */ false).await;
17561761

17571762
// Allocate all available addresses in the second pool.
17581763
let instance_id = Uuid::new_v4();

0 commit comments

Comments
 (0)