-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
891e24b
commit c90ff9c
Showing
8 changed files
with
117 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,77 @@ | ||
package tests | ||
|
||
import ( | ||
"fmt" | ||
|
||
"github.com/flanksource/duty/models" | ||
. "github.com/onsi/ginkgo/v2" | ||
. "github.com/onsi/gomega" | ||
"github.com/samber/lo" | ||
"gorm.io/gorm" | ||
) | ||
|
||
type testCase struct { | ||
name string | ||
jwtClaims string | ||
expectedCount *int64 | ||
} | ||
|
||
func verifyConfigCount(session *gorm.DB, jwtClaims string, expectedCount int64) { | ||
Expect(session.Exec(fmt.Sprintf("SET request.jwt.claims = '%s'", jwtClaims)).Error).To(BeNil()) | ||
|
||
var count int64 | ||
Expect(session.Model(&models.ConfigItem{}).Count(&count).Error).To(BeNil()) | ||
Expect(count).To(Equal(expectedCount)) | ||
} | ||
|
||
var _ = Describe("RLS test", Ordered, func() { | ||
var ( | ||
tx *gorm.DB | ||
totalConfigs int64 | ||
numConfigsWithFlanksourceTag int64 | ||
) | ||
|
||
BeforeAll(func() { | ||
tx = DefaultContext.DB().Begin() | ||
|
||
Expect(DefaultContext.DB().Model(&models.ConfigItem{}).Count(&totalConfigs).Error).To(BeNil()) | ||
Expect(DefaultContext.DB().Where("tags->>'account' = 'flanksource'").Model(&models.ConfigItem{}).Count(&numConfigsWithFlanksourceTag).Error).To(BeNil()) | ||
}) | ||
|
||
AfterAll(func() { | ||
Expect(tx.Commit().Error).To(BeNil()) | ||
}) | ||
|
||
for _, role := range []string{"postgrest_anon", "postgrest_api"} { | ||
Context(role, Ordered, func() { | ||
BeforeAll(func() { | ||
Expect(tx.Exec(fmt.Sprintf("SET ROLE '%s'", role)).Error).To(BeNil()) | ||
|
||
var currentRole string | ||
Expect(tx.Raw("SELECT CURRENT_USER").Scan(¤tRole).Error).To(BeNil()) | ||
Expect(currentRole).To(Equal(role)) | ||
}) | ||
|
||
DescribeTable("JWT claim tests", | ||
func(tc testCase) { | ||
verifyConfigCount(tx, tc.jwtClaims, *tc.expectedCount) | ||
}, | ||
Entry("no permissions", testCase{ | ||
name: "no permissions", | ||
jwtClaims: `{"tags": {"cluster": "testing-cluster"}, "agents": ["10000000-0000-0000-0000-000000000000"]}`, | ||
expectedCount: lo.ToPtr(int64(0)), | ||
}), | ||
Entry("correct agent", testCase{ | ||
name: "correct agent", | ||
jwtClaims: `{"tags": {"cluster": "testing-cluster"}, "agents": ["00000000-0000-0000-0000-000000000000"]}`, | ||
expectedCount: &totalConfigs, | ||
}), | ||
Entry("correct tag", testCase{ | ||
name: "correct tag", | ||
jwtClaims: `{"tags": {"account": "flanksource"}, "agents": ["10000000-0000-0000-0000-000000000000"]}`, | ||
expectedCount: &numConfigsWithFlanksourceTag, | ||
}), | ||
) | ||
}) | ||
} | ||
}) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,16 +1,19 @@ | ||
ALTER TABLE config_items DISABLE ROW LEVEL SECURITY; | ||
-- TODO: Re-enable this | ||
-- For some reason, this script gets executed during tests & all the RLS gets messedup | ||
|
||
ALTER TABLE components DISABLE ROW LEVEL SECURITY; | ||
-- ALTER TABLE config_items DISABLE ROW LEVEL SECURITY; | ||
|
||
-- POLICIES | ||
DROP POLICY IF EXISTS config_items_auth ON config_items; | ||
-- ALTER TABLE components DISABLE ROW LEVEL SECURITY; | ||
|
||
DROP POLICY IF EXISTS components_auth ON components; | ||
-- -- POLICIES | ||
-- DROP POLICY IF EXISTS config_items_auth ON config_items; | ||
|
||
-- View owners | ||
ALTER VIEW config_detail OWNER TO current_user; | ||
ALTER VIEW config_summary OWNER TO current_user; | ||
ALTER VIEW config_labels OWNER TO current_user; | ||
ALTER VIEW config_names OWNER TO current_user; | ||
ALTER VIEW config_statuses OWNER TO current_user; | ||
-- DROP POLICY IF EXISTS components_auth ON components; | ||
|
||
-- -- View owners | ||
-- ALTER VIEW config_detail OWNER TO current_user; | ||
-- ALTER VIEW config_summary OWNER TO current_user; | ||
-- ALTER VIEW config_labels OWNER TO current_user; | ||
-- ALTER VIEW config_names OWNER TO current_user; | ||
-- ALTER VIEW config_statuses OWNER TO current_user; | ||
|