Skip to content

Commit 8055623

Browse files
givinalisGivinalis Omachar
andauthored
356 - Added Authentication checks for cmdlets under Sign In submodule for Entra Beta (#1540)
* feat: added auth checks to all cmdlets * test: added get-entra context mock * tests: added scenatio tests for auth validation --------- Co-authored-by: Givinalis Omachar <giomachar@microsoft.com>
1 parent c199bed commit 8055623

69 files changed

Lines changed: 696 additions & 61 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/EntraBeta/Microsoft.Entra.Beta/SignIns/Add-EntraBetaServicePrincipalPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,15 @@ function Add-EntraBetaServicePrincipalPolicy {
1212
[System.String] $Id
1313
)
1414

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

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaAuthorizationPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ function Get-EntraBetaAuthorizationPolicy {
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-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaConditionalAccessPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ function Get-EntraBetaConditionalAccessPolicy {
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-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaFeatureRolloutPolicy.ps1

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

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

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaIdentityProvider.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,15 @@ function Get-EntraBetaIdentityProvider {
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-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaNamedLocationPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ function Get-EntraBetaNamedLocationPolicy {
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-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaOAuth2PermissionGrant.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,15 @@ function Get-EntraBetaOAuth2PermissionGrant {
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-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaPermissionGrantConditionSet.ps1

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

22-
$params = @{}
23-
$customHeaders = New-EntraBetaCustomHeaders -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")
31+
process {
32+
$params = @{}
33+
$customHeaders = New-EntraBetaCustomHeaders -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+
}
7379

74-
if ("$conditionalSet" -eq "includes") {
75-
$response = Get-MgBetaPolicyPermissionGrantPolicyInclude @params -Headers $customHeaders
76-
}
77-
elseif ("$conditionalSet" -eq "excludes") {
78-
$response = Get-MgBetaPolicyPermissionGrantPolicyExclude @params -Headers $customHeaders
79-
}
80-
else {
81-
Write-Error("Message: Resource not found for the segment '$conditionalSet'.")
82-
return
83-
}
80+
Write-Debug("============================ TRANSFORMATIONS ============================")
81+
$params.Keys | ForEach-Object { "$_ : $($params[$_])" } | Write-Debug
82+
Write-Debug("=========================================================================`n")
8483

85-
$response
84+
if ("$conditionalSet" -eq "includes") {
85+
$response = Get-MgBetaPolicyPermissionGrantPolicyInclude @params -Headers $customHeaders
86+
}
87+
elseif ("$conditionalSet" -eq "excludes") {
88+
$response = Get-MgBetaPolicyPermissionGrantPolicyExclude @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/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaPermissionGrantPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,15 @@ function Get-EntraBetaPermissionGrantPolicy {
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-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

module/EntraBeta/Microsoft.Entra.Beta/SignIns/Get-EntraBetaPolicy.ps1

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,15 @@ function Get-EntraBetaPolicy {
1616
[System.Nullable`1[System.Int32]] $Top
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-EntraBetaCustomHeaders -Command $MyInvocation.MyCommand

0 commit comments

Comments
 (0)