This repository has been archived by the owner on Nov 22, 2023. It is now read-only.
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Wrap OffsetDateTime in ApiDate & update Jackson.
Changes: Update Jackson to the newest stable release (2.6.1). We wrap OffsetDateTime in an ApiDate that provides a thin wrapper around it and custom Json de/serializers. It also has static now() and parse() methods that are heavily used in tests, and equality & hashcode checks implented in the obvious way. There's a lot of mundane changes to pipe the type throughout the API, and to update tests appropriately. Remove the now unused options to set up the date format in Jackson: This makes it easier to find buggy serialization, as they get written as a number instead. - Background & Alternatives: Keywhiz uses Jackson for JSON de/serialization. When OffsetDateTime is serialized with Jackson's JSR310 module, which omits the milliseconds if they're all zero. While this is unlikely to happen, as most times in the keywhiz world are timestamps and the milliseconds are effectively random, it does make Keywhiz-fs unhappy, and unable to list secrets, or read the secret with the unsupported date format. We set the jackson date format explicitly, but that's a red herring: The JSR310 date module doesn't use this. We could explicitly set the pattern with @JsonPattern to be yyyy-MM-dd'T'mm-HH-ss.SSS'Z' which serializes dates properly, but then won't deserialize because OffsetDateTime's constructor doesn't know what timezone it's in. If we used the unquoted 'Z', the timezone will serialize as +0000, which is undesired too, but it deserializes properly. We could load a custom module into Jackson that provides custom de/serializers for OffsetDateTime, but we need to load the Jdk8 module, which provides conflicting de/serializers for OffsetDateTime. You could probably get that to work with the right ordering, but that feels awfully fragile to me (and I didn't get it to work).
- Loading branch information
1 parent
c412714
commit 9c33fdf
Showing
54 changed files
with
285 additions
and
159 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,86 @@ | ||
/* | ||
* Copyright (C) 2015 Square, Inc. | ||
* | ||
* Licensed under the Apache License, Version 2.0 (the "License"); | ||
* you may not use this file except in compliance with the License. | ||
* You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, software | ||
* distributed under the License is distributed on an "AS IS" BASIS, | ||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. | ||
* See the License for the specific language governing permissions and | ||
* limitations under the License. | ||
*/ | ||
|
||
package keywhiz.api; | ||
|
||
import com.fasterxml.jackson.core.JsonGenerator; | ||
import com.fasterxml.jackson.core.JsonParser; | ||
import com.fasterxml.jackson.databind.DeserializationContext; | ||
import com.fasterxml.jackson.databind.JsonDeserializer; | ||
import com.fasterxml.jackson.databind.JsonSerializer; | ||
import com.fasterxml.jackson.databind.SerializerProvider; | ||
import com.fasterxml.jackson.databind.annotation.JsonDeserialize; | ||
import com.fasterxml.jackson.databind.annotation.JsonSerialize; | ||
|
||
import java.io.IOException; | ||
import java.time.OffsetDateTime; | ||
import java.time.format.DateTimeFormatter; | ||
|
||
/** | ||
* This is a wrapper for the date class used in API responses so we can have a custom JSON serializer and deserializer | ||
*/ | ||
@JsonSerialize(using=ApiDate.ApiDateSerializer.class) | ||
@JsonDeserialize(using=ApiDate.ApiDateDeserializer.class) | ||
public class ApiDate { | ||
|
||
static class ApiDateSerializer extends JsonSerializer<ApiDate> { | ||
private static DateTimeFormatter formatter = DateTimeFormatter.ofPattern("yyyy-MM-dd'T'HH:mm:ss.SSS'Z'"); | ||
@Override | ||
public void serialize(ApiDate value, JsonGenerator gen, SerializerProvider serializers) throws IOException { | ||
gen.writeString(formatter.format(value.offsetDateTime)); | ||
} | ||
} | ||
|
||
static class ApiDateDeserializer extends JsonDeserializer<ApiDate> { | ||
@Override | ||
public ApiDate deserialize(JsonParser parser, DeserializationContext ctxt) throws IOException { | ||
return new ApiDate(parser.readValueAs(OffsetDateTime.class)); | ||
} | ||
} | ||
|
||
public static ApiDate parse(String s) { | ||
return new ApiDate(OffsetDateTime.parse(s)); | ||
} | ||
|
||
public static ApiDate now() { | ||
return new ApiDate(OffsetDateTime.now()); | ||
} | ||
|
||
public long toEpochSecond() { | ||
return this.offsetDateTime.toEpochSecond(); | ||
} | ||
|
||
@Override | ||
public boolean equals(Object obj) { | ||
if (obj instanceof ApiDate) { | ||
ApiDate that = (ApiDate) obj; | ||
return this.offsetDateTime.equals(that.offsetDateTime); | ||
} | ||
return false; | ||
} | ||
|
||
@Override | ||
public int hashCode() { | ||
return this.offsetDateTime.hashCode(); | ||
} | ||
|
||
public ApiDate(OffsetDateTime odt) { | ||
this.offsetDateTime = odt; | ||
} | ||
|
||
public OffsetDateTime offsetDateTime; | ||
} | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.