private static ResourceFactory SelectResourceFactoryFor(Resource resource, IRow row) { WindowsAzureActiveDirectoryGroup group = resource as WindowsAzureActiveDirectoryGroup; if (group != null) { ResourceFactory result = new GroupFactory(row); return(result); } Core2EnterpriseUser user = resource as Core2EnterpriseUser; if (user != null) { ResourceFactory result = new UserFactory(row); return(result); } DynamicUser dynamicUser = resource as DynamicUser; if (dynamicUser != null) { ResourceFactory result = new DynamicUserFactory(row); return(result); } string unsupportedSchema = string.Join( Environment.NewLine, resource.Schemas); throw new NotSupportedException(unsupportedSchema); }
private static ColumnsFactory SelectColumnsFactoryFor(Resource resource) { WindowsAzureActiveDirectoryGroup group = resource as WindowsAzureActiveDirectoryGroup; if (group != null) { ColumnsFactory result = new GroupColumnsFactory(group); return(result); } Core2EnterpriseUser user = resource as Core2EnterpriseUser; if (user != null) { ColumnsFactory result = new UserColumnsFactory(user); return(result); } string unsupportedSchema = string.Join( Environment.NewLine, resource.Schemas); throw new NotSupportedException(unsupportedSchema); }
private static IReadOnlyDictionary <string, string> PatchGroup(PatchRequest2 patch, IRow row) { ResourceFactory <WindowsAzureActiveDirectoryGroup> groupFactory = new GroupFactory(row); WindowsAzureActiveDirectoryGroup group = groupFactory.Create(); group.Apply(patch); ColumnsFactory <WindowsAzureActiveDirectoryGroup> groupColumnsFactory = new GroupColumnsFactory(group); IReadOnlyDictionary <string, string> result = groupColumnsFactory.CreateColumns(); return(result); }
public GroupBase ComposeGroupResource() { string value = Guid.NewGuid().ToString(SampleComposer.FormatUniqueIdentifierCompressed); GroupBase result = new WindowsAzureActiveDirectoryGroup(); result.Identifier = Guid.NewGuid().ToString(); result.ExternalIdentifier = value; return(result); }
public override async Task<Resource> Retrieve( IResourceRetrievalParameters parameters, string correlationIdentifier) { if (null == parameters) { throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNameParameters); } if (string.IsNullOrWhiteSpace(correlationIdentifier)) { throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNameCorrelationIdentifier); } if (null == parameters.ResourceIdentifier) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } if (string.IsNullOrWhiteSpace(parameters.ResourceIdentifier.Identifier)) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidResourceIdentifier); } if (string.IsNullOrWhiteSpace(parameters.SchemaIdentifier)) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } string informationStarting = string.Format( CultureInfo.InvariantCulture, AmazonProvisioningAgentResources.InformationRetrieving, parameters.SchemaIdentifier, parameters.ResourceIdentifier.Identifier); ProvisioningAgentMonitor.Instance.Inform(informationStarting, true, correlationIdentifier); AmazonWebServicesProvider.Validate(parameters); IAmazonIdentityManagementService proxy = null; try { proxy = AWSClientFactory.CreateAmazonIdentityManagementServiceClient(this.credentials); switch (parameters.SchemaIdentifier) { case SchemaIdentifiers.Core2EnterpriseUser: Amazon.IdentityManagement.Model.User user = await this.RetrieveUser(parameters.ResourceIdentifier.Identifier, proxy); Core2EnterpriseUser resourceUser = new Core2EnterpriseUser() { Identifier = user.UserId, ExternalIdentifier = user.UserName }; return resourceUser; case SchemaIdentifiers.WindowsAzureActiveDirectoryGroup: Group group = await this.RetrieveGroup(parameters.ResourceIdentifier.Identifier, proxy); WindowsAzureActiveDirectoryGroup resourceGroup = new WindowsAzureActiveDirectoryGroup() { Identifier = group.GroupId, ExternalIdentifier = group.GroupName }; return resourceGroup; default: throw new NotSupportedException(parameters.SchemaIdentifier); } } finally { if (proxy != null) { proxy.Dispose(); proxy = null; } } }
private async Task<Resource[]> QueryReference( IQueryParameters parameters, string correlationIdentifier) { if (null == parameters) { throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNameParameters); } if (string.IsNullOrWhiteSpace(correlationIdentifier)) { throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNameCorrelationIdentifier); } if (null == parameters.RequestedAttributePaths || !parameters.RequestedAttributePaths.Any()) { throw new NotSupportedException(ProvisioningAgentResources.ExceptionUnsupportedQuery); } string selectedAttribute = parameters.RequestedAttributePaths.SingleOrDefault(); if (string.IsNullOrWhiteSpace(selectedAttribute)) { throw new NotSupportedException(ProvisioningAgentResources.ExceptionUnsupportedQuery); } ProvisioningAgentMonitor.Instance.Inform(selectedAttribute, true, correlationIdentifier); if ( !string.Equals( selectedAttribute, Microsoft.SystemForCrossDomainIdentityManagement.AttributeNames.Identifier, StringComparison.OrdinalIgnoreCase) ) { throw new NotSupportedException(ProvisioningAgentResources.ExceptionUnsupportedQuery); } if (null == parameters.AlternateFilters) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } if (string.IsNullOrWhiteSpace(parameters.SchemaIdentifier)) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } string informationAlternateFilterCount = parameters.AlternateFilters.Count.ToString(CultureInfo.InvariantCulture); ProvisioningAgentMonitor.Instance.Inform(informationAlternateFilterCount, true, correlationIdentifier); if (parameters.AlternateFilters.Count != 1) { string exceptionMessage = string.Format( CultureInfo.InvariantCulture, ProvisioningAgentResources.ExceptionFilterCountTemplate, 1, parameters.AlternateFilters.Count); throw new NotSupportedException(exceptionMessage); } AmazonWebServicesProvider.Validate(parameters); IFilter filterPrimary = parameters.AlternateFilters.Single(); if (null == filterPrimary.AdditionalFilter) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } IFilter filterAdditional = filterPrimary.AdditionalFilter; if (filterAdditional.AdditionalFilter != null) { throw new NotSupportedException(ProvisioningAgentResources.ExceptionUnsupportedQuery); } IReadOnlyCollection<IFilter> filters = new IFilter[] { filterPrimary, filterAdditional }; IFilter filterIdentifier = filters .SingleOrDefault( (IFilter item) => string.Equals( AttributeNames.Identifier, item.AttributePath, StringComparison.OrdinalIgnoreCase)); if (null == filterIdentifier) { throw new NotSupportedException(ProvisioningAgentResources.ExceptionUnsupportedQuery); } IFilter filterReference = filters .SingleOrDefault( (IFilter item) => string.Equals( AttributeNames.Members, item.AttributePath, StringComparison.OrdinalIgnoreCase)); if (null == filterReference) { return new Resource[0]; } IAmazonIdentityManagementService proxy = null; try { proxy = AWSClientFactory.CreateAmazonIdentityManagementServiceClient(this.credentials); Amazon.IdentityManagement.Model.User member = await this.RetrieveUser(filterReference.ComparisonValue, proxy); if (member != null) { return new Resource[0]; } ListGroupsForUserRequest request = new ListGroupsForUserRequest() { MaxItems = AmazonWebServicesProvider.SizeListPage, UserName = member.UserName }; while (true) { ListGroupsForUserResponse response = await proxy.ListGroupsForUserAsync(request); if (null == response.Groups || !response.Groups.Any()) { return null; } Group group = response .Groups .SingleOrDefault( (Group item) => string.Equals(item.GroupName, filterReference.ComparisonValue, StringComparison.OrdinalIgnoreCase)); if (group != null) { WindowsAzureActiveDirectoryGroup groupResource = new WindowsAzureActiveDirectoryGroup() { Identifier = group.GroupId, ExternalIdentifier = group.GroupName }; Resource[] results = new Resource[] { groupResource }; return results; } if (string.IsNullOrWhiteSpace(response.Marker)) { return null; } if (string.Equals(request.Marker, response.Marker, StringComparison.OrdinalIgnoreCase)) { return null; } request.Marker = response.Marker; } } finally { if (proxy != null) { proxy.Dispose(); proxy = null; } } }
public override async Task<Resource[]> Query( IQueryParameters parameters, string correlationIdentifier) { if (null == parameters) { throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNameParameters); } if (string.IsNullOrWhiteSpace(correlationIdentifier)) { throw new ArgumentNullException(AmazonWebServicesProvider.ArgumentNameCorrelationIdentifier); } if (null == parameters.AlternateFilters) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } if (string.IsNullOrWhiteSpace(parameters.SchemaIdentifier)) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } string informationAlternateFilterCount = parameters.AlternateFilters.Count.ToString(CultureInfo.InvariantCulture); ProvisioningAgentMonitor.Instance.Inform(informationAlternateFilterCount, true, correlationIdentifier); if (parameters.AlternateFilters.Count != 1) { string exceptionMessage = string.Format( CultureInfo.InvariantCulture, ProvisioningAgentResources.ExceptionFilterCountTemplate, 1, parameters.AlternateFilters.Count); throw new NotSupportedException(exceptionMessage); } Resource[] results; IFilter queryFilter = parameters.AlternateFilters.Single(); if (queryFilter.AdditionalFilter != null) { results = await this.QueryReference(parameters, correlationIdentifier); return results; } AmazonWebServicesProvider.Validate(parameters); if (string.IsNullOrWhiteSpace(queryFilter.AttributePath)) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } if (string.IsNullOrWhiteSpace(queryFilter.ComparisonValue)) { throw new ArgumentException(ProvisioningAgentResources.ExceptionInvalidParameters); } if (!string.Equals(queryFilter.AttributePath, AttributeNames.ExternalIdentifier, StringComparison.Ordinal)) { throw new NotSupportedException(queryFilter.AttributePath); } IAmazonIdentityManagementService proxy = null; try { proxy = AWSClientFactory.CreateAmazonIdentityManagementServiceClient(this.credentials); switch (parameters.SchemaIdentifier) { case SchemaIdentifiers.Core2EnterpriseUser: GetUserRequest getRequestUser = new GetUserRequest() { UserName = queryFilter.ComparisonValue }; GetUserResult responseUser = await proxy.GetUserAsync(getRequestUser); if (null == responseUser.User) { return new Resource[0]; } Core2EnterpriseUser resourceUser = new Core2EnterpriseUser() { Identifier = responseUser.User.UserId, ExternalIdentifier = responseUser.User.UserName }; Resource[] resourceUsers = new Resource[] { resourceUser }; return resourceUsers; case SchemaIdentifiers.WindowsAzureActiveDirectoryGroup: GetGroupRequest getRequestGroup = new GetGroupRequest() { GroupName = queryFilter.ComparisonValue }; GetGroupResult responseGroup = await proxy.GetGroupAsync(getRequestGroup); if (null == responseGroup.Group) { return new Resource[0]; } WindowsAzureActiveDirectoryGroup resourceGroup = new WindowsAzureActiveDirectoryGroup() { Identifier = responseGroup.Group.GroupId, ExternalIdentifier = responseGroup.Group.GroupName }; Resource[] resourceGroups = new Resource[] { resourceGroup }; return resourceGroups; default: throw new NotSupportedException(parameters.SchemaIdentifier); } } finally { if (proxy != null) { proxy.Dispose(); proxy = null; } } }
public void TestLifecycleGroup() { Uri addressBase = new Uri(WebServiceUnitTest.AddressBase); IMonitor monitor = new ConsoleMonitor(); IAmazonWebServicesIdentityAnchoringBehavior anchoringBehavior = new AnchoringByIdentifierBehavior(); AmazonWebServicesProviderBase provider = new AmazonWebServicesProvider(WebServiceUnitTest.CredentialsProfileName, anchoringBehavior); Service webService = null; try { webService = new WebService(monitor, provider); webService.Start(addressBase); string identifierGroup; string identifierGroupExternal; string identifierMemberOne; string identifierMemberTwo; Uri resource; WebClient client = null; try { IDictionary<string, object> json; string characters; byte[] bytes; byte[] response; string responseCharacters; IReadOnlyDictionary<string, object> responseJson; Core2EnterpriseUser user; Member member; IReadOnlyCollection<Member> members; client = new WebClient(); identifierMemberOne = Guid.NewGuid().ToString(); string identifierMemberOneExternal = Guid.NewGuid().ToString(); user = new Core2EnterpriseUser() { Identifier = identifierMemberOne, ExternalIdentifier = identifierMemberOneExternal }; json = user.ToJson(); characters = WebServiceUnitTest.Serializer.Value.Serialize(json); bytes = Encoding.UTF8.GetBytes(characters); resource = new Uri(addressBase, WebServiceUnitTest.AddressRelativeUsers); client.Headers.Clear(); client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson); response = client.UploadData(resource.AbsoluteUri, WebRequestMethods.Http.Post, bytes); responseCharacters = Encoding.UTF8.GetString(response); responseJson = WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters); user = new Core2EnterpriseUserJsonDeserializingFactory().Create(responseJson); identifierMemberOne = user.Identifier; try { member = new Member() { Value = identifierMemberOne }; members = new Member[] { member }; identifierGroup = Guid.NewGuid().ToString(); identifierGroupExternal = Guid.NewGuid().ToString(); WindowsAzureActiveDirectoryGroup group = new WindowsAzureActiveDirectoryGroup() { Identifier = identifierGroup, ExternalIdentifier = identifierGroupExternal, Members = members }; json = group.ToJson(); characters = WebServiceUnitTest.Serializer.Value.Serialize(json); bytes = Encoding.UTF8.GetBytes(characters); resource = new Uri(addressBase, WebServiceUnitTest.AddressRelativeGroups); client.Headers.Clear(); client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson); response = client.UploadData(resource.AbsoluteUri, WebRequestMethods.Http.Post, bytes); responseCharacters = Encoding.UTF8.GetString(response); responseJson = WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters); group = new WindowsAzureActiveDirectoryGroupJsonDeserializingFactory().Create(responseJson); Assert.IsNotNull(group); Assert.IsNotNull( group .Schemas .SingleOrDefault( (string item) => string.Equals( SchemaIdentifiers.WindowsAzureActiveDirectoryGroup, item, StringComparison.Ordinal))); Assert.IsFalse(string.IsNullOrWhiteSpace(group.Identifier)); string identifierGroupAmazon = group.Identifier; try { Assert.IsNotNull(group.Metadata); Assert.IsFalse(string.IsNullOrWhiteSpace(group.Metadata.ResourceType)); Assert.IsFalse(string.Equals(identifierGroup, identifierGroupAmazon, StringComparison.OrdinalIgnoreCase)); string resourcePath = string.Format( CultureInfo.InvariantCulture, WebServiceUnitTest.AddressRelativeGroupTemplate, identifierGroupAmazon); resource = new Uri(addressBase, resourcePath); response = client.DownloadData(resource); responseCharacters = Encoding.UTF8.GetString(response); responseJson = WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters); group = new WindowsAzureActiveDirectoryGroupJsonDeserializingFactory().Create(responseJson); Assert.IsNotNull(group); Assert.IsNotNull( group .Schemas .SingleOrDefault( (string item) => string.Equals( SchemaIdentifiers.Core2Group, item, StringComparison.Ordinal))); Assert.IsFalse(string.IsNullOrWhiteSpace(group.Identifier)); Assert.IsTrue(string.Equals(group.Identifier, identifierGroupAmazon, StringComparison.OrdinalIgnoreCase)); Assert.IsFalse(string.IsNullOrWhiteSpace(group.ExternalIdentifier)); Assert.IsTrue(string.Equals(group.ExternalIdentifier, identifierGroupExternal, StringComparison.OrdinalIgnoreCase)); identifierMemberTwo = Guid.NewGuid().ToString(); string identifierMemberTwoExternal = Guid.NewGuid().ToString(); user = new Core2EnterpriseUser() { Identifier = identifierMemberTwo, ExternalIdentifier = identifierMemberTwoExternal }; json = user.ToJson(); characters = WebServiceUnitTest.Serializer.Value.Serialize(json); bytes = Encoding.UTF8.GetBytes(characters); resource = new Uri(addressBase, WebServiceUnitTest.AddressRelativeUsers); client.Headers.Clear(); client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson); response = client.UploadData(resource.AbsoluteUri, WebRequestMethods.Http.Post, bytes); responseCharacters = Encoding.UTF8.GetString(response); responseJson = WebServiceUnitTest.Serializer.Value.Deserialize<Dictionary<string, object>>(responseCharacters); user = new Core2EnterpriseUserJsonDeserializingFactory().Create(responseJson); identifierMemberTwo = user.Identifier; try { IResourceIdentifier resourceIdentifier = new ResourceIdentifier() { Identifier = identifierGroupAmazon, SchemaIdentifier = SchemaIdentifiers.WindowsAzureActiveDirectoryGroup }; IPath path = Microsoft.SystemForCrossDomainIdentityManagement.Path.Create(AttributeNames.Members); OperationValue operationValue; PatchOperation operation; IReadOnlyCollection<PatchOperation> operations; PatchRequest2 patch; operationValue = new OperationValue() { Value = identifierMemberTwo }; operation = new PatchOperation() { Name = OperationName.Add, Path = path }; operations = new PatchOperation[] { operation }; operation.AddValue(operationValue); patch = new PatchRequest2(); patch.AddOperation(operation); json = patch.ToJson(); characters = WebServiceUnitTest.Serializer.Value.Serialize(json); bytes = Encoding.UTF8.GetBytes(characters); resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon); resource = new Uri(addressBase, resourcePath); client.Headers.Clear(); client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson); response = client.UploadData(resource.AbsoluteUri, WebServiceUnitTest.MethodPatch, bytes); operationValue = new OperationValue() { Value = identifierMemberTwo }; operation = new PatchOperation() { Name = OperationName.Remove, Path = path }; operations = new PatchOperation[] { operation }; operation.AddValue(operationValue); patch = new PatchRequest2(); patch.AddOperation(operation); json = patch.ToJson(); characters = WebServiceUnitTest.Serializer.Value.Serialize(json); bytes = Encoding.UTF8.GetBytes(characters); resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon); resource = new Uri(addressBase, resourcePath); client.Headers.Clear(); client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson); response = client.UploadData(resource.AbsoluteUri, WebServiceUnitTest.MethodPatch, bytes); operationValue = new OperationValue() { Value = identifierMemberOne }; operation = new PatchOperation() { Name = OperationName.Remove, Path = path }; operations = new PatchOperation[] { operation }; operation.AddValue(operationValue); patch = new PatchRequest2(); patch.AddOperation(operation); json = patch.ToJson(); characters = WebServiceUnitTest.Serializer.Value.Serialize(json); bytes = Encoding.UTF8.GetBytes(characters); resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon); resource = new Uri(addressBase, resourcePath); client.Headers.Clear(); client.Headers.Add(HttpRequestHeader.ContentType, WebServiceUnitTest.ContentTypeJson); response = client.UploadData(resource.AbsoluteUri, WebServiceUnitTest.MethodPatch, bytes); } finally { resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeUser, identifierMemberTwo); resource = new Uri(addressBase, resourcePath); bytes = new byte[0]; client.UploadData(resource, WebServiceUnitTest.MethodDelete, bytes); } } finally { string resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeGroup, identifierGroupAmazon); resource = new Uri(addressBase, resourcePath); bytes = new byte[0]; client.UploadData(resource, WebServiceUnitTest.MethodDelete, bytes); } } finally { string resourcePath = string.Concat(WebServiceUnitTest.AddressRelativeUser, identifierMemberOne); resource = new Uri(addressBase, resourcePath); bytes = new byte[0]; client.UploadData(resource, WebServiceUnitTest.MethodDelete, bytes); } } finally { if (client != null) { client.Dispose(); client = null; } } } finally { if (webService != null) { webService.Dispose(); webService = null; } } }