Skip to content

Commit

Permalink
Add isImplicit boolean flag to check result (#233)
Browse files Browse the repository at this point in the history
  • Loading branch information
kkajla12 authored Oct 7, 2023
1 parent cb48bac commit 4e73db3
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 58 deletions.
24 changes: 16 additions & 8 deletions pkg/authz/check/service.go
Original file line number Diff line number Diff line change
Expand Up @@ -208,15 +208,17 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf

if warrantCheck.Op == objecttype.InheritIfAllOf {
var processingTime int64
var isImplicit bool
for _, warrantSpec := range warrantCheck.Warrants {
match, decisionPath, err := svc.Check(ctx, authInfo, CheckSpec{
match, decisionPath, implicit, err := svc.Check(ctx, authInfo, CheckSpec{
CheckWarrantSpec: warrantSpec,
Debug: warrantCheck.Debug,
})
if err != nil {
return nil, err
}

isImplicit = isImplicit || implicit
if warrantCheck.Debug {
checkResult.ProcessingTime = processingTime + time.Since(start).Milliseconds()
if len(decisionPath) > 0 {
Expand All @@ -238,6 +240,7 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf

checkResult.Code = http.StatusForbidden
checkResult.Result = NotAuthorized
checkResult.IsImplicit = false
return &checkResult, nil
}

Expand All @@ -249,13 +252,14 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf

checkResult.Code = http.StatusOK
checkResult.Result = Authorized
checkResult.IsImplicit = isImplicit
return &checkResult, nil
}

if warrantCheck.Op == objecttype.InheritIfAnyOf {
var processingTime int64
for _, warrantSpec := range warrantCheck.Warrants {
match, decisionPath, err := svc.Check(ctx, authInfo, CheckSpec{
match, decisionPath, isImplicit, err := svc.Check(ctx, authInfo, CheckSpec{
CheckWarrantSpec: warrantSpec,
Debug: warrantCheck.Debug,
})
Expand Down Expand Up @@ -284,6 +288,7 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf

checkResult.Code = http.StatusOK
checkResult.Result = Authorized
checkResult.IsImplicit = isImplicit
return &checkResult, nil
}

Expand All @@ -297,6 +302,7 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf

checkResult.Code = http.StatusForbidden
checkResult.Result = NotAuthorized
checkResult.IsImplicit = false
return &checkResult, nil
}

Expand All @@ -305,7 +311,7 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf
}

warrantSpec := warrantCheck.Warrants[0]
match, decisionPath, err := svc.Check(ctx, authInfo, CheckSpec{
match, decisionPath, isImplicit, err := svc.Check(ctx, authInfo, CheckSpec{
CheckWarrantSpec: warrantSpec,
Debug: warrantCheck.Debug,
})
Expand Down Expand Up @@ -334,6 +340,7 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf

checkResult.Code = http.StatusOK
checkResult.Result = Authorized
checkResult.IsImplicit = isImplicit
return &checkResult, nil
}

Expand All @@ -344,11 +351,12 @@ func (svc CheckService) CheckMany(ctx context.Context, authInfo *service.AuthInf

checkResult.Code = http.StatusForbidden
checkResult.Result = NotAuthorized
checkResult.IsImplicit = false
return &checkResult, nil
}

// Check returns true if the subject has a warrant (explicitly or implicitly) for given objectType:objectId#relation and context.
func (svc CheckService) Check(ctx context.Context, authInfo *service.AuthInfo, warrantCheck CheckSpec) (bool, []warrant.WarrantSpec, error) {
func (svc CheckService) Check(ctx context.Context, authInfo *service.AuthInfo, warrantCheck CheckSpec) (bool, []warrant.WarrantSpec, bool, error) {
// Used to automatically append tenant context for session token w/ tenantId checks
if authInfo != nil && authInfo.TenantId != "" {
if warrantCheck.CheckWarrantSpec.Context == nil {
Expand All @@ -365,7 +373,7 @@ func (svc CheckService) Check(ctx context.Context, authInfo *service.AuthInfo, w

checkCtx, err := svc.CreateCheckContext(ctx)
if err != nil {
return false, nil, err
return false, nil, false, err
}
childCtx, cancelFunc := context.WithTimeout(checkCtx, svc.CheckConfig.Timeout)
defer cancelFunc()
Expand All @@ -377,14 +385,14 @@ func (svc CheckService) Check(ctx context.Context, authInfo *service.AuthInfo, w
result := <-resultsC

if result.Err != nil {
return false, nil, result.Err
return false, nil, false, result.Err
}

if result.Matched {
return true, result.DecisionPath, nil
return true, result.DecisionPath, len(result.DecisionPath) != 1 || result.DecisionPath[0].Relation != warrantCheck.Relation, nil
}

return false, nil, nil
return false, nil, false, nil
}

type result struct {
Expand Down
1 change: 1 addition & 0 deletions pkg/authz/check/spec.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,7 @@ func (spec SessionCheckManySpec) ToMap() map[string]interface{} {
type CheckResultSpec struct {
Code int64 `json:"code,omitempty"`
Result string `json:"result"`
IsImplicit bool `json:"isImplicit"`
ProcessingTime int64 `json:"processingTime,omitempty"`
DecisionPath map[string][]warrant.WarrantSpec `json:"decisionPath,omitempty"`
}
27 changes: 18 additions & 9 deletions tests/authz-policy.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,8 @@
"statusCode": 200,
"body": {
"code": 200,
"result": "Authorized"
"result": "Authorized",
"isImplicit": false
}
}
},
Expand Down Expand Up @@ -232,7 +233,8 @@
"statusCode": 200,
"body": {
"code": 403,
"result": "Not Authorized"
"result": "Not Authorized",
"isImplicit": false
}
}
},
Expand Down Expand Up @@ -366,7 +368,8 @@
"statusCode": 200,
"body": {
"code": 200,
"result": "Authorized"
"result": "Authorized",
"isImplicit": false
}
}
},
Expand Down Expand Up @@ -396,7 +399,8 @@
"statusCode": 200,
"body": {
"code": 403,
"result": "Not Authorized"
"result": "Not Authorized",
"isImplicit": false
}
}
},
Expand Down Expand Up @@ -479,7 +483,8 @@
"statusCode": 200,
"body": {
"code": 200,
"result": "Authorized"
"result": "Authorized",
"isImplicit": false
}
}
},
Expand Down Expand Up @@ -512,7 +517,8 @@
"statusCode": 200,
"body": {
"code": 403,
"result": "Not Authorized"
"result": "Not Authorized",
"isImplicit": false
}
}
},
Expand Down Expand Up @@ -637,7 +643,8 @@
"statusCode": 200,
"body": {
"code": 200,
"result": "Authorized"
"result": "Authorized",
"isImplicit": false
}
}
},
Expand Down Expand Up @@ -680,7 +687,8 @@
"statusCode": 200,
"body": {
"code": 403,
"result": "Not Authorized"
"result": "Not Authorized",
"isImplicit": false
}
}
},
Expand All @@ -707,7 +715,8 @@
"statusCode": 200,
"body": {
"code": 403,
"result": "Not Authorized"
"result": "Not Authorized",
"isImplicit": false
}
}
},
Expand Down
Loading

0 comments on commit 4e73db3

Please sign in to comment.