Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Sign in
Toggle navigation
Menu
Open sidebar
小 白蛋
Nomad
Commits
0144b513
Commit
0144b513
authored
7 years ago
by
Michael Schurter
Committed by
GitHub
7 years ago
Browse files
Options
Download
Plain Diff
Merge pull request #3329 from hashicorp/f-acl-client-stats
/v1/client/stats ACL enforcement
parents
4a0b3a6f
bdc65ee3
Changes
3
Hide whitespace changes
Inline
Side-by-side
Showing
3 changed files
command/agent/stats_endpoint.go
+15
-1
command/agent/stats_endpoint.go
command/agent/stats_endpoint_test.go
+52
-0
command/agent/stats_endpoint_test.go
website/source/api/client.html.md
+1
-1
website/source/api/client.html.md
with
68 additions
and
2 deletions
+68
-2
command/agent/stats_endpoint.go
+
15
-
1
View file @
0144b513
package
agent
import
"net/http"
import
(
"net/http"
"github.com/hashicorp/nomad/nomad/structs"
)
func
(
s
*
HTTPServer
)
ClientStatsRequest
(
resp
http
.
ResponseWriter
,
req
*
http
.
Request
)
(
interface
{},
error
)
{
if
s
.
agent
.
client
==
nil
{
return
nil
,
clientNotRunning
}
var
secret
string
s
.
parseToken
(
req
,
&
secret
)
// Check node read permissions
if
aclObj
,
err
:=
s
.
agent
.
Client
()
.
ResolveToken
(
secret
);
err
!=
nil
{
return
nil
,
err
}
else
if
aclObj
!=
nil
&&
!
aclObj
.
AllowNodeRead
()
{
return
nil
,
structs
.
ErrPermissionDenied
}
clientStats
:=
s
.
agent
.
client
.
StatsReporter
()
return
clientStats
.
LatestHostStats
(),
nil
}
This diff is collapsed.
Click to expand it.
command/agent/stats_endpoint_test.go
+
52
-
0
View file @
0144b513
...
...
@@ -4,6 +4,11 @@ import (
"net/http"
"net/http/httptest"
"testing"
"github.com/hashicorp/nomad/acl"
"github.com/hashicorp/nomad/nomad/mock"
"github.com/hashicorp/nomad/nomad/structs"
"github.com/stretchr/testify/assert"
)
func
TestClientStatsRequest
(
t
*
testing
.
T
)
{
...
...
@@ -21,3 +26,50 @@ func TestClientStatsRequest(t *testing.T) {
}
})
}
func
TestClientStatsRequest_ACL
(
t
*
testing
.
T
)
{
t
.
Parallel
()
assert
:=
assert
.
New
(
t
)
httpACLTest
(
t
,
nil
,
func
(
s
*
TestAgent
)
{
state
:=
s
.
Agent
.
server
.
State
()
req
,
err
:=
http
.
NewRequest
(
"GET"
,
"/v1/client/stats/"
,
nil
)
assert
.
Nil
(
err
)
// Try request without a token and expect failure
{
respW
:=
httptest
.
NewRecorder
()
_
,
err
:=
s
.
Server
.
ClientStatsRequest
(
respW
,
req
)
assert
.
NotNil
(
err
)
assert
.
Equal
(
err
.
Error
(),
structs
.
ErrPermissionDenied
.
Error
())
}
// Try request with an invalid token and expect failure
{
respW
:=
httptest
.
NewRecorder
()
token
:=
mock
.
CreatePolicyAndToken
(
t
,
state
,
1005
,
"invalid"
,
mock
.
NodePolicy
(
acl
.
PolicyDeny
))
setToken
(
req
,
token
)
_
,
err
:=
s
.
Server
.
ClientStatsRequest
(
respW
,
req
)
assert
.
NotNil
(
err
)
assert
.
Equal
(
err
.
Error
(),
structs
.
ErrPermissionDenied
.
Error
())
}
// Try request with a valid token
{
respW
:=
httptest
.
NewRecorder
()
token
:=
mock
.
CreatePolicyAndToken
(
t
,
state
,
1007
,
"valid"
,
mock
.
NodePolicy
(
acl
.
PolicyRead
))
setToken
(
req
,
token
)
_
,
err
:=
s
.
Server
.
ClientStatsRequest
(
respW
,
req
)
assert
.
Nil
(
err
)
assert
.
Equal
(
http
.
StatusOK
,
respW
.
Code
)
}
// Try request with a management token
{
respW
:=
httptest
.
NewRecorder
()
setToken
(
req
,
s
.
RootToken
)
_
,
err
:=
s
.
Server
.
ClientStatsRequest
(
respW
,
req
)
assert
.
Nil
(
err
)
assert
.
Equal
(
http
.
StatusOK
,
respW
.
Code
)
}
})
}
This diff is collapsed.
Click to expand it.
website/source/api/client.html.md
+
1
-
1
View file @
0144b513
...
...
@@ -29,7 +29,7 @@ The table below shows this endpoint's support for
| Blocking Queries | ACL Required |
| ---------------- | ------------ |
|
`NO`
|
`no
ne`
|
|
`NO`
|
`no
de:read`
|
### Sample Request
...
...
This diff is collapsed.
Click to expand it.
Write
Preview
Supports
Markdown
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