1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
|
// Copyright Yahoo. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.vespa.athenz.identityprovider.api;
import com.fasterxml.jackson.core.JsonProcessingException;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.SignedIdentityDocumentEntity;
import java.io.IOException;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.StandardCopyOption;
import static com.yahoo.vespa.athenz.identityprovider.api.VespaUniqueInstanceId.fromDottedString;
/**
* Utility class for mapping objects model types and their Jackson binding versions.
*
* @author bjorncs
*/
public class EntityBindingsMapper {
private static final ObjectMapper mapper = new ObjectMapper().registerModule(new JavaTimeModule());
private EntityBindingsMapper() {}
public static String toAttestationData(SignedIdentityDocument model) {
try {
return mapper.writeValueAsString(toSignedIdentityDocumentEntity(model));
} catch (JsonProcessingException e) {
throw new RuntimeException(e);
}
}
public static SignedIdentityDocument toSignedIdentityDocument(SignedIdentityDocumentEntity entity) {
return new SignedIdentityDocument(
entity.signature,
entity.signingKeyVersion,
fromDottedString(entity.providerUniqueId),
new AthenzService(entity.providerService),
entity.documentVersion,
entity.configServerHostname,
entity.instanceHostname,
entity.createdAt,
entity.ipAddresses,
IdentityType.fromId(entity.identityType));
}
public static SignedIdentityDocumentEntity toSignedIdentityDocumentEntity(SignedIdentityDocument model) {
return new SignedIdentityDocumentEntity(
model.signature(),
model.signingKeyVersion(),
model.providerUniqueId().asDottedString(),
model.providerService().getFullName(),
model.documentVersion(),
model.configServerHostname(),
model.instanceHostname(),
model.createdAt(),
model.ipAddresses(),
model.identityType().id());
}
public static SignedIdentityDocument readSignedIdentityDocumentFromFile(Path file) {
try {
SignedIdentityDocumentEntity entity = mapper.readValue(file.toFile(), SignedIdentityDocumentEntity.class);
return EntityBindingsMapper.toSignedIdentityDocument(entity);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static void writeSignedIdentityDocumentToFile(Path file, SignedIdentityDocument document) {
try {
SignedIdentityDocumentEntity entity = EntityBindingsMapper.toSignedIdentityDocumentEntity(document);
Path tempFile = Paths.get(file.toAbsolutePath().toString() + ".tmp");
mapper.writeValue(tempFile.toFile(), entity);
Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
|