Skip to content

Commit

Permalink
removed references to default policy, added configurable default poli…
Browse files Browse the repository at this point in the history
…cy id
  • Loading branch information
bryan committed Feb 22, 2022
1 parent c2f9e50 commit 37dd940
Show file tree
Hide file tree
Showing 9 changed files with 51 additions and 52 deletions.
7 changes: 3 additions & 4 deletions cmd/fleet/main_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -86,10 +86,9 @@ func (s *agentSuite) TestAgentMode(t *testing.T) {
// add a real default fleet server policy
policyId := uuid.Must(uuid.NewV4()).String()
_, err := dl.CreatePolicy(ctx, bulker, model.Policy{
PolicyId: policyId,
RevisionIdx: 1,
DefaultFleetServer: true,
Data: policyData,
PolicyId: policyId,
RevisionIdx: 1,
Data: policyData,
})
require.NoError(t, err)

Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ require (
github.com/hashicorp/errwrap v1.1.0 // indirect
github.com/hashicorp/go-multierror v1.1.1 // indirect
github.com/jcchavezs/porto v0.4.0 // indirect
github.com/jstemmer/go-junit-report v0.9.1 // indirect
github.com/mattn/go-colorable v0.1.12 // indirect
github.com/prometheus/procfs v0.7.3 // indirect
go.elastic.co/apm/module/apmhttprouter v1.14.0
Expand Down
1 change: 1 addition & 0 deletions internal/pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ func (c *Config) InitDefaults() {
c.Inputs = make([]Input, 1)
c.Inputs[0].InitDefaults()
c.HTTP.InitDefaults()
c.Fleet.DefaultPolicyId = "fleet-server-policy"
}

// Validate ensures that the configuration is valid.
Expand Down
2 changes: 2 additions & 0 deletions internal/pkg/config/config_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@ func TestConfig(t *testing.T) {
"fleet-logging": {
cfg: &Config{
Fleet: Fleet{
DefaultPolicyId: "fleet-server-policy",
Agent: Agent{
ID: "1e4954ce-af37-4731-9f4a-407b08e69e42",
Logging: AgentLogging{
Expand Down Expand Up @@ -220,6 +221,7 @@ func defaultHTTP() HTTP {

func defaultFleet() Fleet {
return Fleet{
DefaultPolicyId: "fleet-server-policy",
Agent: Agent{
ID: "1e4954ce-af37-4731-9f4a-407b08e69e42",
Logging: AgentLogging{},
Expand Down
5 changes: 3 additions & 2 deletions internal/pkg/config/fleet.go
Original file line number Diff line number Diff line change
Expand Up @@ -50,8 +50,9 @@ type Host struct {

// Fleet is the configuration of Agent running inside of Fleet.
type Fleet struct {
Agent Agent `config:"agent"`
Host Host `config:"host"`
DefaultPolicyId string `config:"default_policy_id"`
Agent Agent `config:"agent"`
Host Host `config:"host"`
}

func strToLevel(s string) (zerolog.Level, error) {
Expand Down
11 changes: 5 additions & 6 deletions internal/pkg/dl/policies_integration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,12 +23,11 @@ import (
func createRandomPolicy(id string, revisionIdx int) model.Policy {
now := time.Now().UTC()
return model.Policy{
PolicyId: id,
RevisionIdx: int64(revisionIdx),
CoordinatorIdx: 0,
Data: []byte("{}"),
DefaultFleetServer: false,
Timestamp: now.Format(time.RFC3339),
PolicyId: id,
RevisionIdx: int64(revisionIdx),
CoordinatorIdx: 0,
Data: []byte("{}"),
Timestamp: now.Format(time.RFC3339),
}
}

Expand Down
3 changes: 0 additions & 3 deletions internal/pkg/model/schema.go

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

10 changes: 6 additions & 4 deletions internal/pkg/policy/self.go
Original file line number Diff line number Diff line change
Expand Up @@ -45,9 +45,10 @@ type selfMonitorT struct {
bulker bulk.Bulk
monitor monitor.Monitor

policyId string
status proto.StateObserved_Status
reporter status.Reporter
policyId string
defaultPolicyId string
status proto.StateObserved_Status
reporter status.Reporter

policy *model.Policy

Expand All @@ -70,6 +71,7 @@ func NewSelfMonitor(fleet config.Fleet, bulker bulk.Bulk, monitor monitor.Monito
bulker: bulker,
monitor: monitor,
policyId: policyId,
defaultPolicyId: fleet.DefaultPolicyId,
status: proto.StateObserved_STARTING,
reporter: reporter,
policyF: dl.QueryLatestPolicies,
Expand Down Expand Up @@ -171,7 +173,7 @@ func (m *selfMonitorT) processPolicies(ctx context.Context, policies []model.Pol
if m.policyId != "" && policy.PolicyId == m.policyId {
m.policy = &policy
break
} else if m.policyId == "" && policy.DefaultFleetServer {
} else if m.policyId == "" && policy.PolicyId == m.defaultPolicyId {
m.policy = &policy
break
}
Expand Down
63 changes: 30 additions & 33 deletions internal/pkg/policy/self_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,12 @@ import (
"context"
"encoding/json"
"fmt"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"sync"
"testing"
"time"

"github.com/elastic/fleet-server/v7/internal/pkg/config"

"github.com/elastic/elastic-agent-client/v7/pkg/proto"
"github.com/gofrs/uuid"
"github.com/rs/xid"
Expand All @@ -33,6 +34,7 @@ func TestSelfMonitor_DefaultPolicy(t *testing.T) {
defer cancel()

cfg := config.Fleet{
DefaultPolicyId: "fleet-server-policy",
Agent: config.Agent{
ID: "agent-id",
},
Expand Down Expand Up @@ -70,7 +72,7 @@ func TestSelfMonitor_DefaultPolicy(t *testing.T) {
return nil
}, ftesting.RetrySleep(1*time.Second))

policyId := uuid.Must(uuid.NewV4()).String()
policyId := "fleet-server-policy"
rId := xid.New().String()
policyContents, err := json.Marshal(&policyData{Inputs: []policyInput{}})
if err != nil {
Expand All @@ -82,11 +84,10 @@ func TestSelfMonitor_DefaultPolicy(t *testing.T) {
Version: 1,
SeqNo: 1,
},
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
DefaultFleetServer: true,
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
}
pData, err := json.Marshal(&policy)
if err != nil {
Expand Down Expand Up @@ -130,11 +131,10 @@ func TestSelfMonitor_DefaultPolicy(t *testing.T) {
Version: 1,
SeqNo: 1,
},
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 2,
DefaultFleetServer: true,
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 2,
}
pData, err = json.Marshal(&policy)
if err != nil {
Expand Down Expand Up @@ -175,6 +175,7 @@ func TestSelfMonitor_DefaultPolicy_Degraded(t *testing.T) {
defer cancel()

cfg := config.Fleet{
DefaultPolicyId: "fleet-server-policy",
Agent: config.Agent{
ID: "",
},
Expand Down Expand Up @@ -226,7 +227,7 @@ func TestSelfMonitor_DefaultPolicy_Degraded(t *testing.T) {
return nil
}, ftesting.RetrySleep(1*time.Second))

policyId := uuid.Must(uuid.NewV4()).String()
policyId := "fleet-server-policy"
rId := xid.New().String()
policyContents, err := json.Marshal(&policyData{Inputs: []policyInput{
{
Expand All @@ -242,11 +243,10 @@ func TestSelfMonitor_DefaultPolicy_Degraded(t *testing.T) {
Version: 1,
SeqNo: 1,
},
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
DefaultFleetServer: true,
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
}
policyData, err := json.Marshal(&policy)
if err != nil {
Expand Down Expand Up @@ -392,11 +392,10 @@ func TestSelfMonitor_SpecificPolicy(t *testing.T) {
Version: 1,
SeqNo: 1,
},
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 2,
DefaultFleetServer: true,
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 2,
}
pData, err := json.Marshal(&policy)
if err != nil {
Expand Down Expand Up @@ -440,11 +439,10 @@ func TestSelfMonitor_SpecificPolicy(t *testing.T) {
Version: 1,
SeqNo: 2,
},
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
DefaultFleetServer: true,
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
}
pData, err = json.Marshal(&policy)
if err != nil {
Expand Down Expand Up @@ -552,11 +550,10 @@ func TestSelfMonitor_SpecificPolicy_Degraded(t *testing.T) {
Version: 1,
SeqNo: 1,
},
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
DefaultFleetServer: true,
PolicyId: policyId,
CoordinatorIdx: 1,
Data: policyContents,
RevisionIdx: 1,
}
policyData, err := json.Marshal(&policy)
if err != nil {
Expand Down

0 comments on commit 37dd940

Please sign in to comment.