aboutsummaryrefslogtreecommitdiffstats
path: root/vespa-athenz/src/main/java/com/yahoo/vespa/athenz/identityprovider/api/EntityBindingsMapper.java
blob: c654b790836ec977501e675a0bd2014669db99a5 (plain) (blame)
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
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
// Copyright Vespa.ai. 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.AthenzIdentity;
import com.yahoo.vespa.athenz.api.AthenzService;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.DefaultSignedIdentityDocumentEntity;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.IdentityDocumentEntity;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.LegacySignedIdentityDocumentEntity;
import com.yahoo.vespa.athenz.identityprovider.api.bindings.SignedIdentityDocumentEntity;
import com.yahoo.vespa.athenz.utils.AthenzIdentities;
import com.yahoo.yolean.Exceptions;

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.Base64;
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
 * @author mortent
 */
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) {
        if (entity instanceof LegacySignedIdentityDocumentEntity docEntity) {
            IdentityDocument doc = new IdentityDocument(
                    fromDottedString(docEntity.providerUniqueId()),
                    new AthenzService(docEntity.providerService()),
                    docEntity.configServerHostname(),
                    docEntity.instanceHostname(),
                    docEntity.createdAt(),
                    docEntity.ipAddresses(),
                    IdentityType.fromId(docEntity.identityType()),
                    Optional.ofNullable(docEntity.clusterType()).map(ClusterType::from).orElse(null),
                    docEntity.ztsUrl(),
                    Optional.ofNullable(docEntity.serviceIdentity()).map(AthenzIdentities::from).orElse(null),
                    docEntity.unknownAttributes());
            return new LegacySignedIdentityDocument(
                    docEntity.signature(),
                    docEntity.signingKeyVersion(),
                    entity.documentVersion(),
                    doc);
        } else if (entity instanceof DefaultSignedIdentityDocumentEntity docEntity) {
            return new DefaultSignedIdentityDocument(docEntity.signature(),
                                                     docEntity.signingKeyVersion(),
                                                     docEntity.documentVersion(),
                                                     docEntity.data());
        } else {
            throw new IllegalArgumentException("Unknown signed identity document type: " + entity.getClass().getName());
        }
    }

    public static SignedIdentityDocumentEntity toSignedIdentityDocumentEntity(SignedIdentityDocument model) {
        if (model instanceof LegacySignedIdentityDocument legacyModel) {
            IdentityDocument idDoc = legacyModel.identityDocument();
            return new LegacySignedIdentityDocumentEntity(
                    legacyModel.signature(),
                    legacyModel.signingKeyVersion(),
                    idDoc.providerUniqueId().asDottedString(),
                    idDoc.providerService().getFullName(),
                    legacyModel.documentVersion(),
                    idDoc.configServerHostname(),
                    idDoc.instanceHostname(),
                    idDoc.createdAt(),
                    idDoc.ipAddresses(),
                    idDoc.identityType().id(),
                    Optional.ofNullable(idDoc.clusterType()).map(ClusterType::toConfigValue).orElse(null),
                    idDoc.ztsUrl(),
                    Optional.ofNullable(idDoc.serviceIdentity()).map(AthenzIdentity::getFullName).orElse(null),
                    idDoc.unknownAttributes());
        } else if (model instanceof DefaultSignedIdentityDocument defaultModel){
            return new DefaultSignedIdentityDocumentEntity(defaultModel.signature(),
                                                           defaultModel.signingKeyVersion(),
                                                           defaultModel.documentVersion(),
                                                           defaultModel.data());
        } else {
            throw new IllegalArgumentException("Unsupported model type: " + model.getClass().getName());
        }
    }

    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);
        }
    }

    public static IdentityDocument fromIdentityDocumentData(String data) {
        byte[] decoded = Base64.getDecoder().decode(data);
        IdentityDocumentEntity docEntity = Exceptions.uncheck(() -> mapper.readValue(decoded, IdentityDocumentEntity.class));
        return new IdentityDocument(
                fromDottedString(docEntity.providerUniqueId()),
                new AthenzService(docEntity.providerService()),
                docEntity.configServerHostname(),
                docEntity.instanceHostname(),
                docEntity.createdAt(),
                docEntity.ipAddresses(),
                IdentityType.fromId(docEntity.identityType()),
                Optional.ofNullable(docEntity.clusterType()).map(ClusterType::from).orElse(null),
                docEntity.ztsUrl(),
                Optional.ofNullable(docEntity.serviceIdentity()).map(AthenzIdentities::from).orElse(null),
                docEntity.unknownAttributes());
    }

    public static String toIdentityDocumentData(IdentityDocument identityDocument) {
        IdentityDocumentEntity documentEntity = new IdentityDocumentEntity(
                identityDocument.providerUniqueId().asDottedString(),
                identityDocument.providerService().getFullName(),
                identityDocument.configServerHostname(),
                identityDocument.instanceHostname(),
                identityDocument.createdAt(),
                identityDocument.ipAddresses(),
                identityDocument.identityType().id(),
                Optional.ofNullable(identityDocument.clusterType()).map(ClusterType::toConfigValue).orElse(null),
                identityDocument.ztsUrl().toString(),
                identityDocument.serviceIdentity().getFullName());
        try {
            byte[] bytes = mapper.writeValueAsBytes(documentEntity);
            return Base64.getEncoder().encodeToString(bytes);
        } catch (JsonProcessingException e) {
            throw new RuntimeException("Error during serialization of identity document.", e);
        }
    }
}