-
Notifications
You must be signed in to change notification settings - Fork 62
Add checked IPv6 address database types #928
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
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| // This Source Code Form is subject to the terms of the Mozilla Public | ||
| // License, v. 2.0. If a copy of the MPL was not distributed with this | ||
| // file, You can obtain one at https://mozilla.org/MPL/2.0/. | ||
|
|
||
| //! Database-friendly IPv6 addresses | ||
|
|
||
| use diesel::backend::Backend; | ||
| use diesel::backend::RawValue; | ||
| use diesel::deserialize; | ||
| use diesel::deserialize::FromSql; | ||
| use diesel::serialize; | ||
| use diesel::serialize::Output; | ||
| use diesel::serialize::ToSql; | ||
| use diesel::sql_types::Inet; | ||
| use ipnetwork::IpNetwork; | ||
| use ipnetwork::Ipv6Network; | ||
| use omicron_common::api::external::Error; | ||
|
|
||
| #[derive( | ||
| Clone, Copy, AsExpression, FromSqlRow, PartialEq, Ord, PartialOrd, Eq, | ||
| )] | ||
| #[sql_type = "Inet"] | ||
| pub struct Ipv6Addr(std::net::Ipv6Addr); | ||
|
|
||
| NewtypeDebug! { () pub struct Ipv6Addr(std::net::Ipv6Addr); } | ||
| NewtypeFrom! { () pub struct Ipv6Addr(std::net::Ipv6Addr); } | ||
| NewtypeDeref! { () pub struct Ipv6Addr(std::net::Ipv6Addr); } | ||
|
|
||
| impl From<&std::net::Ipv6Addr> for Ipv6Addr { | ||
| fn from(addr: &std::net::Ipv6Addr) -> Self { | ||
| Self(*addr) | ||
| } | ||
| } | ||
|
|
||
| impl<DB> ToSql<Inet, DB> for Ipv6Addr | ||
| where | ||
| DB: Backend, | ||
| IpNetwork: ToSql<Inet, DB>, | ||
| { | ||
| fn to_sql<W: std::io::Write>( | ||
| &self, | ||
| out: &mut Output<W, DB>, | ||
| ) -> serialize::Result { | ||
| IpNetwork::V6(Ipv6Network::from(self.0)).to_sql(out) | ||
| } | ||
| } | ||
|
|
||
| impl<DB> FromSql<Inet, DB> for Ipv6Addr | ||
| where | ||
| DB: Backend, | ||
| IpNetwork: FromSql<Inet, DB>, | ||
| { | ||
| fn from_sql(bytes: RawValue<DB>) -> deserialize::Result<Self> { | ||
| match IpNetwork::from_sql(bytes)?.ip() { | ||
| std::net::IpAddr::V6(ip) => Ok(Self(ip)), | ||
| v4 => { | ||
| Err(Box::new(Error::internal_error( | ||
| format!("Expected an IPv6 address from the database, found IPv4: '{}'", v4).as_str() | ||
| ))) | ||
| } | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
One thing I don't quite understand: I expected this to trigger a change to the generated OpenAPI spec, and a corresponding change in the sled agents (simulated and real) to use a v6 address. But it looks like the generated type for this represents this field with a string:
omicron/sled-agent/src/sim/server.rs
Line 76 in b7f7ea6
so the distinction between v4+v6 and v6-only is not there. I gather that's because json-schema says the schema for an IP address is just a string:
https://docs.rs/schemars/latest/src/schemars/json_schema_impls/primitives.rs.html#54
Kind of a bummer...but well beyond the scope of this PR.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah it's definitely a bummer. It seems that a large number of types in the generated clients are serialized as a string, and require that explicit
.to_string()call. I'm not sure why, since it seems like the serialization should happen after the type is constructed. But yeah, beyond the scope of this PR. Is there an issue or something you'd like me to create for this?There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I don't think so. Thanks.