@@ -929,7 +929,7 @@ func TestAuthorizeScope(t *testing.T) {
929929 Org : map [string ][]Permission {},
930930 User : []Permission {},
931931 },
932- AllowIDList : []string { workspaceID .String ()},
932+ AllowIDList : []AllowListElement {{ Type : ResourceWorkspace . Type , ID : workspaceID .String ()} },
933933 },
934934 }
935935
@@ -1019,7 +1019,9 @@ func TestAuthorizeScope(t *testing.T) {
10191019 User : []Permission {},
10201020 },
10211021 // Empty string allow_list is allowed for actions like 'create'
1022- AllowIDList : []string {"" },
1022+ AllowIDList : []AllowListElement {{
1023+ Type : ResourceWorkspace .Type , ID : "" ,
1024+ }},
10231025 },
10241026 }
10251027
@@ -1145,7 +1147,7 @@ func TestAuthorizeScope(t *testing.T) {
11451147 ResourceUser .Type : {policy .ActionRead },
11461148 }),
11471149 },
1148- AllowIDList : []string { policy . WildcardSymbol },
1150+ AllowIDList : []AllowListElement { AllowListAll () },
11491151 },
11501152 }
11511153
@@ -1163,6 +1165,131 @@ func TestAuthorizeScope(t *testing.T) {
11631165 )
11641166}
11651167
1168+ func TestScopeAllowList (t * testing.T ) {
1169+ t .Parallel ()
1170+
1171+ defOrg := uuid .New ()
1172+
1173+ // Some IDs to use
1174+ wid := uuid .New ()
1175+ gid := uuid .New ()
1176+
1177+ user := Subject {
1178+ ID : "me" ,
1179+ Roles : Roles {
1180+ must (RoleByName (RoleOwner ())),
1181+ },
1182+ Scope : Scope {
1183+ Role : Role {
1184+ Identifier : RoleIdentifier {
1185+ Name : "AllowList" ,
1186+ OrganizationID : defOrg ,
1187+ },
1188+ DisplayName : "AllowList" ,
1189+ // Allow almost everything
1190+ Site : allPermsExcept (ResourceUser ),
1191+ },
1192+ AllowIDList : []AllowListElement {
1193+ {Type : ResourceWorkspace .Type , ID : wid .String ()},
1194+ {Type : ResourceWorkspace .Type , ID : "" }, // Allow to create
1195+ {Type : ResourceTemplate .Type , ID : policy .WildcardSymbol },
1196+ {Type : ResourceGroup .Type , ID : gid .String ()},
1197+
1198+ // This scope allows all users, but the permissions do not.
1199+ {Type : ResourceUser .Type , ID : policy .WildcardSymbol },
1200+ },
1201+ },
1202+ }
1203+
1204+ testAuthorize (t , "AllowList" , user ,
1205+ // Allowed:
1206+ cases (func (c authTestCase ) authTestCase {
1207+ c .allow = true
1208+ return c
1209+ },
1210+ []authTestCase {
1211+ {resource : ResourceWorkspace .InOrg (defOrg ).WithOwner (user .ID ).WithID (wid ), actions : []policy.Action {policy .ActionRead }},
1212+ // matching on empty id
1213+ {resource : ResourceWorkspace .InOrg (defOrg ).WithOwner (user .ID ), actions : []policy.Action {policy .ActionCreate }},
1214+
1215+ // Template has wildcard ID, so any uuid is allowed, including the empty
1216+ {resource : ResourceTemplate .InOrg (defOrg ).WithID (uuid .New ()), actions : AllActions ()},
1217+ {resource : ResourceTemplate .InOrg (defOrg ).WithID (uuid .New ()), actions : AllActions ()},
1218+ {resource : ResourceTemplate .InOrg (defOrg ), actions : AllActions ()},
1219+
1220+ // Group
1221+ {resource : ResourceGroup .InOrg (defOrg ).WithID (gid ), actions : []policy.Action {policy .ActionRead }},
1222+ },
1223+ ),
1224+
1225+ // Not allowed:
1226+ cases (func (c authTestCase ) authTestCase {
1227+ c .allow = false
1228+ return c
1229+ },
1230+ []authTestCase {
1231+ // Has the scope and allow list, but not the permission
1232+ {resource : ResourceUser .WithOwner (user .ID ), actions : []policy.Action {policy .ActionRead }},
1233+
1234+ // `wid` matches on the uuid, but not the type
1235+ {resource : ResourceGroup .WithID (wid ), actions : []policy.Action {policy .ActionRead }},
1236+
1237+ // no empty id for the create action
1238+ {resource : ResourceGroup .InOrg (defOrg ), actions : []policy.Action {policy .ActionCreate }},
1239+ },
1240+ ),
1241+ )
1242+
1243+ // Wildcard type
1244+ user = Subject {
1245+ ID : "me" ,
1246+ Roles : Roles {
1247+ must (RoleByName (RoleOwner ())),
1248+ },
1249+ Scope : Scope {
1250+ Role : Role {
1251+ Identifier : RoleIdentifier {
1252+ Name : "WildcardType" ,
1253+ OrganizationID : defOrg ,
1254+ },
1255+ DisplayName : "WildcardType" ,
1256+ // Allow almost everything
1257+ Site : allPermsExcept (ResourceUser ),
1258+ },
1259+ AllowIDList : []AllowListElement {
1260+ {Type : policy .WildcardSymbol , ID : wid .String ()},
1261+ },
1262+ },
1263+ }
1264+
1265+ testAuthorize (t , "WildcardType" , user ,
1266+ // Allowed:
1267+ cases (func (c authTestCase ) authTestCase {
1268+ c .allow = true
1269+ return c
1270+ },
1271+ []authTestCase {
1272+ // anything with the id is ok
1273+ {resource : ResourceWorkspace .InOrg (defOrg ).WithOwner (user .ID ).WithID (wid ), actions : []policy.Action {policy .ActionRead }},
1274+ {resource : ResourceGroup .InOrg (defOrg ).WithID (wid ), actions : []policy.Action {policy .ActionRead }},
1275+ {resource : ResourceTemplate .InOrg (defOrg ).WithID (wid ), actions : []policy.Action {policy .ActionRead }},
1276+ },
1277+ ),
1278+
1279+ // Not allowed:
1280+ cases (func (c authTestCase ) authTestCase {
1281+ c .allow = false
1282+ return c
1283+ },
1284+ []authTestCase {
1285+ // Anything without the id is not allowed
1286+ {resource : ResourceWorkspace .InOrg (defOrg ).WithOwner (user .ID ), actions : []policy.Action {policy .ActionCreate }},
1287+ {resource : ResourceWorkspace .InOrg (defOrg ).WithOwner (user .ID ).WithID (uuid .New ()), actions : []policy.Action {policy .ActionRead }},
1288+ },
1289+ ),
1290+ )
1291+ }
1292+
11661293// cases applies a given function to all test cases. This makes generalities easier to create.
11671294func cases (opt func (c authTestCase ) authTestCase , cases []authTestCase ) []authTestCase {
11681295 if opt == nil {
0 commit comments