hashicorp/vault-guides

encryption/vault-transit-rewrap throws an error when parsing dates from RandomUser

jkodroff opened this issue · 5 comments

I get the following error when running the Transit Rewrap example:

Unhandled Exception: Newtonsoft.Json.JsonReaderException: Unexpected character encountered while parsing value: {. Path 'results[0].dob', line 1, position 664.
   at Newtonsoft.Json.JsonTextReader.ReadStringValue(ReadType readType)
   at Newtonsoft.Json.JsonTextReader.ReadAsString()
   at Newtonsoft.Json.JsonReader.ReadForType(JsonContract contract, Boolean hasConverter)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateList(IList list, JsonReader reader, JsonArrayContract contract, JsonProperty containerProperty, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateList(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, Object existingValue, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.SetPropertyValue(JsonProperty property, JsonConverter propertyConverter, JsonContainerContract containerContract, JsonProperty containerProperty, JsonReader reader, Object target)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.PopulateObject(Object newObject, JsonReader reader, JsonObjectContract contract, JsonProperty member, String id)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.CreateObject(JsonReader reader, Type objectType, JsonContract contract, JsonProperty member, JsonContainerContract containerContract, JsonProperty containerMember, Object existingValue)
   at Newtonsoft.Json.Serialization.JsonSerializerInternalReader.Deserialize(JsonReader reader, Type objectType, Boolean checkAdditionalContent)
   at Newtonsoft.Json.JsonSerializer.DeserializeInternal(JsonReader reader, Type objectType)
   at Newtonsoft.Json.JsonConvert.DeserializeObject(String value, Type type, JsonSerializerSettings settings)
   at Newtonsoft.Json.JsonConvert.DeserializeObject[T](String value, JsonSerializerSettings settings)
   at RewrapExample.WebHelper.<GetUserRecordsAsync>d__2.MoveNext() in /Users/jkodroff/src/vault-guides/encryption/vault-transit-rewrap/WebHelper.cs:line 36
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at System.Runtime.CompilerServices.TaskAwaiter`1.GetResult()
   at RewrapExample.Program.<SeedDB>d__3.MoveNext() in /Users/jkodroff/src/vault-guides/encryption/vault-transit-rewrap/Program.cs:line 60
--- End of stack trace from previous location where exception was thrown ---
   at System.Runtime.ExceptionServices.ExceptionDispatchInfo.Throw()
   at System.Runtime.CompilerServices.TaskAwaiter.HandleNonSuccessAndDebuggerNotification(Task task)
   at RewrapExample.Program.Main(String[] args) in /Users/jkodroff/src/vault-guides/encryption/vault-transit-rewrap/Program.cs:line 40

And here's what I believe is the relevant part of the JSON payload:

      "dob": {
        "date": "1993-06-09T14:45:48Z",
        "age": 25
      },

I've verified that JSONLint thinks the payload is valid JSON.

Unfortunately, adding this to Program.Main() did not help:

            JsonConvert.DefaultSettings = () => new JsonSerializerSettings
            {
                DateParseHandling = DateParseHandling.None
            };

@norhe Any clue how to fix this?

@norhe @yhyakuna Let me know if you need any additional contextual information. I did a lot of years in C#, so I'm comfortable messin' with the code if you need me to dig deeper.

@norhe @jkodroff

I verified that the my_app database was correctly created, and user_data table was created except the table is empty.

mysql> show tables;
+------------------+
| Tables_in_my_app |
+------------------+
| user_data        |
+------------------+
1 row in set (0.00 sec)

mysql> show columns from user_data;
+------------+--------------+------+-----+---------+----------------+
| Field      | Type         | Null | Key | Default | Extra          |
+------------+--------------+------+-----+---------+----------------+
| user_id    | int(11)      | NO   | PRI | NULL    | auto_increment |
| user_name  | varchar(256) | NO   |     | NULL    |                |
| first_name | varchar(256) | YES  |     | NULL    |                |
| last_name  | varchar(256) | YES  |     | NULL    |                |
| address    | varchar(256) | NO   |     | NULL    |                |
| city       | varchar(256) | NO   |     | NULL    |                |
| state      | varchar(256) | NO   |     | NULL    |                |
| postcode   | varchar(256) | NO   |     | NULL    |                |
| email      | varchar(256) | NO   |     | NULL    |                |
| dob        | varchar(256) | YES  |     | NULL    |                |
+------------+--------------+------+-----+---------+----------------+
10 rows in set (0.00 sec)

mysql> select * from user_data;
Empty set (0.00 sec)

This means that the DBHelper.InsertRecordAsync method failed. Though I don't see anything wrong with the random user web service. All the data seems to be there...

Web service

My guess is the WebHelper.GetUserRecordsAsync method is not properly constructing the record? I know this worked before, so I'm not sure what's going on... :(

Finally, I figured it out! DOB is not a string as defined in Record.cs. This used to work, so maybe the web service added the date and age fields?

"dob": {
         "date": "1991-11-07T11:41:41Z
         "age": 27
 },

Fixed in PR 82. I'm updating the guide instruction as well.