Skip to content

Commit 0c86b5d

Browse files
Handle new PrivilegeLevelChangeAction (#5154)
1 parent 7268406 commit 0c86b5d

File tree

7 files changed

+215
-27
lines changed

7 files changed

+215
-27
lines changed
Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
# Kind can be one of:
2+
# - breaking-change: a change to previously-documented behavior
3+
# - deprecation: functionality that is being removed in a later release
4+
# - bug-fix: fixes a problem in a previous version
5+
# - enhancement: extends functionality but does not break or fix existing behavior
6+
# - feature: new functionality
7+
# - known-issue: problems that we are aware of in a given version
8+
# - security: impacts on the security of a product or a user’s deployment.
9+
# - upgrade: important information for someone upgrading from a prior version
10+
# - other: does not fit into any of the other categories
11+
kind: feature
12+
13+
# Change summary; a 80ish characters long description of the change.
14+
summary: Privileged level change action added
15+
16+
# Long description; in case the summary is not enough to describe the change
17+
# this field accommodate a description without length limits.
18+
# NOTE: This field will be rendered only for breaking-change and known-issue kinds at the moment.
19+
description: Added new PRIVILEGE_LEVEL_CHANGE action type for changing agent privileges.
20+
21+
# Affected component; usually one of "elastic-agent", "fleet-server", "filebeat", "metricbeat", "auditbeat", "all", etc.
22+
component: fleet-server
23+
24+
# PR URL; optional; the PR number that added the changeset.
25+
# If not present is automatically filled by the tooling finding the PR where this changelog fragment has been added.
26+
# NOTE: the tooling supports backports, so it's able to fill the original PR number instead of the backport PR number.
27+
# Please provide it if you are adding a fragment for a different PR.
28+
#pr: https://github.com/owner/repo/1234
29+
30+
# Issue URL; optional; the GitHub issue related to this changeset (either closes or is part of).
31+
# If not present is automatically filled by the tooling with the issue linked to the PR number.
32+
issue: https://github.com/elastic/fleet-server/issues/4989

internal/pkg/api/handleAck_test.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ func TestEventToActionResult(t *testing.T) {
130130
assert.Equal(t, "2022-02-23T18:26:08.506128Z", r.Timestamp)
131131
assert.Equal(t, "error message", r.Error)
132132
})
133+
t.Run("privilege level change action", func(t *testing.T) {
134+
r := eventToActionResult(agentID, "PRIVILEGE_LEVEL_CHANGE", []string{}, AckRequest_Events_Item{json.RawMessage(`{
135+
"action_id": "test-action-id",
136+
"message": "action message",
137+
"timestamp": "2022-02-23T18:26:08.506128Z",
138+
"data": {"unprivileged":"true","user_info":{"username": "demo", "password": "1q2w3e"}},
139+
"error": "error message"
140+
}`)})
141+
assert.Equal(t, agentID, r.AgentID)
142+
assert.Equal(t, "test-action-id", r.ActionID)
143+
assert.Equal(t, "2022-02-23T18:26:08.506128Z", r.Timestamp)
144+
assert.Equal(t, "error message", r.Error)
145+
})
133146
}
134147

135148
type searchRequestFilter struct {

internal/pkg/api/handleCheckin.go

Lines changed: 17 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -57,14 +57,15 @@ const (
5757
// unlisted or invalid types are removed with filterActions().
5858
// action types should have a corresponding case in convertActionData.
5959
var validActionTypes = map[string]bool{
60-
string(CANCEL): true,
61-
string(INPUTACTION): true,
62-
string(POLICYREASSIGN): true,
63-
string(REQUESTDIAGNOSTICS): true,
64-
string(SETTINGS): true,
65-
string(UNENROLL): true,
66-
string(UPGRADE): true,
67-
string(MIGRATE): true,
60+
string(CANCEL): true,
61+
string(INPUTACTION): true,
62+
string(POLICYREASSIGN): true,
63+
string(REQUESTDIAGNOSTICS): true,
64+
string(SETTINGS): true,
65+
string(UNENROLL): true,
66+
string(UPGRADE): true,
67+
string(MIGRATE): true,
68+
string(PRIVILEGELEVELCHANGE): true,
6869
}
6970

7071
type CheckinT struct {
@@ -768,6 +769,14 @@ func convertActionData(aType ActionType, raw json.RawMessage) (ad Action_Data, e
768769
}
769770
err = ad.FromActionMigrate(d)
770771
return
772+
case PRIVILEGELEVELCHANGE:
773+
d := ActionPrivilegeLevelChange{}
774+
err = json.Unmarshal(raw, &d)
775+
if err != nil {
776+
return
777+
}
778+
err = ad.FromActionPrivilegeLevelChange(d)
779+
return
771780
default:
772781
return ad, fmt.Errorf("data conversion unsupported action type: %s", aType)
773782
}

internal/pkg/api/handleCheckin_test.go

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,18 @@ func TestConvertActionData(t *testing.T) {
134134
raw: json.RawMessage(`{"enrollment_token":"et","target_uri":"turi"}`),
135135
expect: Action_Data{json.RawMessage(`{"enrollment_token":"et","target_uri":"turi"}`)},
136136
hasErr: false,
137+
}, {
138+
name: "privilege level change action - with data",
139+
aType: PRIVILEGELEVELCHANGE,
140+
raw: json.RawMessage(`{"unprivileged":true,"user_info":{"password":"1q2w3e","username":"demo"}}`),
141+
expect: Action_Data{json.RawMessage(`{"unprivileged":true,"user_info":{"password":"1q2w3e","username":"demo"}}`)},
142+
hasErr: false,
143+
}, {
144+
name: "privilege level change action",
145+
aType: PRIVILEGELEVELCHANGE,
146+
raw: json.RawMessage(`{}`),
147+
expect: Action_Data{json.RawMessage(`{"unprivileged":false}`)},
148+
hasErr: false,
137149
}, {
138150
name: "unknown action type",
139151
aType: ActionType("UNKNOWN"),

internal/pkg/api/openapi.gen.go

Lines changed: 57 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

model/openapi.yml

Lines changed: 27 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,6 +437,19 @@ components:
437437
x-oapi-codegen-extra-tags: # oapi-codegen tags
438438
yaml: "signature"
439439
json: "signature,omitempty"
440+
userInfo:
441+
description: Optional user info data.
442+
type: object
443+
properties:
444+
username:
445+
type: string
446+
description: Username of custom user used to run Elastic Agent.
447+
password:
448+
type: string
449+
description: Password for user specified by username.
450+
groupname:
451+
type: string
452+
description: Custom group used to access Elastic Agent files.
440453
action:
441454
description: |
442455
An action for an elastic-agent.
@@ -488,6 +501,7 @@ components:
488501
- $ref: "#/components/schemas/actionRequestDiagnostics"
489502
- $ref: "#/components/schemas/actionInputAction"
490503
- $ref: "#/components/schemas/actionMigrate"
504+
- $ref: "#/components/schemas/actionPrivilegeLevelChange"
491505
id:
492506
description: The action ID.
493507
type: string
@@ -513,6 +527,7 @@ components:
513527
- "CANCEL"
514528
- "REQUEST_DIAGNOSTICS"
515529
- "MIGRATE"
530+
- "PRIVILEGE_LEVEL_CHANGE"
516531
x-go-custom-tag: yaml:"type" # openapi-generator
517532
x-oapi-codegen-extra-tags: # oapi-codegen tags
518533
yaml: "type"
@@ -655,6 +670,17 @@ components:
655670
required:
656671
- enrollment_token
657672
- target_uri
673+
actionPrivilegeLevelChange:
674+
description: The PRIVILEGE_LEVEL_CHANGE action.
675+
type: object
676+
properties:
677+
unprivileged:
678+
type: boolean
679+
description: Flag indicating whether target level is unprivileged. If not provided unprivileged is assumed.
680+
user_info:
681+
$ref: "#/components/schemas/userInfo"
682+
required:
683+
- unprivileged
658684
checkinResponse:
659685
type: object
660686
required:
@@ -1901,4 +1927,4 @@ paths:
19011927
"500":
19021928
$ref: "#/components/responses/internalServerError"
19031929
"503":
1904-
$ref: "#/components/responses/unavailable"
1930+
$ref: "#/components/responses/unavailable"

pkg/api/types.gen.go

Lines changed: 57 additions & 9 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

0 commit comments

Comments
 (0)