public async Task CanWriteReadAndDeleteContainer()
        {
            var sut       = CreateAzureBlobRdDataStorage(out var metadataStorage);
            var dataType  = nameof(DataBlob);
            var id        = "BlobStorageTestBlob";
            var dataBlob  = new DataBlob(id, new byte[] { 0x42, 0x43, 0x44 }, "myFile.bin");
            var payload   = DataEncoder.Encode(JsonConvert.SerializeObject(dataBlob));
            var submitter = "jdoe";
            var utcNow    = DateTime.UtcNow;
            var container = new GenericDataContainer(id, submitter, utcNow, submitter, utcNow, ApiVersion.Current, payload);

            if (await sut.ExistsAsync(dataType, id))
            {
                await sut.DeleteDataContainerAsync(dataType, id);
            }

            IdReservationResult idReservation = null;

            Assert.That(async() => idReservation = await sut.ReserveIdAsync(dataType, id, submitter), Throws.Nothing);
            Assert.That(idReservation.IsReserved, Is.True);

            Assert.That(() => sut.StoreAsync(dataType, container, true), Throws.Nothing);
            metadataStorage.Setup(x => x.GetFromIdAsync(dataType, id)).Returns(Task.FromResult(container));
            Assert.That(await sut.ExistsAsync(dataType, id), Is.True);

            GenericDataContainer retreivedContainer = null;

            Assert.That(async() => retreivedContainer = await sut.GetFromIdAsync(dataType, id), Throws.Nothing);
            Assert.That(retreivedContainer, Is.Not.Null);

            Assert.That(async() => await sut.DeleteDataContainerAsync(dataType, id), Throws.Nothing);
            metadataStorage.Setup(x => x.GetFromIdAsync(dataType, id)).Returns(Task.FromResult(default(GenericDataContainer)));
            Assert.That(await sut.ExistsAsync(dataType, id), Is.False);
        }
Ejemplo n.º 2
0
        public void StatementFromContainerAsExpected()
        {
            var dataType       = "SqlUnitTestObject1";
            var createdTimeUtc = DateTime.Parse("2019-08-27 01:02:03");
            var container      = new GenericDataContainer(
                "1234",
                "jdoe",
                createdTimeUtc,
                "jdoe",
                createdTimeUtc,
                ApiVersion.Current,
                DataEncoder.Encode(JsonConvert.SerializeObject(new { Name = "Doe", Age = 101.4, TimeUtc = createdTimeUtc })));
            var actual   = SqlUpdateStatement.CreateFromContainer(dataType, container);
            var expected = $"UPDATE {dataType} SET Submitter='jdoe', SubmissionTimeUtc='2019-08-27 01:02:03', Data#Name='Doe', Data#Age='101.4', Data#TimeUtc='2019-08-27 01:02:03' WHERE Id='1234'";

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 3
0
        public void StatementAsExpected()
        {
            var dataType       = "SqlUnitTestObject1";
            var createdTimeUtc = DateTime.Parse("2019-08-27 01:02:03");
            var container      = new GenericDataContainer(
                "1234",
                "jdoe",
                createdTimeUtc,
                "jdoe",
                createdTimeUtc,
                ApiVersion.Current,
                DataEncoder.Encode(JsonConvert.SerializeObject(new { Name = "Doe", Age = 101.4, TimeUtc = createdTimeUtc })));
            var actual   = SqlInsertStatement.CreateFromContainer(dataType, container);
            var expected = $"INSERT INTO {dataType} (Id, OriginalSubmitter, CreatedTimeUtc, Submitter, SubmissionTimeUtc, Data#Name, Data#Age, Data#TimeUtc) "
                           + "VALUES ('1234', 'jdoe', '2019-08-27 01:02:03', 'jdoe', '2019-08-27 01:02:03', 'Doe', '101.4', '2019-08-27 01:02:03')";

            Assert.That(actual, Is.EqualTo(expected));
        }
Ejemplo n.º 4
0
        private static void RunAlgorithm(DataEncoder b)
        {
            string         buffer = "";
            ConsoleKeyInfo key    = new ConsoleKeyInfo();

            Console.WriteLine("Begin typing!");
            bool mode = true;

            do
            {
                Console.WriteLine("Mode: {0}", mode ? "encode" : "decode");
                key = Console.ReadKey();
                Console.Clear();
                if (key.Key == ConsoleKey.PageUp)
                {
                    mode = !mode;
                    continue;
                }
                if (key.Key == ConsoleKey.Backspace && buffer.Length > 0)
                {
                    buffer = buffer.Substring(0, buffer.Length - 1);
                }
                else
                {
                    buffer += key.KeyChar;
                }

                string buHex = string.Join("", Encoding.UTF8.GetBytes(buffer).Select(x => x.ToString("X2")));

                if (mode)
                {
                    var    encBytes = b.Encode(Encoding.UTF8.GetBytes(buffer));
                    string encoded  = Encoding.ASCII.GetString(encBytes);
                    string enHex    = string.Join("", encBytes.Select(x => x.ToString("X2")));

                    var    decBytes = b.Decode(encBytes);
                    string decoded  = Encoding.UTF8.GetString(decBytes);
                    string deHex    = string.Join("", decBytes.Select(x => x.ToString("X2")));
                    Console.WriteLine("Algorithm: {7}\nInput: {0}\n -> Hex: {5}\n\nEncoded: {1}\n -> Hex: {3}\nDecoded: {2}\n -> Hex: {4}\n\n--> Success: {6}",
                                      buffer,
                                      encoded,
                                      decoded,
                                      enHex,
                                      deHex,
                                      buHex,
                                      buffer.Equals(decoded),
                                      b.GetType().Name
                                      );
                }
                else
                {
                    var    buBytes  = Encoding.ASCII.GetBytes(buffer);
                    var    decBytes = b.Decode(buBytes);
                    string decoded  = Encoding.UTF8.GetString(decBytes);
                    string deHex    = string.Join("", decBytes.Select(x => x.ToString("X2")));

                    var    encBytes = b.Encode(decBytes);
                    string encoded  = Encoding.ASCII.GetString(encBytes);
                    string enHex    = string.Join("", encBytes.Select(x => x.ToString("X2")));
                    Console.WriteLine("Algorithm: {7}\nInput: {0}\n -> Hex: {5}\n\nDecoded: {2}\n -> Hex: {4}\nEncoded: {1}\n -> Hex: {3}\n\n--> Success: {6}",
                                      buffer,
                                      encoded,
                                      decoded,
                                      enHex,
                                      deHex,
                                      buHex,
                                      buffer.Equals(decoded),
                                      b.GetType().Name
                                      );
                }
            } while (key.Key != ConsoleKey.Escape);
        }
Ejemplo n.º 5
0
        public async Task <IActionResult> Submit([FromBody] SubmitDataBody body)
        {
            if (!NamingConventions.IsValidDataType(body.DataType))
            {
                return(BadRequest($"Data type '{body.DataType}' is not a valid name for a collection"));
            }
            if (body.Data == null)
            {
                return(BadRequest("No data submitted"));
            }

            // Authorize
            var loggedInUsername    = UsernameNormalizer.Normalize(HttpContext.User.Identity.Name);
            var resourceDescription = new SubmitDataResourceDescription(body.DataType);
            var authorizationResult = await authorizationModule.AuthorizeAsync(resourceDescription, loggedInUsername);

            if (!authorizationResult.IsAuthorized)
            {
                return(StatusCode((int)HttpStatusCode.Unauthorized, "Not authorized"));
            }

            IRdDataStorage rdDataStorage;

            try
            {
                rdDataStorage = await dataRouter.GetSourceSystemAsync(body.DataType);
            }
            catch (KeyNotFoundException)
            {
                return(BadRequest($"No data storage backend for data type '{body.DataType}'"));
            }

            // Validate
            var suggestedId = await idPolicy.DetermineIdAsync(body, loggedInUsername, rdDataStorage);

            if (!rdDataStorage.IsValidId(suggestedId.Id))
            {
                return(BadRequest($"The ID '{suggestedId.Id}' for object of type '{body.DataType}' is not valid"));
            }
            if (body.Overwrite)
            {
                if (string.IsNullOrEmpty(suggestedId.Id))
                {
                    return(BadRequest("Overwriting of existing data is requested, but no ID is provided."));
                }
            }
            var validators = await validatorManager.GetApprovedValidators(body.DataType);

            foreach (var validator in validators)
            {
                try
                {
                    var validationResult = validator.Validate(body.Data.ToString());
                    if (!validationResult.IsValid)
                    {
                        return(new ContentResult
                        {
                            ContentType = Conventions.JsonContentType,
                            Content = JsonConvert.SerializeObject(validationResult),
                            StatusCode = (int)HttpStatusCode.BadRequest
                        });
                    }
                }
                catch (Exception e)
                {
                    apiEventLogger.Log(LogLevel.Error, e.ToString());
                    return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message));
                }
            }

            var existingData = await GetExistingMetadataAsync(rdDataStorage, body.DataType, suggestedId.Id);

            if (body.Overwrite)
            {
                if (!await IsAllowedToOverwriteDataAsync(body.DataType, existingData, authorizationResult.User))
                {
                    return(StatusCode((int)HttpStatusCode.Unauthorized, "Data from other users cannot be overwritten, unless you're an admin"));
                }
            }
            else if (existingData != null && !suggestedId.HasBeenReserved)
            {
                return(Conflict($"Document of type '{body.DataType}' with ID '{suggestedId.Id}' already exists"));
            }

            // Provide
            var apiVersion    = ApiVersion.Current;
            var data          = DataEncoder.Encode(JsonConvert.SerializeObject(body.Data));
            var utcNow        = DateTime.UtcNow;
            var dataContainer = new GenericDataContainer(
                suggestedId.Id,
                existingData?.OriginalSubmitter ?? authorizationResult.User.UserName,
                existingData?.CreatedTimeUtc ?? utcNow,
                authorizationResult.User.UserName,
                utcNow,
                apiVersion,
                data);

            try
            {
                var storeResult = await rdDataStorage.StoreAsync(body.DataType, dataContainer, body.Overwrite || suggestedId.HasBeenReserved);

                apiEventLogger.Log(LogLevel.Info, $"User '{authorizationResult.User.UserName}' has submitted data of type '{body.DataType}' with ID '{suggestedId.Id}'");
                await subscriptionManager.NotifyDataChangedAsync(body.DataType, suggestedId.Id, storeResult.ModificationType);

                if (storeResult.IsNewCollection)
                {
                    newCollectionTasks.PerformTasks(body.DataType, authorizationResult.User);
                }
                return(new ContentResult
                {
                    ContentType = "text/plain",
                    Content = storeResult.Id,
                    StatusCode = (int)HttpStatusCode.OK
                });
            }
            catch (DocumentAlreadyExistsException)
            {
                return(Conflict($"Document of type '{body.DataType}' with ID '{suggestedId.Id}' already exists"));
            }
            catch (Exception e)
            {
                apiEventLogger.Log(LogLevel.Error, e.ToString());
                return(StatusCode((int)HttpStatusCode.InternalServerError, e.Message));
            }
        }
Ejemplo n.º 6
0
 public GenericDataContainer(
     string originalSubmitter,
     DateTime createdTimeUtc,
     string submitter,
     DateTime submissionTimeUtc,
     string apiVersion,
     IId obj)
     : this(obj.Id, originalSubmitter, createdTimeUtc, submitter, submissionTimeUtc, apiVersion, DataEncoder.Encode(obj))
 {
 }