@@ -1263,3 +1263,135 @@ func TestAllowOperatorOperation(t *testing.T) {
12631263 must .True (t , acl .AllowOperatorOperation (OperatorCapabilityKeyringRotate ))
12641264 })
12651265}
1266+
1267+ func TestAllowSentinelOperation (t * testing.T ) {
1268+ ci .Parallel (t )
1269+
1270+ testCases := []struct {
1271+ name string
1272+ policy string
1273+ operation string
1274+ expect bool
1275+ }{
1276+ {
1277+ name : "policy write allows sentinel-read" ,
1278+ policy : `sentinel { policy = "write" }` ,
1279+ operation : SentinelCapabilityRead ,
1280+ expect : true ,
1281+ },
1282+ {
1283+ name : "policy write allows sentinel-submit" ,
1284+ policy : `sentinel { policy = "write" }` ,
1285+ operation : SentinelCapabilitySubmit ,
1286+ expect : true ,
1287+ },
1288+ {
1289+ name : "policy write allows sentinel-delete" ,
1290+ policy : `sentinel { policy = "write" }` ,
1291+ operation : SentinelCapabilityDelete ,
1292+ expect : true ,
1293+ },
1294+ {
1295+ name : "policy read allows sentinel-read" ,
1296+ policy : `sentinel { policy = "read" }` ,
1297+ operation : SentinelCapabilityRead ,
1298+ expect : true ,
1299+ },
1300+ {
1301+ name : "policy read denies sentinel-submit" ,
1302+ policy : `sentinel { policy = "read" }` ,
1303+ operation : SentinelCapabilitySubmit ,
1304+ expect : false ,
1305+ },
1306+ {
1307+ name : "policy read denies sentinel-delete" ,
1308+ policy : `sentinel { policy = "read" }` ,
1309+ operation : SentinelCapabilityDelete ,
1310+ expect : false ,
1311+ },
1312+ {
1313+ name : "policy deny overrides all capabilities" ,
1314+ policy : `sentinel { policy = "deny"
1315+ capabilities = ["sentinel-read"] }` ,
1316+ operation : SentinelCapabilityRead ,
1317+ expect : false ,
1318+ },
1319+ {
1320+ name : "capability sentinel-submit allows sentinel-submit over read policy" ,
1321+ policy : `sentinel { policy = "read"
1322+ capabilities = ["sentinel-submit"] }` ,
1323+ operation : SentinelCapabilitySubmit ,
1324+ expect : true ,
1325+ },
1326+ {
1327+ name : "capability sentinel-read allows sentinel-read" ,
1328+ policy : `sentinel { capabilities = ["sentinel-read"] }` ,
1329+ operation : SentinelCapabilityRead ,
1330+ expect : true ,
1331+ },
1332+ {
1333+ name : "multiple capabilities allow respective operations" ,
1334+ policy : `sentinel { capabilities = ["sentinel-read", "sentinel-submit"] }` ,
1335+ operation : SentinelCapabilitySubmit ,
1336+ expect : true ,
1337+ },
1338+ {
1339+ name : "capability deny denies all operations" ,
1340+ policy : `sentinel { capabilities = ["deny"] }` ,
1341+ operation : SentinelCapabilityRead ,
1342+ expect : false ,
1343+ },
1344+ {
1345+ name : "capability deny takes precedence over other capabilities" ,
1346+ policy : `sentinel { capabilities = ["sentinel-read", "deny"] }` ,
1347+ operation : SentinelCapabilityRead ,
1348+ expect : false ,
1349+ },
1350+ {
1351+ name : "deny everything without a sentinel policy" ,
1352+ policy : `agent { policy = "read" }` ,
1353+ operation : SentinelCapabilityRead ,
1354+ expect : false ,
1355+ },
1356+ }
1357+
1358+ for _ , tc := range testCases {
1359+ t .Run (tc .name , func (t * testing.T ) {
1360+ policy , err := Parse (tc .policy , PolicyParseStrict )
1361+ must .NoError (t , err )
1362+
1363+ acl , err := NewACL (false , []* Policy {policy })
1364+ must .NoError (t , err )
1365+
1366+ got := acl .AllowSentinelOperation (tc .operation )
1367+ must .Eq (t , tc .expect , got )
1368+ })
1369+ }
1370+
1371+ t .Run ("nil ACL denies all operations" , func (t * testing.T ) {
1372+ var acl * ACL
1373+ must .False (t , acl .AllowSentinelOperation (SentinelCapabilityRead ))
1374+ })
1375+
1376+ t .Run ("management token allows all operations" , func (t * testing.T ) {
1377+ acl , err := NewACL (true , nil )
1378+ must .NoError (t , err )
1379+ must .True (t , acl .AllowSentinelOperation (SentinelCapabilityRead ))
1380+ must .True (t , acl .AllowSentinelOperation (SentinelCapabilitySubmit ))
1381+ must .True (t , acl .AllowSentinelOperation (SentinelCapabilityDelete ))
1382+ })
1383+
1384+ t .Run ("ACLs disabled denies all operations" , func (t * testing.T ) {
1385+ acl := & ACL {aclsDisabled : true }
1386+ must .False (t , acl .AllowSentinelOperation (SentinelCapabilityRead ))
1387+ must .False (t , acl .AllowSentinelOperation (SentinelCapabilitySubmit ))
1388+ must .False (t , acl .AllowSentinelOperation (SentinelCapabilityDelete ))
1389+ })
1390+
1391+ t .Run ("server write policy allows all operations" , func (t * testing.T ) {
1392+ acl := & ACL {server : PolicyWrite }
1393+ must .True (t , acl .AllowSentinelOperation (SentinelCapabilityRead ))
1394+ must .True (t , acl .AllowSentinelOperation (SentinelCapabilitySubmit ))
1395+ must .True (t , acl .AllowSentinelOperation (SentinelCapabilityDelete ))
1396+ })
1397+ }
0 commit comments