Skip to content

Commit 7a2ccb0

Browse files
committed
Merge branch 'accountsas' of https://github.com/wastoresh/azure-powershell-pr into dev
2 parents a51d518 + 2ee5fa5 commit 7a2ccb0

File tree

7 files changed

+195
-0
lines changed

7 files changed

+195
-0
lines changed

src/Common/Storage/Commands.Storage.Test/Service/MockStorageBlobManagement.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -685,6 +685,16 @@ public Task<string> StartCopyAsync(CloudBlob blob, Uri source, AccessCondition s
685685
throw new NotImplementedException();
686686
}
687687

688+
/// <summary>
689+
/// Get the SAS token for an account.
690+
/// </summary>
691+
/// <param name="sharedAccessAccountPolicy">Shared access policy to generate the SAS token.</param>
692+
/// <returns>Account SAS token.</returns>
693+
public string GetStorageAccountSASToken(SharedAccessAccountPolicy sharedAccessAccountPolicy)
694+
{
695+
throw new NotImplementedException();
696+
}
697+
688698
/// <summary>
689699
/// The storage context
690700
/// </summary>

src/Common/Storage/Commands.Storage/Commands.Storage.csproj

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -180,6 +180,7 @@
180180
<Compile Include="Common\BlobToFileSystemNameResolver.cs" />
181181
<Compile Include="Blob\Cmdlet\StartAzureStorageBlobCopy.cs" />
182182
<Compile Include="Blob\Cmdlet\StopAzureStorageBlobCopy.cs" />
183+
<Compile Include="Common\Cmdlet\NewAzureStorageAccountSasToken.cs" />
183184
<Compile Include="Common\Cmdlet\SetAzureStorageCORSRule.cs" />
184185
<Compile Include="Common\Cmdlet\GetAzureStorageCORSRule.cs" />
185186
<Compile Include="Common\Cmdlet\GetAzureStorageServiceLogging.cs" />
Lines changed: 154 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,154 @@
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+
15+
namespace Microsoft.WindowsAzure.Commands.Storage.Common.Cmdlet
16+
{
17+
using System;
18+
using System.Management.Automation;
19+
using System.Security.Permissions;
20+
using Microsoft.WindowsAzure.Commands.Storage.Model.Contract;
21+
using Microsoft.WindowsAzure.Storage;
22+
23+
[Cmdlet(VerbsCommon.New, StorageNouns.AccountSas), OutputType(typeof(String))]
24+
public class NewAzureStorageAccountSasTokenCommand : StorageCloudBlobCmdletBase
25+
{
26+
[Parameter(Mandatory = true, HelpMessage = "Service type that this SAS token applies to.")]
27+
public SharedAccessAccountServices Service { get; set; }
28+
29+
[Parameter(Mandatory = true, HelpMessage = "Resource type that this SAS token applies to.")]
30+
public SharedAccessAccountResourceTypes ResourceType { get; set; }
31+
32+
[Parameter(Mandatory = true, HelpMessage = "Permissions.")]
33+
public string Permission { get; set; }
34+
35+
[Parameter(Mandatory = false, HelpMessage = "Protocol can be used in the request with this SAS token.")]
36+
public SharedAccessProtocol Protocol { get; set; }
37+
38+
[Parameter(Mandatory = false, HelpMessage = "IP, or IP range ACL (access control list) that the request would be accepted from by Azure Storage.")]
39+
public string IPAddressOrRange { get; set; }
40+
41+
[Parameter(Mandatory = false, HelpMessage = "Start Time")]
42+
public DateTime? StartTime { get; set; }
43+
44+
[Parameter(Mandatory = false, HelpMessage = "Expiry Time")]
45+
public DateTime? ExpiryTime { get; set; }
46+
47+
/// <summary>
48+
/// Initializes a new instance of the NewAzureStorageAccountSasTokenCommand class.
49+
/// </summary>
50+
public NewAzureStorageAccountSasTokenCommand()
51+
: this(null)
52+
{
53+
}
54+
55+
/// <summary>
56+
/// Initializes a new instance of the NewAzureStorageAccountSasTokenCommand class.
57+
/// </summary>
58+
/// <param name="channel">IStorageBlobManagement channel</param>
59+
public NewAzureStorageAccountSasTokenCommand(IStorageBlobManagement channel)
60+
{
61+
Channel = channel;
62+
EnableMultiThread = false;
63+
}
64+
65+
/// <summary>
66+
/// Execute command
67+
/// </summary>
68+
[PermissionSet(SecurityAction.Demand, Name = "FullTrust")]
69+
public override void ExecuteCmdlet()
70+
{
71+
var sharedAccessPolicy = new SharedAccessAccountPolicy()
72+
{
73+
Permissions = SetupAccessPolicyPermission(this.Permission),
74+
Services = Service,
75+
ResourceTypes = ResourceType,
76+
Protocols = Protocol,
77+
IPAddressOrRange = SetupIPAddressOrRange(this.IPAddressOrRange)
78+
};
79+
80+
DateTimeOffset? accessStartTime;
81+
DateTimeOffset? accessEndTime;
82+
SasTokenHelper.SetupAccessPolicyLifeTime(StartTime, ExpiryTime,
83+
out accessStartTime, out accessEndTime, true);
84+
sharedAccessPolicy.SharedAccessStartTime = accessStartTime;
85+
sharedAccessPolicy.SharedAccessExpiryTime = accessEndTime;
86+
87+
this.WriteObject(Channel.GetStorageAccountSASToken(sharedAccessPolicy));
88+
}
89+
90+
/// <summary>
91+
/// Set up access policy permission
92+
/// </summary>
93+
/// <param name="policy">SharedAccessBlobPolicy object</param>
94+
/// <param name="permission">Permisson</param>
95+
internal SharedAccessAccountPermissions SetupAccessPolicyPermission(string permission)
96+
{
97+
if (string.IsNullOrEmpty(permission)) return SharedAccessAccountPermissions.None;
98+
99+
SharedAccessAccountPermissions accountPermission = SharedAccessAccountPermissions.None;
100+
permission = permission.ToLower();
101+
foreach (char op in permission)
102+
{
103+
switch (op)
104+
{
105+
case StorageNouns.Permission.Read:
106+
case StorageNouns.Permission.Query:
107+
accountPermission |= SharedAccessAccountPermissions.Read;
108+
break;
109+
case StorageNouns.Permission.Process:
110+
accountPermission |= SharedAccessAccountPermissions.ProcessMessages;
111+
break;
112+
case StorageNouns.Permission.Write:
113+
accountPermission |= SharedAccessAccountPermissions.Write;
114+
break;
115+
case StorageNouns.Permission.Add:
116+
accountPermission |= SharedAccessAccountPermissions.Add;
117+
break;
118+
case StorageNouns.Permission.Create:
119+
accountPermission |= SharedAccessAccountPermissions.Create;
120+
break;
121+
case StorageNouns.Permission.Update:
122+
accountPermission |= SharedAccessAccountPermissions.Update;
123+
break;
124+
case StorageNouns.Permission.Delete:
125+
accountPermission |= SharedAccessAccountPermissions.Delete;
126+
break;
127+
case StorageNouns.Permission.List:
128+
accountPermission |= SharedAccessAccountPermissions.List;
129+
break;
130+
default:
131+
throw new ArgumentException(string.Format(Resources.InvalidAccessPermission, op));
132+
}
133+
}
134+
135+
return accountPermission;
136+
}
137+
138+
internal IPAddressOrRange SetupIPAddressOrRange(string inputIPACL)
139+
{
140+
if (string.IsNullOrEmpty(inputIPACL)) return null;
141+
142+
int separator = inputIPACL.IndexOf('-');
143+
144+
if (-1 == separator)
145+
{
146+
return new IPAddressOrRange(inputIPACL);
147+
}
148+
else
149+
{
150+
return new IPAddressOrRange(inputIPACL.Substring(0, separator), inputIPACL.Substring(separator + 1));
151+
}
152+
}
153+
}
154+
}

src/Common/Storage/Commands.Storage/Common/StorageNouns.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,11 @@ public static class StorageNouns
104104
/// </summary>
105105
public const string StorageCORSRule = "AzureStorageCORSRule";
106106

107+
/// <summary>
108+
/// Azure storage account sas
109+
/// </summary>
110+
public const string AccountSas = "AzureStorageAccountSASToken";
111+
107112
/// <summary>
108113
/// Azure storage container sas
109114
/// </summary>
@@ -214,6 +219,11 @@ public static class Permission
214219
/// Query permission
215220
/// </summary>
216221
public const char Query = 'q';
222+
223+
/// <summary>
224+
/// Create permission.
225+
/// </summary>
226+
public const char Create = 'c';
217227
}
218228
}
219229
}

src/Common/Storage/Commands.Storage/Model/Contract/IStorageBlobManagement.cs

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -208,6 +208,13 @@ public interface IStorageBlobManagement : IStorageManagement
208208
/// <param name="operationContext">Operation context</param>
209209
void SetStorageServiceProperties(StorageServiceType type, ServiceProperties properties, IRequestOptions options, OperationContext operationContext);
210210

211+
/// <summary>
212+
/// Get the SAS token for an account.
213+
/// </summary>
214+
/// <param name="sharedAccessAccountPolicy">Shared access policy to generate the SAS token.</param>
215+
/// <returns>Account SAS token.</returns>
216+
string GetStorageAccountSASToken(SharedAccessAccountPolicy sharedAccessAccountPolicy);
217+
211218
/// <summary>
212219
/// Async get container presssions
213220
/// </summary>

src/Common/Storage/Commands.Storage/Model/Contract/StorageBlobManagement.cs

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -396,6 +396,16 @@ public void SetStorageServiceProperties(StorageServiceType type, ServiceProperti
396396
}
397397
}
398398

399+
/// <summary>
400+
/// Get the SAS token for an account.
401+
/// </summary>
402+
/// <param name="sharedAccessAccountPolicy">Shared access policy to generate the SAS token.</param>
403+
/// <returns>Account SAS token.</returns>
404+
public string GetStorageAccountSASToken(SharedAccessAccountPolicy sharedAccessAccountPolicy)
405+
{
406+
return StorageContext.StorageAccount.GetSharedAccessSignature(sharedAccessAccountPolicy);
407+
}
408+
399409
/// <summary>
400410
/// Async Get container presssions
401411
/// </summary>

src/ResourceManager/KeyVault/Commands.KeyVault/Commands.KeyVault.csproj

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -186,6 +186,9 @@
186186
<SpecificVersion>False</SpecificVersion>
187187
<HintPath>..\..\..\packages\Newtonsoft.Json.6.0.8\lib\net45\Newtonsoft.Json.dll</HintPath>
188188
</Reference>
189+
<Reference Include="System.Spatial, Version=5.6.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL">
190+
<HintPath>..\..\..\packages\System.Spatial.5.6.0\lib\net40\System.Spatial.dll</HintPath>
191+
</Reference>
189192
<Reference Include="System" />
190193
<Reference Include="System.Management.Automation, Version=3.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35, processorArchitecture=MSIL" />
191194
<Reference Include="System.Net" />

0 commit comments

Comments
 (0)