Skip to content

Commit

Permalink
login policy with teams
Browse files Browse the repository at this point in the history
  • Loading branch information
Danielle authored and Daniellem97 committed Jan 4, 2024
1 parent e61a21f commit 2fc9640
Show file tree
Hide file tree
Showing 3 changed files with 99 additions and 0 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ _Access policies have been deprecated. Please [read this](./access/README.md) fo
- [GitHub](./login/external-contributor-access-github.rego)
- [Google](./login/external-contributor-access-google.rego)
- [Managing access levels within an organization](./login/access-levels-within-an-organization.rego)
- [Readers Writers Admins Teams](./login/readers-writers-admins-teams.rego)
- [Rewriting User Teams](./login/rewriting-user-teams.rego)
- [Who When Where Login Restrictions](./login/who-when-where-login-restrictions.rego)

Expand Down
43 changes: 43 additions & 0 deletions login/readers-writers-admins-teams.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
package spacelift

# Define team roles
writers := {"team1", "team2", "team3"}

admins := {"team4", "team5", "team6"}

readers := {"team7", "team8", "team9"}

# Extract login from session
login := input.session.teams

# Allow based on team role
allow { # Allow writers
writers[login]
}

allow { # Allow admins
admins[login]
}

allow { # Allow readers
readers[login]
}

# Space access rules
# Check if user is an admin and assign admin access to the space
space_admin[space.id] {
space := input.spaces[_]
admins[login]
}

# Check if user is a writer and assign write access to the space
space_write[space.id] {
space := input.spaces[_]
writers[login]
}

# Check if user is a reader and assign read access to the space
space_read[space.id] {
space := input.spaces[_]
readers[login]
}
55 changes: 55 additions & 0 deletions login/readers-writers-admins-teams_test.rego
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
package spacelift

# Test for writers
test_allow_writers {
allow with input as {"session": {"teams": "team1"}} with input.spaces as [{"id": "space1"}, {"id": "space2"}]
}

test_deny_non_writers {
not allow with input as {"session": {"teams": "non_writer_team"}} with input.spaces as [{"id": "space1"}, {"id": "space2"}]
}

# Test for admins
test_allow_admins {
allow with input as {"session": {"teams": "team4"}} with input.spaces as [{"id": "space1"}, {"id": "space2"}]
}

test_deny_non_admins {
not allow with input as {"session": {"teams": "non_admin_team"}} with input.spaces as [{"id": "space1"}, {"id": "space2"}]
}

# Test for readers
test_allow_readers {
allow with input as {"session": {"teams": "team7"}} with input.spaces as [{"id": "space1"}, {"id": "space2"}]
}

test_deny_non_readers {
not allow with input as {"session": {"teams": "non_reader_team"}} with input.spaces as [{"id": "space1"}, {"id": "space2"}]
}

# Test space access for admins
test_space_admin_access {
space_admin.space1 with input as {"session": {"teams": "team4"}, "spaces": [{"id": "space1"}, {"id": "space2"}]}
}

test_space_admin_no_access {
not space_admin.space1 with input as {"session": {"teams": "team1"}, "spaces": [{"id": "space1"}, {"id": "space2"}]}
}

# Test space access for writers
test_space_write_access {
space_write.space1 with input as {"session": {"teams": "team1"}, "spaces": [{"id": "space1"}, {"id": "space2"}]}
}

test_space_write_no_access {
not space_write.space1 with input as {"session": {"teams": "team7"}, "spaces": [{"id": "space1"}, {"id": "space2"}]}
}

# Test space access for readers
test_space_read_access {
space_read.space1 with input as {"session": {"teams": "team7"}, "spaces": [{"id": "space1"}, {"id": "space2"}]}
}

test_space_read_no_access {
not space_read.space1 with input as {"session": {"teams": "team1"}, "spaces": [{"id": "space1"}, {"id": "space2"}]}
}

0 comments on commit 2fc9640

Please sign in to comment.