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

Support for remote ES output #3051

Merged
merged 99 commits into from
Nov 29, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
Show all changes
99 commits
Select commit Hold shift + click to select a range
44b6985
first draft of remote output
juliaElastic Oct 24, 2023
f870e55
remove commented code
juliaElastic Oct 24, 2023
4605e04
storing remote es clients in bulker
juliaElastic Oct 26, 2023
b6b47af
remote service_token from policy sent to agent
juliaElastic Oct 26, 2023
d82a117
fix lint
juliaElastic Oct 26, 2023
0ceb201
rename remoteEsList and type
juliaElastic Oct 26, 2023
c63fb1e
added missing check
juliaElastic Oct 26, 2023
6795a82
Merge branch 'main' into remote-es
juliaElastic Oct 30, 2023
6cd8491
fixes after merge main
juliaElastic Oct 30, 2023
4b72d71
fix lint
juliaElastic Oct 30, 2023
1d6c31f
added changelog
juliaElastic Oct 30, 2023
53de755
create bulker for remote es
juliaElastic Oct 30, 2023
5bbc1f1
cleanup unused remoteEsClients and functions
juliaElastic Oct 30, 2023
ab7487d
fix linter
juliaElastic Oct 30, 2023
e901a80
renamed to remote_elasticsearch
juliaElastic Oct 30, 2023
2465b2a
added tracer, removed opts
juliaElastic Nov 1, 2023
1fd90a8
enable intrumentation on remote es client
juliaElastic Nov 1, 2023
8db4da2
Merge branch 'main' into remote-es
juliaElastic Nov 1, 2023
7953456
updated changelog
juliaElastic Nov 1, 2023
9452494
Merge branch 'main' into remote-es
juliaElastic Nov 2, 2023
11bc90c
try copy policyData
juliaElastic Nov 2, 2023
8312727
revert policyData, printing out value of policy
juliaElastic Nov 2, 2023
8cae7a4
setting policyData in other integration tests
juliaElastic Nov 2, 2023
bff9015
added missing param
juliaElastic Nov 2, 2023
a52b32b
watching for remote output cfg changes and restart
juliaElastic Nov 2, 2023
41f4ced
fixes
juliaElastic Nov 2, 2023
649bfdb
fix linter
juliaElastic Nov 2, 2023
f6994bd
fix
juliaElastic Nov 2, 2023
84787bd
copy map instead of reference
juliaElastic Nov 3, 2023
d36f83a
added integration test
juliaElastic Nov 3, 2023
255c28b
unit test on CheckRemoteOutputChanged
juliaElastic Nov 3, 2023
108fe47
fix lint
juliaElastic Nov 3, 2023
fd7e406
fix lint
juliaElastic Nov 3, 2023
4f99d62
added tests on policy_output remote ES
juliaElastic Nov 3, 2023
cc6a02e
added test on parsed_policy change
juliaElastic Nov 3, 2023
6dc08cd
fix lint
juliaElastic Nov 6, 2023
9a0e8b1
invalidate api key if remote output is removed
juliaElastic Nov 7, 2023
830b401
fix lint
juliaElastic Nov 7, 2023
fa4698a
Merge branch 'main' into remote-es
juliaElastic Nov 8, 2023
62a49c6
fix after conflicts
juliaElastic Nov 8, 2023
0f1ed68
reporting remote es error in fleet-server state
juliaElastic Nov 8, 2023
1d865d8
ignore lint error
juliaElastic Nov 8, 2023
72a7249
fix test
juliaElastic Nov 9, 2023
6ee9086
fix test
juliaElastic Nov 9, 2023
374b1ae
fix test
juliaElastic Nov 9, 2023
dad52b0
fixed test
juliaElastic Nov 9, 2023
8d4dbbc
start new bulker if remote config changed
juliaElastic Nov 10, 2023
de6724a
fix tests
juliaElastic Nov 10, 2023
e465d3d
added test for retire remote api key
juliaElastic Nov 10, 2023
bf6c9e3
monitor tests
juliaElastic Nov 10, 2023
2cc1e7d
Merge branch 'main' into remote-es
juliaElastic Nov 13, 2023
608ba26
remote es ping in standalone self monitor
juliaElastic Nov 14, 2023
699ac62
added break
juliaElastic Nov 14, 2023
81899b3
fix lint
juliaElastic Nov 14, 2023
14f9507
fix issue
juliaElastic Nov 14, 2023
963b87a
license header
juliaElastic Nov 14, 2023
180cae8
fix lint
juliaElastic Nov 14, 2023
3989856
cleanup, more test
juliaElastic Nov 15, 2023
6b727b8
added test on engine
juliaElastic Nov 15, 2023
60a3122
use output bulker when read update api key
juliaElastic Nov 15, 2023
8797823
added remote es ping to self.go
juliaElastic Nov 15, 2023
1be0575
review comments
juliaElastic Nov 16, 2023
dcf39f9
invalidate all ids with corresponding output
juliaElastic Nov 16, 2023
bb32a41
only add toRetireAPIKeys if does not exist
juliaElastic Nov 16, 2023
3261472
added a retry loop to integration test
juliaElastic Nov 16, 2023
991c689
fixed monitor tests
juliaElastic Nov 16, 2023
75fdb8f
remove break loop in self monitor
juliaElastic Nov 20, 2023
a8447b2
fix lint
juliaElastic Nov 20, 2023
9c7d6fe
openapi spec: added degraded state to desc
juliaElastic Nov 20, 2023
8df35d1
added 2nd elasticsearch to integration test
juliaElastic Nov 20, 2023
3b2dc11
Merge branch 'main' into remote-es
juliaElastic Nov 20, 2023
10021a8
added build.Info to remote bulkers from fleet
juliaElastic Nov 20, 2023
4c99949
added semaphore for updating bulkerMap
juliaElastic Nov 21, 2023
8182bab
revert self monitor degraded on remote es error
juliaElastic Nov 21, 2023
37106b0
fix tests
juliaElastic Nov 21, 2023
64277ff
verify api key exists in remote es
juliaElastic Nov 21, 2023
ea1d3bd
fix lint
juliaElastic Nov 21, 2023
e15f995
remote remoteOutputErrorMap as not used
juliaElastic Nov 21, 2023
6d4f87e
fix lint
juliaElastic Nov 21, 2023
85142e2
revert openapi description about degraded state
juliaElastic Nov 22, 2023
727daa4
reading output from policies index if bulker not found
juliaElastic Nov 22, 2023
462c063
unit test on handleAck
juliaElastic Nov 22, 2023
96bdb42
added test to query policies
juliaElastic Nov 22, 2023
053fbed
fix test
juliaElastic Nov 23, 2023
60cd6ee
fix integration test
juliaElastic Nov 23, 2023
760ea2f
added integration test for invalidate api key
juliaElastic Nov 23, 2023
9382975
test for child bulker cancel
juliaElastic Nov 23, 2023
0006533
fixed test, replace semaphore with mutex
juliaElastic Nov 27, 2023
41219f0
removed unused arg
juliaElastic Nov 27, 2023
75c890b
added warning log if api keys orphaned
juliaElastic Nov 27, 2023
4d2d67d
removed unused error
juliaElastic Nov 27, 2023
cd877f1
fix lint
juliaElastic Nov 27, 2023
aa7bf11
Merge branch 'main' into remote-es
juliaElastic Nov 27, 2023
d1ffe3f
try to fix test
juliaElastic Nov 27, 2023
5885068
read output secret before prepare remote es
juliaElastic Nov 27, 2023
4d5b906
try to fix test
juliaElastic Nov 27, 2023
d39141a
removed test
juliaElastic Nov 28, 2023
299cdae
removed unused imports
juliaElastic Nov 28, 2023
6fdba62
Merge branch 'main' into remote-es
juliaElastic Nov 29, 2023
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions internal/pkg/policy/parsed_policy.go
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,11 @@ func constructPolicyOutputs(outputsRaw json.RawMessage, roles map[string]RoleT)
p.Role = &role
}

if p.Type == OutputTypeRemoteElasticsearch {
p.ServiceToken = v.GetString(FieldOutputServiceToken)
p.Type = OutputTypeElasticsearch
}

result[k] = p
}

Expand Down
67 changes: 61 additions & 6 deletions internal/pkg/policy/policy_output.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,16 +17,19 @@

"github.com/elastic/fleet-server/v7/internal/pkg/apikey"
"github.com/elastic/fleet-server/v7/internal/pkg/bulk"
"github.com/elastic/fleet-server/v7/internal/pkg/config"
"github.com/elastic/fleet-server/v7/internal/pkg/dl"
"github.com/elastic/fleet-server/v7/internal/pkg/es"
"github.com/elastic/fleet-server/v7/internal/pkg/logger"
"github.com/elastic/fleet-server/v7/internal/pkg/model"
"github.com/elastic/fleet-server/v7/internal/pkg/smap"
)

const (
OutputTypeElasticsearch = "elasticsearch"
OutputTypeLogstash = "logstash"
OutputTypeKafka = "kafka"
OutputTypeElasticsearch = "elasticsearch"
OutputTypeRemoteElasticsearch = "remote-elasticsearch"
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
OutputTypeLogstash = "logstash"
OutputTypeKafka = "kafka"
)

var (
Expand All @@ -35,9 +38,10 @@
)

type Output struct {
Name string
Type string
Role *RoleT
Name string
Type string
ServiceToken string `json:"service_token,omitempty"`
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
Role *RoleT
}

// Prepare prepares the output p to be sent to the elastic-agent
Expand Down Expand Up @@ -229,6 +233,35 @@
output.PermissionsHash = p.Role.Sha2 // for the sake of consistency
}

if outputMap.GetMap(p.Name).GetString("type") == OutputTypeRemoteElasticsearch {
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
om, _ := outputMap.GetMap(p.Name).Marshal()
var outputObj map[string]any
json.Unmarshal(om, &outputObj)

Check failure on line 239 in internal/pkg/policy/policy_output.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Error return value of `json.Unmarshal` is not checked (errcheck)
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
hosts := outputObj["hosts"].([]interface{})

Check failure on line 240 in internal/pkg/policy/policy_output.go

View workflow job for this annotation

GitHub Actions / lint (linux)

Error return value is not checked (errcheck)
outputAPIKey, err :=
generateRemoteOutputAPIKey(ctx, agent.Id, p, []string{hosts[0].(string)})
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
if err != nil {
return fmt.Errorf("failed generate output API key: %w", err)
}

// TODO update agent doc like above
// TODO what about update flow?

output.APIKey = outputAPIKey.Agent()
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
output.APIKeyID = outputAPIKey.ID
output.PermissionsHash = p.Role.Sha2

zlog.Debug().
Str("outputAPIKey.ID", outputAPIKey.ID).
Msg("generated remote output api key")

// replace type remote-elasticsearch with elasticsearch as agent doesn't recognize remote-elasticsearch
if err := setMapObj(outputMap, OutputTypeElasticsearch, p.Name, "type"); err != nil {
return err
}
// TODO remove the service token from the agent policy sent to the agent
}

// Always insert the `api_key` as part of the output block, this is required
// because only fleet server knows the api key for the specific agent, if we don't
// add it the agent will not receive the `api_key` and will not be able to connect
Expand Down Expand Up @@ -411,6 +444,28 @@
)
}

func generateRemoteOutputAPIKey(ctx context.Context,
juliaElastic marked this conversation as resolved.
Show resolved Hide resolved
agentID string,
output *Output,
hosts []string,
) (*apikey.APIKey, error) {
name := fmt.Sprintf("%s:%s", agentID, output.Name)
cfg := config.Config{
Output: config.Output{
Elasticsearch: config.Elasticsearch{
Hosts: hosts,
ServiceToken: output.ServiceToken,
},
},
}
es, err := es.NewClient(ctx, &cfg, false)

if err != nil {
return nil, err
}
return apikey.Create(ctx, es, name, "", "false", output.Role.Raw, apikey.NewMetadata(agentID, output.Name, apikey.TypeOutput))
}

func setMapObj(obj map[string]interface{}, val interface{}, keys ...string) error {
if len(keys) == 0 {
return fmt.Errorf("no key to be updated: %w", ErrFailInjectAPIKey)
Expand Down
Loading