Skip to content
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

remove flags for policy defaulting #30256

Closed
wants to merge 14 commits into from
72 changes: 58 additions & 14 deletions x-pack/elastic-agent/pkg/agent/cmd/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -43,11 +43,12 @@ import (
)

const (
requestRetrySleepEnv = "KIBANA_REQUEST_RETRY_SLEEP"
maxRequestRetriesEnv = "KIBANA_REQUEST_RETRY_COUNT"
defaultRequestRetrySleep = "1s" // sleep 1 sec between retries for HTTP requests
defaultMaxRequestRetries = "30" // maximum number of retries for HTTP requests
defaultStateDirectory = "/usr/share/elastic-agent/state" // directory that will hold the state data
requestRetrySleepEnv = "KIBANA_REQUEST_RETRY_SLEEP"
maxRequestRetriesEnv = "KIBANA_REQUEST_RETRY_COUNT"
defaultRequestRetrySleep = "1s" // sleep 1 sec between retries for HTTP requests
defaultMaxRequestRetries = "30" // maximum number of retries for HTTP requests
defaultStateDirectory = "/usr/share/elastic-agent/state" // directory that will hold the state data
defaultFleetPackagePolicyName = "default-fleet-server-agent-policy"
lykkin marked this conversation as resolved.
Show resolved Hide resolved
)

var (
Expand Down Expand Up @@ -498,7 +499,32 @@ func kibanaFetchPolicy(cfg setupConfig, client *kibana.Client, streams *cli.IOSt
if err != nil {
return nil, err
}
return findPolicy(cfg, policies.Items)
packagePolicies, err := kibanaFetchPackagePolicies(cfg, client, streams)
lykkin marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
return findPolicy(cfg, policies.Items, packagePolicies)
lykkin marked this conversation as resolved.
Show resolved Hide resolved
}

func kibanaFetchPackagePolicies(cfg setupConfig, client *kibana.Client, streams *cli.IOStreams) (*packagePolicyResponse, error) {
var packagePolicies kibanaPackagePolicies
err := performGET(cfg, client, "/api/fleet/package_policies", &packagePolicies, streams.Err, "Kibana fetch package policies")
lykkin marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return nil, err
}
return separatePackagePolicies(&packagePolicies), nil
}

func separatePackagePolicies(packagePolicies *kibanaPackagePolicies) *packagePolicyResponse {
result := packagePolicyResponse{}
for _, packagePolicy := range packagePolicies.Items {
if packagePolicy.Package.Name == "fleet_server" {
lykkin marked this conversation as resolved.
Show resolved Hide resolved
result.Fleet[packagePolicy.PolicyID] = struct{}{}
} else {
result.NonFleet[packagePolicy.PolicyID] = struct{}{}
}
}
lykkin marked this conversation as resolved.
Show resolved Hide resolved
return &result
}

func kibanaFetchToken(cfg setupConfig, client *kibana.Client, policy *kibanaPolicy, streams *cli.IOStreams, tokenName string) (string, error) {
Expand Down Expand Up @@ -541,7 +567,7 @@ func kibanaClient(cfg kibanaConfig, headers map[string]string) (*kibana.Client,
}, 0, "Elastic-Agent")
}

func findPolicy(cfg setupConfig, policies []kibanaPolicy) (*kibanaPolicy, error) {
func findPolicy(cfg setupConfig, policies []kibanaPolicy, packagePolicies *packagePolicyResponse) (*kibanaPolicy, error) {
policyID := ""
policyName := cfg.Fleet.TokenPolicyName
if cfg.FleetServer.Enable {
Expand All @@ -557,11 +583,11 @@ func findPolicy(cfg setupConfig, policies []kibanaPolicy) (*kibanaPolicy, error)
return &policy, nil
}
} else if cfg.FleetServer.Enable {
if policy.IsDefaultFleetServer {
if _, ok := packagePolicies.Fleet[policy.ID]; ok {
return &policy, nil
}
} else {
if policy.IsDefault {
if _, ok := packagePolicies.NonFleet[policy.ID]; ok {
return &policy, nil
}
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the added logging and making it explicit this will helps us.

Expand Down Expand Up @@ -898,12 +924,30 @@ func copyFile(destPath string, srcPath string, mode os.FileMode) error {
return err
}

type kibanaPackage struct {
Name string `json:"name"`
}

type packagePolicyResponse struct {
Fleet map[string]struct{}
NonFleet map[string]struct{}
}

type kibanaPackagePolicy struct {
ID string `json:"id"`
PolicyID string `json:"policy_id"`
Package kibanaPackage `json:"package"`
}

type kibanaPackagePolicies struct {
Items []kibanaPackagePolicy `json:"items"`
}

type kibanaPolicy struct {
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
IsDefault bool `json:"is_default"`
IsDefaultFleetServer bool `json:"is_default_fleet_server"`
ID string `json:"id"`
Name string `json:"name"`
Status string `json:"status"`
PackagePolicies []string `json:"package_policies"`
}

type kibanaPolicies struct {
Expand Down
181 changes: 181 additions & 0 deletions x-pack/elastic-agent/pkg/agent/cmd/container_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// 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 cmd

import (
"testing"

"github.com/stretchr/testify/require"
)

var (
defaultFleetPolicy = kibanaPolicy{
ID: "499b5aa7-d214-5b5d-838b-3cd76469844e",
Name: "Default Fleet Server policy",
Status: "active",
PackagePolicies: []string{
"default-fleet-server-agent-policy",
},
}
defaultAgentPolicy = kibanaPolicy{
ID: "2016d7cc-135e-5583-9758-3ba01f5a06e5",
Name: "Default policy",
Status: "active",
PackagePolicies: []string{
"default-system-policy",
},
}
nondefaultAgentPolicy = kibanaPolicy{
ID: "bc634ea6-8460-4925-babd-7540c3e7df24",
Name: "Another free policy",
Status: "active",
PackagePolicies: []string{
"3668df9e-f2a3-4b65-9e6c-58ed352f2b63",
},
}

nondefaultFleetPolicy = kibanaPolicy{
ID: "7b0093d2-7eab-4862-86c8-63b3dd1db001",
Name: "Some kinda dependent policy",
Status: "active",
PackagePolicies: []string{
"63e2f84f-ab11-439c-93fa-531ff5b53e20",
},
}
)

var policies kibanaPolicies = kibanaPolicies{
Items: []kibanaPolicy{
defaultFleetPolicy,
defaultAgentPolicy,
nondefaultAgentPolicy,
nondefaultFleetPolicy,
},
}

var PackagePolicies = packagePolicyResponse{
Fleet: map[string]struct{}{
"7b0093d2-7eab-4862-86c8-63b3dd1db001": {},
"499b5aa7-d214-5b5d-838b-3cd76469844e": {},
},
NonFleet: map[string]struct{}{
"bc634ea6-8460-4925-babd-7540c3e7df24": {},
"2016d7cc-135e-5583-9758-3ba01f5a06e5": {},
},
}

func TestFindPolicyById(t *testing.T) {
cfg := setupConfig{
FleetServer: fleetServerConfig{
Enable: true,
PolicyID: "7b0093d2-7eab-4862-86c8-63b3dd1db001",
},
}

policy, err := findPolicy(cfg, policies.Items, &PackagePolicies)
require.NoError(t, err)
require.Equal(t, &nondefaultFleetPolicy, policy)
}

func TestFindPolicyByName(t *testing.T) {
cfg := setupConfig{
Fleet: fleetConfig{
TokenPolicyName: "Default policy",
},
}

policy, err := findPolicy(cfg, policies.Items, &PackagePolicies)
require.NoError(t, err)
require.Equal(t, &defaultAgentPolicy, policy)
}

func TestFindPolicyByIdOverName(t *testing.T) {
cfg := setupConfig{
Fleet: fleetConfig{
TokenPolicyName: "Default policy",
},
FleetServer: fleetServerConfig{
Enable: true,
PolicyID: "7b0093d2-7eab-4862-86c8-63b3dd1db001",
},
}

policy, err := findPolicy(cfg, policies.Items, &PackagePolicies)
require.NoError(t, err)
require.Equal(t, &nondefaultFleetPolicy, policy)
}

func TestFindPolicyByIdMiss(t *testing.T) {
cfg := setupConfig{
FleetServer: fleetServerConfig{
Enable: true,
PolicyID: "invalid id",
},
}

policy, err := findPolicy(cfg, policies.Items, &PackagePolicies)
require.Error(t, err)
require.Nil(t, policy)
}

func TestFindPolicyByNameMiss(t *testing.T) {
cfg := setupConfig{
Fleet: fleetConfig{
TokenPolicyName: "invalid name",
},
}

policy, err := findPolicy(cfg, policies.Items, &PackagePolicies)
require.Error(t, err)
require.Nil(t, policy)
}

func TestFindPolicyDefaultFleet(t *testing.T) {
cfg := setupConfig{
FleetServer: fleetServerConfig{
Enable: true,
},
}

policy, err := findPolicy(cfg, policies.Items, &PackagePolicies)
require.NoError(t, err)
require.Equal(t, &defaultFleetPolicy, policy)
}

func TestFindPolicyDefaultNonFleet(t *testing.T) {
cfg := setupConfig{
FleetServer: fleetServerConfig{
Enable: false,
},
}

policy, err := findPolicy(cfg, policies.Items, &PackagePolicies)
require.NoError(t, err)
require.Equal(t, &defaultAgentPolicy, policy)
}

func TestFindPolicyNoMatchNonFleet(t *testing.T) {
cfg := setupConfig{
FleetServer: fleetServerConfig{
Enable: false,
},
}

policy, err := findPolicy(cfg, policies.Items, &packagePolicyResponse{Fleet: PackagePolicies.Fleet})
require.Error(t, err)
require.Nil(t, policy)
}

func TestFindPolicyNoMatchFleet(t *testing.T) {
cfg := setupConfig{
FleetServer: fleetServerConfig{
Enable: true,
},
}

policy, err := findPolicy(cfg, policies.Items, &packagePolicyResponse{NonFleet: PackagePolicies.NonFleet})
require.Error(t, err)
require.Nil(t, policy)
}