Skip to content

Commit

Permalink
Additional changes to the metatadata format
Browse files Browse the repository at this point in the history
  • Loading branch information
aleksmaus committed Apr 1, 2021
1 parent c60f378 commit 64e94bd
Show file tree
Hide file tree
Showing 3 changed files with 23 additions and 20 deletions.
12 changes: 2 additions & 10 deletions cmd/fleet/handleEnroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -294,21 +294,13 @@ 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),
apikey.Metadata{
Application: apikey.FleetAgentApplication,
AgentId: agentId,
Type: apikey.TypeAccess.String(),
})
apikey.NewMetadata(agentId, apikey.TypeAccess))
}

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,
apikey.Metadata{
Application: apikey.FleetAgentApplication,
AgentId: agentId,
Type: apikey.TypeOutput.String(),
})
apikey.NewMetadata(agentId, apikey.TypeOutput))
}

func (et *EnrollerT) fetchEnrollmentKeyRecord(ctx context.Context, id string) (*model.EnrollmentApiKey, error) {
Expand Down
13 changes: 7 additions & 6 deletions internal/pkg/apikey/apikey_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -40,11 +40,7 @@ func TestCreateApiKeyWithMetadata(t *testing.T) {
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(),
})
NewMetadata(agentId, TypeAccess))
if err != nil {
t.Fatal(err)
}
Expand All @@ -55,7 +51,12 @@ func TestCreateApiKeyWithMetadata(t *testing.T) {
t.Fatal(err)
}

diff := cmp.Diff(FleetAgentApplication, aKeyMeta.Metadata.Application)
diff := cmp.Diff(ManagedByFleetServer, aKeyMeta.Metadata.ManagedBy)
if diff != "" {
t.Error(diff)
}

diff = cmp.Diff(true, aKeyMeta.Metadata.Managed)
if diff != "" {
t.Error(diff)
}
Expand Down
18 changes: 14 additions & 4 deletions internal/pkg/apikey/metadata.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@

package apikey

const FleetAgentApplication = "fleet-agent"
const ManagedByFleetServer = "fleet-server"

type Type int

Expand All @@ -18,7 +18,17 @@ func (t Type) String() string {
}

type Metadata struct {
Application string `json:"application,omitempty"`
AgentId string `json:"agent_id,omitempty"`
Type string `json:"type,omitempty"`
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(),
}
}

0 comments on commit 64e94bd

Please sign in to comment.