public SsoToken SingleSignOn(string subscriptionId, string cloudServiceName, string resourceType, string resourceName)
        {
            if (!AzureStoreAuthorization.AuthorizeRequest(this.Request.GetClientCertificate()))
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            try
            {
                using (var provider = AccountDataProvider.Instance)
                {
                    if (String.IsNullOrEmpty(cloudServiceName) || String.IsNullOrEmpty(resourceType) || String.IsNullOrEmpty(resourceName))
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }

                    var resource = provider.GetAzureStoreResource(
                        subscriptionId: subscriptionId,
                        cloudServiceName: cloudServiceName,
                        resourceType: resourceType,
                        resourceName: resourceName
                        );

                    if (resource == null)
                    {
                        Logger.ErrorFormat("SingleSignOn: Unable to find Azure Store resource {1} for subscription {0}.", subscriptionId, resourceName);
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }

                    var timestamp = DateTime.UtcNow;

                    return(new SsoToken
                    {
                        TimeStamp = timestamp.Ticks.ToString(),
                        Token = AzureStoreAuthorization.GetSsoToken(subscriptionId, cloudServiceName, resourceType, resourceName, resource.id_TenantId, timestamp)
                    });
                }
            }
            catch (Exception ex)
            {
                if (Utils.IsFatalException(ex) || ex is HttpResponseException)
                {
                    throw;
                }

                Logger.Error(
                    message: String.Format(
                        "SingleSignOn: Unable to find Azure Store resource {1} for subscription {0}.",
                        subscriptionId,
                        resourceName
                        ),
                    exception: ex
                    );

                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public HttpResponseMessage DeleteCloudService(string subscriptionId, string cloudServiceName)
        {
            if (!AzureStoreAuthorization.AuthorizeRequest(this.Request.GetClientCertificate()))
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            try
            {
                using (var provider = AccountDataProvider.Instance)
                {
                    if (String.IsNullOrEmpty(cloudServiceName))
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }

                    var resources = provider.GetAzureStoreResources(
                        subscriptionId: subscriptionId,
                        cloudServiceName: cloudServiceName
                        );

                    if (resources == null || !resources.Any())
                    {
                        Logger.ErrorFormat("DeleteCloudService: Unable to find Azure Store resources in cloud service {1} for subscription {0}.", subscriptionId, cloudServiceName);
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }

                    foreach (var resource in resources)
                    {
                        provider.DeleteAzureStoreResource(resource);
                    }

                    Logger.InfoFormat("DeleteCloudService: Azure Store resources in cloud service {1} for subscription {0} deleted.", subscriptionId, cloudServiceName);
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }
            catch (Exception ex)
            {
                if (Utils.IsFatalException(ex) || ex is HttpResponseException)
                {
                    throw;
                }

                Logger.Error(
                    message: String.Format(
                        "DeleteCloudService: Unable to find Azure Store resources in cloud service {1} for subscription {0}.",
                        subscriptionId,
                        cloudServiceName
                        ),
                    exception: ex
                    );

                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public CloudServiceOutput GetAllResourcesInCloudService(string subscriptionId, string cloudServiceName)
        {
            if (!AzureStoreAuthorization.AuthorizeRequest(this.Request.GetClientCertificate()))
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            try
            {
                using (var provider = AccountDataProvider.Instance)
                {
                    if (String.IsNullOrEmpty(cloudServiceName))
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }

                    var resources = provider.GetAzureStoreResources(
                        subscriptionId: subscriptionId,
                        cloudServiceName: cloudServiceName
                        );

                    if (resources == null || !resources.Any())
                    {
                        Logger.ErrorFormat("GetAllResourcesInCloudService: Unable to find Azure Store resources in cloud service {1} for subscription {0}.", subscriptionId, cloudServiceName);
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }

                    return(new CloudServiceOutput
                    {
                        GeoRegion = resources.First().nvc_Region,
                        Resources = new ResourceOutputList(resources.Select(resource => this.MapAzureStoreResourceToResourceOutput(resource, expandOutputItems: false)))
                    });
                }
            }
            catch (Exception ex)
            {
                if (Utils.IsFatalException(ex) || ex is HttpResponseException)
                {
                    throw;
                }

                Logger.Error(
                    message: String.Format(
                        "GetAllResourcesInCloudService: Unable to find Azure Store resources in cloud service {1} for subscription {0}.",
                        subscriptionId,
                        cloudServiceName
                        ),
                    exception: ex
                    );

                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
        public ResourceOutput GetResource(string subscriptionId, string cloudServiceName, string resourceType, string resourceName)
        {
            if (!AzureStoreAuthorization.AuthorizeRequest(this.Request.GetClientCertificate()))
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            try
            {
                using (var provider = AccountDataProvider.Instance)
                {
                    if (String.IsNullOrEmpty(cloudServiceName) || String.IsNullOrEmpty(resourceType) || String.IsNullOrEmpty(resourceName))
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }

                    var resource = provider.GetAzureStoreResource(
                        subscriptionId: subscriptionId,
                        cloudServiceName: cloudServiceName,
                        resourceType: resourceType,
                        resourceName: resourceName
                        );

                    if (resource == null)
                    {
                        Logger.ErrorFormat("GetResource: Unable to find Azure Store resource {1} for subscription {0}.", subscriptionId, resourceName);
                        throw new HttpResponseException(HttpStatusCode.NotFound);
                    }

                    return(this.MapAzureStoreResourceToResourceOutput(resource, expandOutputItems: true));
                }
            }
            catch (Exception ex)
            {
                if (Utils.IsFatalException(ex) || ex is HttpResponseException)
                {
                    throw;
                }

                Logger.Error(
                    message: String.Format(
                        "GetResource: Unable to find Azure Store resource {1} for subscription {0}.",
                        subscriptionId,
                        resourceName
                        ),
                    exception: ex
                    );

                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }
Example #5
0
        public ActionResult SingleSignOn(string subid, string cloudServiceName, string resourceType, string resourceName, long timestamp, string token)
        {
            try
            {
                using (var provider = AccountDataProvider.Instance)
                {
                    if (String.IsNullOrEmpty(cloudServiceName) || String.IsNullOrEmpty(resourceType) || String.IsNullOrEmpty(resourceName))
                    {
                        return(new HttpStatusCodeResult((int)HttpStatusCode.BadRequest));
                    }

                    var resource = provider.GetAzureStoreResource(
                        subscriptionId: subid,
                        cloudServiceName: cloudServiceName,
                        resourceType: resourceType,
                        resourceName: resourceName
                        );

                    if (resource == null || timestamp.FromTicks() < DateTime.UtcNow.AddMinutes(-10))
                    {
                        return(new HttpStatusCodeResult((int)HttpStatusCode.Forbidden));
                    }

                    if (token != AzureStoreAuthorization.GetSsoToken(subid, cloudServiceName, resourceType, resourceName, resource.id_TenantId, timestamp.FromTicks()))
                    {
                        return(new HttpStatusCodeResult((int)HttpStatusCode.Forbidden));
                    }

                    FormsAuthentication.SetAuthCookie("azurestore." + resource.id_TenantId, false);

                    return(this.RedirectToAction(
                               actionName: "Index",
                               controllerName: "Home"
                               ));
                }
            }
            catch (Exception ex)
            {
                Logger.Error(
                    message: String.Format(
                        "Single sign on failed for resource {1} and subscription {0}.",
                        subid,
                        resourceName
                        ),
                    exception: ex
                    );

                throw;
            }
        }
Example #6
0
        public HttpResponseMessage HandleSubscriptionEvent(string subscriptionId)
        {
            if (!AzureStoreAuthorization.AuthorizeRequest(this.Request.GetClientCertificate()))
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            EntityEvent entityEvent = null;

            try
            {
                entityEvent = Request.Content.ReadAsAsync <EntityEvent>().Result;

                using (var provider = AccountDataProvider.Instance)
                {
                    var resourceType = entityEvent.GetProperty("ResourceType");
                    var email        = entityEvent.GetProperty("EMail");
                    var optIn        = entityEvent.GetProperty("OptIn");

                    var subscription = provider.GetAzureStoreSubscriptionBySubscriptionId(subscriptionId);
                    if (subscription == null)
                    {
                        if (entityEvent.EntityState != EntityState.Registered)
                        {
                            Logger.ErrorFormat("HandleSubscriptionEvent: Event '{1}' received for unknown Azure store subscription {0}. Raw data:\n{2}\n", subscriptionId, entityEvent.EntityState, entityEvent.AsJson());
                            return(Request.CreateResponse(HttpStatusCode.BadRequest));
                        }

                        if (String.IsNullOrEmpty(email))
                        {
                            Logger.ErrorFormat("HandleSubscriptionEvent: Failed to register new Azure store subscription {0}. Subscription doesn't have associated email. Raw data:\n{1}\n", subscriptionId, entityEvent.AsJson());
                            return(Request.CreateResponse(HttpStatusCode.BadRequest));
                        }

                        Guid tenantId = provider.FindOrCreateTenantForUser(
                            userEmail: email,
                            tenantName: String.Format("Tenant for {0} {1} (via azure store)", email, subscriptionId)
                            );

                        subscription = new AzureStoreSubscription
                        {
                            nvc_SubscriptionId = subscriptionId,
                            id_TenantId        = tenantId
                        };
                    }

                    subscription.nvc_EntityState  = entityEvent.EntityState.ToString();
                    subscription.nvc_ResourceType = resourceType ?? subscription.nvc_ResourceType;
                    subscription.nvc_Email        = email ?? subscription.nvc_Email;
                    subscription.nvc_OptIn        = optIn ?? subscription.nvc_OptIn;
                    subscription.nvc_RawData      = entityEvent.AsJson();

                    provider.CreateOrUpdateAzureStoreSubscription(subscription);

                    Logger.InfoFormat("HandleSubscriptionEvent: Azure store subscription {0} updated. Raw data:\n{1}\n", subscriptionId, entityEvent.AsJson());
                    return(Request.CreateResponse(HttpStatusCode.OK));
                }
            }
            catch (Exception ex)
            {
                if (Utils.IsFatalException(ex) || ex is HttpResponseException)
                {
                    throw;
                }

                Logger.Error(
                    message: String.Format(
                        "HandleSubscriptionEvent: Unable to update Azure store subscription {0}. Raw data:\n{1}\n",
                        subscriptionId,
                        entityEvent != null ? entityEvent.AsJson() : "<null>"
                        ),
                    exception: ex
                    );

                return(Request.CreateResponse(HttpStatusCode.InternalServerError));
            }
        }
        public ResourceOutput CreateOrUpdateResource(string subscriptionId, string cloudServiceName, string resourceType, string resourceName)
        {
            if (!AzureStoreAuthorization.AuthorizeRequest(this.Request.GetClientCertificate()))
            {
                throw new HttpResponseException(HttpStatusCode.Forbidden);
            }

            ResourceInput resourceInput = null;

            try
            {
                resourceInput = Request.Content.ReadAsAsync <ResourceInput>().Result;

                using (var provider = AccountDataProvider.Instance)
                {
                    if (String.IsNullOrEmpty(cloudServiceName) || String.IsNullOrEmpty(resourceType) || String.IsNullOrEmpty(resourceName) || (resourceInput == null))
                    {
                        throw new HttpResponseException(HttpStatusCode.BadRequest);
                    }

                    var eTag          = resourceInput.ETag;
                    var region        = resourceInput.CloudServiceSettings.GeoRegion;
                    var plan          = resourceInput.Plan;
                    var promotionCode = resourceInput.PromotionCode;

                    var resource = provider.GetAzureStoreResource(
                        subscriptionId: subscriptionId,
                        cloudServiceName: cloudServiceName,
                        resourceType: resourceType,
                        resourceName: resourceName
                        );

                    if (resource == null)
                    {
                        var subscription = provider.GetAzureStoreSubscriptionBySubscriptionId(subscriptionId);
                        if (subscription == null)
                        {
                            Logger.ErrorFormat("CreateOrUpdateResource: Unable to find Azure Store resource {1} for subscription {0}. Raw data:\n{2}\n", subscriptionId, resourceName, resourceInput.AsJson());
                            throw new HttpResponseException(HttpStatusCode.BadRequest);
                        }

                        resource = new AzureStoreResource
                        {
                            id_TenantId          = subscription.id_TenantId,
                            nvc_SubscriptionId   = subscriptionId,
                            nvc_CloudServiceName = cloudServiceName,
                            nvc_ResourceType     = resourceType,
                            nvc_ResourceName     = resourceName,
                        };
                    }

                    if (!eTag.Equals(resource.nvc_ETag, StringComparison.OrdinalIgnoreCase))
                    {
                        resource.nvc_ETag          = eTag;
                        resource.nvc_Region        = region ?? resource.nvc_Region;
                        resource.nvc_Plan          = plan ?? resource.nvc_Plan;
                        resource.nvc_PromotionCode = promotionCode ?? resource.nvc_PromotionCode;
                        resource.nvc_RawData       = resourceInput.AsJson();

                        provider.CreateOrUpdateAzureStoreResource(resource);
                    }

                    Logger.InfoFormat("CreateOrUpdateResource: Azure Store resource {1} for subscription {0} create or updated. Raw data:\n{2}\n", subscriptionId, resourceName, resourceInput.AsJson());
                    return(this.MapAzureStoreResourceToResourceOutput(resource, expandOutputItems: true));
                }
            }
            catch (Exception ex)
            {
                if (Utils.IsFatalException(ex) || ex is HttpResponseException)
                {
                    throw;
                }

                Logger.Error(
                    message: String.Format(
                        "CreateOrUpdateResource: Unable to provision or update resource {1} for subscription {0}. Raw data:\n{2}\n",
                        subscriptionId,
                        resourceName,
                        resourceInput != null ? resourceInput.AsJson() : "<null>"
                        ),
                    exception: ex
                    );

                throw new HttpResponseException(HttpStatusCode.InternalServerError);
            }
        }