1
- using Microsoft . Azure . Commands . KeyVault . Models ;
1
+ using Microsoft . Azure . Commands . Common . Exceptions ;
2
+ using Microsoft . Azure . Commands . KeyVault . Models ;
2
3
using Microsoft . Azure . Commands . ResourceManager . Common . ArgumentCompleters ;
4
+ using Microsoft . WindowsAzure . Commands . Utilities . Common ;
5
+
6
+ using Newtonsoft . Json ;
3
7
4
8
using System ;
5
9
using System . Management . Automation ;
10
+ using Track2Sdk = Azure . Security . KeyVault . Keys ;
11
+ using System . IO ;
12
+ using Microsoft . WindowsAzure . Commands . Common ;
13
+ using Microsoft . Azure . Commands . KeyVault . Properties ;
14
+ using System . Collections . Generic ;
6
15
7
16
namespace Microsoft . Azure . Commands . KeyVault . Commands . Key
8
17
{
9
18
/// <summary>
10
19
/// Updates the KeyRotationPolicy for the specified key in Key Vault.
11
20
/// </summary>
12
- [ Cmdlet ( VerbsCommon . Set , ResourceManager . Common . AzureRMConstants . AzurePrefix + "KeyVaultKeyRotationPolicy" , SupportsShouldProcess = true , DefaultParameterSetName = ByVaultNameParameterSet ) ]
21
+ [ Cmdlet ( VerbsCommon . Set , ResourceManager . Common . AzureRMConstants . AzurePrefix + "KeyVaultKeyRotationPolicy" , SupportsShouldProcess = true , DefaultParameterSetName = SetByExpandedPropertiesViaVaultName ) ]
13
22
[ OutputType ( typeof ( PSKeyRotationPolicy ) ) ]
14
- public class SetAzKeyVaultKeyRotationPolicy : KeyVaultOnlyKeyCmdletBase
23
+ public class SetAzKeyVaultKeyRotationPolicy : KeyVaultCmdletBase
15
24
{
16
25
#region Parameter Set Names
17
26
18
- internal const string ByKeyRotationPolicyInputObjectParameterSet = "ByKeyRotationPolicyInputObject" ;
27
+ private const string SetByExpandedPropertiesViaVaultName = "ByVaultName" ;
28
+ private const string SetByRotationPolicyFileViaVaultName = "SetByRotationPolicyFileViaVaultName" ;
29
+
30
+ private const string SetByExpandedPropertiesViaKeyInputObject = "ByKeyInputObject" ;
31
+ private const string SetByRotationPolicyFileViaKeyInputObject = "SetByRotationPolicyFileViaKeyInputObject" ;
32
+
33
+ private const string ByKeyRotationPolicyInputObjectParameterSet = "ByKeyRotationPolicyInputObject" ;
19
34
20
35
#endregion
21
36
22
37
#region Input Parameter Definitions
23
38
24
- [ Parameter ( Mandatory = true ,
25
- Position = 0 ,
26
- ParameterSetName = ByKeyRotationPolicyInputObjectParameterSet ,
27
- ValueFromPipeline = true ,
28
- HelpMessage = "PSKeyRotationPolicy object." ) ]
39
+ /// <summary>
40
+ /// Vault name
41
+ /// </summary>
42
+ [ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = SetByExpandedPropertiesViaVaultName , HelpMessage = "Vault name." ) ]
43
+ [ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = SetByRotationPolicyFileViaVaultName ) ]
44
+ [ ResourceNameCompleter ( "Microsoft.KeyVault/vaults" , "FakeResourceGroupName" ) ]
45
+ [ ValidateNotNullOrEmpty ]
46
+ public string VaultName { get ; set ; }
47
+
48
+ /// <summary>
49
+ /// Key name.
50
+ /// </summary>
51
+ [ Parameter ( Mandatory = true , Position = 1 , ParameterSetName = SetByExpandedPropertiesViaVaultName , HelpMessage = "Key name." ) ]
52
+ [ Parameter ( Mandatory = true , Position = 1 , ParameterSetName = SetByRotationPolicyFileViaVaultName ) ]
53
+ [ ValidateNotNullOrEmpty ]
54
+ [ Alias ( Constants . KeyName ) ]
55
+ public string Name { get ; set ; }
56
+
57
+ /// <summary>
58
+ /// Key object
59
+ /// </summary>
60
+ [ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = SetByExpandedPropertiesViaKeyInputObject ,
61
+ ValueFromPipeline = true , HelpMessage = "Key object" ) ]
62
+ [ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = SetByRotationPolicyFileViaKeyInputObject ,
63
+ ValueFromPipeline = true ) ]
64
+ [ ValidateNotNullOrEmpty ]
65
+ [ Alias ( "Key" ) ]
66
+ public PSKeyVaultKeyIdentityItem InputObject { get ; set ; }
67
+
68
+ [ Parameter ( Mandatory = true , Position = 0 , ParameterSetName = ByKeyRotationPolicyInputObjectParameterSet ,
69
+ ValueFromPipeline = true , HelpMessage = "PSKeyRotationPolicy object." ) ]
29
70
public PSKeyRotationPolicy KeyRotationPolicy { get ; set ; }
30
71
31
- [ Parameter ( ParameterSetName = ByVaultNameParameterSet ,
32
- HelpMessage = "The time span when the key rotation policy will expire . It should be at least 28 days." ) ]
33
- [ Parameter ( ParameterSetName = ByKeyInputObjectParameterSet ) ]
34
- public TimeSpan ExpiresIn { get ; set ; }
72
+ [ Parameter ( ParameterSetName = SetByExpandedPropertiesViaVaultName ,
73
+ HelpMessage = "The expiryTime will be applied on the new key version . It should be at least 28 days. It will be in ISO 8601 Format. Examples: 90 days: P90D, 3 months: P3M, 48 hours: PT48H, 1 year and 10 days: P1Y10D " ) ]
74
+ [ Parameter ( ParameterSetName = SetByExpandedPropertiesViaKeyInputObject ) ]
75
+ public string ExpiresIn { get ; set ; }
35
76
36
77
37
- [ Parameter ( ParameterSetName = ByVaultNameParameterSet ,
78
+ [ Parameter ( ParameterSetName = SetByExpandedPropertiesViaVaultName ,
38
79
HelpMessage = "PSKeyRotationLifetimeAction object." ) ]
39
- [ Parameter ( ParameterSetName = ByKeyInputObjectParameterSet ) ]
80
+ [ Parameter ( ParameterSetName = SetByExpandedPropertiesViaKeyInputObject ) ]
40
81
public PSKeyRotationLifetimeAction [ ] KeyRotationLifetimeAction { get ; set ; }
41
82
83
+ [ Parameter ( Mandatory = true , ParameterSetName = SetByRotationPolicyFileViaVaultName ,
84
+ HelpMessage = "A path to the rotation policy file that contains JSON policy definition." ) ]
85
+ [ Parameter ( Mandatory = true , ParameterSetName = SetByRotationPolicyFileViaKeyInputObject ) ]
86
+ public string PolicyPath { get ; set ; }
42
87
#endregion
43
88
44
- internal override void NormalizeParameterSets ( )
89
+ protected override void BeginProcessing ( )
90
+ {
91
+ PolicyPath = this . TryResolvePath ( PolicyPath ) ;
92
+ base . BeginProcessing ( ) ;
93
+ }
94
+
95
+ internal void ValidateParameters ( )
96
+ {
97
+ if ( ( this . ParameterSetName . Equals ( SetByExpandedPropertiesViaVaultName ) ||
98
+ this . ParameterSetName . Equals ( SetByExpandedPropertiesViaKeyInputObject ) ) &&
99
+ null == ExpiresIn && null == KeyRotationLifetimeAction )
100
+ {
101
+ throw new ArgumentException ( string . Format ( "Must specify ExpiresIn or KeyRotationLifetimeAction." ) ) ;
102
+ }
103
+
104
+ if ( this . IsParameterBound ( c => c . PolicyPath ) && ! File . Exists ( PolicyPath ) )
105
+ {
106
+ throw new AzPSFileNotFoundException ( string . Format ( Resources . FileNotFound , PolicyPath ) , nameof ( PolicyPath ) ) ;
107
+ }
108
+ }
109
+
110
+ internal void NormalizeParameterSets ( )
45
111
{
46
112
if ( null != InputObject )
47
113
{
@@ -57,33 +123,84 @@ internal override void NormalizeParameterSets()
57
123
}
58
124
}
59
125
60
- if ( ! this . ParameterSetName . Equals ( ByKeyRotationPolicyInputObjectParameterSet ) )
126
+ switch ( this . ParameterSetName )
61
127
{
62
-
63
- // Only update specified parameter, others keep same
64
- KeyRotationPolicy = Track2DataClient . GetKeyRotationPolicy ( VaultName , Name ) ??
65
- new PSKeyRotationPolicy ( )
66
- {
128
+ case SetByRotationPolicyFileViaVaultName :
129
+ case SetByRotationPolicyFileViaKeyInputObject :
130
+ KeyRotationPolicy = ConstructKeyRotationPolicyFromFile ( PolicyPath ) ;
131
+ break ;
132
+ case SetByExpandedPropertiesViaVaultName :
133
+ case SetByExpandedPropertiesViaKeyInputObject :
134
+ KeyRotationPolicy = new PSKeyRotationPolicy ( )
135
+ {
67
136
VaultName = VaultName ,
68
137
KeyName = Name ,
69
- ExpiresIn = null ,
70
- LifetimeActions = null
138
+ ExpiresIn = ExpiresIn ?? Track2DataClient . GetKeyRotationPolicy ( VaultName , Name ) . ExpiresIn ,
139
+ LifetimeActions = KeyRotationLifetimeAction ?? Track2DataClient . GetKeyRotationPolicy ( VaultName , Name ) . LifetimeActions
71
140
} ;
141
+ break ;
142
+ default :
143
+ // do nothing
144
+ break ;
145
+ }
72
146
73
- if ( MyInvocation . BoundParameters . ContainsKey ( "ExpiresIn" ) )
74
- {
75
- KeyRotationPolicy . ExpiresIn = ExpiresIn ;
76
- }
147
+ }
148
+
149
+ private PSKeyRotationPolicy ConstructKeyRotationPolicyFromFile ( string policyPath )
150
+ {
151
+ try
152
+ {
153
+ string content = File . ReadAllText ( policyPath ) ;
154
+ // first-level dictionary
155
+ var dict = JsonUtilities . DeserializeJson ( content , true ) ;
77
156
78
- if ( MyInvocation . BoundParameters . ContainsKey ( "KeyRotationLifetimeAction" ) )
157
+ // second-level dictionary
158
+ var attributes = JsonUtilities . DeserializeJson ( JsonConvert . SerializeObject ( dict [ "attributes" ] ) , true ) ;
159
+ var lifetimeActionsArray = JsonConvert . DeserializeObject < object [ ] > ( JsonConvert . SerializeObject ( dict [ "lifetimeActions" ] ) ) ;
160
+
161
+ // third-level dictionary
162
+ string expiresIn = attributes [ "expiryTime" ] . ToString ( ) ;
163
+
164
+ var lifetimeActions = new List < PSKeyRotationLifetimeAction > ( ) ;
165
+ lifetimeActionsArray ? . ForEach ( ( lifetimeAction ) =>
79
166
{
80
- KeyRotationPolicy . LifetimeActions = KeyRotationLifetimeAction ;
81
- }
167
+ var lifetimeActionDict = JsonUtilities . DeserializeJson ( JsonConvert . SerializeObject ( lifetimeAction ) , true ) ;
168
+ var action = JsonUtilities . DeserializeJson ( JsonConvert . SerializeObject ( lifetimeActionDict [ "action" ] ) , true ) ;
169
+ var trigger = JsonUtilities . DeserializeJson ( JsonConvert . SerializeObject ( lifetimeActionDict [ "trigger" ] ) , true ) ;
170
+
171
+ // 4th-level dictionary
172
+ string actionType = action [ "type" ] . ToString ( ) ;
173
+
174
+ // timeAfterCreate or timeBeforeExpiry may absent
175
+ string timeAfterCreate = trigger . ContainsKey ( "timeAfterCreate" ) ? trigger [ "timeAfterCreate" ] ? . ToString ( ) : null ;
176
+ string timeBeforeExpiry = trigger . ContainsKey ( "timeBeforeExpiry" ) ? trigger [ "timeBeforeExpiry" ] ? . ToString ( ) : null ;
177
+
178
+ lifetimeActions . Add ( new PSKeyRotationLifetimeAction ( )
179
+ {
180
+ Action = actionType ,
181
+ TimeAfterCreate = timeAfterCreate ,
182
+ TimeBeforeExpiry = timeBeforeExpiry
183
+ } ) ;
184
+ } ) ;
185
+
186
+ return new PSKeyRotationPolicy ( )
187
+ {
188
+ VaultName = this . VaultName ,
189
+ KeyName = this . Name ,
190
+ ExpiresIn = expiresIn ,
191
+ LifetimeActions = lifetimeActions
192
+ } ;
82
193
}
194
+ catch
195
+ {
196
+ throw new AzPSArgumentException ( string . Format ( "Deserialize {0} failed" , policyPath ) , nameof ( PolicyPath ) ) ;
197
+ }
198
+
83
199
}
84
200
85
201
public override void ExecuteCmdlet ( )
86
202
{
203
+ ValidateParameters ( ) ;
87
204
NormalizeParameterSets ( ) ;
88
205
89
206
ConfirmAction ( KeyRotationPolicy . KeyName , Properties . Resources . SetKeyRotationPolicy , ( ) =>
0 commit comments