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
|
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.container.jdisc.athenz.impl;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.inject.Inject;
import com.yahoo.component.AbstractComponent;
import com.yahoo.container.core.identity.IdentityConfig;
import com.yahoo.container.jdisc.athenz.AthenzIdentityProvider;
import java.io.IOException;
import java.security.KeyPair;
/**
* @author mortent
*/
public final class AthenzIdentityProviderImpl extends AbstractComponent implements AthenzIdentityProvider {
private final ObjectMapper objectMapper = new ObjectMapper();
private InstanceIdentity instanceIdentity;
private final String dnsSuffix;
private final String providerUniqueId;
private final String domain;
private final String service;
@Inject
public AthenzIdentityProviderImpl(IdentityConfig config) throws IOException {
this(config, new ServiceProviderApi(config.loadBalancerAddress()), new AthenzService());
}
// Test only
AthenzIdentityProviderImpl(IdentityConfig config,
ServiceProviderApi serviceProviderApi,
AthenzService athenzService) throws IOException {
KeyPair keyPair = CryptoUtils.createKeyPair();
this.domain = config.domain();
this.service = config.service();
String rawDocument = serviceProviderApi.getSignedIdentityDocument();
SignedIdentityDocument document = objectMapper.readValue(rawDocument, SignedIdentityDocument.class);
this.dnsSuffix = document.dnsSuffix;
this.providerUniqueId = document.providerUniqueId;
InstanceRegisterInformation instanceRegisterInformation = new InstanceRegisterInformation(
document.providerService,
this.domain,
this.service,
rawDocument,
CryptoUtils.toPem(CryptoUtils.createCSR(domain, service, dnsSuffix, providerUniqueId, keyPair)),
true
);
instanceIdentity = athenzService.sendInstanceRegisterRequest( instanceRegisterInformation, document.ztsEndpoint);
}
@Override
public String getNToken() {
return instanceIdentity.getServiceToken();
}
@Override
public String getX509Cert() {
return instanceIdentity.getX509Certificate();
}
@Override
public String domain() {
return domain;
}
@Override
public String service() {
return service;
}
}
|