public async Task SetPermissions_Filesystem(DataLakeFileSystemSasPermissions permissions) { // Arrange await using DisposingFileSystem test = await GetNewFileSystem(); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name }; dataLakeSasBuilder.SetPermissions(permissions); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(TestConfigHierarchicalNamespace.AccountName, TestConfigHierarchicalNamespace.AccountKey); DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(test.FileSystem.Uri) { Sas = dataLakeSasBuilder.ToSasQueryParameters(sharedKeyCredential) }; DataLakeFileSystemClient sasFileSystemClient = InstrumentClient(new DataLakeFileSystemClient(dataLakeUriBuilder.ToUri(), GetOptions())); // Act await foreach (PathItem pathItem in sasFileSystemClient.GetPathsAsync()) { // Just make sure the call succeeds. } }
public async Task DataLakeSasBuilder_BothObjectId_Error() { // Arrange DataLakeServiceClient oauthService = GetServiceClient_OAuth(); string fileSystemName = GetNewFileSystemName(); string directoryName = GetNewDirectoryName(); await using DisposingFileSystem test = await GetNewFileSystem(service : oauthService, fileSystemName : fileSystemName); // Arrange DataLakeDirectoryClient directory = await test.FileSystem.CreateDirectoryAsync(directoryName); DataLakeFileClient file = await directory.CreateFileAsync(GetNewFileName()); Response <UserDelegationKey> userDelegationKey = await oauthService.GetUserDelegationKeyAsync( startsOn : null, expiresOn : Recording.UtcNow.AddHours(1)); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name, PreauthorizedAgentObjectId = Recording.Random.NewGuid().ToString(), AgentObjectId = Recording.Random.NewGuid().ToString() }; dataLakeSasBuilder.SetPermissions(DataLakeSasPermissions.All); TestHelper.AssertExpectedException <InvalidOperationException>( () => dataLakeSasBuilder.ToSasQueryParameters(userDelegationKey, test.FileSystem.AccountName), new InvalidOperationException("SAS cannot have the following parameters specified in conjunction: PreauthorizedAgentObjectId, AgentObjectId")); }
public async Task DataLakeSasBuilder_DirectoryDepth(string directoryName) { // Arrange DataLakeServiceClient oauthService = GetServiceClient_OAuth(); string fileSystemName = GetNewFileSystemName(); await using DisposingFileSystem test = await GetNewFileSystem(service : oauthService, fileSystemName : fileSystemName); DataLakeDirectoryClient directory = test.FileSystem.GetDirectoryClient(directoryName); Response <UserDelegationKey> userDelegationKey = await oauthService.GetUserDelegationKeyAsync( startsOn : null, expiresOn : Recording.UtcNow.AddHours(1)); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name, Path = directoryName, IsDirectory = true }; dataLakeSasBuilder.SetPermissions(DataLakeSasPermissions.All); DataLakeSasQueryParameters sas = dataLakeSasBuilder.ToSasQueryParameters(userDelegationKey, test.FileSystem.AccountName); int expectedDepth = directoryName.Split('/').Length; if (expectedDepth > 0) { expectedDepth -= directoryName.ElementAt(0) == '/' ? 1 : 0; expectedDepth -= directoryName.ElementAt(directoryName.Length - 1) == '/' ? 1 : 0; } Assert.AreEqual(expectedDepth, sas.DirectoryDepth); }
public DataLakeSasQueryParameters GetNewDataLakeSasCredentialsOwner(string fileSystemName, string ownerName, UserDelegationKey userDelegationKey, string accountName) { DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = fileSystemName, AgentObjectId = ownerName }; dataLakeSasBuilder.SetPermissions(DataLakeSasPermissions.All); return(dataLakeSasBuilder.ToSasQueryParameters(userDelegationKey, accountName)); }
public DataLakeSasQueryParameters GetNewDataLakeServiceIdentitySasCredentialsFileSystem(string fileSystemName, UserDelegationKey userDelegationKey, string accountName) { var builder = new DataLakeSasBuilder { FileSystemName = fileSystemName, Protocol = SasProtocol.None, StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(+1), IPRange = new SasIPRange(IPAddress.None, IPAddress.None) }; builder.SetPermissions(DataLakeFileSystemSasPermissions.All); return(builder.ToSasQueryParameters(userDelegationKey, accountName)); }
public DataLakeSasQueryParameters GetNewDataLakeServiceSasCredentialsFileSystem(string fileSystemName, StorageSharedKeyCredential sharedKeyCredentials = default) { var builder = new DataLakeSasBuilder { FileSystemName = fileSystemName, Protocol = SasProtocol.None, StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(+1), IPRange = new SasIPRange(IPAddress.None, IPAddress.None) }; builder.SetPermissions(DataLakeFileSystemSasPermissions.All); return(builder.ToSasQueryParameters(sharedKeyCredentials ?? GetNewSharedKeyCredentials())); }
public async Task DataLakeSasBuilder_AgentObjectId() { // Arrange DataLakeServiceClient oauthService = GetServiceClient_OAuth(); string fileSystemName = GetNewFileSystemName(); string directoryName = GetNewDirectoryName(); string unknownGuid = Recording.Random.NewGuid().ToString(); await using DisposingFileSystem test = await GetNewFileSystem(service : oauthService, fileSystemName : fileSystemName); // Arrange DataLakeDirectoryClient directory = test.FileSystem.GetRootDirectoryClient(); Response <UserDelegationKey> userDelegationKey = await oauthService.GetUserDelegationKeyAsync( startsOn : null, expiresOn : Recording.UtcNow.AddHours(1)); // Give UnknownGuid rights IList <PathAccessControlItem> accessControlList = new List <PathAccessControlItem>() { new PathAccessControlItem( AccessControlType.User, RolePermissions.Read | RolePermissions.Write | RolePermissions.Execute, false, unknownGuid) }; await directory.SetAccessControlListAsync(accessControlList); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name, AgentObjectId = unknownGuid }; dataLakeSasBuilder.SetPermissions(DataLakeSasPermissions.All); DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(test.FileSystem.Uri) { Sas = dataLakeSasBuilder.ToSasQueryParameters(userDelegationKey, test.FileSystem.AccountName) }; DataLakeDirectoryClient sasDirectoryClient = new DataLakeDirectoryClient(dataLakeUriBuilder.ToUri(), GetOptions()); // Act DataLakeFileClient file = await sasDirectoryClient.CreateFileAsync(GetNewFileName()); }
public void EnsureStateTests() { DataLakeSasBuilder sasBuilder = new DataLakeSasBuilder(); // No Identifier, Permissions and ExpiresOn not present. TestHelper.AssertExpectedException( () => sasBuilder.EnsureState(), new InvalidOperationException("SAS is missing required parameter: Permissions")); sasBuilder.SetPermissions(_sasPermissions); // No Identifier, ExpiresOn not present. TestHelper.AssertExpectedException( () => sasBuilder.EnsureState(), new InvalidOperationException("SAS is missing required parameter: ExpiresOn")); }
// </Snippet_GetServiceSasUriForContainer> #endregion #region GetServiceSasUriForDirectory //------------------------------------------------- // Get service SAS for directory //------------------------------------------------- // <Snippet_GetServiceSasUriForDirectory> private static Uri GetServiceSasUriForDirectory(DataLakeDirectoryClient directoryClient, string storedPolicyName = null) { if (directoryClient.CanGenerateSasUri) { // Create a SAS token that's valid for one hour. DataLakeSasBuilder sasBuilder = new DataLakeSasBuilder() { // Specify the file system name, the path, and indicate that // the client object points to a directory. FileSystemName = directoryClient.FileSystemName, Resource = "d", IsDirectory = true, Path = directoryClient.Path, }; // If no stored access policy is specified, create the policy // by specifying expiry and permissions. if (storedPolicyName == null) { sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1); sasBuilder.SetPermissions(DataLakeSasPermissions.Read | DataLakeSasPermissions.Write | DataLakeSasPermissions.List); } else { sasBuilder.Identifier = storedPolicyName; } // Get the SAS URI for the specified directory. Uri sasUri = directoryClient.GenerateSasUri(sasBuilder); Console.WriteLine("SAS URI for ADLS directory is: {0}", sasUri); Console.WriteLine(); return(sasUri); } else { Console.WriteLine(@"DataLakeDirectoryClient must be authorized with Shared Key credentials to create a service SAS."); return(null); } }
public async Task DataLakeSasBuilder_DirectoryDepth_Exists() { // Arrange DataLakeServiceClient oauthService = GetServiceClient_OAuth(); string fileSystemName = GetNewFileSystemName(); await using DisposingFileSystem test = await GetNewFileSystem(service : oauthService, fileSystemName : fileSystemName); DataLakeDirectoryClient directory = await test.FileSystem.CreateDirectoryAsync(GetNewDirectoryName()); DataLakeDirectoryClient subdirectory = await directory.CreateSubDirectoryAsync(GetNewDirectoryName()); DataLakeDirectoryClient subdirectory2 = await subdirectory.CreateSubDirectoryAsync(GetNewDirectoryName()); DataLakeDirectoryClient subdirectory3 = await subdirectory2.CreateSubDirectoryAsync(GetNewDirectoryName()); DataLakeFileClient file = await subdirectory3.CreateFileAsync(GetNewFileName()); Response <UserDelegationKey> userDelegationKey = await oauthService.GetUserDelegationKeyAsync( startsOn : null, expiresOn : Recording.UtcNow.AddHours(1)); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name, Path = subdirectory3.Path, IsDirectory = true }; dataLakeSasBuilder.SetPermissions(DataLakeSasPermissions.All); DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(subdirectory3.Uri) { Sas = dataLakeSasBuilder.ToSasQueryParameters(userDelegationKey, test.FileSystem.AccountName) }; DataLakeDirectoryClient sasDirectoryClient = InstrumentClient(new DataLakeDirectoryClient(dataLakeUriBuilder.ToUri(), GetOptions())); // Act await sasDirectoryClient.ExistsAsync(); }
public async Task DataLakeSasBuilderRawPermissions_2020_02_10(string permissionsString) { // Arrange DataLakeServiceClient oauthService = GetServiceClient_OAuth(); string fileSystemName = GetNewFileSystemName(); string directoryName = GetNewDirectoryName(); await using DisposingFileSystem test = await GetNewFileSystem(service : oauthService, fileSystemName : fileSystemName); // Arrange DataLakeDirectoryClient directory = await test.FileSystem.CreateDirectoryAsync(directoryName); DataLakeFileClient file = await directory.CreateFileAsync(GetNewFileName()); Response <UserDelegationKey> userDelegationKey = await oauthService.GetUserDelegationKeyAsync( startsOn : null, expiresOn : Recording.UtcNow.AddHours(1)); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name }; dataLakeSasBuilder.SetPermissions( rawPermissions: permissionsString, normalize: true); DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(test.FileSystem.Uri) { Sas = dataLakeSasBuilder.ToSasQueryParameters(userDelegationKey, test.FileSystem.AccountName) }; DataLakeFileSystemClient sasFileSystemClient = InstrumentClient(new DataLakeFileSystemClient(dataLakeUriBuilder.ToUri(), GetOptions())); // Act await foreach (PathItem pathItem in sasFileSystemClient.GetPathsAsync()) { // Just make sure the call succeeds. } }
public DataLakeSasQueryParameters GetNewDataLakeServiceSasCredentialsPath(string fileSystemName, string path, StorageSharedKeyCredential sharedKeyCredentials = default) { var builder = new DataLakeSasBuilder { FileSystemName = fileSystemName, Path = path, Protocol = SasProtocol.None, StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(+1), IPRange = new SasIPRange(IPAddress.None, IPAddress.None) }; builder.SetPermissions( DataLakeSasPermissions.Read | DataLakeSasPermissions.Add | DataLakeSasPermissions.Create | DataLakeSasPermissions.Delete | DataLakeSasPermissions.Write); return(builder.ToSasQueryParameters(sharedKeyCredentials ?? Tenants.GetNewHnsSharedKeyCredentials())); }
public async Task DataLakeSasBuilder_DirectoryDepth_SharedKey() { // Arrange DataLakeServiceClient oauthService = GetServiceClient_OAuth(); string fileSystemName = GetNewFileSystemName(); await using DisposingFileSystem test = await GetNewFileSystem(service : oauthService, fileSystemName : fileSystemName); DataLakeDirectoryClient directory = await test.FileSystem.CreateDirectoryAsync(GetNewDirectoryName()); DataLakeDirectoryClient subdirectory = await directory.CreateSubDirectoryAsync(GetNewDirectoryName()); DataLakeDirectoryClient subdirectory2 = await subdirectory.CreateSubDirectoryAsync(GetNewDirectoryName()); DataLakeDirectoryClient subdirectory3 = await subdirectory2.CreateSubDirectoryAsync(GetNewDirectoryName()); DataLakeFileClient file = await subdirectory3.CreateFileAsync(GetNewFileName()); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name, Path = subdirectory3.Path, IsDirectory = true }; dataLakeSasBuilder.SetPermissions(DataLakeSasPermissions.All); StorageSharedKeyCredential sharedKeyCredential = new StorageSharedKeyCredential(TestConfigHierarchicalNamespace.AccountName, TestConfigHierarchicalNamespace.AccountKey); DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(subdirectory3.Uri) { Sas = dataLakeSasBuilder.ToSasQueryParameters(sharedKeyCredential) }; DataLakeDirectoryClient sasDirectoryClient = InstrumentClient(new DataLakeDirectoryClient(dataLakeUriBuilder.ToUri(), GetOptions())); // Act await sasDirectoryClient.ExistsAsync(); }
public async Task DataLakeSasBuilder_AgentObjectId_Error() { // Arrange DataLakeServiceClient oauthService = GetServiceClient_OAuth(); string fileSystemName = GetNewFileSystemName(); string directoryName = GetNewDirectoryName(); await using DisposingFileSystem test = await GetNewFileSystem(service : oauthService, fileSystemName : fileSystemName); // Arrange DataLakeDirectoryClient directory = await test.FileSystem.CreateDirectoryAsync(directoryName); DataLakeFileClient file = await directory.CreateFileAsync(GetNewFileName()); Response <UserDelegationKey> userDelegationKey = await oauthService.GetUserDelegationKeyAsync( startsOn : null, expiresOn : Recording.UtcNow.AddHours(1)); DataLakeSasBuilder dataLakeSasBuilder = new DataLakeSasBuilder { StartsOn = Recording.UtcNow.AddHours(-1), ExpiresOn = Recording.UtcNow.AddHours(1), FileSystemName = test.FileSystem.Name, AgentObjectId = Recording.Random.NewGuid().ToString() }; dataLakeSasBuilder.SetPermissions(DataLakeSasPermissions.All); DataLakeUriBuilder dataLakeUriBuilder = new DataLakeUriBuilder(test.FileSystem.Uri) { Sas = dataLakeSasBuilder.ToSasQueryParameters(userDelegationKey, test.FileSystem.AccountName) }; DataLakeFileSystemClient sasFileSystemClient = InstrumentClient(new DataLakeFileSystemClient(dataLakeUriBuilder.ToUri(), GetOptions())); // Act await TestHelper.AssertExpectedExceptionAsync <RequestFailedException>( sasFileSystemClient.ExistsAsync(), e => Assert.IsNotNull(e.ErrorCode)); }
// </Snippet_ListBlobsWithSasAsync> #endregion #region // <Snippet_GetUserDelegationSasDirectory> async static Task <Uri> GetUserDelegationSasDirectory(DataLakeDirectoryClient directoryClient) { try { // Get service endpoint from the directory URI. DataLakeUriBuilder dataLakeServiceUri = new DataLakeUriBuilder(directoryClient.Uri) { FileSystemName = null, DirectoryOrFilePath = null }; // Get service client. DataLakeServiceClient dataLakeServiceClient = new DataLakeServiceClient(dataLakeServiceUri.ToUri(), new DefaultAzureCredential()); // Get a user delegation key that's valid for seven days. // You can use the key to generate any number of shared access signatures // over the lifetime of the key. Azure.Storage.Files.DataLake.Models.UserDelegationKey userDelegationKey = await dataLakeServiceClient.GetUserDelegationKeyAsync(DateTimeOffset.UtcNow, DateTimeOffset.UtcNow.AddDays(7)); // Create a SAS token that's valid for seven days. DataLakeSasBuilder sasBuilder = new DataLakeSasBuilder() { // Specify the file system name and path, and indicate that // the client object points to a directory. FileSystemName = directoryClient.FileSystemName, Resource = "d", IsDirectory = true, Path = directoryClient.Path, ExpiresOn = DateTimeOffset.UtcNow.AddDays(7) }; // Specify racwl permissions for the SAS. sasBuilder.SetPermissions( DataLakeSasPermissions.Read | DataLakeSasPermissions.Add | DataLakeSasPermissions.Create | DataLakeSasPermissions.Write | DataLakeSasPermissions.List ); // Construct the full URI, including the SAS token. DataLakeUriBuilder fullUri = new DataLakeUriBuilder(directoryClient.Uri) { Sas = sasBuilder.ToSasQueryParameters(userDelegationKey, dataLakeServiceClient.AccountName) }; Console.WriteLine("Directory user delegation SAS URI: {0}", fullUri); Console.WriteLine(); return(fullUri.ToUri()); } catch (Exception e) { Console.WriteLine(e.Message); throw; } }
public override void ExecuteCmdlet() { IStorageBlobManagement localChannel = Channel; // When the input context is Oauth bases, can't generate normal SAS, but UserDelegationSas bool generateUserDelegationSas = false; if (Channel != null && Channel.StorageContext != null && Channel.StorageContext.StorageAccount.Credentials.IsToken) { if (ShouldProcess(this.Path, "Generate User Delegation SAS, since input Storage Context is OAuth based.")) { generateUserDelegationSas = true; } else { return; } } if (this.ParameterSetName == ItemParameterSet) { if (this.InputObject.IsDirectory) { this.FileSystem = this.InputObject.Directory.FileSystemName; this.Path = this.InputObject.Directory.Path; } else { this.FileSystem = this.InputObject.File.FileSystemName; this.Path = this.InputObject.File.Path; } } DataLakeSasBuilder sasBuilder = new DataLakeSasBuilder(); sasBuilder.FileSystemName = this.FileSystem; sasBuilder.Path = this.Path; sasBuilder.SetPermissions(this.Permission, true); if (StartTime != null) { sasBuilder.StartsOn = StartTime.Value.ToUniversalTime(); } if (ExpiryTime != null) { sasBuilder.ExpiresOn = ExpiryTime.Value.ToUniversalTime(); } else { if (sasBuilder.StartsOn != DateTimeOffset.MinValue) { sasBuilder.ExpiresOn = sasBuilder.StartsOn.AddHours(1).ToUniversalTime(); } else { sasBuilder.ExpiresOn = DateTimeOffset.UtcNow.AddHours(1); } } if (this.IPAddressOrRange != null) { sasBuilder.IPRange = Util.SetupIPAddressOrRangeForSASTrack2(this.IPAddressOrRange); } if (this.Protocol != null) { sasBuilder.Protocol = this.Protocol.Value; } DataLakeFileSystemClient fileSystem = GetFileSystemClientByName(localChannel, this.FileSystem); DataLakePathClient pathClient; DataLakeFileClient fileClient; DataLakeDirectoryClient dirClient; if (GetExistDataLakeGen2Item(fileSystem, this.Path, out fileClient, out dirClient)) { // Directory sasBuilder.IsDirectory = true; pathClient = dirClient; //WriteDataLakeGen2Item(localChannel, dirClient); // sasBuilder.ToSasQueryParameters() } else { //File sasBuilder.IsDirectory = false; pathClient = fileClient; //WriteDataLakeGen2Item(Channel, fileClient); } string sasToken = SasTokenHelper.GetDatalakeGen2SharedAccessSignature(Channel.StorageContext, sasBuilder, generateUserDelegationSas, DataLakeClientOptions, CmdletCancellationToken); if (FullUri) { string fullUri = pathClient.Uri.ToString(); fullUri = fullUri + "?" + sasToken; WriteObject(fullUri); } else { WriteObject(sasToken); } }