Skip to content

Commit

Permalink
more linting
Browse files Browse the repository at this point in the history
  • Loading branch information
bryan committed Mar 23, 2022
1 parent 4585280 commit f87c6c1
Show file tree
Hide file tree
Showing 5 changed files with 184 additions and 41 deletions.
4 changes: 2 additions & 2 deletions cmd/fleet/handleEnroll.go
Original file line number Diff line number Diff line change
Expand Up @@ -443,7 +443,7 @@ func generateOutputApiKey(ctx context.Context, bulk bulk.Bulk, agentId, outputNa

func (et *EnrollerT) fetchEnrollmentKeyRecord(ctx context.Context, id string) (*model.EnrollmentAPIKey, error) {

if key, ok := et.cache.GetEnrollmentApiKey(id); ok {
if key, ok := et.cache.GetEnrollmentAPIKey(id); ok {
return &key, nil
}

Expand All @@ -458,7 +458,7 @@ func (et *EnrollerT) fetchEnrollmentKeyRecord(ctx context.Context, id string) (*
}

cost := int64(len(rec.APIKey))
et.cache.SetEnrollmentApiKey(id, rec, cost)
et.cache.SetEnrollmentAPIKey(id, rec, cost)

return &rec, nil
}
Expand Down
22 changes: 11 additions & 11 deletions internal/pkg/cache/cache.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,17 +23,17 @@ type Cache interface {
SetAction(model.Action)
GetAction(id string) (model.Action, bool)

SetApiKey(key ApiKey, enabled bool)
ValidApiKey(key ApiKey) bool
SetApiKey(key APIKey, enabled bool)
ValidApiKey(key APIKey) bool

SetEnrollmentApiKey(id string, key model.EnrollmentAPIKey, cost int64)
GetEnrollmentApiKey(id string) (model.EnrollmentAPIKey, bool)
SetEnrollmentAPIKey(id string, key model.EnrollmentAPIKey, cost int64)
GetEnrollmentAPIKey(id string) (model.EnrollmentAPIKey, bool)

SetArtifact(artifact model.Artifact)
GetArtifact(ident, sha2 string) (model.Artifact, bool)
}

type ApiKey = apikey.ApiKey
type APIKey = apikey.ApiKey
type SecurityInfo = apikey.SecurityInfo

type CacheT struct {
Expand Down Expand Up @@ -151,7 +151,7 @@ func (c *CacheT) GetAction(id string) (model.Action, bool) {
}

// SetApiKey sets the API key in the cache.
func (c *CacheT) SetApiKey(key ApiKey, enabled bool) {
func (c *CacheT) SetApiKey(key APIKey, enabled bool) {
c.mut.RLock()
defer c.mut.RUnlock()

Expand Down Expand Up @@ -188,7 +188,7 @@ func (c *CacheT) SetApiKey(key ApiKey, enabled bool) {
}

// ValidApiKey returns true if the ApiKey is valid (aka. also present in cache).
func (c *CacheT) ValidApiKey(key ApiKey) bool {
func (c *CacheT) ValidApiKey(key APIKey) bool {
c.mut.RLock()
defer c.mut.RUnlock()

Expand All @@ -210,8 +210,8 @@ func (c *CacheT) ValidApiKey(key ApiKey) bool {
return ok
}

// GetEnrollmentApiKey returns the enrollment API key by ID.
func (c *CacheT) GetEnrollmentApiKey(id string) (model.EnrollmentAPIKey, bool) {
// GetEnrollmentAPIKey returns the enrollment API key by ID.
func (c *CacheT) GetEnrollmentAPIKey(id string) (model.EnrollmentAPIKey, bool) {
c.mut.RLock()
defer c.mut.RUnlock()

Expand All @@ -231,8 +231,8 @@ func (c *CacheT) GetEnrollmentApiKey(id string) (model.EnrollmentAPIKey, bool) {
return model.EnrollmentAPIKey{}, false
}

// SetEnrollmentApiKey adds the enrollment API key into the cache.
func (c *CacheT) SetEnrollmentApiKey(id string, key model.EnrollmentAPIKey, cost int64) {
// SetEnrollmentAPIKey adds the enrollment API key into the cache.
func (c *CacheT) SetEnrollmentAPIKey(id string, key model.EnrollmentAPIKey, cost int64) {
c.mut.RLock()
defer c.mut.RUnlock()

Expand Down
143 changes: 143 additions & 0 deletions internal/pkg/policy/policy_output_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,143 @@
// 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 policy

import (
"context"
"testing"

"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/model"
"github.com/elastic/fleet-server/v7/internal/pkg/smap"
ftesting "github.com/elastic/fleet-server/v7/internal/pkg/testing"
"github.com/rs/zerolog"
"github.com/stretchr/testify/require"
)

var TestPayload []byte

func TestPolicyLogstashOutputPrepare(t *testing.T) {
bulker := ftesting.NewMockBulk(&bulk.APIKey{
Id: "test id",
Key: "test key",
})
po := PolicyOutput{
Type: OutputTypeLogstash,
Name: "test output",
Role: &RoleT{
Sha2: "fake sha",
Raw: TestPayload,
},
}

err := po.Prepare(context.Background(), zerolog.Logger{}, bulker, &model.Agent{}, smap.Map{}, false)
require.Nil(t, err, "expected prepare to pass")
}
func TestPolicyLogstashOutputPrepareNoRole(t *testing.T) {
bulker := ftesting.NewMockBulk(&bulk.APIKey{
Id: "test id",
Key: "test key",
})
po := PolicyOutput{
Type: OutputTypeLogstash,
Name: "test output",
Role: nil,
}

err := po.Prepare(context.Background(), zerolog.Logger{}, bulker, &model.Agent{}, smap.Map{}, false)
// No permissions are required by logstash currently
require.Nil(t, err, "expected prepare to pass")
}

func TestPolicyDefaultLogstashOutputPrepare(t *testing.T) {
bulker := ftesting.NewMockBulk(&bulk.APIKey{
Id: "test id",
Key: "test key",
})
po := PolicyOutput{
Type: OutputTypeLogstash,
Name: "test output",
Role: &RoleT{
Sha2: "fake sha",
Raw: TestPayload,
},
}

err := po.Prepare(context.Background(), zerolog.Logger{}, bulker, &model.Agent{}, smap.Map{}, true)
require.Nil(t, err, "expected prepare to pass")
}

func TestPolicyESOutputPrepareNoRole(t *testing.T) {
bulker := ftesting.NewMockBulk(&bulk.APIKey{
Id: "test id",
Key: "test key",
})
po := PolicyOutput{
Type: OutputTypeElasticsearch,
Name: "test output",
Role: nil,
}

err := po.Prepare(context.Background(), zerolog.Logger{}, bulker, &model.Agent{}, smap.Map{}, false)
require.NotNil(t, err, "expected prepare to error")
}

func TestPolicyOutputESPrepare(t *testing.T) {
bulker := ftesting.NewMockBulk(&bulk.APIKey{
Id: "test id",
Key: "test key",
})
po := PolicyOutput{
Type: OutputTypeElasticsearch,
Name: "test output",
Role: &RoleT{
Sha2: "fake sha",
Raw: TestPayload,
},
}
policyMap := smap.Map{
"test output": map[string]interface{}{
"api_key": "",
},
}

err := po.Prepare(context.Background(), zerolog.Logger{}, bulker, &model.Agent{}, policyMap, false)
require.Nil(t, err, "expected prepare to pass")

updatedKey, ok := policyMap.GetMap("test output")["api_key"].(*bulk.APIKey)

require.True(t, ok, "unable to case api key")
require.Equal(t, updatedKey.Key, bulker.MockedAPIKey.Key)
require.Equal(t, updatedKey.Id, bulker.MockedAPIKey.Id)
require.Equal(t, len(bulker.ArgumentData.Update), 0, "update should not be called")
}

func TestPolicyOutputDefaultESPrepare(t *testing.T) {
bulker := ftesting.NewMockBulk(&bulk.APIKey{
Id: "test id",
Key: "test key",
})
po := PolicyOutput{
Type: OutputTypeElasticsearch,
Name: "test output",
Role: &RoleT{
Sha2: "fake sha",
Raw: TestPayload,
},
}
policyMap := smap.Map{
"test output": map[string]interface{}{},
}
testAgent := &model.Agent{}
err := po.Prepare(context.Background(), zerolog.Logger{}, bulker, testAgent, policyMap, true)
require.Nil(t, err, "expected prepare to pass")

updatedKey, ok := policyMap.GetMap("test output")["api_key"].(*bulk.APIKey)

require.True(t, ok, "unable to case api key")
require.Equal(t, updatedKey.Key, bulker.MockedAPIKey.Key)
require.Equal(t, updatedKey.Id, bulker.MockedAPIKey.Id)
require.Greater(t, len(bulker.ArgumentData.Update), 0, "update should be called")
}
18 changes: 9 additions & 9 deletions internal/pkg/policy/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -223,11 +223,11 @@ func (m *selfMonitorT) updateStatus(ctx context.Context) (proto.StateObserved_St
// no fleet-server input
m.status = proto.StateObserved_STARTING
if m.policyID == "" {
m.reporter.Status(proto.StateObserved_STARTING, "Waiting on fleet-server input to be added to default policy", nil)
err = m.reporter.Status(proto.StateObserved_STARTING, "Waiting on fleet-server input to be added to default policy", nil)
} else {
m.reporter.Status(proto.StateObserved_STARTING, fmt.Sprintf("Waiting on fleet-server input to be added to policy: %s", m.policyID), nil)
err = m.reporter.Status(proto.StateObserved_STARTING, fmt.Sprintf("Waiting on fleet-server input to be added to policy: %s", m.policyID), nil)
}
return proto.StateObserved_STARTING, nil
return proto.StateObserved_STARTING, err
}

status := proto.StateObserved_HEALTHY
Expand All @@ -247,23 +247,23 @@ func (m *selfMonitorT) updateStatus(ctx context.Context) (proto.StateObserved_St
if len(tokens) == 0 {
// no tokens created for the policy, still starting
if m.policyID == "" {
m.reporter.Status(proto.StateObserved_STARTING, "Waiting on active enrollment keys to be created in default policy with Fleet Server integration", nil)
err = m.reporter.Status(proto.StateObserved_STARTING, "Waiting on active enrollment keys to be created in default policy with Fleet Server integration", nil)
} else {
m.reporter.Status(proto.StateObserved_STARTING, fmt.Sprintf("Waiting on active enrollment keys to be created in policy with Fleet Server integration: %s", m.policyID), nil)
err = m.reporter.Status(proto.StateObserved_STARTING, fmt.Sprintf("Waiting on active enrollment keys to be created in policy with Fleet Server integration: %s", m.policyID), nil)
}
return proto.StateObserved_STARTING, nil
return proto.StateObserved_STARTING, err
}
payload = map[string]interface{}{
"enrollment_token": tokens[0].APIKey,
}
}
m.status = status
if m.policyID == "" {
m.reporter.Status(status, fmt.Sprintf("Running on default policy with Fleet Server integration%s", extendMsg), payload)
err = m.reporter.Status(status, fmt.Sprintf("Running on default policy with Fleet Server integration%s", extendMsg), payload)
} else {
m.reporter.Status(status, fmt.Sprintf("Running on policy with Fleet Server integration: %s%s", m.policyID, extendMsg), payload)
err = m.reporter.Status(status, fmt.Sprintf("Running on policy with Fleet Server integration: %s%s", m.policyID, extendMsg), payload)
}
return status, nil
return status, err
}

type policyData struct {
Expand Down
Loading

0 comments on commit f87c6c1

Please sign in to comment.