feat(host): Add host plugin related schemas and tests (#1475)

* feat(host): Add host plugin related schemas and tests

This adds the schema files for host plugins, plugin based catalogs and
plugin based sets.  This also adds the protos and tests which utilize
gorm to ensure the schemas enforce the constraints we expect.

Along with this change was a migration which moved catalog names to the
base type table so we can ensure all catalogs in a single scope,
regardless of subtype, have a unique name.

* Updated plugin schemas to do the following:
- Add plugin_executable table
- plugin table now also has scope_id and name w/ uniqueness constraints
- host_plugin no longer has id_prefix column
- host_plugin separates name and plugin_name
- plugin_version table renamed version to symantic_version column.

* Fix typo in delete_plugin_subtype trigger.  Add update test for host plugins.

* Update id prefixes. Fix public_id typos in protos and plugin_executable name in sql diagrams.

* Created internal/plugin and moved base plugin structs to it and moved host plugin subdirectory to it.

* Created internal/plugin and moved base plugin structs to it and moved host plugin subdirectory to it.

* Add first pass at enums for OS and Arch supported by plugin executables.

* Capitalizing the const enums for OS and Architectures.

* Add comments to sql files.

* Reviewer comments.

Remove unsupported OSes and Archs from SQL and go constants.
Add missing tests.

* Be more permissive in the values the db accepts.

The go logic can restrict which values it will allow getting populated into the db.

* Removing constraints on which values can be placed inside operating_system and architecture columns.

* Updating test helper functions to only create a single catalog or set.

* Move files to new migration version.

* Fixing job/plugin test.

* Update table names, named a number of constraints, remove plugin version and executable tables.

* Add constraints for plugin name and id prefix.

* Rename constraint, remove unhelpful comment block.

* Adding semantic version to plugin_host and set attributes to not null.

* Remove semantic versioning from all locations.

* Remove semantic_version from plugin_host table comment.

* Fix op copy/paste error. Don't use deprecated errors methods. Update constraint names.  Add docs.

* Apply suggestions from code review

Fixing constraint names.

Co-authored-by: Michael Gaffney <mgaffney@users.noreply.github.com>
This commit is contained in:
Todd KnightandMichael Gaffney authored and GitHub committed 2021-09-08 10:06:38 -07:00
1 parent 71cba858e3
commit db9e1106a9
39 files changed
+3987 -4

No files matched your search

@@ -0,0 +1,78 @@
begin;
-- We are updating the plugin table here to have a scope id, since
-- subtypes of plugin should be scoped. We use 'global' as the default
-- just for the purpose of the migration since the scope cannot be null
-- and the one plugin that already exists (pi_system) can be globally
-- scoped. The immediately following statement removes this default.
-- We add a name so we can enforce that all plugins have a unique name
-- in a specific scope across all plugin subtypes.
alter table plugin
add column name wt_name,
add column scope_id wt_scope_id
not null
default 'global'
constraint iam_scope_global_fkey
references iam_scope_global(scope_id)
on delete cascade
on update cascade;
alter table plugin
alter column scope_id drop default;
-- Add constraints that enforce names are unique across all plugin subtypes
-- in a specific scope.
alter table plugin
add constraint plugin_scope_id_public_id_uq
unique (scope_id, public_id),
add constraint plugin_scope_id_name_uq
unique (scope_id, name);
-- insert, update, and delete plugin_subtypes are created since we are adding
-- subtyped plugins and we need to keep the base table plugin in sync with all
-- subtype tables.
create function insert_plugin_subtype()
returns trigger
as $$
begin
insert into plugin
(public_id, scope_id, name)
values
(new.public_id, new.scope_id, new.name);
return new;
end;
$$ language plpgsql;
comment on function insert_plugin_subtype() is
'insert_plugin_subtype() inserts sub type name into the base type plugin table';
create function update_plugin_subtype()
returns trigger
as $$
begin
update plugin set name = new.name where public_id = new.public_id and new.name != name;
return new;
end;
$$ language plpgsql;
comment on function update_plugin_subtype() is
'update_plugin_subtype() will update base plugin type name column with new values from sub type';
-- delete_plugin_subtype() is an after delete trigger function
-- for subtypes of plugin
create function delete_plugin_subtype()
returns trigger
as $$
begin
delete from plugin
where public_id = old.public_id;
return null; -- result is ignored since this is an after trigger
end;
$$ language plpgsql;
comment on function delete_plugin_subtype is
'delete_plugin_subtype() is an after trigger function for subytypes of plugin';
insert into oplog_ticket (name, version)
values
('plugin', 1);
commit;
@@ -0,0 +1,89 @@
begin;
/*
┌──────────────────┐
│ plugin │
├──────────────────┤
│public_id (pk) │
│scope_id (fk) │
└──────────────────┘
┼
┼
│
┼
┼
┌──────────────────┐
│ plugin_host │
├──────────────────┤
│public_id (pk) │
│scope_id (fk) │
│name │
│description │
│version │
│plugin_name │
│id_prefix │
└──────────────────┘
*/
create table plugin_host (
public_id wt_plugin_id primary key,
scope_id wt_scope_id not null
-- TODO: Allow plugins to be created in different scopes and
-- constrain the host-catalog's plugin reference accordingly.
constraint iam_scope_global_fkey
references iam_scope_global(scope_id)
on delete cascade
on update cascade,
name wt_name,
description text,
create_time wt_timestamp,
update_time wt_timestamp,
version wt_version,
plugin_name text not null
constraint plugin_name_must_be_not_empty
check(length(trim(plugin_name)) > 0)
constraint plugin_name_must_be_lowercase
check(lower(trim(plugin_name)) = plugin_name)
constraint plugin_host_plugin_name_uq
unique,
id_prefix text not null
constraint plugin_id_prefix_must_be_not_empty
check(length(trim(id_prefix)) > 0)
constraint plugin_id_prefix_must_fit_format
check (id_prefix ~ '^[a-z0-9]*$')
constraint plugin_host_id_prefix_uq
unique,
constraint plugin_fkey
foreign key (scope_id, public_id)
references plugin(scope_id, public_id)
on delete cascade
on update cascade,
constraint plugin_host_scope_id_name_uq
unique(scope_id, name)
);
create trigger update_version_column after update on plugin_host
for each row execute procedure update_version_column();
create trigger update_time_column before update on plugin_host
for each row execute procedure update_time_column();
create trigger default_create_time_column before insert on plugin_host
for each row execute procedure default_create_time();
create trigger immutable_columns before update on plugin_host
for each row execute procedure immutable_columns('public_id', 'create_time', 'plugin_name');
create trigger insert_plugin_subtype before insert on plugin_host
for each row execute procedure insert_plugin_subtype();
create trigger update_plugin_subtype before update on plugin_host
for each row execute procedure update_plugin_subtype();
create trigger delete_plugin_subtype after delete on plugin_host
for each row execute procedure delete_plugin_subtype();
insert into oplog_ticket (name, version)
values
('plugin_host', 1);
commit;
@@ -0,0 +1,61 @@
begin;
-- We are adding the name to the base host catalog type. This allows the db
-- to ensure that catalog names are unique in a scope across all subtypes.
alter table host_catalog
add column name wt_name;
alter table host_catalog
add constraint host_catalog_scope_id_name_uq
unique (scope_id, name);
-- Now that we've added the name column to the base type, we copy
-- the name from the static host catalog table into the base table.
update host_catalog
set name = st.name
from
static_host_catalog st
where
host_catalog.public_id = st.public_id and
st.name is not null;
-- Replace the insert_host_catalog_subtype function defined in 0/20_host.up.sql
-- to include the name.
-- insert_host_catalog_subtype() is a before insert trigger
-- function for subtypes of host_catalog
create or replace function insert_host_catalog_subtype()
returns trigger
as $$
begin
insert into host_catalog
(public_id, scope_id, name)
values
(new.public_id, new.scope_id, new.name);
return new;
end;
$$ language plpgsql;
comment on function insert_host_catalog_subtype() is
'insert_host_catalog_subtype() inserts sub type name into the base type host catalog table';
-- Now that we are tracking the name, which is mutable, we need to also
-- update the base table when the subtype tables are updated.
-- update_host_catalog_subtype() is intended to be used as a before update
-- trigger for all host catalog sub types. The purpose is to ensure that the
-- base table for host catalog to contain the updated names for each host catalog
-- in order to enforce uniqueness across all host catalogs, regardless of subtype,
-- in a given scope.
create function update_host_catalog_subtype()
returns trigger
as $$
begin
update host_catalog set name = new.name where public_id = new.public_id and new.name != name;
return new;
end;
$$ language plpgsql;
comment on function update_host_catalog_subtype() is
'update_host_catalog_subtype() will update base host catalog type name column with new values from sub type';
create trigger update_host_catalog_subtype before update on static_host_catalog
for each row execute procedure update_host_catalog_subtype();
commit;
@@ -0,0 +1,167 @@
begin;
/*
┌──────────────────┐
│ plugin_host │
├──────────────────┤
│public_id (pk) │
│... │
└──────────────────┘
┼
┼
○
╱│╲
┌─────────────────────┐
│ host_plugin_catalog │
├─────────────────────┤ ┌───────────────────────────┐
┌────────────────┐ │public_id (pk) │ │host_plugin_catalog_secret │
│host_catalog │ │plugin_id (fk) │ ├───────────────────────────┤
├────────────────┤ │scope_id (fk) │ │host_catalog_id (pk, fk) │
│public_id │┼┼──○┼│name │┼┼──○┼│secret │
│scope_id │ │description │ │key_id (fk) │
└────────────────┘ │attributes │ └───────────────────────────┘
┼ └─────────────────────┘
┼ ┼
│ ┼
│ ○
○ ╱│╲
╱│╲ ┌────────────────────┐
┌────────────────┐ │ host_plugin_set │
│ host_set │ ├────────────────────┤
├────────────────┤ │public_id (pk) │
│public_id │ │host_catalog_id (fk)│
│host_catalog_id │┼┼──○┼│name │
└────────────────┘ │description │
│attributes │
└────────────────────┘
*/
create table host_plugin_catalog (
public_id wt_public_id primary key,
scope_id wt_scope_id not null
constraint iam_scope_fkey
references iam_scope (public_id)
on delete cascade
on update cascade,
plugin_id wt_plugin_id not null
constraint plugin_host_fkey
references plugin_host (public_id)
on delete cascade
on update cascade,
name wt_name,
description text,
create_time wt_timestamp,
update_time wt_timestamp,
version wt_version,
attributes bytea not null
constraint attributes_must_not_be_empty
check(length(attributes) > 0),
constraint host_catalog_fkey
foreign key (scope_id, public_id)
references host_catalog (scope_id, public_id)
on delete cascade
on update cascade,
constraint host_plugin_catalog_scope_id_name_uq
unique(scope_id, name)
);
create trigger update_version_column after update on host_plugin_catalog
for each row execute procedure update_version_column();
create trigger update_time_column before update on host_plugin_catalog
for each row execute procedure update_time_column();
create trigger default_create_time_column before insert on host_plugin_catalog
for each row execute procedure default_create_time();
create trigger immutable_columns before update on host_plugin_catalog
for each row execute procedure immutable_columns('public_id', 'scope_id', 'plugin_id', 'create_time');
create trigger insert_host_catalog_subtype before insert on host_plugin_catalog
for each row execute procedure insert_host_catalog_subtype();
create trigger update_host_catalog_subtype before update on host_plugin_catalog
for each row execute procedure update_host_catalog_subtype();
create trigger delete_host_catalog_subtype after delete on host_plugin_catalog
for each row execute procedure delete_host_catalog_subtype();
create table host_plugin_catalog_secret (
catalog_id wt_public_id primary key
constraint host_plugin_catalog_fkey
references host_plugin_catalog (public_id)
on delete cascade
on update cascade,
create_time wt_timestamp,
update_time wt_timestamp,
secret bytea not null -- encrypted value
constraint secret_must_not_be_empty
check(length(secret) > 0),
key_id wt_private_id not null
constraint kms_database_key_version_fkey
references kms_database_key_version (private_id)
on delete restrict
on update cascade
);
create trigger update_time_column before update on host_plugin_catalog_secret
for each row execute procedure update_time_column();
create trigger default_create_time_column before insert on host_plugin_catalog_secret
for each row execute procedure default_create_time();
create trigger immutable_columns before update on host_plugin_catalog_secret
for each row execute procedure immutable_columns('catalog_id', 'create_time');
create table host_plugin_set (
public_id wt_public_id primary key,
catalog_id wt_public_id not null
constraint host_plugin_catalog_fkey
references host_plugin_catalog (public_id)
on delete cascade
on update cascade,
name wt_name,
description text,
create_time wt_timestamp,
update_time wt_timestamp,
version wt_version,
attributes bytea not null
constraint attributes_must_not_be_empty
check(length(attributes) > 0),
constraint host_plugin_set_catalog_id_name_uq
unique(catalog_id, name),
constraint host_set_fkey
foreign key (catalog_id, public_id)
references host_set (catalog_id, public_id)
on delete cascade
on update cascade,
constraint host_plugin_set_catalog_id_public_id_uq
unique(catalog_id, public_id)
);
create trigger update_version_column after update on host_plugin_set
for each row execute procedure update_version_column();
create trigger update_time_column before update on host_plugin_set
for each row execute procedure update_time_column();
create trigger default_create_time_column before insert on host_plugin_set
for each row execute procedure default_create_time();
create trigger immutable_columns before update on host_plugin_set
for each row execute procedure immutable_columns('public_id', 'catalog_id','create_time');
create trigger insert_host_set_subtype before insert on host_plugin_set
for each row execute procedure insert_host_set_subtype();
create trigger delete_host_set_subtype after delete on host_plugin_set
for each row execute procedure delete_host_set_subtype();
insert into oplog_ticket (name, version)
values
('host_plugin_catalog', 1),
('host_plugin_catalog_secret', 1),
('host_plugin_set', 1);
commit;