-
Notifications
You must be signed in to change notification settings - Fork 83
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
Implement support for API Key metadata #195
Changes from 4 commits
b8a3a3c
53e5683
e8c51d1
c60f378
64e94bd
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -293,12 +293,22 @@ func createFleetAgent(ctx context.Context, bulker bulk.Bulk, id string, agent mo | |
} | ||
|
||
func generateAccessApiKey(ctx context.Context, client *elasticsearch.Client, agentId string) (*apikey.ApiKey, error) { | ||
return apikey.Create(ctx, client, agentId, "", []byte(kFleetAccessRolesJSON)) | ||
return apikey.Create(ctx, client, agentId, "", []byte(kFleetAccessRolesJSON), | ||
apikey.Metadata{ | ||
Application: apikey.FleetAgentApplication, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Naming: Should we call this application? Service? ... There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. naming is hard. the original ES ticket used "application" elastic/elasticsearch#48182 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tvernum I think you were the one using application there. Any preference? |
||
AgentId: agentId, | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Torn if we should call it There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. @tsg FYI, we already add the agent id. Any opinion on format? There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, that's awesome, thanks for the ping. Also FYI @andrewkroh. I'd say that we don't really need the There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. just had similar debate on the other PR elastic/kibana#95935 There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 😆 Can't see the discussion on the commit change. Was the reason there? In the end agree with @tsg it probably does not matter here, especially if in kibana we also use There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. it was a discussion on Slack with one of the reviewers, thus few commits there iterating on the final shape |
||
Type: apikey.TypeAccess.String(), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I like the idea to have |
||
}) | ||
} | ||
|
||
func generateOutputApiKey(ctx context.Context, client *elasticsearch.Client, agentId, outputName string, roles []byte) (*apikey.ApiKey, error) { | ||
name := fmt.Sprintf("%s:%s", agentId, outputName) | ||
return apikey.Create(ctx, client, name, "", roles) | ||
return apikey.Create(ctx, client, name, "", roles, | ||
apikey.Metadata{ | ||
Application: apikey.FleetAgentApplication, | ||
AgentId: agentId, | ||
Type: apikey.TypeOutput.String(), | ||
}) | ||
} | ||
|
||
func (et *EnrollerT) fetchEnrollmentKeyRecord(ctx context.Context, id string) (*model.EnrollmentApiKey, error) { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,78 @@ | ||
// 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), | ||
Metadata{ | ||
Application: FleetAgentApplication, | ||
AgentId: agentId, | ||
Type: TypeAccess.String(), | ||
}) | ||
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(FleetAgentApplication, aKeyMeta.Metadata.Application) | ||
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) | ||
} | ||
} |
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 | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,24 @@ | ||
// 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 FleetAgentApplication = "fleet-agent" | ||
|
||
type Type int | ||
|
||
const ( | ||
TypeAccess Type = iota | ||
TypeOutput | ||
) | ||
|
||
func (t Type) String() string { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 commentThe reason will be displayed to describe this comment to others. Learn more. that the Go way |
||
return []string{"access", "output"}[t] | ||
} | ||
|
||
type Metadata struct { | ||
Application string `json:"application,omitempty"` | ||
AgentId string `json:"agent_id,omitempty"` | ||
Type string `json:"type,omitempty"` | ||
} |
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.
One metadata we have for other assets is
"managed": true
to indicate that this is managed by tooling internally. I think we should add it.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.
will add