//[OneTimeTearDown]
 public void TearDown()
 {
     DeviceRepository.Clear();
     AccessKeyRepository.Clear();
     WorkflowRepository.Clear();
     RulesRepository.Clear();
     ScheduleRepository.Clear();
 }
Exemple #2
0
        public static bool CanAccessWorkflows(string accessKey)
        {
            if (!UseAccessKeys)
            {
                return(true);
            }

            if (string.IsNullOrEmpty(accessKey))
            {
                return(false);
            }

            var accessKeyObj = AccessKeyRepository.Get(accessKey);

            return(accessKeyObj != null && accessKeyObj.IsAdmin());
        }
        public void DeleteAccessKey(string id, string accessKey = null)
        {
            if (!AccessKeyHelper.CanAdminAccessKeys(accessKey))
            {
                Response.StatusCode = StatusCodes.Status401Unauthorized;
                return;
            }

            var accessKeyObj = AccessKeyRepository.Get(id);

            if (accessKeyObj != null)
            {
                AccessKeyRepository.Delete(accessKeyObj);
            }

            Response.StatusCode = StatusCodes.Status200OK;
        }
        public JArray Get(string accessKey)
        {
            if (!AccessKeyHelper.CanAdminAccessKeys(accessKey))
            {
                Response.StatusCode = StatusCodes.Status401Unauthorized;
                return(null);
            }

            var jArray = new JArray();

            foreach (var accessKeyObj in AccessKeyRepository.Get())
            {
                jArray.Add(accessKeyObj.ToJObject());
            }

            return(jArray);
        }
Exemple #5
0
        public static bool CanAccessWorkflow(string accessKey, string workflowId)
        {
            if (!UseAccessKeys)
            {
                return(true);
            }

            if (string.IsNullOrEmpty(accessKey) || string.IsNullOrEmpty(workflowId))
            {
                return(false);
            }

            var accessKeyObj = AccessKeyRepository.Get(accessKey);

            if (accessKeyObj == null)
            {
                return(false);
            }

            return(accessKeyObj.CanAccessWorkflow(workflowId));
        }
        public JObject CreateAccessKey([FromBody] JObject body, string accessKey = null)
        {
            if (!AccessKeyHelper.CanAdminAccessKeys(accessKey))
            {
                Response.StatusCode = StatusCodes.Status401Unauthorized;
                return(null);
            }

            var name = body["name"]?.ToString();

            if (string.IsNullOrEmpty(name))
            {
                Response.StatusCode = StatusCodes.Status400BadRequest;
                return(new JObject {
                    "error", "'name' is required"
                });
            }

            var accessKeyObj = new AccessKey(body);

            AccessKeyRepository.Add(accessKeyObj);

            return(AccessKeyRepository.Get(accessKeyObj.Id).ToJObject());
        }
Exemple #7
0
        public static void Load()
        {
            var isTesting = Environment.GetEnvironmentVariable("ASPNETCORE_ENVIRONMENT") == "Test";

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MONGO_DB")))
            {
                MongoDatabase = Environment.GetEnvironmentVariable("MONGO_DB");
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MONGO_HOST")))
            {
                _mongoHost = Environment.GetEnvironmentVariable("MONGO_HOST");
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("MONGO_PORT")))
            {
                int.TryParse(Environment.GetEnvironmentVariable("MONGO_PORT"), out _mongoPort);
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("TIMEOUT")))
            {
                int.TryParse(Environment.GetEnvironmentVariable("TIMEOUT"), out _timeout);
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("RETRY_DELAY")))
            {
                int.TryParse(Environment.GetEnvironmentVariable("RETRY_DELAY"), out _retryDelay);
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("SECURITY")))
            {
                int.TryParse(Environment.GetEnvironmentVariable("SECURITY"), out var val);
                if (val == 0)
                {
                    SecurityEnabled = false;
                }
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("DEVICE_RETRIES")))
            {
                int.TryParse(Environment.GetEnvironmentVariable("DEVICE_RETRIES"), out DeviceRetries);
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("CACHING")))
            {
                if (int.TryParse(Environment.GetEnvironmentVariable("CACHING"), out var val) && val == 0)
                {
                    UseCache = false;
                }
            }

            if (UseMongo)
            {
                Console.WriteLine($"[Configuration] Configured to use mongo @ {MongoDbUrl}");
            }
            else
            {
                Console.WriteLine(
                    "[Configuration] Configured to use in-memory storage. Data will not persist across application restarts");
            }

            if (UseCache || !UseMongo)
            {
                DataAccess <AccessKey> .Initialize();

                DataAccess <IDevice> .Initialize();

                DataAccess <Rule> .Initialize();

                DataAccess <Schedule> .Initialize();

                DataAccess <Workflow> .Initialize();
            }

            if (!string.IsNullOrEmpty(Environment.GetEnvironmentVariable("RESET_AUTH")))
            {
                if (int.TryParse(Environment.GetEnvironmentVariable("RESET_AUTH"), out var val) && val == 1)
                {
                    Console.WriteLine("[auth reset flag set! clearing access keys!");
                    DataAccess <AccessKey> .Clear();
                }
            }

            if (SecurityEnabled && !AccessKeyRepository.Get().Any())
            {
                var json = new JObject {
                    { "name", "default admin" }, { "admin", true }
                };

                var accessKeyObj = new AccessKey(json);
                AccessKeyRepository.Add(accessKeyObj);

                Console.WriteLine($"[Configuration] Security is enabled. Default admin access key: {accessKeyObj.Id}");
            }

            if (isTesting && SecurityEnabled)
            {
                var accessKeyObj = AccessKeyRepository.Get().First(x => x.IsAdmin());
                Environment.SetEnvironmentVariable("ACCESSKEY", accessKeyObj.Id);
            }
        }