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

Add support for pre existing Active Directory user #5988

Merged
merged 34 commits into from
Dec 3, 2024

Conversation

michalpristas
Copy link
Contributor

@michalpristas michalpristas commented Nov 11, 2024

Waiting on custom windows image for testing with AD e2e.

New flags are introduced

  • user
  • group
  • password (windows only)

These flags are taken into account only when --unprivileged is used.

New user is added same permissions as elastic-agent user when created in order to be able to log on as a service (otherwise agent won't start)

Custom user won't be created and needs to be present

Testing steps

Create a windows VM (windows server 2022)
Run script 1 that will prepare Active Directory

Script will also wait for AD services to start

$domainname = 'testing.local'
$addsSecurePass = ConvertTo-SecureString 'Changeme123+' -AsPlainText -Force

# install ad domain services
Install-WindowsFeature -name AD-Domain-Services -IncludeManagementTools


# setup pass for administrator to conform to forest requirements
net user Administrator 'Changeme123+' /passwordreq:yes

# add a new forest testing.com
Install-ADDSForest -DomainName $domainname -InstallDNS -ErrorAction Stop -NoRebootOnCompletion -SafeModeAdministratorPassword $addsSecurePass -Confirm:$false 

# reboot needs to be performed so changes are effective
Reboot to make changes effective
Run script that will prepare user
$domainname = 'testing.local'
$addsSecurePass = ConvertTo-SecureString 'Changeme123+' -AsPlainText -Force

# wait for AD to boot up
$domainFound = $true

$newObjectParameters = @{
    TypeName = 'System.DirectoryServices.ActiveDirectory.DirectoryContext'
    ArgumentList = @(
        "Domain"
        $domainname
    )
}

$DirectoryContext = New-Object @newObjectParameters

$startDate = Get-Date
do {
    try
    {
        [System.DirectoryServices.ActiveDirectory.DomainController]::FindOne($DirectoryContext)
        $domainFound = $true
    }
    catch
    {
        $domainFound = $false
        Start-Sleep -s 2
    }
} until ($domainFound -and $startDate.AddMinutes(5) -gt (Get-Date))


# add user
New-ADUser -Name 'TestingUser' -AccountPassword $addsSecurePass -Enabled $true -ChangePasswordAtLogon $false -PasswordNeverExpires $true

Scenario 1

  • install as unpriviledged elastic-agent install --unprivileged --user="testing.local\TestUser" --password=Changeme123+
  • check permissions are set to user and service is running as this user

Scenario 2

  • install as admin
  • switch to unpriviledged elastic-agent unprivileged --user="testing.local\TestUser" --password=Changeme123+
  • check permissions are set to user and service is running as this user

Resolves #4585

@michalpristas michalpristas added enhancement New feature or request Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team backport-8.x Automated backport to the 8.x branch with mergify labels Nov 11, 2024
@michalpristas michalpristas self-assigned this Nov 11, 2024
internal/pkg/agent/cmd/install.go Outdated Show resolved Hide resolved
internal/pkg/agent/cmd/install.go Outdated Show resolved Hide resolved
internal/pkg/agent/cmd/unprivileged.go Outdated Show resolved Hide resolved
internal/pkg/agent/cmd/unprivileged.go Outdated Show resolved Hide resolved
internal/pkg/agent/install/install_windows.go Show resolved Hide resolved
@michalpristas michalpristas marked this pull request as ready for review November 18, 2024 08:17
@michalpristas michalpristas requested a review from a team as a code owner November 18, 2024 08:17
@elasticmachine
Copy link
Contributor

Pinging @elastic/elastic-agent-control-plane (Team:Elastic-Agent-Control-Plane)

username = ElasticUsername
groupName = ElasticGroupName
ownership, err = EnsureUserAndGroup(username, groupName, pt)
username, password = UnprivilegedUser(customUser, userPassword)
Copy link
Contributor

Choose a reason for hiding this comment

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

Why only allow custom user for unprivileged mode? Do we not allow to specify custom user when installing in privileged mode?

return false, nil
}

match, err := regexp.MatchString(`^.*(\\)(.*)$`, username)
Copy link
Contributor

Choose a reason for hiding this comment

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

Isn't this regex match equivalent to the strings.Contains?

Copy link
Contributor Author

@michalpristas michalpristas Nov 19, 2024

Choose a reason for hiding this comment

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

yeah basically. but i'd rather change regexp to be more strict and changing regexp and not allow multiple \s instead of using contains
need to check windows reqs for domain and user names

Copy link
Contributor

@blakerouse blakerouse left a comment

Choose a reason for hiding this comment

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

This looks good. Thanks for the fixes on checking the domain name in the user.

@amolnater-qasource
Copy link

Thank you for the update. @ycombinator
We have setup the Windows VM at our end using above scripts.
Further, we will be testing this as soon as these changes are merged and are available in latest snapshot.

Thanks!

@kilfoyle
Copy link
Contributor

Thanks for the summary @ycombinator!
@michalpristas, here's a PR for the documentation. Please let me know whatever I've missed.

Copy link

Quality Gate failed Quality Gate failed

Failed conditions
0.0% Coverage on New Code (required ≥ 40%)

See analysis details on SonarQube

@ycombinator
Copy link
Contributor

Not sure why SonarQube is reporting 0% coverage when there is a unit test added in this PR. Perhaps it's because it's a Windows-only test file?

The remaining CI checks are green. Force merging as we're trying to get this feature into 8.17.0.

@ycombinator ycombinator merged commit dccfb70 into elastic:main Dec 3, 2024
13 of 14 checks passed
@ycombinator ycombinator added the backport-8.17 Automated backport with mergify label Dec 3, 2024
mergify bot pushed a commit that referenced this pull request Dec 3, 2024
* unpriviledged ad works'

* Ensure rights for AD users

* changelog

* support for mac and unix

* Update internal/pkg/agent/cmd/install.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* Update internal/pkg/agent/cmd/unprivileged.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* added e2e tests for darwin and linux

* reverted sample_test

* mage fmt

* do not require password

* more strict regex

* fix description in changelog

* resolved review comments

* linter

* handle g115 in user_windows.go

* more test coverage

* fix broken windows UT

* coverage

* fixed logic for UnprivilegedUser

* fixed windows tests

---------

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>
(cherry picked from commit dccfb70)
mergify bot pushed a commit that referenced this pull request Dec 3, 2024
* unpriviledged ad works'

* Ensure rights for AD users

* changelog

* support for mac and unix

* Update internal/pkg/agent/cmd/install.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* Update internal/pkg/agent/cmd/unprivileged.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* added e2e tests for darwin and linux

* reverted sample_test

* mage fmt

* do not require password

* more strict regex

* fix description in changelog

* resolved review comments

* linter

* handle g115 in user_windows.go

* more test coverage

* fix broken windows UT

* coverage

* fixed logic for UnprivilegedUser

* fixed windows tests

---------

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>
(cherry picked from commit dccfb70)
michalpristas added a commit that referenced this pull request Dec 3, 2024
* unpriviledged ad works'

* Ensure rights for AD users

* changelog

* support for mac and unix

* Update internal/pkg/agent/cmd/install.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* Update internal/pkg/agent/cmd/unprivileged.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* added e2e tests for darwin and linux

* reverted sample_test

* mage fmt

* do not require password

* more strict regex

* fix description in changelog

* resolved review comments

* linter

* handle g115 in user_windows.go

* more test coverage

* fix broken windows UT

* coverage

* fixed logic for UnprivilegedUser

* fixed windows tests

---------

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>
(cherry picked from commit dccfb70)

Co-authored-by: Michal Pristas <michal.pristas@gmail.com>
michalpristas added a commit that referenced this pull request Dec 4, 2024
…user (#6201)

* Add support for pre existing Active Directory user (#5988)

* unpriviledged ad works'

* Ensure rights for AD users

* changelog

* support for mac and unix

* Update internal/pkg/agent/cmd/install.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* Update internal/pkg/agent/cmd/unprivileged.go

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>

* added e2e tests for darwin and linux

* reverted sample_test

* mage fmt

* do not require password

* more strict regex

* fix description in changelog

* resolved review comments

* linter

* handle g115 in user_windows.go

* more test coverage

* fix broken windows UT

* coverage

* fixed logic for UnprivilegedUser

* fixed windows tests

---------

Co-authored-by: Blake Rouse <blake.rouse@elastic.co>
(cherry picked from commit dccfb70)

* lint, remove G115 directives

---------

Co-authored-by: Michal Pristas <michal.pristas@gmail.com>
@amolnater-qasource
Copy link

Hi @ycombinator

We have revalidated this on latest 8.17.0 BC6 kibana cloud environment and had below observations:

Observations:
Scenario 1:

  • Install agent as unpriviledged: elastic-agent install --unprivileged --user="testing.local\TestUser" --password=Changeme123+
  • Checked permissions are set to TestUser and service is running as TestUser.
    17
    18

Scenario 2:

  • Installed agent as admin.
  • Then switched to unpriviledged: elastic-agent unprivileged --user="testing.local\TestUser" --password=Changeme123+
  • Checked permissions are set to TestUser and service is running as TestUser.
    image
    image
    21
    19
    20

Logs:
elastic-agent-diagnostics-2024-12-11T08-50-00Z-00.zip

Build details:
VERSION: 8.17.0 BC6
BUILD: 80521
COMMIT: e8a820624a03a412433584d3e3df951838e4c63c
Artifact Link: https://staging.elastic.co/8.17.0-6b31e673/downloads/beats/elastic-agent/elastic-agent-8.17.0-windows-x86_64.zip

Hence we are marking this ticket as QA:Validated.

Please let us know if we are missing anything here.

cc: @michalpristas
Thanks!

@amolnater-qasource amolnater-qasource added QA:Validated Validated by the QA Team and removed QA:Ready For Testing Code is merged and ready for QA to validate labels Dec 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
backport-8.x Automated backport to the 8.x branch with mergify backport-8.17 Automated backport with mergify enhancement New feature or request QA:Validated Validated by the QA Team Team:Elastic-Agent-Control-Plane Label for the Agent Control Plane team
Projects
None yet
Development

Successfully merging this pull request may close these issues.

Allow using pre-existing user and group when installing unprivileged agents
9 participants