PageRenderTime 50ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/src/Storage/Commands.Storage/Common/AccessPolicyHelper.cs

https://gitlab.com/jslee1/azure-powershell
C# | 190 lines | 144 code | 16 blank | 30 comment | 55 complexity | 89913d394c20e71830611eeed09e8f71 MD5 | raw file
  1. // ----------------------------------------------------------------------------------
  2. //
  3. // Copyright Microsoft Corporation
  4. // Licensed under the Apache License, Version 2.0 (the "License");
  5. // you may not use this file except in compliance with the License.
  6. // You may obtain a copy of the License at
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. // Unless required by applicable law or agreed to in writing, software
  9. // distributed under the License is distributed on an "AS IS" BASIS,
  10. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  11. // See the License for the specific language governing permissions and
  12. // limitations under the License.
  13. // ----------------------------------------------------------------------------------
  14. namespace Microsoft.WindowsAzure.Commands.Storage.Common
  15. {
  16. using Microsoft.WindowsAzure.Commands.Utilities.Common;
  17. using Microsoft.WindowsAzure.Storage.Blob;
  18. using Microsoft.WindowsAzure.Storage.File;
  19. using Microsoft.WindowsAzure.Storage.Queue;
  20. using Microsoft.WindowsAzure.Storage.Table;
  21. using System;
  22. using System.Collections.Generic;
  23. using System.Globalization;
  24. using System.Management.Automation;
  25. internal class AccessPolicyHelper
  26. {
  27. /// <summary>
  28. /// Set the shared access policy
  29. /// </summary>
  30. /// <typeparam name="T">SharedAccessTablePolicy, SharedAccessBlobPolicy or SharedAccessQueuePolicy</typeparam>
  31. /// <param name="policy">the policy object</param>
  32. /// <param name="startTime">start time of the policy</param>
  33. /// <param name="expiryTime">end time of the policy</param>
  34. /// <param name="permission">the permission of the policy</param>
  35. internal static void SetupAccessPolicy<T>(T policy, DateTime? startTime, DateTime? expiryTime, string permission, bool noStartTime = false, bool noExpiryTime = false)
  36. {
  37. if (!(typeof(T) == typeof(SharedAccessTablePolicy) ||
  38. typeof(T) == typeof(SharedAccessFilePolicy) ||
  39. typeof(T) == typeof(SharedAccessBlobPolicy) ||
  40. (typeof(T) == typeof(SharedAccessQueuePolicy))))
  41. {
  42. throw new ArgumentException(Resources.InvalidAccessPolicyType);
  43. }
  44. if (noStartTime && startTime != null)
  45. {
  46. throw new ArgumentException(Resources.StartTimeParameterConflict);
  47. }
  48. if (noExpiryTime && expiryTime != null)
  49. {
  50. throw new ArgumentException(Resources.ExpiryTimeParameterConflict);
  51. }
  52. DateTimeOffset? accessStartTime;
  53. DateTimeOffset? accessExpiryTime;
  54. SetupAccessPolicyLifeTime(startTime, expiryTime,
  55. out accessStartTime, out accessExpiryTime);
  56. if (startTime != null || noStartTime)
  57. {
  58. policy.GetType().GetProperty("SharedAccessStartTime").SetValue(policy, accessStartTime);
  59. }
  60. if (expiryTime != null || noExpiryTime)
  61. {
  62. policy.GetType().GetProperty("SharedAccessExpiryTime").SetValue(policy, accessExpiryTime);
  63. }
  64. SetupAccessPolicyPermission<T>(policy, permission);
  65. }
  66. /// <summary>
  67. /// Set up the shared access policy lift time
  68. /// </summary>
  69. /// <param name="startTime">start time of the shared access policy</param>
  70. /// <param name="expiryTime">end time of the shared access policy</param>
  71. /// <param name="SharedAccessStartTime">the converted value of start time</param>
  72. /// <param name="SharedAccessExpiryTime">the converted value of end time</param>
  73. internal static void SetupAccessPolicyLifeTime(DateTime? startTime, DateTime? expiryTime,
  74. out DateTimeOffset? SharedAccessStartTime, out DateTimeOffset? SharedAccessExpiryTime)
  75. {
  76. SharedAccessStartTime = null;
  77. SharedAccessExpiryTime = null;
  78. if (startTime != null)
  79. {
  80. SharedAccessStartTime = startTime.Value.ToUniversalTime();
  81. }
  82. if (expiryTime != null)
  83. {
  84. SharedAccessExpiryTime = expiryTime.Value.ToUniversalTime();
  85. }
  86. if (SharedAccessStartTime != null && SharedAccessExpiryTime.HasValue
  87. && SharedAccessExpiryTime <= SharedAccessStartTime)
  88. {
  89. throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.ExpiryTimeGreatThanStartTime,
  90. SharedAccessExpiryTime.ToString(), SharedAccessStartTime.ToString()));
  91. }
  92. }
  93. internal static void SetupAccessPolicyPermission<T>(T policy, string permission)
  94. {
  95. //set permission as none if passed-in value is empty
  96. if (permission == null)
  97. return;
  98. if (string.IsNullOrEmpty(permission))
  99. {
  100. if (typeof(T) == typeof(SharedAccessTablePolicy))
  101. {
  102. ((SharedAccessTablePolicy)(Object)policy).Permissions = SharedAccessTablePermissions.None;
  103. }
  104. else if (typeof(T) == typeof(SharedAccessFilePolicy))
  105. {
  106. ((SharedAccessFilePolicy)(Object)policy).Permissions = SharedAccessFilePermissions.None;
  107. }
  108. else if (typeof(T) == typeof(SharedAccessBlobPolicy))
  109. {
  110. ((SharedAccessBlobPolicy)(Object)policy).Permissions = SharedAccessBlobPermissions.None;
  111. }
  112. else if ((typeof(T) == typeof(SharedAccessQueuePolicy)))
  113. {
  114. ((SharedAccessQueuePolicy)(Object)policy).Permissions = SharedAccessQueuePermissions.None;
  115. }
  116. else
  117. {
  118. throw new ArgumentException(Resources.InvalidAccessPolicyType);
  119. }
  120. return;
  121. }
  122. permission = permission.ToLower(CultureInfo.InvariantCulture);
  123. try
  124. {
  125. if (typeof(T) == typeof(SharedAccessTablePolicy))
  126. {
  127. //PowerShell will convert q to r in genreate table SAS. Add this to avoid regression
  128. string convertedPermission = permission.Replace('q', 'r');
  129. ((SharedAccessTablePolicy)(Object)policy).Permissions = SharedAccessTablePolicy.PermissionsFromString(convertedPermission);
  130. }
  131. else if (typeof(T) == typeof(SharedAccessFilePolicy))
  132. {
  133. ((SharedAccessFilePolicy)(Object)policy).Permissions = SharedAccessFilePolicy.PermissionsFromString(permission);
  134. }
  135. else if (typeof(T) == typeof(SharedAccessBlobPolicy))
  136. {
  137. ((SharedAccessBlobPolicy)(Object)policy).Permissions = SharedAccessBlobPolicy.PermissionsFromString(permission);
  138. }
  139. else if ((typeof(T) == typeof(SharedAccessQueuePolicy)))
  140. {
  141. ((SharedAccessQueuePolicy)(Object)policy).Permissions = SharedAccessQueuePolicy.PermissionsFromString(permission);
  142. }
  143. else
  144. {
  145. throw new ArgumentException(Resources.InvalidAccessPolicyType);
  146. }
  147. }
  148. catch (System.ArgumentOutOfRangeException)
  149. {
  150. throw new ArgumentException(string.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPermission, permission));
  151. }
  152. }
  153. internal static PSObject ConstructPolicyOutputPSObject<T>(IDictionary<string, T> sharedAccessPolicies, string policyName)
  154. {
  155. if (!(typeof(T) == typeof(SharedAccessTablePolicy) ||
  156. typeof(T) == typeof(SharedAccessBlobPolicy) ||
  157. (typeof(T) == typeof(SharedAccessQueuePolicy)) ||
  158. (typeof(T) == typeof(SharedAccessFilePolicy))))
  159. {
  160. throw new ArgumentException(Resources.InvalidAccessPolicyType);
  161. }
  162. return PowerShellUtilities.ConstructPSObject(
  163. typeof(PSObject).FullName,
  164. "Policy",
  165. policyName,
  166. "Permissions",
  167. (sharedAccessPolicies[policyName]).GetType().GetProperty("Permissions").GetValue(sharedAccessPolicies[policyName]),
  168. "StartTime",
  169. (sharedAccessPolicies[policyName]).GetType().GetProperty("SharedAccessStartTime").GetValue(sharedAccessPolicies[policyName]),
  170. "ExpiryTime",
  171. (sharedAccessPolicies[policyName]).GetType().GetProperty("SharedAccessExpiryTime").GetValue(sharedAccessPolicies[policyName]));
  172. }
  173. }
  174. }