public ActionResult ProfileChooser()
        {
            string            currentUsername = _owinContext.Authentication.User.Identity.Name;
            List <AwsProfile> profiles        = _userProfileAccessManager.GetProfilesForUser(currentUsername).ToList();

            var model = new SessionViewModel
            {
                Profiles          = profiles,
                SelectedProfileId = (Guid)Session[WebPortalConstants.ActiveProfileId]
            };

            return(PartialView(model));
        }
Example #2
0
        public async Task <ActionResult> Login(LoginViewModel model, string returnUrl)
        {
            if (!ModelState.IsValid)
            {
                return(View(model));
            }

            IdentityUser authenticatedUser;

            try
            {
                authenticatedUser = _authenticatedUserClient.AuthenticateUser(model.UserName, model.Password);
            }
            catch (AuthenticatedUserClientException)
            {
                ModelState.AddModelError(string.Empty, _apiExceptionMessage);
                return(View(model));
            }

            if (authenticatedUser == null)
            {
                ModelState.AddModelError(string.Empty, _unauthenticatedErrorMessage);
                return(View(model));
            }
            await _signInManager.SignInAsync(authenticatedUser, model.RememberMe, rememberBrowser : true);

            var profile = _userProfileAccessManager.GetProfilesForUser(model.UserName).FirstOrDefault();

            if (profile == null)
            {
                // The user doesn't have permission to use *any* profiles
                ModelState.AddModelError("", "You do not have permission to use any AWS profiles. Contact an administrator.");
                return(View(model));
            }
            Session[WebPortalConstants.ActiveProfileId] = profile.Id;

            if (!returnUrl.IsNullOrWhiteSpace() && Url.IsLocalUrl(returnUrl))
            {
                return(Redirect(returnUrl));
            }
            else
            {
                return(RedirectToAction("Index", "Home"));
            }
        }
        public ActionResult CreateStack(CreateStackViewModel viewModel)
        {
            // If the user doesn't have permission to the selected profile, fail early
            string currentUserName             = _owinContext.Authentication.User.Identity.Name;
            var    userProfiles                = _userProfileAccessManager.GetProfilesForUser(currentUserName).ToList();
            var    selectedProfile             = userProfiles.FirstOrDefault(x => x.Id == viewModel.SelectedProfileId);
            bool   userHasPermissionForProfile = selectedProfile != null;

            if (!userHasPermissionForProfile)
            {
                throw new InvalidOperationException("User does not have permission to use this profile.");
            }

            var stackComponentDefinition = viewModel.SelectedProductIds
                                           .Zip(viewModel.SelectedVersionNames, (productId, versionName) => new { productId, versionName })
                                           .Zip(viewModel.SelectedRoleNames, (prodVer, roleName) => new { prodVer.productId, prodVer.versionName, roleName })
                                           .Zip(viewModel.Options, (prodVerRole, options) => new { prodVerRole.productId, prodVerRole.versionName, prodVerRole.roleName, options })
                                           .Select(x => new { x.productId, x.versionName, x.roleName, x.options }).ToList();

            var configuration = new StackConfiguration
            {
                StackName              = viewModel.StackName,
                OwnerProfileId         = viewModel.SelectedProfileId,
                OwnerUserName          = currentUserName,
                VpcId                  = viewModel.SelectedVpcId,
                SubnetId               = viewModel.SelectedSubnetId,
                HostedZone             = userProfiles.Single(x => x.Id == viewModel.SelectedProfileId).HostedZone,
                BootstrapperUrl        = _stackItConfiguration.PuppetConfiguration.BootstrapperUrl,
                PuppetInstallerUrl     = _stackItConfiguration.PuppetConfiguration.PuppetInstallerUrl,
                PuppetHost             = _stackItConfiguration.PuppetConfiguration.PuppetHost,
                DefaultSecurityGroupId = selectedProfile.DefaultSecurityGroupId,
                Notes                  = viewModel.Notes,
                ScheduleId             = viewModel.SelectedScheduleId,
                ScheduleEnabled        = viewModel.ScheduleEnabled
            };

            foreach (var entry in stackComponentDefinition)
            {
                var     scopedEntry = entry;
                Product product     = _productRepository.Find(scopedEntry.productId);
                Version version     = product.Versions.Single(x => x.Name == scopedEntry.versionName);
                Role    role        = version.Roles.Single(x => x.Name == scopedEntry.roleName);

                OverwriteRoleOptions(role, scopedEntry.options);

                // If the instance type is not whitelisted, fail.
                // Do so after the role mapping so that website defaults (i.e. when a user doesn't alter the options)
                // values can be handled first.
                if (!_stackItConfiguration.InstanceTypes.Select(x => x.Name).Contains(role.Options.InstanceType))
                {
                    throw new InvalidOperationException("Instance type not supported.");
                }

                bool   useDefaultName = scopedEntry.options == null || string.IsNullOrEmpty(scopedEntry.options.InstanceName);
                string instanceName   = useDefaultName
                                        ? string.Format("{0}{1}{2}", viewModel.StackName, product.Name, role.Name)
                                        .RemoveAllWhitespace()
                                        .RemoveNonAlphaNumericCharacters()
                                        : scopedEntry.options.InstanceName;

                instanceName = _numberedStringGenerator.GetNextString(instanceName);

                var instance = new Instance
                {
                    Name         = instanceName,
                    InstanceType = role.Options.InstanceType,
                    Role         = role,
                    Tags         = new List <Tag> {
                        new Tag {
                            Name = "Name", Value = instanceName
                        }
                    },
                    VpcId           = configuration.VpcId,
                    SubnetId        = configuration.SubnetId,
                    ProductName     = product.Name,
                    VersionName     = version.Name,
                    NeedsRefreshing = true,
                    OwnerProfileId  = configuration.OwnerProfileId,
                    IamRole         = entry.options.IamRole ?? version.IamRole
                };

                configuration.Instances.Add(instance);
            }

            _backgroundJobClient.Enqueue <CreateStack>(x => x.Execute(viewModel.SelectedProfileId, configuration));

            return(RedirectToAction("Index"));
        }