예제 #1
0
    public async Task <ICommandResult> HandleAsync(DeploymentScopeCreateCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

        if (commandQueue is null)
        {
            throw new ArgumentNullException(nameof(commandQueue));
        }

        var commandResult = command.CreateResult();

        try
        {
            commandResult.Result = await deploymentScopeRepository
                                   .AddAsync(command.Payload)
                                   .ConfigureAwait(false);

            if (adapterProvider.GetAdapter(commandResult.Result.Type) is IAdapterIdentity adapterIdentity)
            {
                var servicePrincipal = await adapterIdentity
                                       .GetServiceIdentityAsync(commandResult.Result)
                                       .ConfigureAwait(false);

                var servicePrincipalUser = await userRepository
                                           .GetAsync(commandResult.Result.Organization, servicePrincipal.Id.ToString())
                                           .ConfigureAwait(false);

                if (servicePrincipalUser is null)
                {
                    servicePrincipalUser ??= new User
                    {
                        Id               = servicePrincipal.Id.ToString(),
                        Role             = OrganizationUserRole.Adapter,
                        UserType         = Model.Data.UserType.Service,
                        Organization     = commandResult.Result.Organization,
                        OrganizationName = commandResult.Result.OrganizationName
                    };

                    await commandQueue
                    .AddAsync(new OrganizationUserCreateCommand(command.User, servicePrincipalUser))
                    .ConfigureAwait(false);
                }
            }

            commandResult.RuntimeStatus = CommandRuntimeStatus.Completed;
        }
        catch (Exception exc)
        {
            commandResult.Errors.Add(exc);
        }

        return(commandResult);
    }
    public async Task ExpandAsync(DeploymentScope document)
    {
        if (document is null)
        {
            throw new ArgumentNullException(nameof(document));
        }

        var adapter = adapterProvider.GetAdapter(document.Type);

        if (adapter is not null)
        {
            document.InputDataSchema = await adapter
                                       .GetInputDataSchemaAsync()
                                       .ConfigureAwait(false);

            document.ComponentTypes = adapter.ComponentTypes.ToList();

            if (adapter is IAdapterAuthorize adapterAuthorize)
            {
                document.Authorizable = true;

                document.Authorized = await adapterAuthorize
                                      .IsAuthorizedAsync(document)
                                      .ConfigureAwait(false);

                document.AuthorizeUrl = (await authorizationEndpointsResolver
                                         .GetAuthorizationEndpointsAsync(document)
                                         .ConfigureAwait(false))?.AuthorizationUrl;
            }
            else
            {
                document.Authorizable = false;
                document.Authorized   = !(adapter is null);
                document.AuthorizeUrl = null;
            }

            if (document.Type == DeploymentScopeType.AzureResourceManager && !string.IsNullOrWhiteSpace(document.InputData))
            {
                // TODO: remove this special handling for AzureResourceManager deployment scopes
                // when adapters are fully implemented and ManagementGroupId and SubscriptionIds are gone.

                var inputData = JObject.Parse(document.InputData);

                document.ManagementGroupId = inputData.SelectToken("$..managementGroupId")?.ToString();
                document.SubscriptionIds   = (inputData.SelectToken("$..subscriptionIds") as JArray)?.Select(t => Guid.Parse(t.ToString())).ToList() ?? new List <Guid>();
            }
        }
    }
    private async Task <bool> ValidInputDataAsync(DeploymentScopeDefinition deploymentScopeDefinition, CancellationToken cancellationToken = default)
    {
        var json = string.IsNullOrEmpty(deploymentScopeDefinition.InputData)
            ? null : JToken.Parse(deploymentScopeDefinition.InputData);

        var adapter = adapterProvider.GetAdapter(deploymentScopeDefinition.Type);

        if (adapter is not null)
        {
            var schemaJson = await adapter
                             .GetInputDataSchemaAsync()
                             .ConfigureAwait(false);

            if (!string.IsNullOrEmpty(schemaJson))
            {
                return(json.IsValid(JSchema.Parse(schemaJson)));
            }
        }

        return(false);
    }
예제 #4
0
    protected override async Task <AuthenticateResult> HandleAuthenticateAsync()
    {
        var authenticateResult = await base
                                 .HandleAuthenticateAsync()
                                 .ConfigureAwait(false);

        if (authenticateResult.None && Request.RouteValues.TryGetValue("organizationId", out var organizationId) && Request.RouteValues.TryGetValue("deploymentScopeId", out var deploymentScopeId))
        {
            var deploymentScope = await deploymentScopeRepository
                                  .GetAsync($"{organizationId}", $"{deploymentScopeId}")
                                  .ConfigureAwait(false);

            var adapter = deploymentScope is null
                ? default(Adapter)
                : adapterProvider.GetAdapter(deploymentScope.Type);

            if (adapter is IAdapterAuthorize adapterAuthorize)
            {
                var servicePrincial = await adapterAuthorize
                                      .ResolvePrincipalAsync(deploymentScope, Context.Request)
                                      .ConfigureAwait(false);

                if (servicePrincial is not null)
                {
                    var claimsIdentity = servicePrincial.ToClaimsIdentity(AdapterAuthenticationDefaults.AuthenticationType);

                    claimsIdentity.AddClaims(await Context
                                             .ResolveClaimsAsync(servicePrincial.TenantId.ToString(), servicePrincial.Id.ToString())
                                             .ConfigureAwait(false));

                    var claimsPrincipal = new ClaimsPrincipal(claimsIdentity);

                    authenticateResult = AuthenticateResult.Success(new AuthenticationTicket(claimsPrincipal, AdapterAuthenticationDefaults.AuthenticationScheme));
                }
            }
        }

        return(authenticateResult);
    }
    public override async Task <ICommandResult> HandleAsync(ComponentCreateCommand command, IAsyncCollector <ICommand> commandQueue, IDurableOrchestrationContext orchestrationContext, ILogger log)
    {
        if (command is null)
        {
            throw new ArgumentNullException(nameof(command));
        }

        if (commandQueue is null)
        {
            throw new ArgumentNullException(nameof(commandQueue));
        }

        var commandResult = command.CreateResult();

        try
        {
            commandResult.Result = await componentRepository
                                   .AddAsync(command.Payload)
                                   .ConfigureAwait(false);

            var deploymentScope = await deploymentScopeRepository
                                  .GetAsync(commandResult.Result.Organization, commandResult.Result.DeploymentScopeId)
                                  .ConfigureAwait(false);

            if (adapterProvider.GetAdapter(deploymentScope.Type) is IAdapterIdentity adapterIdentity)
            {
                var servicePrincipal = await adapterIdentity
                                       .GetServiceIdentityAsync(commandResult.Result)
                                       .ConfigureAwait(false);

                var servicePrincipalUser = await userRepository
                                           .GetAsync(commandResult.Result.Organization, servicePrincipal.Id.ToString())
                                           .ConfigureAwait(false);

                if (servicePrincipalUser is null)
                {
                    servicePrincipalUser ??= new User
                    {
                        Id               = servicePrincipal.Id.ToString(),
                        Role             = OrganizationUserRole.Adapter,
                        UserType         = Model.Data.UserType.Service,
                        Organization     = commandResult.Result.Organization,
                        OrganizationName = commandResult.Result.OrganizationName
                    };

                    servicePrincipalUser.EnsureProjectMembership(commandResult.Result.ProjectId, ProjectUserRole.Adapter);

                    await commandQueue
                    .AddAsync(new ProjectUserCreateCommand(command.User, servicePrincipalUser, commandResult.Result.ProjectId))
                    .ConfigureAwait(false);
                }
            }

            var componentTask = new ComponentTask
            {
                Organization     = commandResult.Result.Organization,
                OrganizationName = commandResult.Result.OrganizationName,
                ComponentId      = commandResult.Result.Id,
                ComponentName    = commandResult.Result.Slug,
                ProjectId        = commandResult.Result.ProjectId,
                ProjectName      = commandResult.Result.ProjectName,
                Type             = ComponentTaskType.Create,
                RequestedBy      = commandResult.Result.Creator,
                InputJson        = commandResult.Result.InputJson
            };

            await commandQueue
            .AddAsync(new ComponentTaskCreateCommand(command.User, componentTask))
            .ConfigureAwait(false);

            commandResult.RuntimeStatus = CommandRuntimeStatus.Completed;
        }
        catch (Exception exc)
        {
            commandResult.Errors.Add(exc);
        }

        return(commandResult);
    }