feat(dw): Insert/update auth token fact when auth token updated

This adds a function and update trigger to auth_token to ensure that:

1. If the auth token is updated to the issued state, a auth token fact
   is added to the warehouse.
2. If the auth token's approximate last access time is updated, the
   corresponding auth token fact is updated with the new time.
This commit is contained in:
Timothy Messier committed 2024-02-23 15:44:08 +00:00
1 parent 585b344da6
commit b938dd6706
2 files changed
+152

No files matched your search

@@ -0,0 +1,38 @@
-- Copyright (c) HashiCorp, Inc.
-- SPDX-License-Identifier: BUSL-1.1
begin;
create function wh_auth_token_accessed(p_auth_token_id wt_public_id, last_accessed_ts wt_timestamp) returns void
as $$
begin
update wh_auth_token_accumulating_fact
set auth_token_approximate_last_access_date_key = wh_date_key(last_accessed_ts),
auth_token_approximate_last_access_time_key = wh_time_key(last_accessed_ts),
auth_token_approximate_last_access_time = last_accessed_ts,
auth_token_approximate_active_time_range = tstzrange(lower(auth_token_approximate_active_time_range), last_accessed_ts, '[]')
where auth_token_id = p_auth_token_id;
return;
end;
$$ language plpgsql;
comment on function wh_auth_token_accessed is
'wh_auth_token_accessed is a function that updates the wh_auth_token_accumulating_fact'
'when an auth_token is accessed.';
create function auth_token_updated() returns trigger
as $$
begin
case
when new.status = 'token issued' and new.status is distinct from old.status then
perform wh_auth_token_issued(new.public_id, new.update_time, new.approximate_last_access_time);
when new.approximate_last_access_time is distinct from old.approximate_last_access_time then
perform wh_auth_token_accessed(new.public_id, new.approximate_last_access_time);
else
return null;
end case;
return null;
end;
$$ language plpgsql;
create trigger auth_token_updated after update on auth_token
for each row execute procedure auth_token_updated();
commit;
@@ -0,0 +1,114 @@
-- Copyright (c) HashiCorp, Inc.
-- SPDX-License-Identifier: BUSL-1.1
begin;
select plan(20);
-- should have an auth token for carly
select is(count(*), 1::bigint)
from auth_token
where public_id = 'tok____carly';
-- should have one fact for carly.
select is(count(*), 1::bigint)
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
-- the fact should have an access time that is less than now.
select cmp_ok(auth_token_approximate_last_access_date_key, '<=', wh_date_key(now()))
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select cmp_ok(auth_token_approximate_last_access_time_key, '<', wh_time_key(now()))
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select cmp_ok(auth_token_approximate_last_access_time, '<', now()::wh_timestamp)
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select cmp_ok(lower(auth_token_approximate_active_time_range), '<', now())
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select cmp_ok(upper(auth_token_approximate_active_time_range), '<', now())
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
-- update access time for carly's auth token.
update auth_token
set approximate_last_access_time = now()
where public_id = 'tok____carly';
-- should still only have one fact for carly.
select is(count(*), 1::bigint)
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
-- the fact should have an access time of now
select is(auth_token_approximate_last_access_date_key, wh_date_key(now()))
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select is(auth_token_approximate_last_access_time_key, wh_time_key(now()))
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select is(auth_token_approximate_last_access_time, now()::wh_timestamp)
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select cmp_ok(lower(auth_token_approximate_active_time_range), '<', now())
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
select is(upper(auth_token_approximate_active_time_range), now())
from wh_auth_token_accumulating_fact
where user_id = 'u______carly';
-- should have an auth token for cora that is in pending status
select is(count(*), 1::bigint)
from auth_token
where public_id = 'tok_____cora'
and status = 'auth token pending';
-- since the auth token is pending, there should be no fact for cora.
select is(count(*), 0::bigint)
from wh_auth_token_accumulating_fact
where user_id = 'u_______cora';
-- updating a pending token to issued should result in a fact for cora.
update auth_token
set status = 'token issued'
where public_id = 'tok_____cora';
select is(count(*), 1::bigint)
from wh_auth_token_accumulating_fact
where user_id = 'u_______cora';
select is(
wh_auth_token_accumulating_fact.*,
row('tok_____cora',
'u_______cora', (select key from wh_user_dimension where user_id = 'u_______cora'),
wh_date_key(now()), wh_time_key(now()), now(),
-1, -1, 'infinity'::timestamptz,
wh_date_key(now()), wh_time_key(now()), now(),
tstzrange(now(), now(), '[]'),
tstzrange(now(), 'infinity'::timestamptz, '[]'),
1
)::wh_auth_token_accumulating_fact
)
from wh_auth_token_accumulating_fact
where user_id = 'u_______cora';
-- should have an auth token for ciara that is in pending status
select is(count(*), 1::bigint)
from auth_token
where public_id = 'tok____ciara'
and status = 'auth token pending';
-- since the auth token is pending, there should be no fact for ciara.
select is(count(*), 0::bigint)
from wh_auth_token_accumulating_fact
where user_id = 'u______ciara';
-- updating a pending token to something other than issued should not result in a fact.
update auth_token
set status = 'authentication failed'
where public_id = 'tok____ciara';
select is(count(*), 0::bigint)
from wh_auth_token_accumulating_fact
where user_id = 'u______ciara';
select * from finish();
rollback;