Exemple #1
0
        private ConfigurationSet GetAndWriteLocal(string id, string env, string localFile)
        {
            ConfigurationSet configData;

            configData = ConfigCacheHelper.GetConfiguration(id, env, localFile);
            ValidateAccess(configData, env);
            return(configData);
        }
Exemple #2
0
        public static void UpdateConfigSets()
        {
            Logging.DebugMessage("Updating files");
            var configSets = Directory.GetFiles(AppDomain.CurrentDomain.BaseDirectory + "\\App_Data");

            foreach (var configSet in configSets)
            {
                var cs           = JsonConvert.DeserializeObject <ConfigWrapper>(File.ReadAllText(configSet));
                var newConfigSet = ConfigCacheHelper.GetConfiguration(cs.Id, cs.Environment, configSet, true);
                ConfigCacheHelper.UpdateCache(configSet, newConfigSet, cs);
            }
        }
Exemple #3
0
        private static void UpdateEnvironmet(string id, string name)
        {
            Logging.DebugMessage("Config set updated: {0}-{1}", id, name);
            var fileName = ConfigCacheHelper.GetLocalFileName(id, name);

            ConfigCacheHelper.GetConfiguration(id, name, fileName);
            try
            {
                var hubSender = GlobalHost.ConnectionManager.GetHubContext("notificationHub");
                var groupName = string.Format("{0}-{1}", id, name).ToLower();
                hubSender.Clients.Group(groupName).notify(id, name);
            }
            catch (Exception ex)
            {
                ex.Log("failure sending message");
            }
        }
Exemple #4
0
        public HttpResponseMessage GetHostParameter(string configSet, string environment, string host, string key)
        {
            var localFile = ConfigCacheHelper.GetLocalFileName(configSet, environment);
            ConfigurationSet configData = null;

            try
            {
                if (!File.Exists(localFile))
                {
                    configData = ConfigCacheHelper.GetConfiguration(configSet, environment, localFile);
                }
                else
                {
                    configData = ConfigCacheHelper.GetConfigFromCache(configSet, environment, localFile).Set;
                }
                ValidateAccess(configData, environment);
            }
            catch (UnauthorizedAccessException ex)
            {
                return(CreateUnauthenticatedMessage(ex));
            }
            catch (Exception ex)
            {
                return(Request.CreateErrorResponse(HttpStatusCode.BadGateway, new HttpError(ex.Message)));
            }
            if (configData == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Invalid config set" }));
            }
            var env = configData.Environments.SingleOrDefault(e => e.EnvironmentName.Equals(environment, StringComparison.OrdinalIgnoreCase));

            if (env == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Environment not defined" }));
            }
            var hostData = configData.Services.SingleOrDefault(h => h.ServiceName.Equals(host, StringComparison.OrdinalIgnoreCase));

            if (hostData == null)
            {
                return(Request.CreateResponse(HttpStatusCode.NotFound, new { Message = "Service host not defined" }));
            }
            var param = hostData.GetConfigParameter(key);

            return(Request.CreateResponse(param.ContainsCharacters() ? HttpStatusCode.OK : HttpStatusCode.NoContent, param.ContainsCharacters() ? new { Name = key, Value = param } : null));
        }
        ///// <summary>
        ///// Wraps a function that is called when a client connects to the <see cref="T:Microsoft.AspNet.SignalR.Hubs.HubDispatcher"/> for each
        /////             <see cref="T:Microsoft.AspNet.SignalR.Hubs.IHub"/> the client connects to. By default, this results in the <see cref="T:Microsoft.AspNet.SignalR.Hubs.IHub"/>'s
        /////             OnConnected method being invoked.
        ///// </summary>
        ///// <param name="connect">A function to be called when a client connects to a hub.</param>
        ///// <returns>
        ///// A wrapped function to be called when a client connects to a hub.
        ///// </returns>
        //public override Func<IHub, Task> BuildConnect(Func<IHub, Task> connect)
        //{
        //    connect = hub =>
        //        {
        //            try
        //            {
        //                var set = hub.Context.Headers["set"];
        //                var env = hub.Context.Headers["env"];
        //                Logging.DebugMessage("Connecting to: {0}", string.Format("{0}-{1}", set, env));
        //                hub.Groups.Add(hub.Context.ConnectionId, string.Format("{0}-{1}", set, env));
        //            }
        //            catch (Exception ex)
        //            {
        //                ex.Log();
        //            }
        //            return Task.FromResult(1);
        //        };
        //    return connect;
        //}

        /// <summary>
        /// Wraps a function that determines which of the groups belonging to the hub described by the <see cref="T:Microsoft.AspNet.SignalR.Hubs.HubDescriptor"/>
        ///             the client should be allowed to rejoin.
        ///             By default, clients will rejoin all the groups they were in prior to reconnecting.
        /// </summary>
        /// <param name="rejoiningGroups">A function that determines which groups the client should be allowed to rejoin.</param>
        /// <returns>
        /// A wrapped function that determines which groups the client should be allowed to rejoin.
        /// </returns>
        //public override Func<HubDescriptor, IRequest, IList<string>, IList<string>> BuildRejoiningGroups(Func<HubDescriptor, IRequest, IList<string>, IList<string>> rejoiningGroups)
        //{
        //    rejoiningGroups = (descriptor, request, arg3) =>
        //        {
        //            try
        //            {
        //                var set = request.Headers["set"];
        //                var env = request.Headers["env"];
        //                Logging.DebugMessage("reconnecting to: {0}", string.Format("{0}-{1}", set, env));
        //                return new List<string> { string.Format("{0}-{1}", set, env) };
        //            }
        //            catch (Exception ex)
        //            {
        //                ex.Log();
        //                return new List<string>();
        //            }
        //        };
        //    return rejoiningGroups;
        //}

        /// <summary>
        /// Wraps a function to be called before a client subscribes to signals belonging to the hub described by the
        ///             <see cref="T:Microsoft.AspNet.SignalR.Hubs.HubDescriptor"/>. By default, the <see cref="T:Microsoft.AspNet.SignalR.Hubs.AuthorizeModule"/> will look for attributes on the
        ///             <see cref="T:Microsoft.AspNet.SignalR.Hubs.IHub"/> to help determine if the client is authorized to subscribe to method invocations for the
        ///             described hub.
        ///             The function returns true if the client is authorized to subscribe to client-side hub method
        ///             invocations; false, otherwise.
        /// </summary>
        /// <param name="authorizeConnect">A function that dictates whether or not the client is authorized to connect to the described Hub.
        ///             </param>
        /// <returns>
        /// A wrapped function that dictates whether or not the client is authorized to connect to the described Hub.
        /// </returns>
        public override Func <HubDescriptor, IRequest, bool> BuildAuthorizeConnect(Func <HubDescriptor, IRequest, bool> authorizeConnect)
        {
            authorizeConnect = (descriptor, request) =>
            {
                try
                {
                    Logging.DebugMessage("Authorizing hub connection..");
                    var set       = request.Headers["set"];
                    var env       = request.Headers["env"];
                    var key       = request.Headers["key"];
                    var localFile = ConfigCacheHelper.GetLocalFileName(set, env);
                    var token     = ExtractToken(request);
                    var config    = ConfigCacheHelper.GetConfigFromCache(set, env, localFile);
                    if (config == null)
                    {
                        ConfigCacheHelper.GetConfiguration(set, env, localFile);
                        config = ConfigCacheHelper.GetConfigFromCache(set, env, localFile);
                    }
                    var auth = config.TryValidateToken(env, token, key);
                    if (!auth)
                    {
                        Logging.DebugMessage("Unauthorized access");
                    }

                    else
                    {
                        Logging.DebugMessage("Authorized '{0}'", request.User.Identity.Name);
                    }
                    return(auth);
                }
                catch (Exception ex)
                {
                    ex.Log();
                    return(false);
                }
            };
            return(authorizeConnect);
        }
Exemple #6
0
 // GET: Auth
 public ActionResult Index()
 {
     try
     {
         var env        = Request.Headers["env"];
         var set        = Request.Headers["set"];
         var localfile  = ConfigCacheHelper.GetLocalFileName(set, env);
         var configData = ConfigCacheHelper.GetConfigFromCache(set, env, localfile);
         if (configData == null)
         {
             ConfigCacheHelper.GetConfiguration(set, env, localfile);
             configData = ConfigCacheHelper.GetConfigFromCache(set, env, localfile);
         }
         ValidateAccess(configData.Set, env);
         return(new ContentResult {
             Content = "OK", ContentEncoding = Encoding.UTF8, ContentType = ""
         });
     }
     catch (Exception ex)
     {
         ex.Log();
         return(new HttpStatusCodeResult(HttpStatusCode.Forbidden, "Invalid credentials"));
     }
 }