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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
|
// 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.InputStream;
import java.io.OutputStream;
import java.io.UncheckedIOException;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.StandardCopyOption;
import java.util.Optional;
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 {
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 fromInputStream(InputStream in) throws IOException {
return EntityBindingsMapper.toSignedIdentityDocument(mapper.readValue(in, SignedIdentityDocumentEntity.class));
}
public static SignedIdentityDocument fromString(String json) throws IOException {
return EntityBindingsMapper.toSignedIdentityDocument(mapper.readValue(json, SignedIdentityDocumentEntity.class));
}
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()),
Optional.ofNullable(entity.clusterType()).map(ClusterType::from).orElse(null),
entity.unknownAttributes());
}
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(),
Optional.ofNullable(model.clusterType()).map(ClusterType::toConfigValue).orElse(null),
model.unknownAttributes());
}
public static SignedIdentityDocument readSignedIdentityDocumentFromFile(Path file) {
try (InputStream inputStream = Files.newInputStream(file)) {
return fromInputStream(inputStream);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
public static void writeSignedIdentityDocumentToFile(Path file, SignedIdentityDocument document) {
try {
SignedIdentityDocumentEntity entity = EntityBindingsMapper.toSignedIdentityDocumentEntity(document);
Path tempFile = file.resolveSibling(file.getFileName() + ".tmp");
try (OutputStream outputStream = Files.newOutputStream(tempFile)) {
mapper.writeValue(outputStream, entity);
}
Files.move(tempFile, file, StandardCopyOption.ATOMIC_MOVE);
} catch (IOException e) {
throw new UncheckedIOException(e);
}
}
}
|