Skip to content

Commit

Permalink
refactor: configuration structure for limits
Browse files Browse the repository at this point in the history
  • Loading branch information
zepatrik committed Feb 7, 2022
1 parent 3af469e commit ffa99ec
Show file tree
Hide file tree
Showing 5 changed files with 26 additions and 18 deletions.
2 changes: 1 addition & 1 deletion internal/check/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ func (e *Engine) checkOneIndirectionFurther(

func (e *Engine) SubjectIsAllowed(ctx context.Context, r *relationtuple.InternalRelationTuple, restDepth int) (bool, error) {
// global max-depth takes precedence when it is the lesser or if the request max-depth is less than or equal to 0
if globalMaxDepth := e.d.Config().ReadAPIMaxDepth(); restDepth <= 0 || globalMaxDepth < restDepth {
if globalMaxDepth := e.d.Config().MaxReadDepth(); restDepth <= 0 || globalMaxDepth < restDepth {
restDepth = globalMaxDepth
}

Expand Down
6 changes: 3 additions & 3 deletions internal/check/engine_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,7 @@ func TestEngine(t *testing.T) {
}

// global max-depth defaults to 5
assert.Equal(t, reg.Config().ReadAPIMaxDepth(), 5)
assert.Equal(t, reg.Config().MaxReadDepth(), 5)

// req max-depth takes precedence, max-depth=2 is not enough
res, err := e.SubjectIsAllowed(context.Background(), userHasAccess, 2)
Expand All @@ -106,13 +106,13 @@ func TestEngine(t *testing.T) {
assert.True(t, res)

// global max-depth takes precedence and max-depth=2 is not enough
require.NoError(t, reg.Config().Set(config.KeyReadMaxDepth, 2))
require.NoError(t, reg.Config().Set(config.KeyLimitMaxReadDepth, 2))
res, err = e.SubjectIsAllowed(context.Background(), userHasAccess, 3)
require.NoError(t, err)
assert.False(t, res)

// global max-depth takes precedence and max-depth=3 is enough
require.NoError(t, reg.Config().Set(config.KeyReadMaxDepth, 3))
require.NoError(t, reg.Config().Set(config.KeyLimitMaxReadDepth, 3))
res, err = e.SubjectIsAllowed(context.Background(), userHasAccess, 0)
require.NoError(t, err)
assert.True(t, res)
Expand Down
24 changes: 16 additions & 8 deletions internal/driver/config/config.schema.json
Original file line number Diff line number Diff line change
Expand Up @@ -232,14 +232,6 @@
},
"tls": {
"$ref": "#/definitions/tlsx"
},
"max-depth": {
"type": "integer",
"default": 5,
"title": "Global maximum depth",
"description": "The global maximum depth on all read operations. This can be decreased for a request by a value specified on the request, this applies only if the request-specific value is greater than 1 and less than the global maximum depth.",
"minimum": 1,
"maximum": 65535
}
}
},
Expand Down Expand Up @@ -310,6 +302,22 @@
}
]
},
"limit": {
"type": "object",
"title": "Limits",
"description": "Limits aiming to control the resource consumption. These limits are not a sufficient replacement for rate-limiting.",
"properties": {
"max_read_depth": {
"type": "integer",
"default": 5,
"title": "Global maximum read depth",
"description": "The global maximum depth on all read operations. Note that this does not affect how deeply nested the tuples can be. This value can be decreased for a request by a value specified on the request, only if the request-specific value is greater than 1 and less than the global maximum depth.",
"minimum": 1,
"maximum": 65535
}
},
"additionalProperties": false
},
"version": {
"type": "string",
"title": "The Keto version this config is written for.",
Expand Down
10 changes: 5 additions & 5 deletions internal/driver/config/provider.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,9 +29,9 @@ var Schema []byte
const (
KeyDSN = "dsn"

KeyReadMaxDepth = "serve.read.max-depth"
KeyReadAPIHost = "serve.read.host"
KeyReadAPIPort = "serve.read.port"
KeyLimitMaxReadDepth = "limit.max_read_depth"
KeyReadAPIHost = "serve.read.host"
KeyReadAPIPort = "serve.read.port"

KeyWriteAPIHost = "serve.write.host"
KeyWriteAPIPort = "serve.write.port"
Expand Down Expand Up @@ -140,8 +140,8 @@ func (k *Config) ReadAPIListenOn() string {
)
}

func (k *Config) ReadAPIMaxDepth() int {
return k.p.Int(KeyReadMaxDepth)
func (k *Config) MaxReadDepth() int {
return k.p.Int(KeyLimitMaxReadDepth)
}

func (k *Config) WriteAPIListenOn() string {
Expand Down
2 changes: 1 addition & 1 deletion internal/expand/engine.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ func NewEngine(d EngineDependencies) *Engine {

func (e *Engine) BuildTree(ctx context.Context, subject relationtuple.Subject, restDepth int) (*Tree, error) {
// global max-depth takes precedence when it is the lesser or if the request max-depth is less than or equal to 0
if globalMaxDepth := e.d.Config().ReadAPIMaxDepth(); restDepth <= 0 || globalMaxDepth < restDepth {
if globalMaxDepth := e.d.Config().MaxReadDepth(); restDepth <= 0 || globalMaxDepth < restDepth {
restDepth = globalMaxDepth
}

Expand Down

0 comments on commit ffa99ec

Please sign in to comment.