Skip to content

Conversation

@anth0nyleung
Copy link
Contributor

This commit addresses issue #371

Introduce a function to list all the allowed dependencies and their respective versions and features.

E.g Given a toml like this,

$ > cat ~/allow.toml
owo-colors = "=3.5.0"
tokio = { version = "=1.19.2", features = ["rt", "net"] }
plutonium = "*"
syn = { version = "=2.0.28", default-features = false }
rand = ["=0.8.3", { version = ">0.8.4, <0.8.6", features = ["getrandom"] }]

the function returns a table

postgres=# SELECT * FROM plrust.list_allowed_dependencies();
    name    |    version     |  features   | default_features
------------+----------------+-------------+------------------
 owo-colors | =3.5.0         | {}          | t
 plutonium  | *              | {}          | t
 rand       | =0.8.3         | {}          | t
 rand       | >0.8.4, <0.8.6 | {getrandom} | t
 syn        | =2.0.28        | {}          | f
 tokio      | =1.19.2        | {rt,net}    | t
(6 rows)

This commit also bumps plrust to 1.1 and introduce the sql script for schema upgrade


Testing

  • Unit test passed
  • Tested extension upgrade
  1. Install plrust 1.0 (v1.2.3)
postgres=# CREATE EXTENSION plrust ;
CREATE EXTENSION
postgres=# \dx plrust
List of installed extensions
-[ RECORD 1 ]-----------------------------------------------------------
Name        | plrust
Version     | 1.0
Schema      | plrust
Description | plrust:  A Trusted Rust procedural language for PostgreSQL

postgres=# \df plrust.*
List of functions
-[ RECORD 1 ]-------+--------------------
Schema              | plrust
Name                | plrust_call_handler
Result data type    | language_handler
Argument data types |
Type                | func
-[ RECORD 2 ]-------+--------------------
Schema              | plrust
Name                | plrust_validator
Result data type    | void
Argument data types | fn_oid oid
Type                | func
  1. Update plrust to 1.1, new function is installed
postgres=# ALTER EXTENSION plrust UPDATE TO '1.1';
ALTER EXTENSION
postgres=# \dx plrust
List of installed extensions
-[ RECORD 1 ]-----------------------------------------------------------
Name        | plrust
Version     | 1.1
Schema      | plrust
Description | plrust:  A Trusted Rust procedural language for PostgreSQL

postgres=# \df plrust.*
List of functions
-[ RECORD 1 ]-------+--------------------------------------------------------------------------
Schema              | plrust
Name                | list_allowed_dependencies
Result data type    | TABLE(name text, version text, features text[], default_features boolean)
Argument data types |
Type                | func
-[ RECORD 2 ]-------+--------------------------------------------------------------------------
Schema              | plrust
Name                | plrust_call_handler
Result data type    | language_handler
Argument data types |
Type                | func
-[ RECORD 3 ]-------+--------------------------------------------------------------------------
Schema              | plrust
Name                | plrust_validator
Result data type    | void
Argument data types | fn_oid oid
Type                | func

postgres=# SELECT * FROM plrust.list_allowed_dependencies();
    name    |    version     |  features   | default_features
------------+----------------+-------------+------------------
 owo-colors | =3.5.0         | {}          | t
 plutonium  | *              | {}          | t
 rand       | =0.8.3         | {}          | t
 rand       | >0.8.4, <0.8.6 | {getrandom} | t
 syn        | =2.0.28        | {}          | f
 tokio      | =1.19.2        | {rt,net}    | t
(6 rows)

@eeeebbbbrrrr
Copy link
Contributor

Thanks! I’ll provide some feedback in the morning.

Copy link
Contributor

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

Some nits.

Comment on lines 209 to 213
Box<dyn std::error::Error + Send + Sync + 'static>,
> {
let allowed_dependencies = allow_list::get_allowed_dependencies_as_tuple_vector();
Ok(Some(TableIterator::new(allowed_dependencies)))
}
Copy link
Contributor

Choose a reason for hiding this comment

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

The only unfortunate thing is I don't think there's an easy way yet to simply yield a TableIterator that yields the equivalent of a Rust struct so I guess this would have to .map() the struct back down to a tuple anyway... hmm... annoying... I should fix that. I mean, PL/Rust users aren't exposed to how grody it is, but

let name = dep.name.clone();
let ver = version
.get("version")
.unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

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

If it's possible for the "version" to be None, then we should account for that here and in our data structure instead of panicking.

Copy link
Contributor Author

@anth0nyleung anth0nyleung Aug 17, 2023

Choose a reason for hiding this comment

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

version can be None, but i think load_allowlist is already checking if version is set for an allowed dependency

(23-08-17 12:55:17) [~/pg_data]
$ > cat ~/allow.toml
foo = { features = ["bar"] }

(23-08-17 12:55:58) [~/pg_data]
$ > psql -d postgres -c 'SELECT * FROM plrust.allowed_dependencies();'
ERROR:  Error loading dependency allow-list:
   0: Dependency entry is missing the `version` attribute

Location:
   plrust/src/allow_list.rs:432

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

it also validates the Value is a string

(23-08-17 12:57:51) [~/pg_data]
$ > cat ~/allow.toml
foo = { version = ["1.0"], features = ["bar"] }

(23-08-17 12:57:56) [~/pg_data]
$ > psql -d postgres -c 'SELECT * FROM plrust.allowed_dependencies();'
ERROR:  Error loading dependency allow-list:
   0: Unsupported value type: Array([String("1.0")])

Location:
   plrust/src/allow_list.rs:432

Backtrace omitted. Run with RUST_BACKTRACE=1 environment variable to display it.
Run with RUST_BACKTRACE=full to include source snippets.

Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think we're fine here. A dependency is only coherent with a version of some sort, since a Rust dependency, as far as cargo is concerned, is not a crate name but also its version, so panicking is okay.

let name = dep.name.clone();
let ver = version
.get("version")
.unwrap()
Copy link
Contributor

Choose a reason for hiding this comment

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

Yeah, I think we're fine here. A dependency is only coherent with a version of some sort, since a Rust dependency, as far as cargo is concerned, is not a crate name but also its version, so panicking is okay.

Copy link
Contributor

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

Apologies, Eric's remarks highlighted some things I missed on my first scan.

…n toml file; Update doc for using the plrust.allowed_dependencies function
Copy link
Contributor

@workingjubilee workingjubilee left a comment

Choose a reason for hiding this comment

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

This looks good to me, thank you!

@workingjubilee workingjubilee dismissed eeeebbbbrrrr’s stale review August 22, 2023 19:57

Addressed as needed.

@workingjubilee workingjubilee merged commit 9368363 into tcdi:develop Aug 22, 2023
@workingjubilee workingjubilee mentioned this pull request Aug 22, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants