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

Fix ip pool policy for instance allocation #2113

Merged
merged 5 commits into from
Jan 6, 2023
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions nexus/src/authz/omicron.polar
Original file line number Diff line number Diff line change
Expand Up @@ -373,6 +373,11 @@ resource IpPoolList {
has_relation(fleet: Fleet, "parent_fleet", ip_pool_list: IpPoolList)
if ip_pool_list.fleet = fleet;

# Any authenticated user can create a child of a provided IP Pool.
# This is necessary to use the pools when provisioning instances.
has_permission(actor: AuthenticatedActor, "create_child", ip_pool: IpPool)
if silo in actor.silo and silo.fleet = ip_pool.fleet;

# Describes the policy for accessing "/system/images" (in the API)
resource GlobalImageList {
permissions = [
Expand Down
97 changes: 97 additions & 0 deletions nexus/tests/integration_tests/instances.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,9 @@ use nexus_test_utils::http_testing::NexusRequest;
use nexus_test_utils::http_testing::RequestBuilder;
use nexus_test_utils::resource_helpers::create_disk;
use nexus_test_utils::resource_helpers::create_ip_pool;
use nexus_test_utils::resource_helpers::create_local_user;
use nexus_test_utils::resource_helpers::create_silo;
use nexus_test_utils::resource_helpers::grant_iam;
use nexus_test_utils::resource_helpers::object_create;
use nexus_test_utils::resource_helpers::objects_list_page_authz;
use nexus_test_utils::resource_helpers::populate_ip_pool;
Expand All @@ -26,9 +29,11 @@ use omicron_common::api::external::InstanceState;
use omicron_common::api::external::Ipv4Net;
use omicron_common::api::external::Name;
use omicron_common::api::external::NetworkInterface;
use omicron_nexus::authz::SiloRole;
use omicron_nexus::external_api::shared::IpKind;
use omicron_nexus::external_api::shared::IpRange;
use omicron_nexus::external_api::shared::Ipv4Range;
use omicron_nexus::external_api::shared::SiloIdentityMode;
use omicron_nexus::external_api::views;
use omicron_nexus::TestInterfaces as _;
use omicron_nexus::{external_api::params, Nexus};
Expand Down Expand Up @@ -2889,6 +2894,98 @@ async fn test_instance_ephemeral_ip_from_correct_pool(
);
}

#[nexus_test]
async fn test_instance_create_in_silo(cptestctx: &ControlPlaneTestContext) {
let client = &cptestctx.external_client;

// Create a silo with a Collaborator User
let silo =
create_silo(&client, "authz", true, SiloIdentityMode::LocalOnly).await;
let user_id = create_local_user(
client,
&silo,
&"unpriv".parse().unwrap(),
params::UserPassword::InvalidPassword,
)
.await
.id;
grant_iam(
client,
"/system/silos/authz",
SiloRole::Collaborator,
user_id,
AuthnMode::PrivilegedUser,
)
.await;

// Populate IP Pool
populate_ip_pool(&client, "default", None).await;

// Create test organization and projects.
NexusRequest::objects_post(
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Not really part of this PR, but it would be neat if the resource_helpers used a builder pattern to make these calls a little easier to customize. They currently hard-code "privileged user", which means I need to do the request myself.

Copy link
Contributor

Choose a reason for hiding this comment

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

Let's open a bug for that?

Copy link
Collaborator Author

Choose a reason for hiding this comment

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

client,
"/organizations",
&params::OrganizationCreate {
identity: IdentityMetadataCreateParams {
name: ORGANIZATION_NAME.parse().unwrap(),
description: String::new(),
},
},
)
.authn_as(AuthnMode::SiloUser(user_id))
.execute()
.await
.expect("failed to create Organization")
.parsed_body::<views::Organization>()
.expect("failed to parse new Organization");
NexusRequest::objects_post(
client,
&format!("/organizations/{ORGANIZATION_NAME}/projects"),
&params::ProjectCreate {
identity: IdentityMetadataCreateParams {
name: PROJECT_NAME.parse().unwrap(),
description: String::new(),
},
},
)
.authn_as(AuthnMode::SiloUser(user_id))
.execute()
.await
.expect("failed to create Project")
.parsed_body::<views::Project>()
.expect("failed to parse new Project");

// Create an instance using the authorization granted to the collaborator
// Silo User.
let instance_params = params::InstanceCreate {
identity: IdentityMetadataCreateParams {
name: Name::try_from(String::from("ip-pool-test")).unwrap(),
description: String::from("instance to test IP Pool authz"),
},
ncpus: InstanceCpuCount::try_from(2).unwrap(),
memory: ByteCount::from_gibibytes_u32(4),
hostname: String::from("inst"),
user_data: vec![],
network_interfaces: params::InstanceNetworkInterfaceAttachment::Default,
external_ips: vec![params::ExternalIpCreate::Ephemeral {
pool_name: Some(Name::try_from(String::from("default")).unwrap()),
}],
disks: vec![],
start: true,
};
let url_instances = format!(
"/organizations/{}/projects/{}/instances",
ORGANIZATION_NAME, PROJECT_NAME
);
NexusRequest::objects_post(client, &url_instances, &instance_params)
.authn_as(AuthnMode::SiloUser(user_id))
.execute()
.await
.expect("Failed to create instance")
.parsed_body::<Instance>()
.expect("Failed to parse instance");
Comment on lines +2980 to +2986
Copy link
Collaborator Author

Choose a reason for hiding this comment

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

Before updating the polar policy, this failed.

The instance creation pathway checks for authz::Action::CreateChild on the target IP pool, which works fine for AuthnMode::PrivilegedUser, but fails for a silo user.

For example:

.ip_pools_fetch_default_for(&opctx, authz::Action::CreateChild)

}

async fn instance_get(
client: &ClientTestContext,
instance_url: &str,
Expand Down