Skip to content

Commit

Permalink
Merge branch 'master' into docs-apikey-promote
Browse files Browse the repository at this point in the history
  • Loading branch information
axw authored Apr 14, 2021
2 parents 9c3139b + 90d05c3 commit ce7082a
Show file tree
Hide file tree
Showing 4 changed files with 26 additions and 6 deletions.
1 change: 1 addition & 0 deletions changelogs/head.asciidoc
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ https://github.com/elastic/apm-server/compare/7.12\...master[View commits]
* The server now responds with a reason for some 401 Unauthorized requests {pull}5053[5053]
* Add `session.id` and `session.sequence` fields for RUM session tracking {pull}5056[5056]
* Support for ingesting `user.domain` {pull}5067[5067]
* Add `"application": "apm"` metadata to API Keys created with `apm-server apikey create` {pull}5090[5090]
* API Key auth is no longer considered experimental {pull}5091[5091]

[float]
Expand Down
1 change: 1 addition & 0 deletions cmd/apikey.go
Original file line number Diff line number Diff line change
Expand Up @@ -320,6 +320,7 @@ PUT /_security/role/my_role {
},
},
},
Metadata: map[string]interface{}{"application": "apm"},
}
if expiry != "" {
apikeyRequest.Expiration = &expiry
Expand Down
14 changes: 8 additions & 6 deletions elasticsearch/security_api.go
Original file line number Diff line number Diff line change
Expand Up @@ -67,9 +67,10 @@ func HasPrivileges(ctx context.Context, client Client, privileges HasPrivilegesR
}

type CreateAPIKeyRequest struct {
Name string `json:"name"`
Expiration *string `json:"expiration,omitempty"`
RoleDescriptors RoleDescriptor `json:"role_descriptors"`
Name string `json:"name"`
Expiration *string `json:"expiration,omitempty"`
RoleDescriptors RoleDescriptor `json:"role_descriptors"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type CreateAPIKeyResponse struct {
Expand Down Expand Up @@ -121,9 +122,10 @@ type Application struct {

type APIKeyResponse struct {
APIKey
Creation int64 `json:"creation"`
Invalidated bool `json:"invalidated"`
Username string `json:"username"`
Creation int64 `json:"creation"`
Invalidated bool `json:"invalidated"`
Username string `json:"username"`
Metadata map[string]interface{} `json:"metadata,omitempty"`
}

type APIKeyQuery struct {
Expand Down
16 changes: 16 additions & 0 deletions systemtest/apikeycmd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package systemtest_test

import (
"bytes"
"context"
"encoding/json"
"io"
"net/http"
Expand Down Expand Up @@ -76,6 +77,21 @@ func TestAPIKeyCreate(t *testing.T) {

es := systemtest.NewElasticsearchClientWithAPIKey(attrs["credentials"].(string))
assertAuthenticateSucceeds(t, es)

// Check that the API Key has expected metadata.
type apiKey struct {
ID string `json:"id"`
Metadata map[string]interface{} `json:"metadata"`
}
var resp struct {
APIKeys []apiKey `json:"api_keys"`
}
_, err = systemtest.Elasticsearch.Do(context.Background(), &esapi.SecurityGetAPIKeyRequest{
ID: attrs["id"].(string),
}, &resp)
require.NoError(t, err)
require.Len(t, resp.APIKeys, 1)
assert.Equal(t, map[string]interface{}{"application": "apm"}, resp.APIKeys[0].Metadata)
}

func TestAPIKeyCreateExpiration(t *testing.T) {
Expand Down

0 comments on commit ce7082a

Please sign in to comment.