Skip to content

Commit 4c89448

Browse files
committed
update auth
1 parent a1475c7 commit 4c89448

4 files changed

Lines changed: 163 additions & 103 deletions

File tree

core/api/auth/Connect-MonkeyCloud.ps1

Lines changed: 56 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,10 @@ Function Connect-MonkeyCloud{
3535
#>
3636

3737
[CmdletBinding()]
38-
Param ()
38+
Param (
39+
[parameter(Mandatory=$false, HelpMessage="Used when tokens are imported from init param")]
40+
[Switch]$Connected
41+
)
3942
#Using MSAL authentication
4043
If($null -ne $O365Object.msal_application_args){
4144
#Connect to MSGraph
@@ -47,7 +50,9 @@ Function Connect-MonkeyCloud{
4750
Tags = @('TokenRequestInfoMessage');
4851
}
4952
Write-Information @msg
50-
$O365Object.auth_tokens.MSGraph = Connect-MonkeyMSGraph
53+
If(!$Connected.IsPresent){
54+
$O365Object.auth_tokens.MSGraph = Connect-MonkeyMSGraph
55+
}
5156
If($null -ne $O365Object.auth_tokens.MSGraph){
5257
#Check If valid TenantId
5358
If($null -ne $O365Object.TenantId){
@@ -108,14 +113,16 @@ Function Connect-MonkeyCloud{
108113
}
109114
Write-Information @msg
110115
#Connect to Resource management
111-
$p = @{
112-
Resource = $O365Object.Environment.ResourceManager;
113-
AzureService = "AzurePowershell";
114-
InformationAction = $O365Object.InformationAction;
115-
Verbose = $O365Object.verbose;
116-
Debug = $O365Object.debug;
116+
If(!$Connected.IsPresent){
117+
$p = @{
118+
Resource = $O365Object.Environment.ResourceManager;
119+
AzureService = "AzurePowershell";
120+
InformationAction = $O365Object.InformationAction;
121+
Verbose = $O365Object.verbose;
122+
Debug = $O365Object.debug;
123+
}
124+
$O365Object.auth_tokens.ResourceManager = Connect-MonkeyGenericApplication @p
117125
}
118-
$O365Object.auth_tokens.ResourceManager = Connect-MonkeyGenericApplication @p
119126
}
120127
#Select tenant
121128
If($null -eq $O365Object.TenantId -and $null -ne $O365Object.auth_tokens.ResourceManager){
@@ -145,14 +152,16 @@ Function Connect-MonkeyCloud{
145152
}
146153
Write-Information @msg
147154
#Connect to Microsoft legacy Graph
148-
$p = @{
149-
Resource = $O365Object.Environment.Graph;
150-
AzureService = "AzurePowershell";
151-
InformationAction = $O365Object.InformationAction;
152-
Verbose = $O365Object.verbose;
153-
Debug = $O365Object.debug;
155+
If(!$Connected.IsPresent){
156+
$p = @{
157+
Resource = $O365Object.Environment.Graph;
158+
AzureService = "AzurePowershell";
159+
InformationAction = $O365Object.InformationAction;
160+
Verbose = $O365Object.verbose;
161+
Debug = $O365Object.debug;
162+
}
163+
$O365Object.auth_tokens.Graph = Connect-MonkeyGenericApplication @p
154164
}
155-
$O365Object.auth_tokens.Graph = Connect-MonkeyGenericApplication @p
156165
#Connect to Azure Portal
157166
If($O365Object.isConfidentialApp -eq $false -and $O365Object.IncludeEntraID){
158167
$msg = @{
@@ -163,24 +172,32 @@ Function Connect-MonkeyCloud{
163172
Tags = @('TokenRequestInfoMessage');
164173
}
165174
Write-Information @msg
166-
$p = @{
167-
Resource = (Get-WellKnownAzureService -AzureService AzurePortal);
168-
AzureService = "AzurePowershell";
169-
InformationAction = $O365Object.InformationAction;
170-
Verbose = $O365Object.verbose;
171-
Debug = $O365Object.debug;
175+
If(!$Connected.IsPresent){
176+
$p = @{
177+
Resource = (Get-WellKnownAzureService -AzureService AzurePortal);
178+
AzureService = "AzurePowershell";
179+
InformationAction = $O365Object.InformationAction;
180+
Verbose = $O365Object.verbose;
181+
Debug = $O365Object.debug;
182+
}
183+
$O365Object.auth_tokens.AzurePortal = Connect-MonkeyGenericApplication @p
172184
}
173-
$O365Object.auth_tokens.AzurePortal = Connect-MonkeyGenericApplication @p
174-
#$O365Object.auth_tokens.AzurePortal = Connect-MonkeyAzurePortal
175185
}
176-
#Get Tenant Information
177-
Get-TenantInformation
178186
}
187+
$p = @{
188+
InformationAction = $O365Object.InformationAction;
189+
Verbose = $O365Object.verbose;
190+
Debug = $O365Object.Debug;
191+
}
192+
#Get information about current identity
193+
$O365Object.me = Get-MonkeyMe @p
194+
#Get Tenant Information
195+
Get-TenantInformation
179196
#Check If Azure services is selected
180197
If($O365Object.initParams.Instance -eq "Azure"){
181-
Connect-MonkeyAzure
198+
Connect-MonkeyAzure @PSBoundParameters
182199
#Set Azure connections to True If connection and subscription are present
183-
If($null -ne $O365Object.auth_tokens.ResourceManager -and $null -ne $O365Object.auth_tokens.Graph -and $null -ne $O365Object.auth_tokens.MSGraph -and $null -ne $O365Object.subscriptions){
200+
If($null -ne $O365Object.auth_tokens.ResourceManager -and $null -ne $O365Object.auth_tokens.MSGraph -and $null -ne $O365Object.subscriptions){
184201
$O365Object.onlineServices.Azure = $True
185202
}
186203
Else{
@@ -190,20 +207,13 @@ Function Connect-MonkeyCloud{
190207
}
191208
#Check If Microsoft 365 is selected
192209
ElseIf($O365Object.initParams.Instance -eq "Microsoft365"){
193-
Connect-MonkeyM365
210+
Connect-MonkeyM365 @PSBoundParameters
194211
}
195212
#Get licensing information
196213
$O365Object.Licensing = Get-MonkeySKUInfo
197214
#Check If current identity can request users and groups from Microsoft Graph
198-
$p = @{
199-
InformationAction = $O365Object.InformationAction;
200-
Verbose = $O365Object.verbose;
201-
Debug = $O365Object.Debug;
202-
}
203215
$O365Object.canRequestUsersFromMsGraph = Test-CanRequestUser @p
204216
$O365Object.canRequestGroupsFromMsGraph = Test-CanRequestGroup @p
205-
#Get information about current identity
206-
$O365Object.me = Get-MonkeyMe @p
207217
#Check If connected to Azure AD
208218
If($O365Object.canRequestUsersFromMsGraph -eq $false -and $null -eq $O365Object.Tenant.CompanyInfo){
209219
$msg = @{
@@ -227,7 +237,7 @@ Function Connect-MonkeyCloud{
227237
Else{
228238
$authObject = $O365Object.auth_tokens.GetEnumerator() | Where-Object {$null -ne $_.Value} | Select-Object -ExpandProperty Value -First 1
229239
If($null -ne $authObject){
230-
$O365Object.userId = $authObject | Get-UserIdFromToken
240+
$O365Object.userId = $O365Object.me.id;
231241
}
232242
}
233243
#Get Azure AD permissions
@@ -298,15 +308,16 @@ Function Connect-MonkeyCloud{
298308
}
299309
Write-Information @msg
300310
#Connect to PIM
301-
$p = @{
302-
Resource = (Get-WellKnownAzureService -AzureService MSPIM);
303-
AzureService = "AzurePowershell";
304-
InformationAction = $O365Object.InformationAction;
305-
Verbose = $O365Object.verbose;
306-
Debug = $O365Object.debug;
311+
If(!$Connected.IsPresent){
312+
$p = @{
313+
Resource = (Get-WellKnownAzureService -AzureService MSPIM);
314+
AzureService = "AzurePowershell";
315+
InformationAction = $O365Object.InformationAction;
316+
Verbose = $O365Object.verbose;
317+
Debug = $O365Object.debug;
318+
}
319+
$O365Object.auth_tokens.MSPIM = Connect-MonkeyGenericApplication @p
307320
}
308-
$O365Object.auth_tokens.MSPIM = Connect-MonkeyGenericApplication @p
309-
#$O365Object.auth_tokens.MSPIM = Connect-MonkeyPIM
310321
}
311322
#Get collectors
312323
$p = @{

core/api/auth/aipservice/Connect-MonkeyAIPService.ps1

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -65,19 +65,19 @@ Function Connect-MonkeyAIPService{
6565
Debug = $O365Object.debug;
6666
}
6767
$O365Object.auth_tokens.AADRM = Connect-MonkeyGenericApplication @p
68-
If($null -ne $O365Object.auth_tokens.AADRM){
69-
#Get Service locator url
70-
$service_locator = Get-AADRMServiceLocatorUrl
71-
If($null -ne $service_locator){
72-
#set internal object
73-
If($O365Object.Environment.ContainsKey('aadrm_service_locator')){
74-
$O365Object.Environment.aadrm_service_locator = $service_locator;
75-
}
76-
Else{
77-
$O365Object.Environment.Add('aadrm_service_locator',$service_locator)
78-
}
79-
$O365Object.onlineServices.Item($service) = $true
68+
}
69+
If($null -ne $O365Object.auth_tokens.AADRM){
70+
#Get Service locator url
71+
$service_locator = Get-AADRMServiceLocatorUrl
72+
If($null -ne $service_locator){
73+
#set internal object
74+
If($O365Object.Environment.ContainsKey('aadrm_service_locator')){
75+
$O365Object.Environment.aadrm_service_locator = $service_locator;
76+
}
77+
Else{
78+
$O365Object.Environment.Add('aadrm_service_locator',$service_locator)
8079
}
80+
$O365Object.onlineServices.Item($service) = $true
8181
}
8282
}
8383
}

core/api/auth/azure/Connect-MonkeyAzure.ps1

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -34,17 +34,17 @@ Function Connect-MonkeyAzure{
3434
https://github.com/silverhack/monkey365
3535
#>
3636
[CmdletBinding()]
37-
Param ()
37+
Param (
38+
[parameter(Mandatory=$false, HelpMessage="Used when tokens are imported from init param")]
39+
[Switch]$Connected
40+
)
3841
Begin{
3942
$azure_services = @{
4043
ResourceManager = $O365Object.Environment.ResourceManager;
41-
Graph = $O365Object.Environment.Graph;
4244
ServiceManagement = $O365Object.Environment.Servicemanagement;
43-
AzurePortal = Get-WellKnownAzureService -AzureService AzurePortal;
4445
SecurityPortal = $O365Object.Environment.Servicemanagement;
4546
AzureStorage = $O365Object.Environment.Storage;
4647
AzureVault = $O365Object.Environment.Vaults;
47-
MSGraph =$O365Object.Environment.Graphv2;
4848
LogAnalytics = $O365Object.Environment.LogAnalytics;
4949
}
5050
$app_params = @{
@@ -61,8 +61,8 @@ Function Connect-MonkeyAzure{
6161
}
6262
}
6363
End{
64-
if($null -ne $O365Object.subscriptions -and $null -ne $app_params){
65-
foreach($service in $azure_services.GetEnumerator()){
64+
If($null -ne $O365Object.subscriptions -and $null -ne $app_params){
65+
ForEach($service in $azure_services.GetEnumerator()){
6666
$msg = @{
6767
MessageData = ($message.TokenRequestInfoMessage -f $service.Name)
6868
callStack = (Get-PSCallStack | Select-Object -First 1);
@@ -80,15 +80,17 @@ Function Connect-MonkeyAzure{
8080
#Add resource parameter
8181
$new_params.Resource = $service.Value
8282
try{
83-
$O365Object.auth_tokens.$($azure_service) = Connect-MonkeyGenericApplication @new_params
84-
$msg = @{
85-
MessageData = ($message.TokenAcquiredInfoMessage -f $service.Name)
86-
callStack = (Get-PSCallStack | Select-Object -First 1);
87-
logLevel = 'info';
88-
InformationAction = $O365Object.InformationAction;
89-
Tags = @('TokenAcquiredMessage');
83+
IF(!$Connected.IsPresent){
84+
$O365Object.auth_tokens.$($azure_service) = Connect-MonkeyGenericApplication @new_params
85+
$msg = @{
86+
MessageData = ($message.TokenAcquiredInfoMessage -f $service.Name)
87+
callStack = (Get-PSCallStack | Select-Object -First 1);
88+
logLevel = 'info';
89+
InformationAction = $O365Object.InformationAction;
90+
Tags = @('TokenAcquiredMessage');
91+
}
92+
Write-Information @msg
9093
}
91-
Write-Information @msg
9294
}
9395
catch{
9496
$msg = @{

0 commit comments

Comments
 (0)