Skip to content
GitLab
Projects
Groups
Snippets
Help
Loading...
Help
What's new
7
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Open sidebar
ai3
accountserver
Commits
8563c725
Commit
8563c725
authored
Oct 01, 2020
by
ale
Browse files
Options
Browse Files
Download
Email Patches
Plain Diff
Add limits to backend search methods
parent
b50f0ae9
Pipeline
#7845
passed with stages
in 1 minute and 48 seconds
Changes
6
Pipelines
1
Show whitespace changes
Inline
Side-by-side
Showing
6 changed files
with
22 additions
and
16 deletions
+22
-16
actions_resource.go
actions_resource.go
+2
-1
actions_test.go
actions_test.go
+2
-2
actions_user.go
actions_user.go
+2
-1
backend/ldap/model.go
backend/ldap/model.go
+12
-8
backend/ldap/model_test.go
backend/ldap/model_test.go
+2
-2
service.go
service.go
+2
-2
No files found.
actions_resource.go
View file @
8563c725
...
...
@@ -33,6 +33,7 @@ type SearchResourceRequest struct {
AdminRequestBase
Pattern
string
`json:"pattern"`
Limit
int
`json:"limit"`
}
// Validate the request.
...
...
@@ -50,7 +51,7 @@ type SearchResourceResponse struct {
// Serve the request.
func
(
r
*
SearchResourceRequest
)
Serve
(
rctx
*
RequestContext
)
(
interface
{},
error
)
{
results
,
err
:=
rctx
.
TX
.
SearchResource
(
rctx
.
Context
,
r
.
Pattern
)
results
,
err
:=
rctx
.
TX
.
SearchResource
(
rctx
.
Context
,
r
.
Pattern
,
r
.
Limit
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
actions_test.go
View file @
8563c725
...
...
@@ -56,7 +56,7 @@ func (b *fakeBackend) GetUser(_ context.Context, username string) (*RawUser, err
},
nil
}
func
(
b
*
fakeBackend
)
SearchUser
(
_
context
.
Context
,
pattern
string
)
([]
string
,
error
)
{
func
(
b
*
fakeBackend
)
SearchUser
(
_
context
.
Context
,
pattern
string
,
limit
int
)
([]
string
,
error
)
{
var
out
[]
string
for
username
:=
range
b
.
users
{
if
strings
.
HasPrefix
(
username
,
pattern
)
{
...
...
@@ -95,7 +95,7 @@ func (b *fakeBackend) FindResource(_ context.Context, req FindResourceRequest) (
return
nil
,
nil
}
func
(
b
*
fakeBackend
)
SearchResource
(
_
context
.
Context
,
pattern
string
)
([]
*
RawResource
,
error
)
{
func
(
b
*
fakeBackend
)
SearchResource
(
_
context
.
Context
,
pattern
string
,
limit
int
)
([]
*
RawResource
,
error
)
{
var
out
[]
*
RawResource
for
id
,
r
:=
range
b
.
resources
{
owner
:=
strings
.
Split
(
id
,
"/"
)[
0
]
...
...
actions_user.go
View file @
8563c725
...
...
@@ -31,6 +31,7 @@ type SearchUserRequest struct {
AdminRequestBase
Pattern
string
`json:"pattern"`
Limit
int
`json:"limit"`
}
// Validate the request.
...
...
@@ -48,7 +49,7 @@ type SearchUserResponse struct {
// Serve the request.
func
(
r
*
SearchUserRequest
)
Serve
(
rctx
*
RequestContext
)
(
interface
{},
error
)
{
usernames
,
err
:=
rctx
.
TX
.
SearchUser
(
rctx
.
Context
,
r
.
Pattern
)
usernames
,
err
:=
rctx
.
TX
.
SearchUser
(
rctx
.
Context
,
r
.
Pattern
,
r
.
Limit
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
backend/ldap/model.go
View file @
8563c725
...
...
@@ -348,10 +348,12 @@ func (tx *backendTX) GetUser(ctx context.Context, username string) (*as.RawUser,
return
user
,
nil
}
func
(
tx
*
backendTX
)
SearchUser
(
ctx
context
.
Context
,
pattern
string
)
([]
string
,
error
)
{
func
(
tx
*
backendTX
)
SearchUser
(
ctx
context
.
Context
,
pattern
string
,
limit
int
)
([]
string
,
error
)
{
// First of all, find the main user object, and just that one.
vars
:=
templateVars
{
"pattern"
:
rawVariable
(
pattern
)}
result
,
err
:=
tx
.
search
(
ctx
,
tx
.
backend
.
searchUserQuery
.
query
(
vars
))
req
:=
tx
.
backend
.
searchUserQuery
.
query
(
vars
)
req
.
SizeLimit
=
limit
result
,
err
:=
tx
.
search
(
ctx
,
req
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -493,15 +495,17 @@ func (tx *backendTX) HasAnyResource(ctx context.Context, resourceIDs []as.FindRe
return
false
,
nil
}
func
(
tx
*
backendTX
)
searchResourcesByType
(
ctx
context
.
Context
,
pattern
,
resourceType
string
)
([]
*
as
.
RawResource
,
error
)
{
func
(
tx
*
backendTX
)
searchResourcesByType
(
ctx
context
.
Context
,
pattern
,
resourceType
string
,
limit
int
)
([]
*
as
.
RawResource
,
error
)
{
tpl
,
err
:=
tx
.
backend
.
resources
.
SearchQuery
(
resourceType
)
if
err
!=
nil
{
return
nil
,
err
}
re
sult
,
err
:=
tx
.
search
(
ctx
,
tpl
.
query
(
templateVars
{
re
q
:=
tpl
.
query
(
templateVars
{
"resource"
:
rawVariable
(
pattern
),
"type"
:
resourceType
,
}))
})
req
.
SizeLimit
=
limit
result
,
err
:=
tx
.
search
(
ctx
,
req
)
if
err
!=
nil
{
if
ldap
.
IsErrorWithCode
(
err
,
ldap
.
LDAPResultNoSuchObject
)
{
return
nil
,
nil
...
...
@@ -528,7 +532,7 @@ func (tx *backendTX) searchResourcesByType(ctx context.Context, pattern, resourc
// FindResource fetches a specific resource by type and name.
func
(
tx
*
backendTX
)
FindResource
(
ctx
context
.
Context
,
spec
as
.
FindResourceRequest
)
(
*
as
.
RawResource
,
error
)
{
result
,
err
:=
tx
.
searchResourcesByType
(
ctx
,
spec
.
Name
,
spec
.
Type
)
result
,
err
:=
tx
.
searchResourcesByType
(
ctx
,
spec
.
Name
,
spec
.
Type
,
0
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
@@ -543,11 +547,11 @@ func (tx *backendTX) FindResource(ctx context.Context, spec as.FindResourceReque
}
// SearchResource returns all the resources matching the pattern.
func
(
tx
*
backendTX
)
SearchResource
(
ctx
context
.
Context
,
pattern
string
)
([]
*
as
.
RawResource
,
error
)
{
func
(
tx
*
backendTX
)
SearchResource
(
ctx
context
.
Context
,
pattern
string
,
limit
int
)
([]
*
as
.
RawResource
,
error
)
{
// Aggregate results for all known resource types.
var
out
[]
*
as
.
RawResource
for
_
,
typ
:=
range
tx
.
backend
.
resources
.
types
{
r
,
err
:=
tx
.
searchResourcesByType
(
ctx
,
pattern
,
typ
)
r
,
err
:=
tx
.
searchResourcesByType
(
ctx
,
pattern
,
typ
,
limit
)
if
err
!=
nil
{
return
nil
,
err
}
...
...
backend/ldap/model_test.go
View file @
8563c725
...
...
@@ -197,7 +197,7 @@ func TestModel_SearchUser(t *testing.T) {
stop
,
b
:=
startServer
(
t
)
defer
stop
()
tx
,
_
:=
b
.
NewTransaction
()
users
,
err
:=
tx
.
SearchUser
(
context
.
Background
(),
"uno"
)
users
,
err
:=
tx
.
SearchUser
(
context
.
Background
(),
"uno"
,
0
)
if
err
!=
nil
{
t
.
Fatal
(
err
)
}
...
...
@@ -288,7 +288,7 @@ func TestModel_SearchResource(t *testing.T) {
for
_
,
pattern
:=
range
[]
string
{
"uno@investici.org"
,
"uno*"
}
{
tx
,
_
:=
b
.
NewTransaction
()
resources
,
err
:=
tx
.
SearchResource
(
context
.
Background
(),
"uno@investici.org"
)
resources
,
err
:=
tx
.
SearchResource
(
context
.
Background
(),
"uno@investici.org"
,
0
)
if
err
!=
nil
{
t
.
Fatalf
(
"SearchUser(%s): %v"
,
pattern
,
err
)
}
...
...
service.go
View file @
8563c725
...
...
@@ -69,10 +69,10 @@ type TX interface {
// Lightweight user search (backend-specific pattern).
// Returns list of matching usernames.
SearchUser
(
context
.
Context
,
string
)
([]
string
,
error
)
SearchUser
(
context
.
Context
,
string
,
int
)
([]
string
,
error
)
// Resource search (backend-specific pattern).
SearchResource
(
context
.
Context
,
string
)
([]
*
RawResource
,
error
)
SearchResource
(
context
.
Context
,
string
,
int
)
([]
*
RawResource
,
error
)
// Resource ACL check (does not necessarily hit the database).
CanAccessResource
(
context
.
Context
,
string
,
*
Resource
)
bool
...
...
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment