Framework/Core/CredHygiene/CredHygieneScript.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
Set-StrictMode -Version Latest

class CredHygiene : CommandBase{
    [string] $credName;
    [string] $credLocation;
    [int] $rotationInt;
    [int] $nextExpiry;
    [string] $comment;
    hidden [string] $AzSKTemp = (Join-Path $([Constants]::AzSKAppFolderPath) $([Constants]::RotationMetadataSubPath)); 
    hidden [PSObject] $AzSKResourceGroup = $null;
    hidden [PSObject] $AzSKStorageAccount = $null;
    hidden [PSObject] $RotationMetadataContainer = $null;
    hidden [string] $RotationMetadataContainerName = [Constants]::RotationMetadataContainerName

    CredHygiene([string] $subscriptionId, [InvocationInfo] $invocationContext): 
        Base($subscriptionId, $invocationContext)
        {
            $this.DoNotOpenOutputFolder = $true;
        }

    hidden [void] GetAzSKRotationMetadatContainer()
    {
        if($null -eq $this.AzSKStorageAccount)
        {
            $this.GetAzSKStorageAccount()
        }
        if($null -eq $this.AzSKStorageAccount)
        {
            return;
        }


        try
        {
            $containerObject = Get-AzStorageContainer -Context $this.AzSKStorageAccount.Context -Name $this.RotationMetadataContainerName -ErrorAction Stop
            $this.RotationMetadataContainer = $containerObject;
        }
        catch
        {
            try
            {
                New-AzStorageContainer -Context $this.AzSKStorageAccount.Context -Name $this.RotationMetadataContainerName -ErrorAction SilentlyContinue
                $containerObject = Get-AzStorageContainer -Context $this.AzSKStorageAccount.Context -Name $this.RotationMetadataContainerName -ErrorAction SilentlyContinue
                $this.RotationMetadataContainer = $containerObject;
            }
            catch
            {
                #Do nothing
            }
        }
    }

    hidden [void] GetAzSKStorageAccount()
    {
        if($null -eq $this.AzSKResourceGroup)
        {
            $this.GetAzSKRG();
        }
        if($null -ne $this.AzSKResourceGroup)
        {
            $StorageAccount = Get-AzStorageAccount -ResourceGroupName $this.AzSKResourceGroup.ResourceGroupName | Where-Object {$_.StorageAccountName -like 'azsk*'} -ErrorAction SilentlyContinue
            #if no storage account found then it assumes that there is no control state feature is not used and if there are more than one storage account found it assumes the same
            $this.AzSKStorageAccount = $StorageAccount;
        }
    }

    hidden [PSObject] GetAzSKRG()
    {
        $azSKConfigData = [ConfigurationManager]::GetAzSKConfigData()
        $resourceGroup = Get-AzResourceGroup -Name $azSKConfigData.AzSKRGName -ErrorAction SilentlyContinue
        $this.AzSKResourceGroup = $resourceGroup
        return $resourceGroup;
    }

    [void] PrintDetails($credentialInfo,$messageType){
        $this.PublishCustomMessage("`n")
        $this.PublishCustomMessage("Settings for the AzSK tracked credential [$($credentialInfo.credName)] `n`n", [MessageType]::Info) 
        $this.PublishCustomMessage("`n")

        $table = $credentialInfo | Format-List @{Label = "Name"; Expression = { $_.credName }} , @{Label = "Location"; Expression = { $_.credLocation }}, @{Label = "Rotation interval (days)"; Expression = { $_.rotationInt }}, @{Label = "Credential Group"; Expression = { $_.credGroup }}, @{Label = "Created on"; Expression = { $_.firstUpdatedOn }}, @{Label = "Created by"; Expression = { $_.firstUpdatedBy }}, @{Label = "Last update"; Expression = { $_.lastUpdatedOn }}, @{Label = "Updated by"; Expression = { $_.lastUpdatedBy }}, @{Label = "Comment"; Expression = { $_.comment }} | Out-String
        $this.PublishCustomMessage($table, $messageType)

        if($credentialInfo.credLocation -eq "AppService"){
            $this.PublishCustomMessage([Constants]::SingleDashLine);
            $this.PublishCustomMessage("Additional Details:");
            $table = $credentialInfo | Format-List @{Label = "AppService Name"; Expression = { $_.resourceName }} , @{Label = "AppService config type"; Expression = { $_.appConfigType }}, @{Label = "AppService config name"; Expression = { $_.appConfigName }} | Out-String
            $this.PublishCustomMessage($table, $messageType)
        }
        if($credentialInfo.credLocation -eq "KeyVault"){
            $this.PublishCustomMessage([Constants]::SingleDashLine);
            $this.PublishCustomMessage("Additional Details:");
            $table = $credentialInfo | Format-List @{Label = "Key vault Name"; Expression = { $_.kvName }} , @{Label = "Credential type"; Expression = { $_.kvCredType }}, @{Label = "Credential name"; Expression = { $_.kvCredName }}, @{Label = "Expiry time"; Expression = { $_.expiryTime }}, @{Label = "Version"; Expression = { $_.version }} | Out-String
            $this.PublishCustomMessage($table, $messageType)
        }

        $this.PublishCustomMessage("`n")        
    }

    [void] PrintInfo ($credentialInfo){
        $this.PublishCustomMessage("`n")
        $this.PublishCustomMessage("Settings for the AzSK tracked credential [$($credentialInfo.credName)] `n`n", [MessageType]::Info) 
        $this.PublishCustomMessage("`n")
        $table = $credentialInfo | Format-List @{Label = "Name"; Expression = { $_.credName }} , @{Label = "Location"; Expression = { $_.credLocation }}, @{Label = "Rotation interval (days)"; Expression = { $_.rotationInt }}, @{Label = "Credential Group"; Expression = { $_.credGroup }}, @{Label = "Comment"; Expression = { $_.comment }} | Out-String
        $this.PublishCustomMessage($table, [MessageType]::Default)

        if($credentialInfo.credLocation -eq "AppService"){
            $this.PublishCustomMessage([Constants]::SingleDashLine);
            $this.PublishCustomMessage("Additional Details:");
            $table = $credentialInfo | Format-List @{Label = "AppService Name"; Expression = { $_.resourceName }} , @{Label = "AppService config type"; Expression = { $_.appConfigType }}, @{Label = "AppService config name"; Expression = { $_.appConfigName }} | Out-String
            $this.PublishCustomMessage($table, [MessageType]::Default)
        }
        if($credentialInfo.credLocation -eq "KeyVault"){
            $this.PublishCustomMessage([Constants]::SingleDashLine);
            $this.PublishCustomMessage("Additional Details:");
            $table = $credentialInfo | Format-List @{Label = "Key vault Name"; Expression = { $_.kvName }} , @{Label = "Credential type"; Expression = { $_.kvCredType }}, @{Label = "Credential name"; Expression = { $_.kvCredName }}, @{Label = "Expiry time"; Expression = { $_.expiryTime }}, @{Label = "Version"; Expression = { $_.version }} | Out-String
            $this.PublishCustomMessage($table, [MessageType]::Default)
        }

        $this.PublishCustomMessage("`n")        
    }

    [void] GetAlert($CredentialName,$DetailedView)
    {          
        $file = Join-Path $($this.AzSKTemp) -ChildPath $($this.SubscriptionContext.SubscriptionId) | Join-Path -ChildPath $CredentialName
        $file += ".json"
        $this.GetAzSKRotationMetadatContainer()

        $tempSubPath = Join-Path $($this.AzSKTemp) $($this.SubscriptionContext.SubscriptionId)

        if(![string]::isnullorwhitespace($this.SubscriptionContext.SubscriptionId)){
            if(-not (Test-Path $tempSubPath))
            {
                New-Item -ItemType Directory -Path $tempSubPath -ErrorAction Stop | Out-Null
            }    
        }
        else{
            if(-not (Test-Path "$($this.AzSKTemp)"))
            {
                New-Item -ItemType Directory -Path "$($this.AzSKTemp)" -ErrorAction Stop | Out-Null
            }
        }

        $controlSettings = $this.LoadServerConfigFile("ControlSettings.json");

        if($CredentialName){
            $blobName = $CredentialName.ToLower() + ".json"
            $blobContent = Get-AzStorageBlobContent -Blob $blobName -Container $this.RotationMetadataContainerName -Context $this.AzSKStorageAccount.Context -Destination $file -Force -ErrorAction Ignore
            if($blobContent){    
                $credentialInfo = Get-ChildItem -Path $file -Force | Get-Content | ConvertFrom-Json

                $messageType = [MessageType]::Default
                
                $currentTime = [DateTime]::UtcNow;
                $lastRotatedTime = $credentialInfo.lastUpdatedOn;
                $expiryTime = $lastRotatedTime.AddDays($credentialInfo.rotationInt);
                
                if($expiryTime -le $currentTime.AddDays($controlSettings.SubscriptionCore.credHighTH)){ #Checking for expired/about to expire credentials
                    $messageType = [MessageType]::Critical
                }
                elseif(($expiryTime -gt $currentTime.AddDays($controlSettings.SubscriptionCore.credHighTH)) -and ($expiryTime -le $currentTime.AddDays($controlSettings.SubscriptionCore.credModerateTH))){ #Checking for credentials nearing expiry.
                    $messageType = [MessageType]::Warning
                }
                $this.PrintDetails($credentialInfo,$messageType);
            }
            else{
                $this.PublishCustomMessage("Could not find an entry for credential [$CredentialName].",[MessageType]::Critical)
                $this.PublishCustomMessage("Please check the name or run Get-AzSKTrackedCredential to list all credentials currently being tracked for rotation/expiry.")
            }
        }
        else{
            $blob = @();
            $blob = Get-AzStorageBlob -Container $this.RotationMetadataContainerName -Context $this.AzSKStorageAccount.Context -ErrorAction Ignore

            if($blob){
                $this.PublishCustomMessage("`n")
                $this.PublishCustomMessage("`nListing settings for all the credentials `n",[MessageType]::Update)
                
                # array to store cred info in ascending order of expiry time.

                $sortedBlob = @();
                
                $blob | where {
                    $_ | Get-AzStorageBlobContent -Destination $file -Force | Out-Null
                    $credentialInfo = Get-ChildItem -Path $file -Force | Get-Content | ConvertFrom-Json
                    $sortedBlob += $credentialInfo;
                }

                $sortedBlob = $sortedBlob | Sort-Object -Property @{Expression = {($_.lastUpdatedOn).AddDays($_.rotationInt)}; Descending = $False} 
                
                $currentTime = [DateTime]::UtcNow;

                if($DetailedView){
                    $sortedBlob | where{
                        $lastRotatedTime = $_.lastUpdatedOn;
                        $expiryTime = $lastRotatedTime.AddDays($_.rotationInt);
                        $messageType = [MessageType]::Default
                        if($expiryTime -le $currentTime.AddDays($controlSettings.SubscriptionCore.credHighTH)){ #Checking for expired/about to expire credentials
                            $messageType = [MessageType]::Critical
                        }
                        elseif(($expiryTime -gt $currentTime.AddDays($controlSettings.SubscriptionCore.credHighTH)) -and ($expiryTime -le $currentTime.AddDays($controlSettings.SubscriptionCore.credModerateTH))){ #Checking for credentials nearing expiry.
                            $messageType = [MessageType]::Warning
                        }

                        $this.PrintDetails($_,$messageType);
                    }
                }
                else{
                    $table = $sortedBlob | Format-Table -AutoSize -Wrap @{Label = "Name"; Expression = { $_.credName }} , @{Label = "Location"; Expression = { $_.credLocation }}, @{Label = "Rotation interval (days)"; Expression = { $_.rotationInt }; align='left'}, @{Label = "Credential Group"; Expression = { $_.credGroup }}, @{Label = "Created on"; Expression = { $_.firstUpdatedOn }}, @{Label = "Created by"; Expression = { $_.firstUpdatedBy }}, @{Label = "Last update"; Expression = { $_.lastUpdatedOn }}, @{Label = "Updated by"; Expression = { $_.lastUpdatedBy }}, @{Label = "Comment"; Expression = { $_.comment }} | Out-String
                    $this.PublishCustomMessage($table, [MessageType]::Default)
                }
            }
            else{
                $this.PublishCustomMessage("No credential entries found for rotation/expiry alert.", [MessageType]::Critical)
            }        
        }

        if(Test-Path $file)
        {
            Remove-Item -Path $file
        }
    }

    [void] NewAlert($CredentialLocation,$CredentialGroup)
    {
        $file = Join-Path $($this.AzSKTemp) -ChildPath $($this.SubscriptionContext.SubscriptionId) | Join-Path -ChildPath $($this.credName)
        $file += ".json"
        $this.GetAzSKRotationMetadatContainer()
        $blobName = ($this.credName).ToLower() + ".json"
        [bool] $found = $true;

        $tempSubPath = Join-Path $($this.AzSKTemp) $($this.SubscriptionContext.SubscriptionId)

        if(![string]::isnullorwhitespace($this.SubscriptionContext.SubscriptionId)){
            if(-not (Test-Path $tempSubPath))
            {
                New-Item -ItemType Directory -Path $tempSubPath -ErrorAction Stop | Out-Null
            }    
        }
        else{
            if(-not (Test-Path "$($this.AzSKTemp)"))
            {
                New-Item -ItemType Directory -Path "$($this.AzSKTemp)" -ErrorAction Stop | Out-Null
            }
        }

        $blobContent = Get-AzStorageBlobContent -Blob $blobName -Container $this.RotationMetadataContainerName -Context $this.AzSKStorageAccount.Context -Destination $file -Force -ErrorAction Ignore
        if($blobContent){
            $this.PublishCustomMessage("Entry for the credential [$($this.credName)] already exists.", [MessageType]::Error);
            $this.PublishCustomMessage("Run Update-AzSKTrackedCredential to update alert configurations for the existing credential.", [MessageType]::Default)
        }
        else{
            
            $startTime = [DateTime]::UtcNow
            $user = ([ContextHelper]::GetCurrentRMContext()).Account.Id
            $this.PublishCustomMessage("Onboarding the credential [$($this.credName)] for rotation/expiry notification", [MessageType]::Default)
            $credentialInfo = New-Object PSObject
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name credLocation -Value $this.credLocation
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name credName -Value $this.credName
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name rotationInt -Value $this.rotationInt
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name nextExpiry -Value $this.nextExpiry
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name firstUpdatedOn -Value $startTime
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name lastUpdatedOn -Value $startTime
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name comment -Value $this.comment
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name firstUpdatedBy -Value $user
            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name lastUpdatedBy -Value $user

            if($credentialInfo.rotationInt -gt $credentialInfo.nextExpiry){
                $credentialInfo.lastUpdatedOn = ($credentialInfo.lastUpdatedOn).AddDays($credentialInfo.nextExpiry - $credentialInfo.rotationInt) 
            }

            if($CredentialLocation -eq "AppService"){

                $appsvc = Get-AzWebApp -ErrorAction Ignore

                if(($appsvc|Measure-Object).Count -gt 0){

                    Write-Host "`nPlease select app service name from below:" -ForegroundColor Cyan

                    $i=0;
                    $appsvc | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                    
                    $choice = Read-Host "App service name choice"
                    while($choice -notin 0..($i-1)){
                        Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                        Write-Host "Please select app service name from below:" -ForegroundColor Cyan

                        $i=0;
                        $appsvc | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                        $choice = Read-Host "App service name choice"
                    }

                    $ResourceName = $appsvc[$choice].Name
                    $rsc = Get-AzWebApp -Name $ResourceName
                    
                    $appConfig = $null
                    $AppConfigType = $null
                    Write-Host "`nPlease select app config type from below: `n[1]: Application Settings`n[2]: Connection Strings" -ForegroundColor Cyan

                    $input = Read-Host "App config type"
                    
                    while(($input -ne 1) -and ($input -ne 2)){
                        Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                        Write-Host "Please select app config type from below: `n[1]: Application Settings`n[2]: Connection Strings" -ForegroundColor Cyan
                        $input = Read-Host "App config type"
                    }
                    
                    if($input -eq 1)
                    {
                        $AppConfigType = "Application Settings"
                        $appConfig = $rsc.SiteConfig.AppSettings
                    }
                    elseif($input -eq 2)
                    {
                        $AppConfigType = "Connection Strings"
                        $appConfig = $rsc.SiteConfig.ConnectionStrings
                    }
                    
                    if(($appConfig|Measure-Object).Count -gt 0){
                        Write-Host "`nPlease select app config name from below:" -ForegroundColor Cyan
                        $i=0;
                        $appConfig | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                        $choice = Read-Host "App config name"
                        while($choice -notin 0..($i-1)){
                            Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                            Write-Host "Please select app config name from below:" -ForegroundColor Cyan
                            $i=0;
                            $appConfig | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                            $choice = Read-Host "App config name"
                        }
                        $AppConfigName = $appConfig[$choice].Name       

                        Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name resourceName -Value $ResourceName
                        Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name appConfigType -Value $AppConfigType
                        Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name appConfigName -Value $AppConfigName
                    }    
                    else{
                        $found = $false
                        $this.PublishCustomMessage("There are no $AppConfigType in the app service [$ResourceName].", [MessageType]::Error)
                    }
                }
                else{
                    $found = $false
                    $this.PublishCustomMessage("There are no app services in your subscription.", [MessageType]::Error)
                }

            }

            if($CredentialLocation -eq "KeyVault"){

                $keyVault = Get-AzKeyVault -ErrorAction Ignore

                if(($keyVault|Measure-Object).Count -gt 0){
                    Write-Host "`nPlease select key vault name from below:" -ForegroundColor Cyan

                    $i=0;
                    $keyVault | where{Write-Host "[$i] $($_.VaultName)" -ForegroundColor Cyan; $i++}
                    
                    $choice = Read-Host "Key vault name choice"
                    while($choice -notin 0..($i-1)){
                        Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                        Write-Host "Please select key vault name from below:" -ForegroundColor Cyan

                        $i=0;
                        $keyVault | where{Write-Host "[$i] $($_.VaultName)" -ForegroundColor Cyan; $i++}
                        $choice = Read-Host "Key vault name choice"
                    }
                    $KVName = $keyVault[$choice].VaultName
                    Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name kvName -Value $KVName
                    
                    $KVCredentialType = $null
                    $KVCredentialName = $null

                    Write-Host "`nPlease select key vault credential type from below: `n[1]: Key`n[2]: Secret" -ForegroundColor Cyan
                    $input = Read-Host "`Key Vault credential type"
                    
                    while(($input -ne 1) -and ($input -ne 2)){
                        Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                        Write-Host "Please select key vault credential type from below: `n[1]: Key`n[2]: Secret" -ForegroundColor Cyan
                        $input = Read-Host "Key Vault credential type"
                    }

                    if($input -eq 1)
                    {
                        $KVCredentialType = "Key"
                        Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name kvCredType -Value $KVCredentialType
                        $keys = Get-AzKeyVaultKey -VaultName $KVName -ErrorAction Ignore
                        if($keys){
                            Write-Host "`nPlease select key name from below:" -ForegroundColor Cyan
                            $i=0;
                            $keys | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                            $choice = Read-Host "Key name choice"
                            while($choice -notin 0..($i-1)){
                                Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                                Write-Host "Please select key name from below:" -ForegroundColor Cyan
            
                                $i=0;
                                $keys | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                                $choice = Read-Host "Key name choice"
                            }
                            $KVCredentialName = $keys[$choice].Name
                            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name kvCredName -Value $KVCredentialName
                            $key = Get-AzKeyVaultKey -VaultName $KVName -Name $KVCredentialName -ErrorAction Ignore
                            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name expiryTime -Value $key.Expires
                            if($key.Expires){
                                if($startTime.AddDays($credentialInfo.rotationInt) -gt $key.Expires){
                                    $credentialInfo.lastUpdatedOn = ($key.Expires).AddDays(-($credentialInfo.rotationInt))
                                }
                                else{
                                    $credentialInfo.lastUpdatedOn = $startTime
                                }
                            }
                            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name version -Value $key.Version

                        }
                        else{
                            $found = $false;
                            $this.PublishCustomMessage("Either there are no keys in the key vault [$KVName] or you do not have sufficient permissions to access them.",[MessageType]::Error)
                        }
                    }
                    elseif($input -eq 2)
                    {
                        $KVCredentialType = "Secret"
                        Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name kvCredType -Value $KVCredentialType
                        $secrets = Get-AzKeyVaultSecret -VaultName $KVName -ErrorAction Ignore
                        if($secrets){
                            Write-Host "`nPlease select secret name from below:" -ForegroundColor Cyan
                            $i=0;
                            $secrets | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                            $choice = Read-Host "Secret name choice"
                            while($choice -notin 0..($i-1)){
                                Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                                Write-Host "Please select secret name from below:" -ForegroundColor Cyan
            
                                $i=0;
                                $secrets | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                                $choice = Read-Host "Secret name choice"
                            }
                            $KVCredentialName = $secrets[$choice].Name
                            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name kvCredName -Value $KVCredentialName
                            $secret = Get-AzKeyVaultSecret -VaultName $KVName -Name $KVCredentialName -ErrorAction Ignore
                            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name expiryTime -Value $secret.Expires
                            if($secret.Expires){
                                if($startTime.AddDays($credentialInfo.rotationInt) -gt $secret.Expires){
                                    $credentialInfo.lastUpdatedOn = ($secret.Expires).AddDays(-($credentialInfo.rotationInt))
                                }
                                else{
                                    $credentialInfo.lastUpdatedOn = $startTime
                                }
                            }
                            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name version -Value $secret.Version

                        }
                        else{
                            $found = $false;
                            $this.PublishCustomMessage("Either there are no secrets in the key vault [$KVName] or you do not have sufficient permissions to access them.",[MessageType]::Error)
                        }
                    }
                }        
                else{
                    $found = $false
                    $this.PublishCustomMessage("There are no key vaults in your subscription.", [MessageType]::Error)
                }
            }

            $ag = $null;
            if($found -and $CredentialGroup){
                $actionGroups = Get-AzActionGroup -ErrorAction Ignore -WarningAction Ignore

                if(($actionGroups|Measure-Object).Count -gt 0){
                    $ag = $actionGroups | where{$_.Name -eq $CredentialGroup}
                    
                    if(-not $ag){
                        $this.PublishCustomMessage("The action group [$CredentialGroup] does not exist in the subscription.",[MessageType]::Error)
                        Write-Host "`nPlease select action group name from below:" -ForegroundColor Cyan
                        Write-Host "[0] Create new credential group" -ForegroundColor Cyan
                        $i=1;
                        $actionGroups | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                        $choice = Read-Host "Credential group choice"
                        while($choice -notin 0..$i){
                            Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                            Write-Host "Please select action group name from below:" -ForegroundColor Cyan
                            Write-Host "[0] Create new credential group" -ForegroundColor Cyan
                            $i=1;
                            $actionGroups | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                            $choice = Read-Host "Credential group choice"
                        }
                        if($choice -ne 0){
                            $ag = $actionGroups[$choice-1]
                            $CredentialGroup = $ag.Name
                        }
                        elseif($choice -eq 0){
                            $emailR = $null;
                            $phoneR = $null;
                            $rgName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName

                            Write-Host "`nInitiating flow for creating a new credential group..." -ForegroundColor Yellow
                            $name = Read-Host "Please enter the name of the credential group"
                            $shortName = Read-Host "Please enter the short name (less than 12 characters) of the credential group"
                            
                            Write-Host "`nPlease enter: `n[0]: to skip email configuration `n[1]: to configure email for the credential group" -ForegroundColor Cyan
                            $choice = Read-Host "User choice"
                            while($choice -notin 0..1){
                                Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                                Write-Host "`nPlease enter: `n[0]: to skip email configuration `n[1]: to configure email for the credential group" -ForegroundColor Cyan
                                $choice = Read-Host "User choice"
                            }
                            if($choice -eq 1){
                                $email = Read-Host "Enter a valid email address"
                                $emailR = New-AzActionGroupReceiver -Name 'email' -EmailReceiver -EmailAddress $email -ErrorAction Ignore
                            }

                            Write-Host "`nPlease enter: `n[0]: to skip SMS configuration `n[1]: to configure SMS for the credential group" -ForegroundColor Cyan
                            $choice = Read-Host "User choice"
                            while($choice -notin 0..1){
                                Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                                Write-Host "`nPlease enter: `n[0]: to skip SMS configuration `n[1]: to configure SMS for the credential group" -ForegroundColor Cyan
                                $choice = Read-Host "User choice"
                            }
                            if($choice -eq 1){
                                $countryCode = Read-Host "Enter a valid country code"
                                $phone = Read-Host "Enter a valid contact number"
                                $phoneR = New-AzActionGroupReceiver -Name 'SMS' -SmsReceiver -CountryCode $countryCode -PhoneNumber $phone -ErrorAction Ignore
                            }

                            if($emailR -and $phoneR){
                                $ag = Set-AzActionGroup -Name $name -ResourceGroupName $rgName -ShortName $shortName -Receiver $emailR,$phoneR -ErrorAction Ignore -WarningAction Ignore
                            }
                            elseif($emailR){
                                $ag = Set-AzActionGroup -Name $name -ResourceGroupName $rgName -ShortName $shortName -Receiver $emailR -ErrorAction Ignore -WarningAction Ignore
                            }
                            elseif($phoneR) {
                                $ag = Set-AzActionGroup -Name $name -ResourceGroupName $rgName -ShortName $shortName -Receiver $phoneR -ErrorAction Ignore -WarningAction Ignore
                            }
                        }
                        
                    }

                    if($ag){
                        $CredentialGroup = $ag.Name
                        $this.InstallCredentialGroupAlert($ag);
                        Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name credGroup -Value $CredentialGroup
                    }
                    else{
                        $this.PublishCustomMessage("Error occured while creating the new credential group.", [MessageType]::Error)
                    }
                    
                }    
                else{
                    Write-Host "`nPlease enter [0] to create a new credential group" -ForegroundColor Cyan
                    $choice = Read-Host "User choice"

                    if($choice -eq 0){
                        $emailR = $null;
                        $phoneR = $null;
                        $rgName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName

                        Write-Host "`nInitiating flow for creating a new credential group..." -ForegroundColor Yellow
                        $name = Read-Host "Please enter the name of the credential group"
                        $shortName = Read-Host "Please enter the short name (less than 12 characters) of the credential group"
                            
                        Write-Host "`nPlease enter: `n[0]: to skip email configuration `n[1]: to configure email for the credential group" -ForegroundColor Cyan
                        $choice = Read-Host "User choice"
                        while($choice -notin 0..1){
                            Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                            Write-Host "`nPlease enter: `n[0]: to skip email configuration `n[1]: to configure email for the credential group" -ForegroundColor Cyan
                            $choice = Read-Host "User choice"
                        }
                        if($choice -eq 1){
                            $email = Read-Host "Enter a valid email address"
                            $emailR = New-AzActionGroupReceiver -Name 'email' -EmailReceiver -EmailAddress $email -ErrorAction Ignore
                        }
                        Write-Host "`nPlease enter: `n[0]: to skip SMS configuration `n[1]: to configure SMS for the credential group" -ForegroundColor Cyan
                        $choice = Read-Host "User choice"
                        while($choice -notin 0..1){
                            Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                            Write-Host "`nPlease enter: `n[0]: to skip SMS configuration `n[1]: to configure SMS for the credential group" -ForegroundColor Cyan
                            $choice = Read-Host "User choice"
                        }
                        if($choice -eq 1){
                            $countryCode = Read-Host "Enter a valid country code"
                            $phone = Read-Host "Enter a valid contact number"
                            $phoneR = New-AzActionGroupReceiver -Name 'SMS' -SmsReceiver -CountryCode $countryCode -PhoneNumber $phone -ErrorAction Ignore
                        }

                        if($emailR -and $phoneR){
                            $ag = Set-AzActionGroup -Name $name -ResourceGroupName $rgName -ShortName $shortName -Receiver $emailR,$phoneR -ErrorAction Ignore -WarningAction Ignore
                        }
                        elseif($emailR){
                            $ag = Set-AzActionGroup -Name $name -ResourceGroupName $rgName -ShortName $shortName -Receiver $emailR -ErrorAction Ignore -WarningAction Ignore
                        }
                        elseif($phoneR) {
                            $ag = Set-AzActionGroup -Name $name -ResourceGroupName $rgName -ShortName $shortName -Receiver $phoneR -ErrorAction Ignore -WarningAction Ignore
                        }

                        if($ag){
                            $CredentialGroup = $ag.Name
                            $this.InstallCredentialGroupAlert($ag);
                            Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name credGroup -Value $CredentialGroup
                        }
                        else{
                            $this.PublishCustomMessage("Error occured while creating the new credential group.", [MessageType]::Error)
                        }
                    }
                    else{
                        $this.PublishCustomMessage("There are no action groups in your subscription.", [MessageType]::Error)
                    }
                }    
            }

            
            if($found){
                $credentialInfo | ConvertTo-Json -Depth 10 | Out-File $file -Force
                Set-AzStorageBlobContent -Blob $blobName -Container $this.RotationMetadataContainerName -Context $this.AzSKStorageAccount.Context -File $file -Force | Out-Null
                $this.PublishCustomMessage("Successfully onboarded the credential [$($this.credName)] for rotation/expiry notification", [MessageType]::Update)
                $this.PrintInfo($credentialInfo);
            }
            else{
                $this.PublishCustomMessage("Could not onboard the credential [$($this.credName)] for rotation/expiry notification", [MessageType]::Error)
            }
        }
        if(Test-Path $file)
        {
            Remove-Item -Path $file
        }
        $this.PublishCustomMessage("Run Get-AzSKTrackedCredential to list all the credentials onboarded for rotation/expiry notification.")
    }

    [void] RemoveAlert($CredentialName,$Force)
    {           
        $file = Join-Path $($this.AzSKTemp) -ChildPath $($this.SubscriptionContext.SubscriptionId) | Join-Path -ChildPath $CredentialName
        $file += ".json"
        $this.GetAzSKRotationMetadatContainer()
        $blobName = $CredentialName.ToLower() + ".json"

        $tempSubPath = Join-Path $($this.AzSKTemp) $($this.SubscriptionContext.SubscriptionId)

        if(![string]::isnullorwhitespace($this.SubscriptionContext.SubscriptionId)){
            if(-not (Test-Path $tempSubPath))
            {
                New-Item -ItemType Directory -Path $tempSubPath -ErrorAction Stop | Out-Null
            }    
        }
        else{
            if(-not (Test-Path "$($this.AzSKTemp)"))
            {
                New-Item -ItemType Directory -Path "$($this.AzSKTemp)" -ErrorAction Stop | Out-Null
            }
        }

        $blobContent = Get-AzStorageBlobContent -Blob $blobName -Container $this.RotationMetadataContainerName -Context $this.AzSKStorageAccount.Context -Destination $file -Force -ErrorAction Ignore
        if($blobContent){
            
            $title = "Confirm the removal of credential rotation/expiry notification"
            $yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes", "This means Yes"
            $no = New-Object System.Management.Automation.Host.ChoiceDescription "&No", "This means No"
            $options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
            #below is hack for removing error due to strict mode - host variable is not assigned in the method
            $host = $host
            # Ask for confirmation only if force switch is not present
            $result = 0

            if(!$Force)
            {
                #user confirmation before deleting container
                $storageConfirmMsg = "Are you sure you want to remove expiry alerting on [$CredentialName] credential?"
                $result = $host.ui.PromptForChoice($title, $storageConfirmMsg, $options, 1)
            }
            if($result -eq 0)
            {
                #user selected yes
                $this.PublishCustomMessage("Removing the rotation/expiry notification on the credential [$CredentialName]", [MessageType]::Default) 
                $blobContent | Remove-AzStorageBlob 
                $this.PublishCustomMessage("Successfully removed the rotation/expiry notification on the credential [$CredentialName]", [MessageType]::Update)
            }
            #user selected no in confirmation box
            else
            {
                $this.PublishCustomMessage("You have chosen not to remove rotation/expiry notification on the credential [$CredentialName]", [MessageType]::Default) 
            }        

        }
        else{
            $this.PublishCustomMessage("Could not find an entry for credential [$CredentialName].", [MessageType]::Critical)
        }
        if(Test-Path $file)
        {
            Remove-Item -Path $file
        }
    }
    
    [void] UpdateAlert($CredentialName,$RotationIntervalInDays,$CredentialGroup,$UpdateCredential,$ResetLastUpdate,$Comment)
    {           
        $file = Join-Path $($this.AzSKTemp) -ChildPath $($this.SubscriptionContext.SubscriptionId) | Join-Path -ChildPath $CredentialName
        $file += ".json"
        $this.GetAzSKRotationMetadatContainer()
        $blobName = $CredentialName.ToLower() + ".json"

        $tempSubPath = Join-Path $($this.AzSKTemp) $($this.SubscriptionContext.SubscriptionId)

        if(![string]::isnullorwhitespace($this.SubscriptionContext.SubscriptionId)){
            if(-not (Test-Path $tempSubPath))
            {
                New-Item -ItemType Directory -Path $tempSubPath -ErrorAction Stop | Out-Null
            }    
        }
        else{
            if(-not (Test-Path "$($this.AzSKTemp)"))
            {
                New-Item -ItemType Directory -Path "$($this.AzSKTemp)" -ErrorAction Stop | Out-Null
            }
        }

        $blobContent = Get-AzStorageBlobContent -Blob $blobName -Container $this.RotationMetadataContainerName -Context $this.AzSKStorageAccount.Context -Destination $file -Force -ErrorAction Ignore
        if($blobContent){
            $this.PublishCustomMessage("Updating settings for AzSK tracked credential [$CredentialName]", [MessageType]::Default) 
            $credentialInfo = Get-ChildItem -Path $file -Force | Get-Content | ConvertFrom-Json
            $user = ([ContextHelper]::GetCurrentRMContext()).Account.Id;
            $currentTime = [DateTime]::UtcNow

            if ($RotationIntervalInDays)
            {
                $credentialInfo.rotationInt = $RotationIntervalInDays;
            }

            $ag = $null;
            if($CredentialGroup){
                $actionGroups = Get-AzActionGroup -ErrorAction Ignore -WarningAction Ignore
                if(($actionGroups|Measure-Object).Count -gt 0){
                    $ag = $actionGroups | where{$_.Name -eq $CredentialGroup}
                    
                    if(-not $ag){
                        $this.PublishCustomMessage("The action group [$CredentialGroup] does not exist in the subscription.",[MessageType]::Error)
                        Write-Host "`nPlease select action group name from below:" -ForegroundColor Cyan
                        $i=0;
                        $actionGroups | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                        $choice = Read-Host "Credential group choice"
                        while($choice -notin 0..($i-1)){
                            Write-Host "`nIncorrect value supplied." -ForegroundColor Red
                            Write-Host "Please select action group name from below:" -ForegroundColor Cyan
                            $i=0;
                            $actionGroups | where{Write-Host "[$i] $($_.Name)" -ForegroundColor Cyan; $i++}
                            $choice = Read-Host "Credential group choice"
                        }
                        $ag = $actionGroups[$choice]
                        $CredentialGroup = $ag.Name
                    }

                    $this.InstallCredentialGroupAlert($ag);
                    if([Helpers]::CheckMember($credentialInfo,"credGroup")){
                        $credentialInfo.credGroup = $CredentialGroup
                    }
                    else{
                        Add-Member -InputObject $credentialInfo -MemberType NoteProperty -Name credGroup -Value $CredentialGroup
                    }
                }
                else{
                    $this.PublishCustomMessage("Could not update the credential group for the credential [$CredentialName] as there are no action groups in your subscription.", [MessageType]::Error)
                }
                
            }

            if($UpdateCredential){
            
                if($credentialInfo.credLocation -eq "AppService"){
                    
                    $resource = Get-AzWebApp -Name $credentialInfo.resourceName
                    
                    $hash = @{}

                    if($credentialInfo.appConfigType -eq "Application Settings"){
                        $this.PublishCustomMessage("Updating the application setting [$($credentialInfo.appConfigName)] in the app service [$($credentialInfo.resourceName)]", [MessageType]::Default)  
                        $appConfig = $resource.SiteConfig.AppSettings 
                        $appConfigValue = Read-Host "Enter the new secret for the application setting - [$($credentialInfo.appConfigName)]" -AsSecureString 
                        $appConfigValue = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($appConfigValue))

                        foreach ($setting in $appConfig) {
                            if($setting.Name -eq $credentialInfo.appConfigName){
                                $hash[$setting.Name] = $appConfigValue
                            }
                            else{
                                $hash[$setting.Name] = $setting.Value
                            }
                        }
                    }
                    elseif($credentialInfo.appConfigType -eq "Connection Strings"){
                        $this.PublishCustomMessage("Updating the connection string [$($credentialInfo.appConfigName)] in the app service [$($credentialInfo.resourceName)]", [MessageType]::Default) 
                        $appConfig = $resource.SiteConfig.ConnectionStrings
                        $appConfigValue = Read-Host "Enter the new secret for the connection string - [$($credentialInfo.appConfigName)]" -AsSecureString
                        $appConfigValue = [System.Runtime.InteropServices.marshal]::PtrToStringAuto([System.Runtime.InteropServices.marshal]::SecureStringToBSTR($appConfigValue))

                        foreach ($setting in $appConfig) {
                            if($setting.Name -eq $credentialInfo.appConfigName){
                                $hash[$setting.Name] = @{Type=$setting.Type.ToString(); Value=$appConfigValue}
                            }
                            else{
                                $hash[$setting.Name] = @{Type=$setting.Type.ToString(); Value=$setting.ConnectionString}
                            }
                        }
                    }

                    if($credentialInfo.appConfigType -eq "Application Settings"){
                        Set-AzWebApp -Name $credentialInfo.resourceName -ResourceGroupName $resource.ResourceGroup -AppSettings $hash | Out-Null
                        $this.PublishCustomMessage("Successfully updated the application setting [$($credentialInfo.appConfigName)] in the app service [$($credentialInfo.resourceName)]", [MessageType]::Update)
                    }
                    elseif($credentialInfo.appConfigType -eq "Connection Strings"){
                        Set-AzWebApp -Name $credentialInfo.resourceName -ResourceGroupName $resource.ResourceGroup -ConnectionStrings $hash | Out-Null
                        $this.PublishCustomMessage("Successfully updated connection string [$($credentialInfo.appConfigName)] in the app service [$($credentialInfo.resourceName)]", [MessageType]::Update)
                    }
                    
                }            

                if($credentialInfo.credLocation -eq "KeyVault")
                {
                    if($credentialInfo.kvCredType -eq "Key")
                    {
                        $currentKey = Get-AzKeyVaultKey -VaultName $credentialInfo.kvName -Name $credentialInfo.kvCredName -ErrorAction SilentlyContinue
                        if(($currentKey | Measure-Object).Count -ne 0)
                        {
                            $expiryTime = $currentTime.AddDays($credentialInfo.rotationInt)

                            $this.PublishCustomMessage("Updating the key [$($credentialInfo.kvCredName)] in the key vault [$($credentialInfo.kvName)]", [MessageType]::Default)
                            $newKey = Add-AzKeyVaultKey -VaultName $credentialInfo.kvName -Name $credentialInfo.kvCredName -Expires $ExpiryTime -Destination Software
                            
                            $credentialInfo.expiryTime = $newKey.Expires
                            $credentialInfo.version = $newKey.Version

                            $this.PublishCustomMessage("Successfully updated the key [$($credentialInfo.kvCredName)] in the key vault [$($credentialInfo.kvName)]", [MessageType]::Update)
                        }
                    }
                    elseif($credentialInfo.kvCredType -eq "Secret")
                    {
                        $currentSecret = Get-AzKeyVaultSecret -VaultName $credentialInfo.kvName -Name $credentialInfo.kvCredName -ErrorAction SilentlyContinue
                        if(($currentSecret | Measure-Object).Count -ne 0)
                        {
                            $expiryTime = $currentTime.AddDays($credentialInfo.rotationInt)

                            $this.PublishCustomMessage("Updating the secret [$($credentialInfo.kvCredName)] in the key vault [$($credentialInfo.kvName)]", [MessageType]::Default)
                            $secret = Read-Host "Enter the new secret value for the key vault credential - [$($credentialInfo.kvCredName)]" -AsSecureString 
                        
                            $newSecret = Set-AzKeyVaultSecret -VaultName $credentialInfo.kvName -Name $credentialInfo.kvCredName -SecretValue $secret -Expires $ExpiryTime
                            $credentialInfo.expiryTime = $newSecret.Expires
                            $credentialInfo.version = $newSecret.Version

                            $this.PublishCustomMessage("Successfully updated the secret [$($credentialInfo.kvCredName)] in the key vault [$($credentialInfo.kvName)]", [MessageType]::Update)
                        }
                    }
                    $this.PublishCustomMessage("To avoid impacting availability, the previous version of this $($credentialInfo.kvCredType) has been kept in enabled state.", [MessageType]::Warning)
                    $this.PublishCustomMessage("If you are using key/secret URLs with specific version identifier in them, please update to the new version before disabling it.")
                }

                $credentialInfo.lastUpdatedOn = $currentTime
                $credentialInfo.lastUpdatedBy = $user
            }
            else{
                if($ResetLastUpdate){
                    $credentialInfo.lastUpdatedOn = $currentTime
                    $credentialInfo.lastUpdatedBy = $user
                }
                if([Helpers]::CheckMember($credentialInfo,'expiryTime')){
                    if($credentialInfo.expiryTime){
                        $credentialInfo.lastUpdatedOn = ($credentialInfo.expiryTime).AddDays(-$credentialInfo.rotationInt)
                    }
                }
            }

            $credentialInfo.comment = $Comment
            $credentialInfo | ConvertTo-Json -Depth 10 | Out-File $file -Force
            Set-AzStorageBlobContent -Blob $blobName -Container $this.RotationMetadataContainerName -Context $this.AzSKStorageAccount.Context -File $file -Force | Out-Null
            $this.PublishCustomMessage("Successfully updated settings for AzSK tracked credential [$CredentialName]", [MessageType]::Update) 
            $this.PrintInfo($credentialInfo);
        }
        else{
            $this.PublishCustomMessage("Could not find an entry for credential [$CredentialName].", [MessageType]::Critical)
        }
        if(Test-Path $file)
        {
            Remove-Item -Path $file
        }
    }

    [void] InstallAlert($AlertEmail)
    {
        $rgName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName
        $credHygieneAGName = [Constants]::CredHygieneActionGroupName
        $credHygieneAGShortName = [Constants]::CredHygieneActionGroupShortName
        
        $email = New-AzActionGroupReceiver -Name $AlertEmail -EmailReceiver -EmailAddress $AlertEmail
        $actionGroup = Set-AzActionGroup -Name $credHygieneAGName -ResourceGroupName $rgName -ShortName $credHygieneAGShortName -Receiver $email -ErrorAction Ignore -WarningAction SilentlyContinue
        
        if($actionGroup){
            # We are using LAWS from the same sub for Alert REST API call.
            $automationAccDetails= Get-AzAutomationAccount -ResourceGroupName $rgName -ErrorAction SilentlyContinue 
            if($automationAccDetails)
            {
                #Fetch LAWS Id from CA variables
                $laWSId = Get-AzAutomationVariable -ResourceGroupName $automationAccDetails.ResourceGroupName -AutomationAccountName $automationAccDetails.AutomationAccountName -Name "LAWSId" -ErrorAction SilentlyContinue

                if($laWSId){
                    $laWS = Get-AzOperationalInsightsWorkspace | where{$_.CustomerId -eq $laWSId.Value} # Verify whether the LA resource ith the WS id exists.
                    if($laWS){
                        $body = [ConfigurationManager]::LoadServerConfigFile("CredentialHygieneAlert.json");
                        $dataSourceId = $body.properties.source.dataSourceId | ConvertTo-Json -Depth 10
                        $dataSourceId = $dataSourceId.Replace("{0}",$this.SubscriptionContext.SubscriptionId).Replace("{1}",$laWS.ResourceGroupName).Replace("{2}",$laWS.CustomerId) | ConvertFrom-Json
                        $body.properties.source.dataSourceId = $dataSourceId

                        $ag = $body.properties.action.aznsAction.actionGroup[0] | ConvertTo-Json -Depth 10
                        $ag = $ag.Replace("{3}",$actionGroup.Id) | ConvertFrom-Json
                        $body.properties.action.aznsAction.actionGroup[0] = $ag
                            
                        $ResourceAppIdURI = [WebRequestHelper]::GetResourceManagerUrl()    
                        $uri = $ResourceAppIdURI + "subscriptions/$($this.SubscriptionContext.SubscriptionId)/resourcegroups/$($laWS.ResourceGroupName)/providers/microsoft.insights/scheduledQueryRules/AzSK_CredHygiene_Alert?api-version=2018-04-16"
                                
                        [WebRequestHelper]::InvokeWebRequest([Microsoft.PowerShell.Commands.WebRequestMethod]::Put, $uri, $body);
                        $this.PublishCustomMessage("Alert for the credential group [$credHygieneAGName] is successfully configured.");
                    }
                    else{ # LA resource not found.
                        $this.PublishCustomMessage("Log Analytics resource with workspace Id [$($laWSId.Value)] provided in the CA automation account variables doesn't exist. Please verify the value of Log Analytics workspace Id.", [MessageType]::Error)
                        $this.PublishCustomMessage("Couldn't create credential hygiene alert for the current subscription.", [MessageType]::Error)
                        $this.PublishCustomMessage("Run Update-AzSKContinuousAssurance to update the Log Analytics workspace Id with the correct value in the CA automation account.")
                    }
                }
                else{ # LAWS id variable not found.
                    $this.PublishCustomMessage("Log Analytics workspace Id not found in the CA automation account variables.", [MessageType]::Error)
                    $this.PublishCustomMessage("Couldn't create credential hygiene alert for the current subscription.", [MessageType]::Error)
                    $this.PublishCustomMessage("Run Update-AzSKContinuousAssurance to update the Log Analytics workspace Id with the correct value in the CA automation account.")
                }
            }
            else{ # CA setup not found.
                $this.PublishCustomMessage("Continuous Assurance setup was not found in the current subscription.", [MessageType]::Error)
                $this.PublishCustomMessage("Couldn't create credential hygiene alert for the current subscription.", [MessageType]::Error)
            }
        }
        else{ # Action group not found/created.
            $this.PublishCustomMessage("Couldn't create action group [$credHygieneAGName] for credential hygiene alerts.", [MessageType]::Error)
            $this.PublishCustomMessage("Couldn't create credential hygiene alert for the current subscription.", [MessageType]::Error)
        }
    }     

    [void] InstallCredentialGroupAlert($actionGroup)
    {    
        $rgName = [ConfigurationManager]::GetAzSKConfigData().AzSKRGName

        # We are using LAWS from the same sub for Alert REST API call.
        $automationAccDetails= Get-AzAutomationAccount -ResourceGroupName $rgName -ErrorAction SilentlyContinue 
        if($automationAccDetails)
        {
            #Fetch LAWS Id from CA variables
            $laWSId = Get-AzAutomationVariable -ResourceGroupName $automationAccDetails.ResourceGroupName -AutomationAccountName $automationAccDetails.AutomationAccountName -Name "LAWSId" -ErrorAction SilentlyContinue
            if($laWSId){
                $laWS = Get-AzOperationalInsightsWorkspace | where{$_.CustomerId -eq $laWSId.Value} # Verify whether the LA resource ith the WS id exists.
                if($laWS){
                    
                    $body = [ConfigurationManager]::LoadServerConfigFile("CredentialHygieneAlert_CredentialGroup.json");
                    $dataSourceId = $body.properties.source.dataSourceId | ConvertTo-Json -Depth 10
                    $dataSourceId = $dataSourceId.Replace("{0}",$this.SubscriptionContext.SubscriptionId).Replace("{1}",$laWS.ResourceGroupName).Replace("{2}",$laWS.CustomerId) | ConvertFrom-Json
                    $body.properties.source.dataSourceId = $dataSourceId
                    
                    $ag = $body.properties.action.aznsAction.actionGroup[0] | ConvertTo-Json -Depth 10
                    $ag = $ag.Replace("{3}",$actionGroup.Id) | ConvertFrom-Json
                    $body.properties.action.aznsAction.actionGroup[0] = $ag
                    
                    $cg = $body.properties.source.query | ConvertTo-Json -Depth 10
                    $cg = $cg.Replace("{4}",$actionGroup.Name) | ConvertFrom-Json
                    $body.properties.source.query = $cg
                        
                    $ResourceAppIdURI = [WebRequestHelper]::GetResourceManagerUrl()    
                    $uri = $ResourceAppIdURI + "subscriptions/$($this.SubscriptionContext.SubscriptionId)/resourcegroups/$($laWS.ResourceGroupName)/providers/microsoft.insights/scheduledQueryRules/AzSK_CredHygiene_Alert_$($actionGroup.GroupShortName)?api-version=2018-04-16"
                                
                    [WebRequestHelper]::InvokeWebRequest([Microsoft.PowerShell.Commands.WebRequestMethod]::Put, $uri, $body);
                    $this.PublishCustomMessage("Alert for the credential group [$($actionGroup.Name)] is successfully configured.");
                }
                else{ # LA resource not found.
                    $this.PublishCustomMessage("Log Analytics resource with workspace Id [$($laWSId.Value)] provided in the CA automation account variables doesn't exist. Please verify the value of Log Analytics workspace Id.", [MessageType]::Error)
                    $this.PublishCustomMessage("Couldn't create credential hygiene alert for the current subscription.", [MessageType]::Error)
                    $this.PublishCustomMessage("Run Update-AzSKContinuousAssurance to update the Log Analytics workspace Id with the correct value in the CA automation account.")
                }
            }
            else{ # LAWS id variable not found.
                $this.PublishCustomMessage("Log Analytics workspace Id not found in the CA automation account variables.", [MessageType]::Error)
                $this.PublishCustomMessage("Couldn't create credential hygiene alert for the current subscription.", [MessageType]::Error)
                $this.PublishCustomMessage("Run Update-AzSKContinuousAssurance to update the Log Analytics workspace Id with the correct value in the CA automation account.")
            }
        }
        else{ # CA setup not found.
            $this.PublishCustomMessage("Continuous Assurance setup was not found in the current subscription.", [MessageType]::Error)
            $this.PublishCustomMessage("Couldn't create credential hygiene alert for the current subscription.", [MessageType]::Error)
        }    
    }     
}