-
Notifications
You must be signed in to change notification settings - Fork 204
Handle PRIVILEGE_LEVEL_CHANGE action
#10231
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
Merged
Merged
Changes from 21 commits
Commits
Show all changes
38 commits
Select commit
Hold shift + click to select a range
f340dc5
poc
michalpristas 6c8a2e4
conflicts
michalpristas 39d1fb8
Merge branch 'main' of github.com:elastic/elastic-agent into feat/swi…
michalpristas a882267
setuid to drop permissions
michalpristas 7c0bce4
setuid to drop permissions
michalpristas 8f36b1f
Merge branch 'main' of github.com:elastic/elastic-agent into feat/swi…
michalpristas b231c1c
resolved
michalpristas fa2cae1
Merge branch 'main' of github.com:elastic/elastic-agent into feat/swi…
michalpristas fc05260
support for linux
michalpristas dd606ac
Merge branch 'main' of github.com:elastic/elastic-agent into feat/swi…
michalpristas 104487e
fmt
michalpristas d806184
merge with main, otel conflicts
michalpristas b165455
linter
michalpristas 9bd7311
changelog
michalpristas 9ff43ca
lint
michalpristas 89e4fcf
lint
michalpristas 7cd5e9f
Update internal/pkg/agent/application/actions/handlers/handle_action_…
michalpristas 596edd9
Update internal/pkg/agent/cmd/run_darwin.go
michalpristas c269603
imports
michalpristas de325b1
naming
michalpristas b432690
unsaved file
michalpristas 6d4ae3a
Update internal/pkg/fleetapi/action.go
michalpristas 5d937b6
Update internal/pkg/fleetapi/action.go
michalpristas ed2fe10
Rename Describer interface
michalpristas cfa5315
Merge branch 'feat/switch-action' of github.com:michalpristas/elastic…
michalpristas dfa8800
Merge branch 'main' of github.com:elastic/elastic-agent into feat/swi…
michalpristas 52e1634
Update changelog/fragments/1759318728-support-for-privilege_level_cha…
michalpristas 69c9c7e
Update internal/pkg/agent/application/actions/handlers/handle_action_…
michalpristas 521ba4f
Update internal/pkg/agent/application/actions/handlers/handle_action_…
michalpristas 9173efc
helper for stopping components
michalpristas f30b811
Merge branch 'feat/switch-action' of github.com:michalpristas/elastic…
michalpristas 109df8a
Merge branch 'main' of github.com:elastic/elastic-agent into feat/swi…
michalpristas 43c65bb
conflicts with main
michalpristas 3fe91c8
move check to action
michalpristas a8d9299
resolved otel cycle imports
michalpristas 159c745
unused describer
michalpristas 85c1a6d
conflicts
michalpristas 075605d
fixed conflict with otel work
michalpristas File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
45 changes: 45 additions & 0 deletions
45
changelog/fragments/1759318728-support-for-privilege_level_change.yaml
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,45 @@ | ||
| # REQUIRED | ||
| # Kind can be one of: | ||
| # - breaking-change: a change to previously-documented behavior | ||
| # - deprecation: functionality that is being removed in a later release | ||
| # - bug-fix: fixes a problem in a previous version | ||
| # - enhancement: extends functionality but does not break or fix existing behavior | ||
| # - feature: new functionality | ||
| # - known-issue: problems that we are aware of in a given version | ||
| # - security: impacts on the security of a product or a user’s deployment. | ||
| # - upgrade: important information for someone upgrading from a prior version | ||
| # - other: does not fit into any of the other categories | ||
| kind: feature | ||
|
|
||
| # REQUIRED for all kinds | ||
| # Change summary; a 80ish characters long description of the change. | ||
| summary: support for privilege_level_change | ||
|
|
||
| # REQUIRED for breaking-change, deprecation, known-issue | ||
| # Long description; in case the summary is not enough to describe the change | ||
| # this field accommodate a description without length limits. | ||
| # description: | ||
|
|
||
| # REQUIRED for breaking-change, deprecation, known-issue | ||
| # impact: | ||
|
|
||
| # REQUIRED for breaking-change, deprecation, known-issue | ||
| # action: | ||
|
|
||
| # REQUIRED for all kinds | ||
| # Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc. | ||
| component: elastic-agent | ||
|
|
||
| # AUTOMATED | ||
| # OPTIONAL to manually add other PR URLs | ||
| # PR URL: A link the PR that added the changeset. | ||
| # If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added. | ||
| # NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number. | ||
| # Please provide it if you are adding a fragment for a different PR. | ||
| # pr: https://github.com/owner/repo/1234 | ||
|
|
||
| # AUTOMATED | ||
| # OPTIONAL to manually add other issue URLs | ||
| # Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of). | ||
| # If not present is automatically filled by the tooling with the issue linked to the PR number. | ||
| # issue: https://github.com/owner/repo/1234 | ||
136 changes: 136 additions & 0 deletions
136
internal/pkg/agent/application/actions/handlers/handle_action_privilege_level_change.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,136 @@ | ||
| // Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
| // or more contributor license agreements. Licensed under the Elastic License 2.0; | ||
| // you may not use this file except in compliance with the Elastic License 2.0. | ||
|
|
||
| package handlers | ||
|
|
||
| import ( | ||
| "context" | ||
| "fmt" | ||
|
|
||
| "github.com/elastic/elastic-agent/internal/pkg/agent/application/coordinator" | ||
| "github.com/elastic/elastic-agent/internal/pkg/agent/application/paths" | ||
| "github.com/elastic/elastic-agent/internal/pkg/agent/application/reexec" | ||
| "github.com/elastic/elastic-agent/internal/pkg/agent/install" | ||
| "github.com/elastic/elastic-agent/internal/pkg/agent/install/componentvalidation" | ||
| "github.com/elastic/elastic-agent/internal/pkg/config" | ||
| "github.com/elastic/elastic-agent/internal/pkg/fleetapi" | ||
| "github.com/elastic/elastic-agent/internal/pkg/fleetapi/acker" | ||
| "github.com/elastic/elastic-agent/pkg/core/logger" | ||
| ) | ||
|
|
||
| type reexecCoordinator interface { | ||
| ReExec(callback reexec.ShutdownCallbackFn, argOverrides ...string) | ||
| } | ||
|
|
||
| type PrivilegeLevelChange struct { | ||
| log *logger.Logger | ||
| coord reexecCoordinator | ||
| ch chan coordinator.ConfigChange | ||
| } | ||
|
|
||
| func NewPrivilegeLevelChange( | ||
| log *logger.Logger, | ||
| coord reexecCoordinator, | ||
| ch chan coordinator.ConfigChange, | ||
| ) *PrivilegeLevelChange { | ||
| return &PrivilegeLevelChange{ | ||
| log: log, | ||
| coord: coord, | ||
| ch: ch, | ||
| } | ||
| } | ||
|
|
||
| // Handle handles PRIVILEGE_LEVEL_CHANGE action. | ||
| func (h *PrivilegeLevelChange) Handle(ctx context.Context, a fleetapi.Action, acker acker.Acker) error { | ||
| return h.handle(ctx, a, acker) | ||
| } | ||
|
|
||
| func (h *PrivilegeLevelChange) handle(ctx context.Context, a fleetapi.Action, acker acker.Acker) (rerr error) { | ||
| action, ok := a.(*fleetapi.ActionPrivilegeLevelChange) | ||
| if !ok { | ||
| return fmt.Errorf("invalid type, expected ActionPrivilegeLevelChange and received %T", a) | ||
| } | ||
|
|
||
| defer func() { | ||
| if rerr != nil { | ||
| h.ackFailure(ctx, rerr, action, acker) | ||
| } | ||
| }() | ||
|
|
||
| if !action.Data.Unprivileged { | ||
| // only unprivileged supported at this point | ||
| return fmt.Errorf("unsupported action, ActionPrivilegeLevelChange supports only downgrading permissions") | ||
| } | ||
|
|
||
| // ensure no component issues | ||
| err := componentvalidation.EnsureNoServiceComponentIssues() | ||
| if err != nil { | ||
| h.log.Debugf("handlerPrivilegeLevelChange: found issues with components: %v", err) | ||
| return err | ||
| } | ||
|
|
||
| var username, groupname, password string | ||
| if action.Data.UserInfo != nil { | ||
| username = action.Data.UserInfo.Username | ||
| groupname = action.Data.UserInfo.Groupname | ||
| password = action.Data.UserInfo.Password | ||
| } | ||
| username, password = install.UnprivilegedUser(username, password) | ||
| groupname = install.UnprivilegedGroup(groupname) | ||
|
|
||
| // apply empty config to stop processing | ||
| unenrollPolicy := newPolicyChange(ctx, config.New(), a, acker, true, false) | ||
| h.ch <- unenrollPolicy | ||
pchila marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| unenrollCtx, cancel := context.WithTimeout(ctx, unenrollTimeout) | ||
| defer cancel() | ||
|
|
||
| unenrollPolicy.WaitAck(unenrollCtx) | ||
michalpristas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
|
|
||
| // fix permissions | ||
| topPath := paths.Top() | ||
| _, err = install.SwitchServiceUser(topPath, &debugDescriber{h.log}, username, groupname, password) | ||
| if err != nil { | ||
| // error already adds context | ||
| return err | ||
| } | ||
|
|
||
| // ack | ||
| if err := acker.Ack(ctx, a); err != nil { | ||
| h.log.Errorw("failed to ACK an action", | ||
| "error.message", err) | ||
michalpristas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
michalpristas marked this conversation as resolved.
Show resolved
Hide resolved
|
||
| if err := acker.Commit(ctx); err != nil { | ||
| h.log.Errorw("failed to commit ACK of an action", | ||
| "error.message", err) | ||
michalpristas marked this conversation as resolved.
Outdated
Show resolved
Hide resolved
|
||
| } | ||
|
|
||
| // restart | ||
| h.coord.ReExec(nil) | ||
| return nil | ||
| } | ||
|
|
||
| func (h *PrivilegeLevelChange) ackFailure(ctx context.Context, err error, action *fleetapi.ActionPrivilegeLevelChange, acker acker.Acker) { | ||
| action.Err = err | ||
|
|
||
| if err := acker.Ack(ctx, action); err != nil { | ||
| h.log.Errorw("failed to ack privilege level change action", | ||
| "error.message", err, | ||
| "action", action) | ||
| } | ||
|
|
||
| if err := acker.Commit(ctx); err != nil { | ||
| h.log.Errorw("failed to commit privilege level change action", | ||
| "error.message", err, | ||
| "action", action) | ||
| } | ||
| } | ||
|
|
||
| type debugDescriber struct { | ||
| l *logger.Logger | ||
| } | ||
|
|
||
| func (d *debugDescriber) Describe(a string) { | ||
| d.l.Debug(a) | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.