Files
threshold/internal/db/schema/migrations/oss/postgres/60/02_sessions.up.sql
T
hashicorp-copywrite[bot]andJohan Brandhorst-Satzkorn 29da0bcb92 [COMPLIANCE] License changes (#3567)
* Adding explicit MPL license for sub-package.

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Adding explicit MPL license for sub-package

This directory and its subdirectories (packages) contain files licensed with the MPLv2 `LICENSE` file in this directory and are intentionally licensed separately from the BSL `LICENSE` file at the root of this repository.

* Updating the license from MPL to Business Source License

Going forward, this project will be licensed under the Business Source License v1.1. Please see our blog post for more details at https://hashi.co/bsl-blog, FAQ at www.hashicorp.com/licensing-faq, and details of the license at www.hashicorp.com/bsl.

* Update copyright file headers to BUS-1.1

* Rerun make gen

This will pick up the last of the license changes

* Revert "Rerun make gen"

This reverts commit dbe0968bffd92745d8663c6940debae098395a6b.

* Remove copywrite from generation script

The current version of copywrite is not sophisticated enough
to only apply the copywrite changes to certain subdirectories,
so to avoid accidentally relicensing our api and sdk modules,
we will stop running the copywrite script until such time
that it can do the right thing.

* License protobuf API as MPL

The protobuf API files are used to generate the SDK,
since we want the SDK to be MPL, the protobuf files
must also be MPL. This extends to all protobuf files
whose sources end up in the SDK directory, including:

- internal/proto/controller/api
- internal/proto/controller/custom_options
- internal/proto/plugin

It also includes the generated service directory in
internal/gen/controller/api/services, since they are
generated from the protobuf sources used for the SDK.

* Change website license file name

This makes the naming scheme consistent with the rest
of the repository.

* Add note on licensing to README

* Set copywrite license to BUSL

* Revert "Add note on licensing to README"

This reverts commit 4d865a91a7a4f11605f0dc4609355d7e3c3cfffe.

---------

Co-authored-by: hashicorp-copywrite[bot] <110428419+hashicorp-copywrite[bot]@users.noreply.github.com>
Co-authored-by: Johan Brandhorst-Satzkorn <johan.brandhorst@gmail.com>
2023-08-11 10:31:53 -07:00

223 lines
7.3 KiB
PL/PgSQL

-- Copyright (c) HashiCorp, Inc.
-- SPDX-License-Identifier: BUSL-1.1
begin;
-- Replaces trigger from 56/02_add_data_key_foreign_key_references.up.sql
drop trigger cancel_session_with_null_fk on session;
create or replace function cancel_session_with_null_fk() returns trigger
as $$
begin
-- Note that we need each of these to run in case
-- more than one of them is null.
if new.auth_token_id is null then
perform cancel_session(new.public_id);
end if;
if new.project_id is null then
-- Setting the key_id to null will allow the scope
-- to cascade delete its keys.
new.key_id = null;
perform cancel_session(new.public_id);
end if;
if new.target_id is null then
perform cancel_session(new.public_id);
end if;
if new.user_id is null then
perform cancel_session(new.public_id);
end if;
return new;
end;
$$ language plpgsql;
create trigger cancel_session_with_null_fk before update of auth_token_id, project_id, target_id, user_id on session
for each row execute procedure cancel_session_with_null_fk();
create table session_target_address (
session_id wt_public_id primary key,
target_id wt_public_id,
-- the following foreign key constraint is set to null on a delete
-- because we want to be able to invoke a trigger to call cancel_session
-- when the references column is deleted
-- rather than canceling a session for any reason that may cause a row to be deleted.
constraint target_address_fkey foreign key (target_id)
references target_address (target_id)
on delete set null
on update cascade,
constraint session_fkey foreign key (session_id)
references session (public_id)
on delete cascade
on update cascade,
constraint session_target_address_session_id_target_id_uq
unique(session_id, target_id)
);
comment on table session_target_address is
'session_target_address entries represent a session that is using a network address that is assigned directly to a Target.';
create trigger immutable_columns before update on session_target_address
for each row execute procedure immutable_columns('session_id');
create function cancel_session_with_null_target_address_fk() returns trigger
as $$
begin
if new.target_id is null then
perform cancel_session(new.session_id);
delete from session_target_address where session_id = new.session_id;
end if;
return new;
end;
$$ language plpgsql;
create trigger cancel_session_with_null_target_address_fk after update of target_id on session_target_address
for each row execute procedure cancel_session_with_null_target_address_fk();
create function insert_session_target_address() returns trigger
as $$
begin
if new.target_id is null then
raise exception 'target_id is null';
end if;
return new;
end;
$$ language plpgsql;
create trigger insert_session_target_address before insert on session_target_address
for each row execute procedure insert_session_target_address();
create table session_host_set_host (
session_id wt_public_id primary key,
host_set_id wt_public_id,
host_id wt_public_id,
-- the following foreign key constraints for host_set_id & host_id is set to null on a delete
-- because we want to be able to invoke a trigger to call cancel_session
-- when the references column is deleted
-- rather than canceling a session for any reason that may cause a row to be deleted.
constraint host_set_fkey foreign key (host_set_id)
references host_set (public_id)
on delete set null
on update cascade,
constraint host_fkey foreign key (host_id)
references host (public_id)
on delete set null
on update cascade,
constraint session_fkey foreign key (session_id)
references session (public_id)
on delete cascade
on update cascade,
constraint session_host_set_host_session_id_host_set_id_host_id_uq
unique(session_id, host_set_id, host_id)
);
comment on table session_host_set_host is
'session_host_set entries represent a session that is using a Host Set.';
create trigger immutable_columns before update on session_host_set_host
for each row execute procedure immutable_columns('session_id');
create function cancel_session_with_null_host_source_fk() returns trigger
as $$
begin
if new.host_set_id is null or new.host_id is null then
perform cancel_session(new.session_id);
delete from session_host_set_host where session_id = new.session_id;
end if;
return new;
end;
$$ language plpgsql;
create trigger cancel_session_with_null_host_source_fk after update of host_set_id, host_id on session_host_set_host
for each row execute procedure cancel_session_with_null_host_source_fk();
create function insert_session_host_set_host() returns trigger
as $$
begin
if new.host_set_id is null then
raise exception 'host_set_id is null';
end if;
if new.host_id is null then
raise exception 'host_id is null';
end if;
return new;
end;
$$ language plpgsql;
create trigger insert_session_host_set_host before insert on session_host_set_host
for each row execute procedure insert_session_host_set_host();
drop view session_list;
alter table session
drop constraint session_host_id_fkey,
drop constraint session_host_set_id_fkey,
drop column host_id,
drop column host_set_id
;
-- Replaces trigger from 44/04_sessions.up.sql
drop trigger insert_session on session;
create or replace function insert_session() returns trigger
as $$
begin
case
when new.user_id is null then
raise exception 'user_id is null';
when new.target_id is null then
raise exception 'target_id is null';
when new.auth_token_id is null then
raise exception 'auth_token_id is null';
when new.project_id is null then
raise exception 'project_id is null';
when new.endpoint is null then
raise exception 'endpoint is null';
else
end case;
return new;
end;
$$ language plpgsql;
create trigger insert_session before insert on session
for each row execute procedure insert_session();
-- Replaces view from 59/01_target_ingress_egress_worker_filters.up.sql
-- Replaced in 64/04_session_list.up.sql
create view session_list as
select
s.public_id,
s.user_id,
shsh.host_id,
s.target_id,
shsh.host_set_id,
s.auth_token_id,
s.project_id,
s.certificate,
s.certificate_private_key,
s.expiration_time,
s.connection_limit,
s.tofu_token,
s.key_id,
s.termination_reason,
s.version,
s.create_time,
s.update_time,
s.endpoint,
s.worker_filter,
s.egress_worker_filter,
s.ingress_worker_filter,
ss.state,
ss.previous_end_time,
ss.start_time,
ss.end_time,
sc.public_id as connection_id,
sc.client_tcp_address,
sc.client_tcp_port,
sc.endpoint_tcp_address,
sc.endpoint_tcp_port,
sc.bytes_up,
sc.bytes_down,
sc.closed_reason
from session s
join session_state ss on
s.public_id = ss.session_id
left join session_connection sc on
s.public_id = sc.session_id
left join session_host_set_host shsh on s.public_id = shsh.session_id;
commit;