Exemple #1
0
        public async Task <InvokeResult> UpdateDeviceRepositoryAsync(DeviceRepository repo, EntityHeader org, EntityHeader user)
        {
            ValidationCheck(repo, Actions.Update);
            await AuthorizeAsync(repo, AuthorizeResult.AuthorizeActions.Update, user, org);

            if (!String.IsNullOrEmpty(repo.AccessKey))
            {
                var addSecretResult = await _secureStorage.AddSecretAsync(repo.AccessKey);

                if (!addSecretResult.Successful)
                {
                    return(addSecretResult.ToInvokeResult());
                }

                if (!string.IsNullOrEmpty(repo.SecureAccessKeyId))
                {
                    await _secureStorage.RemoveSecretAsync(repo.SecureAccessKeyId);
                }

                repo.SecureAccessKeyId = addSecretResult.Result;
                repo.AccessKey         = null;
            }

            await _deviceRepositoryRepo.UpdateDeviceRepositoryAsync(repo);

            return(InvokeResult.Success);
        }
        public async Task <InvokeResult <Device> > CreateDeviceAsync(DeviceRepository deviceRepo, string deviceTypeId, EntityHeader org, EntityHeader user)
        {
            if (!deviceRepo.AutoGenerateDeviceIds)
            {
                return(InvokeResult <Device> .FromError("Can only create a device without a device id and name with a repository that is configure to auto generate device ids."));
            }

            var timeStamp = DateTime.UtcNow.ToJSONString();

            var device = new Device();

            /* Note we just create it here for now then the record gets inserted we go ahead assign the name */
            device.DeviceId = $"{deviceRepo.Key}{deviceRepo.IncrementingDeviceNumber:0000000}";
            deviceRepo.IncrementingDeviceNumber++;
            deviceRepo.LastUpdatedBy   = user;
            deviceRepo.LastUpdatedDate = timeStamp;
            await _deviceRepoRepo.UpdateDeviceRepositoryAsync(deviceRepo);

            device.Name = device.DeviceId;
            device.OwnerOrganization = org;
            device.CreatedBy         = user;
            device.LastUpdatedBy     = user;
            device.CreationDate      = timeStamp;
            device.LastUpdatedDate   = timeStamp;

            if (String.IsNullOrEmpty(device.PrimaryAccessKey))
            {
                var guidBytes      = new List <byte>(Guid.NewGuid().ToByteArray());
                var timeStampBytes = new List <byte>(BitConverter.GetBytes(DateTime.Now.Ticks));
                guidBytes.AddRange(timeStampBytes);
                device.PrimaryAccessKey = System.Convert.ToBase64String(guidBytes.ToArray());
            }

            if (String.IsNullOrEmpty(device.SecondaryAccessKey))
            {
                var guidBytes      = new List <byte>(Guid.NewGuid().ToByteArray());
                var timeStampBytes = new List <byte>(BitConverter.GetBytes(DateTime.Now.Ticks));
                guidBytes.AddRange(timeStampBytes);
                device.SecondaryAccessKey = System.Convert.ToBase64String(guidBytes.ToArray());
            }

            var deviceType = await _deviceTypeRepo.GetDeviceTypeAsync(deviceTypeId);

            if (!EntityHeader.IsNullOrEmpty(deviceType.Firmware))
            {
                device.DesiredFirmware = deviceType.Firmware;
            }

            if (!EntityHeader.IsNullOrEmpty(deviceType.FirmwareRevision))
            {
                device.DesiredFirmwareRevision = deviceType.FirmwareRevision;
            }

            device.DeviceType = new EntityHeader <DeviceAdmin.Models.DeviceType>()
            {
                Id = deviceType.Id, Text = deviceType.Name, Key = deviceType.Key
            };
            device.DeviceConfiguration = deviceType.DefaultDeviceConfiguration;

            if (deviceRepo.UserOwnedDevicesOnly)
            {
                device.AssignedUser = user;
                device.OwnerUser    = user;
            }

            var result = await AddDeviceAsync(deviceRepo, device, org, user);

            if (!result.Successful)
            {
                return(InvokeResult <Device> .FromInvokeResult(result));
            }

            return(InvokeResult <Device> .Create(device));
        }
Exemple #3
0
        public async Task <InvokeResult> UpdateDeviceRepositoryAsync(DeviceRepository repo, EntityHeader org, EntityHeader user)
        {
            ValidationCheck(repo, Actions.Update);
            await AuthorizeAsync(repo, AuthorizeResult.AuthorizeActions.Update, user, org);

            if (repo.RepositoryType.Value == RepositoryTypes.NuvIoT ||
                repo.RepositoryType.Value == RepositoryTypes.AzureIoTHub)
            {
                if (String.IsNullOrEmpty(repo.DeviceArchiveStorageSettingsSecureId))
                {
                    repo.DeviceArchiveStorageSettings = new ConnectionSettings()
                    {
                        AccountId = _deviceMgmtSettings.DefaultDeviceTableStorage.AccountId,
                        AccessKey = _deviceMgmtSettings.DefaultDeviceTableStorage.AccessKey
                    };
                }

                if (String.IsNullOrEmpty(repo.PEMStorageSettingsSecureId))
                {
                    repo.PEMStorageSettings = new ConnectionSettings()
                    {
                        AccountId = _deviceMgmtSettings.DefaultDeviceTableStorage.AccountId,
                        AccessKey = _deviceMgmtSettings.DefaultDeviceTableStorage.AccessKey
                    };
                }

                if (String.IsNullOrEmpty(repo.DeviceStorageSecureSettingsId))
                {
                    repo.DeviceStorageSettings = new ConnectionSettings()
                    {
                        Uri          = _deviceMgmtSettings.DefaultDeviceStorage.Uri,
                        AccessKey    = _deviceMgmtSettings.DefaultDeviceStorage.AccessKey,
                        ResourceName = _deviceMgmtSettings.DefaultDeviceStorage.ResourceName
                    };
                }

                if (String.IsNullOrEmpty(repo.DeviceWatchdogStorageSecureId))
                {
                    repo.DeviceWatchdogStorageSettings = new ConnectionSettings()
                    {
                        Uri          = _deviceMgmtSettings.DefaultDeviceTableStorage.Uri,
                        AccessKey    = _deviceMgmtSettings.DefaultDeviceTableStorage.AccessKey,
                        ResourceName = $"Watchdlog{repo.Id}{repo.Key}"
                    };
                }
            }
            else if (repo.RepositoryType.Value == RepositoryTypes.Local)
            {
                //TODO: when we update this for remote server access, we need to figure out what if anything needs to be secured.
                if (String.IsNullOrEmpty(repo.DeviceArchiveStorageSettingsSecureId))
                {
                    repo.DeviceArchiveStorageSettings = new ConnectionSettings()
                    {
                        Uri          = "mysql",
                        ResourceName = "nuviot",
                        Port         = "3306"
                    };
                }

                if (String.IsNullOrEmpty(repo.DeviceWatchdogStorageSecureId))
                {
                    repo.DeviceWatchdogStorageSettings = new ConnectionSettings()
                    {
                        Uri          = "mysql",
                        ResourceName = "nuviot",
                        Port         = "3306"
                    };
                }

                if (String.IsNullOrEmpty(repo.PEMStorageSettingsSecureId))
                {
                    repo.PEMStorageSettings = new ConnectionSettings()
                    {
                        Uri          = "mongodb",
                        Port         = "27017",
                        ResourceName = "nuviot"
                    };
                }

                if (String.IsNullOrEmpty(repo.DeviceStorageSecureSettingsId))
                {
                    repo.DeviceStorageSettings = new ConnectionSettings()
                    {
                        Uri          = "mongodb",
                        ResourceName = "nuviot",
                        Port         = "27017"
                    };
                }
            }

            if (!string.IsNullOrEmpty(repo.AccessKey))
            {
                var addSecretResult = await _secureStorage.AddSecretAsync(org, repo.AccessKey);

                if (!addSecretResult.Successful)
                {
                    return(addSecretResult.ToInvokeResult());
                }

                if (!string.IsNullOrEmpty(repo.SecureAccessKeyId))
                {
                    await _secureStorage.RemoveSecretAsync(org, repo.SecureAccessKeyId);
                }

                repo.SecureAccessKeyId = addSecretResult.Result;
                repo.AccessKey         = null;
            }

            if (repo.DeviceArchiveStorageSettings != null)
            {
                var addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.DeviceArchiveStorageSettings));

                if (!addKeyResult.Successful)
                {
                    return(addKeyResult.ToInvokeResult());
                }

                if (!string.IsNullOrEmpty(repo.DeviceArchiveStorageSettingsSecureId))
                {
                    await _secureStorage.RemoveSecretAsync(org, repo.DeviceArchiveStorageSettingsSecureId);
                }

                repo.DeviceArchiveStorageSettingsSecureId = addKeyResult.Result;
                repo.DeviceArchiveStorageSettings         = null;
            }

            if (repo.DeviceStorageSettings != null)
            {
                var addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.DeviceStorageSettings));

                if (!addKeyResult.Successful)
                {
                    return(addKeyResult.ToInvokeResult());
                }

                if (!string.IsNullOrEmpty(repo.DeviceStorageSecureSettingsId))
                {
                    await _secureStorage.RemoveSecretAsync(org, repo.DeviceStorageSecureSettingsId);
                }

                repo.DeviceStorageSecureSettingsId = addKeyResult.Result;
                repo.DeviceStorageSettings         = null;
            }

            if (repo.PEMStorageSettings != null)
            {
                var addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.PEMStorageSettings));

                if (!addKeyResult.Successful)
                {
                    return(addKeyResult.ToInvokeResult());
                }

                if (!string.IsNullOrEmpty(repo.PEMStorageSettingsSecureId))
                {
                    await _secureStorage.RemoveSecretAsync(org, repo.PEMStorageSettingsSecureId);
                }

                repo.PEMStorageSettingsSecureId = addKeyResult.Result;
                repo.PEMStorageSettings         = null;
            }

            if (repo.DeviceWatchdogStorageSettings != null)
            {
                var addKeyResult = await _secureStorage.AddSecretAsync(org, JsonConvert.SerializeObject(repo.DeviceWatchdogStorageSettings));

                if (!addKeyResult.Successful)
                {
                    return(addKeyResult.ToInvokeResult());
                }

                if (!string.IsNullOrEmpty(repo.DeviceWatchdogStorageSecureId))
                {
                    await _secureStorage.RemoveSecretAsync(org, repo.DeviceWatchdogStorageSecureId);
                }

                repo.DeviceWatchdogStorageSecureId = addKeyResult.Result;
                repo.DeviceWatchdogStorageSettings = null;
            }

            await _deviceRepositoryRepo.UpdateDeviceRepositoryAsync(repo);

            return(InvokeResult.Success);
        }