sourcegraph/internal/authz
Geoffrey Gilmore 57de59cb3c
internal/database/sub_repo_permissions: modify store to be able to insert ip based permissions (#63811)
Closes https://linear.app/sourcegraph/issue/SRC-459/
Closes 

This PR adds support for saving and retreiving the IP addressess
associated with each path rule in the sub_repo_permissions store.

It does this by:

**Adding a new permissions type to the internal/authz package**:


1be7df6d79/internal/authz/iface.go (L52-L96)

**Adding new `*WithIPs` versions of all the setter and getter methods** 

The new setter methods uses the above `authz.SubRepoPermissionsWithIPs`
type that write to the appropriate `ips` column in the DB.

The new getter methods retrieve the ip addresses associated with each
path entry. However, here there is an additional complication: It's
possible for someone to call the `*WithIPs` getters when the ips column
is still NULL (indicating that the perforce syncer hasn't been updated /
ran in order to save the IP addresses from the protection table yet.

| repo_id | user_id | version | updated_at | paths | ips |
|---------|---------|---------|------------|-------|-----|
| 1 | 1 | 1 | 2023-07-01 10:00:00 | {`"/depot/main/..."`,
`"/depot/dev/..."`, `"-/depot/secret/..."`} | NULL |
| 2 | 1 | 1 | 2023-07-01 11:00:00 | {`"/depot/public/..."`,
`"-/depot/private/..."`} | NULL |

In order to address this, the getters each have a `backfill` boolean
that allows the caller to choose the behavior that they want.

- If `backfill = true`, the paths without IP entries will be returned
with a `*` (wildcard) IP indicating that any client IP address is okay.
(This is effectively the behavior we have today since we don't check IPs
for sub_repo_permisisons). I imagine this can be used when callers don't
care about enforcing IP-based permissions (such as when IP address
enforcement is disabled in site configuration).

- If `backfill = false`, if the IPs column is NULL - an error is
returned instead of backfilling ("The IP addresses associated with this
sub-repository-permissions entry have not been synced yet."). This
allows for callers that care about IP address enforcement to know
_explicitly_ if the IP address information hasn't been updated yet - so
we can't know whether or not the user is able to view the file (e.g when
IP based enforcement is enabled).


**Ensuring that the old setter methods set the IPs column to NULL**: 

self-explanatory, if someone uses the non `*WithIP` variants of the
setters, we want to ensure that we zero out that column so that we don't
leave stale / inconsistent information for those Path entries.

---

Overall, the design this adds the new IP address functionality without
having to immediately update all the call sites in the codebase to force
them to interpret all this information (which would make for a
gargantuan PR). Eventually, we should be able to simply delete the old
versions of the setters/getters once the IP address functioanlity has
been threaded through everywhere.

## Test plan

Extensive unit tests. 

For each new setter and getter, I added unit tests that tested along all
of the following dimenisons:

- **initial store state**: empty database, database seeded with
permissions with no IP information (paths column only), database seeded
with permissions that have the IP information synced
- **insertion method**: was the data for the test inserted **with IP
information** (using the `withIP` variant of upsert, etc.), or was it
inserted with the old legacy way with no ip information
- **retreieval method**: was the data reterived with the legacy getters
(that don't look at the IP information), with the new IP getters that
either backfill (if the IP information for that paths entry hasn't been
synced yet, it will return an `*` for that entry), or avoids backfilling
(will return the information in the IPs column, or hard-error)?
## Changelog

- The sub_repository_permissions_ database store can now save and
retrieve the IP addresses associated with each path rule.
2024-07-18 14:05:30 -07:00
..
permssync bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
providers rcache: Explicitly pass redis pool to use (#63644) 2024-07-10 01:23:19 +02:00
subrepoperms chore: Simplify path trimming logic in perm checking (#63574) 2024-07-01 17:53:41 +08:00
types bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
BUILD.bazel bazel: transcribe test ownership to bazel tags (#62664) 2024-05-16 15:51:16 +01:00
CODENOTIFY CODENOTIFY: subscribe changes for joe (#15141) 2020-10-29 15:31:53 +08:00
consts.go Move [enterprise/]cmd/frontend/authz to [enterprise/]internal/authz (#12538) 2020-07-29 10:08:36 -05:00
header_test.go dotcom: MockSourcegraphDotComMode requires a T for cleanup (#61172) 2024-03-14 20:27:21 +00:00
header.go Move dotcom check out of cmd/frontend (#60810) 2024-03-04 16:05:16 +00:00
iface_test.go errors: Introduce internal package (#30558) 2022-02-07 15:03:45 +00:00
iface.go internal/database/sub_repo_permissions: modify store to be able to insert ip based permissions (#63811) 2024-07-18 14:05:30 -07:00
mocks_temp.go authz: Move sub-repo perms to enterprise (#45659) 2022-12-28 10:44:07 +02:00
perms_test.go Replace all traditional for-loops (#60988) 2024-03-11 16:05:47 +02:00
perms.go chore: Add specialized function for sorting primitive sets (#63269) 2024-06-17 17:02:50 +02:00
register.go allow repo access by default on dotcom (#63367) 2024-06-20 11:55:20 -07:00
scopes.go Move [enterprise/]cmd/frontend/authz to [enterprise/]internal/authz (#12538) 2020-07-29 10:08:36 -05:00
sub_repo_perms_test.go [sub-repo perms] Move check to CanReadAnyPath instead (#53222) 2023-06-09 14:24:45 +02:00
sub_repo_perms.go Cache results of sub-repo perms enabled checks (#53463) 2023-06-14 14:28:02 +00:00