-
Notifications
You must be signed in to change notification settings - Fork 102
Implement support for API Key metadata #195
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
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
b8a3a3c
Implement support for API Key metadata
aleksmaus 53e5683
Adjust apikey.Create to make the metadata functional options
aleksmaus e8c51d1
Address code review feedback
aleksmaus c60f378
Make metadata properties json omitempty
aleksmaus 64e94bd
Additional changes to the metatadata format
aleksmaus File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,79 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| // +build integration | ||
|
|
||
| package apikey | ||
|
|
||
| import ( | ||
| "context" | ||
| "errors" | ||
| "testing" | ||
|
|
||
| ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing" | ||
|
|
||
| "github.com/gofrs/uuid" | ||
| "github.com/google/go-cmp/cmp" | ||
| ) | ||
|
|
||
| const testFleetRoles = ` | ||
| { | ||
| "fleet-apikey-access": { | ||
| "cluster": [], | ||
| "applications": [{ | ||
| "application": ".fleet", | ||
| "privileges": ["no-privileges"], | ||
| "resources": ["*"] | ||
| }] | ||
| } | ||
| } | ||
| ` | ||
|
|
||
| func TestCreateApiKeyWithMetadata(t *testing.T) { | ||
| ctx, cn := context.WithCancel(context.Background()) | ||
| defer cn() | ||
|
|
||
| bulker := ftesting.SetupBulk(ctx, t) | ||
|
|
||
| // Create the key | ||
| agentId := uuid.Must(uuid.NewV4()).String() | ||
| name := uuid.Must(uuid.NewV4()).String() | ||
| akey, err := Create(ctx, bulker.Client(), name, "", []byte(testFleetRoles), | ||
| NewMetadata(agentId, TypeAccess)) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| // Get the key and verify that metadata was saved correctly | ||
| aKeyMeta, err := Get(ctx, bulker.Client(), akey.Id) | ||
| if err != nil { | ||
| t.Fatal(err) | ||
| } | ||
|
|
||
| diff := cmp.Diff(ManagedByFleetServer, aKeyMeta.Metadata.ManagedBy) | ||
| if diff != "" { | ||
| t.Error(diff) | ||
| } | ||
|
|
||
| diff = cmp.Diff(true, aKeyMeta.Metadata.Managed) | ||
| if diff != "" { | ||
| t.Error(diff) | ||
| } | ||
|
|
||
| diff = cmp.Diff(agentId, aKeyMeta.Metadata.AgentId) | ||
| if diff != "" { | ||
| t.Error(diff) | ||
| } | ||
|
|
||
| diff = cmp.Diff(TypeAccess.String(), aKeyMeta.Metadata.Type) | ||
| if diff != "" { | ||
| t.Error(diff) | ||
| } | ||
|
|
||
| // Try to get the key that doesn't exists, expect ErrApiKeyNotFound | ||
| aKeyMeta, err = Get(ctx, bulker.Client(), "0000000000000") | ||
| if !errors.Is(err, ErrApiKeyNotFound) { | ||
| t.Errorf("Unexpected error type: %v", err) | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,66 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package apikey | ||
|
|
||
| import ( | ||
| "context" | ||
| "encoding/json" | ||
| "fmt" | ||
|
|
||
| "github.com/elastic/go-elasticsearch/v8" | ||
| "github.com/elastic/go-elasticsearch/v8/esapi" | ||
| ) | ||
|
|
||
| type ApiKeyMetadata struct { | ||
| Id string | ||
| Metadata Metadata | ||
| } | ||
|
|
||
| func Get(ctx context.Context, client *elasticsearch.Client, id string) (apiKey ApiKeyMetadata, err error) { | ||
|
|
||
| opts := []func(*esapi.SecurityGetAPIKeyRequest){ | ||
| client.Security.GetAPIKey.WithContext(ctx), | ||
| client.Security.GetAPIKey.WithID(id), | ||
| } | ||
|
|
||
| res, err := client.Security.GetAPIKey( | ||
| opts..., | ||
| ) | ||
|
|
||
| if err != nil { | ||
| return | ||
| } | ||
|
|
||
| defer res.Body.Close() | ||
|
|
||
| if res.IsError() { | ||
| return apiKey, fmt.Errorf("fail GetAPIKey: %s, %w", res.String(), ErrApiKeyNotFound) | ||
| } | ||
|
|
||
| type APIKeyResponse struct { | ||
| Id string `json:"id"` | ||
| Metadata Metadata `json:"metadata"` | ||
| } | ||
| type GetAPIKeyResponse struct { | ||
| ApiKeys []APIKeyResponse `json:"api_keys"` | ||
| } | ||
|
|
||
| var resp GetAPIKeyResponse | ||
| d := json.NewDecoder(res.Body) | ||
| if err = d.Decode(&resp); err != nil { | ||
| return | ||
| } | ||
|
|
||
| if len(resp.ApiKeys) == 0 { | ||
| return apiKey, ErrApiKeyNotFound | ||
| } | ||
|
|
||
| first := resp.ApiKeys[0] | ||
|
|
||
| return ApiKeyMetadata{ | ||
| Id: first.Id, | ||
| Metadata: first.Metadata, | ||
| }, nil | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,34 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License; | ||
| // you may not use this file except in compliance with the Elastic License. | ||
|
|
||
| package apikey | ||
|
|
||
| const ManagedByFleetServer = "fleet-server" | ||
|
|
||
| type Type int | ||
|
|
||
| const ( | ||
| TypeAccess Type = iota | ||
| TypeOutput | ||
| ) | ||
|
|
||
| func (t Type) String() string { | ||
| return []string{"access", "output"}[t] | ||
| } | ||
|
|
||
| type Metadata struct { | ||
| AgentId string `json:"agent_id,omitempty"` | ||
| Managed bool `json:"managed,omitempty"` | ||
| ManagedBy string `json:"managed_by,omitempty"` | ||
| Type string `json:"type,omitempty"` | ||
| } | ||
|
|
||
| func NewMetadata(agentId string, typ Type) Metadata { | ||
| return Metadata{ | ||
| AgentId: agentId, | ||
| Managed: true, | ||
| ManagedBy: ManagedByFleetServer, | ||
| Type: typ.String(), | ||
| } | ||
| } | ||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This is some magic. Switch too boring?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
that the Go way