Last active
November 30, 2022 15:43
-
-
Save davidroberts63/6ac11c5333f0062a1012d932d07dfa41 to your computer and use it in GitHub Desktop.
Octopus Deploy Permissions Report
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
[CmdletBinding()] | |
param( | |
[string] | |
$ProjectName, | |
[string] | |
$ProjectGroup, | |
[string] | |
$RootUri, | |
[string] | |
$ApiKey | |
) | |
$Headers = @{ "X-Octopus-ApiKey" = $ApiKey } | |
function CleanTemplatedUri($uri) { | |
# Remove the query parameter templates | |
return ($uri -replace '{.*?}','') -replace '^/','' | |
} | |
function Invoke-OctopusRequest { | |
[CmdletBinding()] | |
param( | |
[Parameter(Mandatory)] | |
[String] | |
$Path, | |
[String] | |
$Method = "GET", | |
[Object] | |
$PayloadObject | |
) | |
$uri = $RootUri + "/$path" | |
if($PayloadObject) { | |
$body = ConvertTo-Json $PayloadObject -Depth 20 | |
Write-Verbose $body | |
Invoke-RestMethod -Uri $uri -Method $Method -Headers $Headers -Body $body | |
} else { | |
Invoke-RestMethod -Uri $uri -Method $Method -Headers $Headers | |
} | |
} | |
$api = Invoke-OctopusRequest -Path '/api' | |
# | |
# Getting the list of projects from the group, if specified. | |
# | |
$projects = @() | |
if($ProjectGroup) { | |
$projectGroup = Invoke-OctopusRequest -Path ($api.Links.ProjectGroups + '?name=$ProjectGroup') | |
if($projectGroup.Items) { | |
$projects = (Invoke-OctopusRequest -Path $projectGroup.Items[0].Links.Projects).Items | |
} else { | |
Write-Warning 'Did not find a project group by that name.' | |
exit 1 | |
} | |
} | |
# | |
# Finding the individual project if specified. | |
# | |
if($ProjectName) { | |
Write-Host 'Getting project' | |
$api = Invoke-OctopusRequest -Path '/api' | |
$projectUri = $api.Links.Projects -Replace '{.*?}','' | |
$projectUri += "?name=$ProjectName" | |
$project = Invoke-OctopusRequest -Path $projectUri -Verbose:$VerbosePreference | |
if($project.Items) { | |
$project = $project.Items[0] | |
Write-Verbose "Project $($project.Name) with id $($project.Id) found" | |
} else { | |
Write-Warning 'Did not find a project by that name.' | |
exit 2 | |
} | |
$projects += $project | |
} | |
# | |
# Get all the teams to look for these projects referenced within. | |
# | |
Write-Host 'Getting list of all teams, including scoped user roles..' | |
# Need to cleanup the uris a bit. | |
$userRolesUri = CleanTemplatedUri $api.Links.UserRoles | |
$teamsUri = (CleanTemplatedUri $api.Links.Teams) + '/all' | |
$teams = Invoke-OctopusRequest -Path $teamsUri -Verbose:$VerbosePreference | |
$teams | ForEach-Object { | |
$team = $_ | |
Write-Verbose "Getting roles for $($team.name)" | |
$scopedRolesUri = CleanTemplatedUri($team.Links.ScopedUserRoles) | |
$scopedRoles = Invoke-OctopusRequest -Path $scopedRolesUri -Verbose:$VerbosePreference | |
$scopedRoles.Items | ForEach-Object { | |
$scopedRole = $_ | |
$userRoleUri = $userRolesUri + "/$($scopedRole.UserRoleId)" | |
$userRole = Invoke-OctopusRequest -Path $userRoleUri -Verbose:$VerbosePreference | |
$scopedRole | Add-Member -NotePropertyName UserRole -NotePropertyValue $userRole | |
} | |
$team | Add-Member -NotePropertyName ScopedUserRoles -NotePropertyValue $scopedRoles.Items | |
} | |
$teams = $teams | Where-Object ScopedUserRoles | |
$projects | ForEach-Object { | |
$project = $_ | |
Write-Host "Getting permissions for $($project.Name)" | |
# | |
# Filter for teams that have some amount of access to the project in question. | |
# Logic is based upon https://octopus.com/docs/security/users-and-teams#restricting-project-and-project-group-access | |
# | |
$accessTeams = $teams | Where-Object { | |
($_.ScopedUserRoles.ProjectIds -Contains $project.Id) -or # Project is explicit. | |
($_.ScopedUserRoles.ProjectGroupIds -Contains $project.ProjectGroupId) -or # Project group is explicit. | |
($_.ScopedUserRoles.ProjectGroupIds.Count -eq 0 -And $_.ScopedUserRoles.ProjectIds.Count -eq 0) # No groups or projects explicitly specified = all. | |
} | |
# | |
# Get the members of the teams that have access | |
# | |
Write-Host 'Getting teams explicit user names (non AD members)' | |
$userUri = CleanTemplatedUri($api.Links.Users) | |
$accessTeams | Where-Object { -not $_.ExplicitUserNames } | ForEach-Object { | |
$team = $_ | |
Write-Host "Getting members of $($team.Name)" | |
$userNames = $team.MemberuserIds | ForEach-Object { | |
$userId = $_ | |
$user = Invoke-OctopusRequest -Path ($userUri + "/$UserId") -Verbose:$VerbosePreference | |
$user.Username | |
} | |
$team | Add-Member -NotePropertyName ExplicitUserNames -NotePropertyValue $userNames -Force | |
} | |
# | |
# Get the environments of the teams that have access | |
# | |
$environmentUri = CleanTemplatedUri($api.Links.Environments) | |
$accessTeams | Where-Object { -not $_.Environments } | ForEach-Object { | |
$team = $_ | |
$team.ScopedUserRoles | ForEach-Object { | |
$scopedRole = $_ | |
$environmentNames = $scopedRole.EnvironmentIds | ForEach-Object { | |
$environmentId = $_ | |
$environment = Invoke-OctopusRequest -Path ($environmentUri + "/$environmentId") -Verbose:$VerbosePreference | |
$environment.Name | |
} | |
$scopedRole | Add-Member -NotePropertyName Environments -NotePropertyValue $environmentNames -Force | |
} | |
} | |
# | |
# Produce report | |
# | |
# $reportData = $accessTeams | Select-Object Name,@{Name='Members';Expression={$_.ExternalSecurityGroups.DisplayName + $_.ExplicitUserNames }},@{Name='Role';Expression={$_.ScopedUserRoles.UserRole.Name}},@{Name='Environments';Expression={$_.ScopedUserRoles.Environments}} | |
$report = "Permissions report for $ProjectName" | |
$report += $accessTeams | ForEach-Object { | |
$team = $_ | |
Write-Output "`r===== $($team.Name):$($team.id) =====" | |
Write-Output "`rMembers:" | |
if($team.ExternalSecurityGroups) { | |
$team.ExternalSecurityGroups.DisplayName | %{ Write-Output "`r$_" } | |
} | |
if($team.ExplicitUserNames) { | |
$team.ExplicitUserNames | %{ Write-Output "`r$_" } | |
} | |
# Write-Output "`r" | |
Write-Output "`r`rRoles Environment:" | |
$team.ScopedUserRoles | ForEach-Object { | |
[PSCustomObject]@{ | |
Name = $_.UserRole.Name | |
Environments = if($_.Environments) { $_.Environments } else { 'All' } | |
} | |
} | Format-Table -HideTableHeaders | Out-String | |
Write-Output "`r" | |
} | |
} | |
$report | Out-Host | |
$report | Out-File -FilePath .\report.txt -Encoding ascii | |
if($OctopusParameters) { | |
New-OctopusArtifact -Path .\report.txt -Name ProjectAccessReport.txt | |
Set-OctopusVariable -Name 'FormattedReport' -Value $report | |
} |
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
Copyright 2022 David J Roberts | |
Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: | |
The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. | |
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment