Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

parse ACL token from authorization header #12534

Merged
merged 3 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions command/agent/http.go
Original file line number Diff line number Diff line change
Expand Up @@ -806,6 +806,27 @@ func (s *HTTPServer) parseToken(req *http.Request, token *string) {
*token = other
return
}

if other := req.Header.Get("Authorization"); other != "" {
// HTTP Authorization headers are in the format: <Scheme>[SPACE]<Value>
// Ref. https://tools.ietf.org/html/rfc7236#section-3
parts := strings.Split(other, " ")

// Authorization Header is invalid if containing 1 or 0 parts, e.g.:
// "" || "<Scheme><Value>" || "<Scheme>" || "<Value>"
if len(parts) > 1 {
scheme := parts[0]
// Everything after "<Scheme>" is "<Value>", trimmed
value := strings.TrimSpace(strings.Join(parts[1:], " "))

// <Scheme> must be "Bearer"
if strings.ToLower(scheme) == "bearer" {
// Since Bearer tokens shouldn't contain spaces (rfc6750#section-2.1)
// "value" is tokenized, only the first item is used
*token = strings.TrimSpace(strings.Split(value, " ")[0])
}
}
}
}

// parse is a convenience method for endpoints that need to parse multiple flags
Expand Down
47 changes: 39 additions & 8 deletions command/agent/http_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -535,16 +535,47 @@ func TestParseToken(t *testing.T) {
s := makeHTTPServer(t, nil)
defer s.Shutdown()

req, err := http.NewRequest("GET", "/v1/jobs", nil)
req.Header.Add("X-Nomad-Token", "foobar")
if err != nil {
t.Fatalf("err: %v", err)
cases := []struct {
Name string
HeaderKey string
HeaderValue string
ExpectedToken string
}{
{
Name: "Parses token from X-Nomad-Token",
HeaderKey: "X-Nomad-Token",
HeaderValue: "foobar",
ExpectedToken: "foobar",
},
{
Name: "Parses token from bearer authentication",
HeaderKey: "Authorization",
HeaderValue: "Bearer foobar",
ExpectedToken: "foobar",
},
{
Name: "Fails to parse token from bad bearer authentication",
HeaderKey: "Authorization",
HeaderValue: "foobar",
ExpectedToken: "",
},
}

var token string
s.Server.parseToken(req, &token)
if token != "foobar" {
t.Fatalf("bad %s", token)
for i := range cases {
tc := cases[i]
t.Run(tc.Name, func(t *testing.T) {
req, err := http.NewRequest("GET", "/v1/jobs", nil)
req.Header.Add(tc.HeaderKey, tc.HeaderValue)
if err != nil {
t.Fatalf("err: %v", err)
}

var token string
s.Server.parseToken(req, &token)
if token != tc.ExpectedToken {
t.Fatalf("bad %s", token)
}
})
}
}

Expand Down
14 changes: 11 additions & 3 deletions website/content/api-docs/index.mdx
Original file line number Diff line number Diff line change
Expand Up @@ -66,22 +66,30 @@ administration.

## ACLs

Several endpoints in Nomad use or require ACL tokens to operate. The token are used to authenticate the request and determine if the request is allowed based on the associated authorizations. Tokens are specified per-request by using the `X-Nomad-Token` request header set to the `SecretID` of an ACL Token.
Several endpoints in Nomad use or require ACL tokens to operate. The token are used to authenticate the request and determine if the request is allowed based on the associated authorizations. Tokens are specified per-request by using the `X-Nomad-Token` request header or with the Bearer scheme in the authorization header set to the `SecretID` of an ACL Token.

For more details about ACLs, please see the [ACL Guide](https://learn.hashicorp.com/collections/nomad/access-control).

## Authentication

When ACLs are enabled, a Nomad token should be provided to API requests using the `X-Nomad-Token` header. When using authentication, clients should communicate via TLS.
When ACLs are enabled, a Nomad token should be provided to API requests using the `X-Nomad-Token` header or with the Bearer scheme in the authorization header. When using authentication, clients should communicate via TLS.

Here is an example using curl:
Here is an example using curl with `X-Nomad-Token`:

```shell-session
$ curl \
--header "X-Nomad-Token: aa534e09-6a07-0a45-2295-a7f77063d429" \
https://localhost:4646/v1/jobs
```

Below is an example using `curl` with a [RFC6750](https://tools.ietf.org/html/rfc6750) Bearer token:

```shell-session
$ curl \
--header "Authorization: Bearer <token>" \
http://localhost:4646/v1/jobs
```

## Namespaces

Nomad has support for namespaces, which allow jobs and their associated objects
Expand Down