Skip to content

Commit 7215c81

Browse files
givinalisGivinalis Omachar
andauthored
356 - Added Authentication checks for cmdlets under SignIns submodule for Entra (#1537)
* feat: Added auth checks to signin sub-module on Entra * test: Added mock of Get-EntraContext to tests * chore: relocated the Update-EntraOauth2PermissionsGrant sub-module to correct folder * tests: Added auth checks on all tests * tests: updated Invalid tests and mock instances for Get-EntraContext * tests: removed auth checks in Invalid.tests files across Entra --------- Co-authored-by: Givinalis Omachar <giomachar@microsoft.com>
1 parent 65d4e7e commit 7215c81

86 files changed

Lines changed: 790 additions & 229 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

module/Entra/Microsoft.Entra/SignIns/Get-EntraAuthorizationPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ function Get-EntraAuthorizationPolicy {
1414
[System.String[]] $Property
1515
)
1616

17+
begin {
18+
# Ensure connection to Microsoft Entra
19+
if (-not (Get-EntraContext)) {
20+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Policy.Read.All' to authenticate."
21+
Write-Error -Message $errorMessage -ErrorAction Stop
22+
return
23+
}
24+
}
25+
1726
PROCESS {
1827
$params = @{}
1928
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

module/Entra/Microsoft.Entra/SignIns/Get-EntraConditionalAccessPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ function Get-EntraConditionalAccessPolicy {
1414
[System.String[]] $Property
1515
)
1616

17+
begin {
18+
# Ensure connection to Microsoft Entra
19+
if (-not (Get-EntraContext)) {
20+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Policy.Read.All' to authenticate."
21+
Write-Error -Message $errorMessage -ErrorAction Stop
22+
return
23+
}
24+
}
25+
1726
PROCESS {
1827
$params = @{}
1928
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

module/Entra/Microsoft.Entra/SignIns/Get-EntraFeatureRolloutPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,15 @@ function Get-EntraFeatureRolloutPolicy {
2020
[System.String[]] $Property
2121
)
2222

23+
begin {
24+
# Ensure connection to Microsoft Entra
25+
if (-not (Get-EntraContext)) {
26+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Directory.ReadWrite.All' to authenticate."
27+
Write-Error -Message $errorMessage -ErrorAction Stop
28+
return
29+
}
30+
}
31+
2332
PROCESS {
2433
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
2534
$params = @{}

module/Entra/Microsoft.Entra/SignIns/Get-EntraIdentityProvider.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ function Get-EntraIdentityProvider {
1414
[System.String[]] $Property
1515
)
1616

17+
begin {
18+
# Ensure connection to Microsoft Entra
19+
if (-not (Get-EntraContext)) {
20+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes IdentityProvider.Read.All' to authenticate."
21+
Write-Error -Message $errorMessage -ErrorAction Stop
22+
return
23+
}
24+
}
25+
1726
PROCESS {
1827
$params = @{}
1928
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

module/Entra/Microsoft.Entra/SignIns/Get-EntraNamedLocationPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ function Get-EntraNamedLocationPolicy {
1313
[System.String[]] $Property
1414
)
1515

16+
begin {
17+
# Ensure connection to Microsoft Entra
18+
if (-not (Get-EntraContext)) {
19+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Policy.Read.All' to authenticate."
20+
Write-Error -Message $errorMessage -ErrorAction Stop
21+
return
22+
}
23+
}
24+
1625
PROCESS {
1726
$params = @{}
1827
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

module/Entra/Microsoft.Entra/SignIns/Get-EntraOAuth2PermissionGrant.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ function Get-EntraOAuth2PermissionGrant {
1717
[System.String[]] $Property
1818
)
1919

20+
begin {
21+
# Ensure connection to Microsoft Entra
22+
if (-not (Get-EntraContext)) {
23+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Directory.Read.All' to authenticate."
24+
Write-Error -Message $errorMessage -ErrorAction Stop
25+
return
26+
}
27+
}
28+
2029
PROCESS {
2130
$params = @{}
2231
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

module/Entra/Microsoft.Entra/SignIns/Get-EntraPermissionGrantConditionSet.ps1

Lines changed: 73 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -19,69 +19,80 @@ function Get-EntraPermissionGrantConditionSet {
1919
[System.String[]] $Property
2020
)
2121

22-
$params = @{}
23-
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
24-
if ($PSBoundParameters.ContainsKey("Verbose")) {
25-
$params["Verbose"] = $PSBoundParameters["Verbose"]
26-
}
27-
if ($null -ne $PSBoundParameters["Id"]) {
28-
$params["PermissionGrantConditionSetId"] = $PSBoundParameters["Id"]
29-
}
30-
if ($null -ne $PSBoundParameters["ConditionSetType"]) {
31-
$conditionalSet = $PSBoundParameters["ConditionSetType"]
32-
}
33-
if ($null -ne $PSBoundParameters["PolicyId"]) {
34-
$params["PermissionGrantPolicyId"] = $PSBoundParameters["PolicyId"]
35-
}
36-
if ($PSBoundParameters.ContainsKey("Debug")) {
37-
$params["Debug"] = $PSBoundParameters["Debug"]
38-
}
39-
if ($null -ne $PSBoundParameters["WarningVariable"]) {
40-
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
41-
}
42-
if ($null -ne $PSBoundParameters["InformationVariable"]) {
43-
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
44-
}
45-
if ($null -ne $PSBoundParameters["InformationAction"]) {
46-
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
47-
}
48-
if ($null -ne $PSBoundParameters["OutVariable"]) {
49-
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
50-
}
51-
if ($null -ne $PSBoundParameters["OutBuffer"]) {
52-
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
53-
}
54-
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
55-
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
56-
}
57-
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
58-
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
59-
}
60-
if ($null -ne $PSBoundParameters["ErrorAction"]) {
61-
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
62-
}
63-
if ($null -ne $PSBoundParameters["WarningAction"]) {
64-
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
65-
}
66-
if ($null -ne $PSBoundParameters["Property"]) {
67-
$params["Property"] = $PSBoundParameters["Property"]
22+
begin {
23+
# Ensure connection to Microsoft Entra
24+
if (-not (Get-EntraContext)) {
25+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Policy.Read.PermissionGrant' to authenticate."
26+
Write-Error -Message $errorMessage -ErrorAction Stop
27+
return
28+
}
6829
}
6930

70-
Write-Debug("============================ TRANSFORMATIONS ============================")
71-
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
72-
Write-Debug("=========================================================================`n")
73-
74-
if ("$conditionalSet" -eq "includes") {
75-
$response = Get-MgPolicyPermissionGrantPolicyInclude @params -Headers $customHeaders
76-
}
77-
elseif ("$conditionalSet" -eq "excludes") {
78-
$response = Get-MgPolicyPermissionGrantPolicyExclude @params -Headers $customHeaders
79-
}
80-
else {
81-
Write-Error("Message: Resource not found for the segment '$conditionalSet'.")
82-
return
83-
}
84-
85-
$response
31+
process {
32+
$params = @{}
33+
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand
34+
if ($PSBoundParameters.ContainsKey("Verbose")) {
35+
$params["Verbose"] = $PSBoundParameters["Verbose"]
36+
}
37+
if ($null -ne $PSBoundParameters["Id"]) {
38+
$params["PermissionGrantConditionSetId"] = $PSBoundParameters["Id"]
39+
}
40+
if ($null -ne $PSBoundParameters["ConditionSetType"]) {
41+
$conditionalSet = $PSBoundParameters["ConditionSetType"]
42+
}
43+
if ($null -ne $PSBoundParameters["PolicyId"]) {
44+
$params["PermissionGrantPolicyId"] = $PSBoundParameters["PolicyId"]
45+
}
46+
if ($PSBoundParameters.ContainsKey("Debug")) {
47+
$params["Debug"] = $PSBoundParameters["Debug"]
48+
}
49+
if ($null -ne $PSBoundParameters["WarningVariable"]) {
50+
$params["WarningVariable"] = $PSBoundParameters["WarningVariable"]
51+
}
52+
if ($null -ne $PSBoundParameters["InformationVariable"]) {
53+
$params["InformationVariable"] = $PSBoundParameters["InformationVariable"]
54+
}
55+
if ($null -ne $PSBoundParameters["InformationAction"]) {
56+
$params["InformationAction"] = $PSBoundParameters["InformationAction"]
57+
}
58+
if ($null -ne $PSBoundParameters["OutVariable"]) {
59+
$params["OutVariable"] = $PSBoundParameters["OutVariable"]
60+
}
61+
if ($null -ne $PSBoundParameters["OutBuffer"]) {
62+
$params["OutBuffer"] = $PSBoundParameters["OutBuffer"]
63+
}
64+
if ($null -ne $PSBoundParameters["ErrorVariable"]) {
65+
$params["ErrorVariable"] = $PSBoundParameters["ErrorVariable"]
66+
}
67+
if ($null -ne $PSBoundParameters["PipelineVariable"]) {
68+
$params["PipelineVariable"] = $PSBoundParameters["PipelineVariable"]
69+
}
70+
if ($null -ne $PSBoundParameters["ErrorAction"]) {
71+
$params["ErrorAction"] = $PSBoundParameters["ErrorAction"]
72+
}
73+
if ($null -ne $PSBoundParameters["WarningAction"]) {
74+
$params["WarningAction"] = $PSBoundParameters["WarningAction"]
75+
}
76+
if ($null -ne $PSBoundParameters["Property"]) {
77+
$params["Property"] = $PSBoundParameters["Property"]
78+
}
79+
80+
Write-Debug("============================ TRANSFORMATIONS ============================")
81+
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
82+
Write-Debug("=========================================================================`n")
83+
84+
if ("$conditionalSet" -eq "includes") {
85+
$response = Get-MgPolicyPermissionGrantPolicyInclude @params -Headers $customHeaders
86+
}
87+
elseif ("$conditionalSet" -eq "excludes") {
88+
$response = Get-MgPolicyPermissionGrantPolicyExclude @params -Headers $customHeaders
89+
}
90+
else {
91+
Write-Error("Message: Resource not found for the segment '$conditionalSet'.")
92+
return
93+
}
94+
95+
$response
96+
}
8697
}
8798

module/Entra/Microsoft.Entra/SignIns/Get-EntraPermissionGrantPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ function Get-EntraPermissionGrantPolicy {
1313
[System.String[]] $Property
1414
)
1515

16+
begin {
17+
# Ensure connection to Microsoft Entra
18+
if (-not (Get-EntraContext)) {
19+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Policy.Read.PermissionGrant' to authenticate."
20+
Write-Error -Message $errorMessage -ErrorAction Stop
21+
return
22+
}
23+
}
24+
1625
PROCESS {
1726
$params = @{}
1827
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

module/Entra/Microsoft.Entra/SignIns/Get-EntraPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ function Get-EntraPolicy {
1616
[switch] $All
1717
)
1818

19+
begin {
20+
# Ensure connection to Microsoft Entra
21+
if (-not (Get-EntraContext)) {
22+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Policy.Read.All' to authenticate."
23+
Write-Error -Message $errorMessage -ErrorAction Stop
24+
return
25+
}
26+
}
27+
1928
PROCESS {
2029
$params = @{}
2130
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

module/Entra/Microsoft.Entra/SignIns/Get-EntraTrustedCertificateAuthority.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ function Get-EntraTrustedCertificateAuthority {
1616
[System.String[]] $Property
1717
)
1818

19+
begin {
20+
# Ensure connection to Microsoft Entra
21+
if (-not (Get-EntraContext)) {
22+
$errorMessage = "Not connected to Microsoft Graph. Use 'Connect-Entra -Scopes Organization.Read.All' to authenticate."
23+
Write-Error -Message $errorMessage -ErrorAction Stop
24+
return
25+
}
26+
}
27+
1928
PROCESS {
2029
$params = @{}
2130
$customHeaders = New-EntraCustomHeaders -Command $MyInvocation.MyCommand

0 commit comments

Comments
 (0)