/
CertificateServiceImpl.java
243 lines (195 loc) · 9.02 KB
/
CertificateServiceImpl.java
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
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
/*******************************************************************************
* Copyright (c) 2017 Eurotech and/or its affiliates and others
*
* All rights reserved. This program and the accompanying materials
* are made available under the terms of the Eclipse Public License v1.0
* which accompanies this distribution, and is available at
* http://www.eclipse.org/legal/epl-v10.html
*
* Contributors:
* Eurotech - initial API and implementation
*******************************************************************************/
package org.eclipse.kapua.service.certificate.internal;
import com.google.common.base.Strings;
import com.google.common.collect.Lists;
import com.google.common.collect.Sets;
import org.eclipse.kapua.KapuaException;
import org.eclipse.kapua.commons.security.KapuaSecurityUtils;
import org.eclipse.kapua.commons.util.ArgumentValidator;
import org.eclipse.kapua.commons.util.KapuaFileUtils;
import org.eclipse.kapua.locator.KapuaLocator;
import org.eclipse.kapua.locator.KapuaProvider;
import org.eclipse.kapua.model.config.metatype.KapuaTad;
import org.eclipse.kapua.model.config.metatype.KapuaTicon;
import org.eclipse.kapua.model.config.metatype.KapuaTocd;
import org.eclipse.kapua.model.domain.Actions;
import org.eclipse.kapua.model.id.KapuaId;
import org.eclipse.kapua.model.query.KapuaQuery;
import org.eclipse.kapua.service.authorization.AuthorizationService;
import org.eclipse.kapua.service.authorization.permission.PermissionFactory;
import org.eclipse.kapua.service.certificate.Certificate;
import org.eclipse.kapua.service.certificate.CertificateCreator;
import org.eclipse.kapua.service.certificate.CertificateDomains;
import org.eclipse.kapua.service.certificate.CertificateFactory;
import org.eclipse.kapua.service.certificate.CertificateGenerator;
import org.eclipse.kapua.service.certificate.CertificateListResult;
import org.eclipse.kapua.service.certificate.CertificateService;
import org.eclipse.kapua.service.certificate.CertificateUsage;
import org.eclipse.kapua.service.certificate.KeyUsage;
import org.eclipse.kapua.service.certificate.KeyUsageSetting;
import org.eclipse.kapua.service.certificate.exception.KapuaCertificateErrorCodes;
import org.eclipse.kapua.service.certificate.exception.KapuaCertificateException;
import org.eclipse.kapua.service.certificate.internal.setting.KapuaCertificateSetting;
import org.eclipse.kapua.service.certificate.internal.setting.KapuaCertificateSettingKeys;
import org.eclipse.kapua.service.certificate.util.CertificateUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Collections;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.xml.namespace.QName;
@KapuaProvider
public class CertificateServiceImpl implements CertificateService {
public static final Logger LOG = LoggerFactory.getLogger(CertificateServiceImpl.class);
private static final KapuaLocator LOCATOR = KapuaLocator.getInstance();
private static final AuthorizationService AUTHORIZATION_SERVICE = LOCATOR.getService(AuthorizationService.class);
private static final PermissionFactory PERMISSION_FACTORY = LOCATOR.getFactory(PermissionFactory.class);
private static final CertificateFactory CERTIFICATE_FACTORY = LOCATOR.getFactory(CertificateFactory.class);
private String certificate;
private String privateKey;
/**
* Constructor
*/
public CertificateServiceImpl() throws KapuaException {
KapuaSecurityUtils.doPrivileged(() -> {
KapuaCertificateSetting setting = KapuaCertificateSetting.getInstance();
String privateKeyPath = setting.getString(KapuaCertificateSettingKeys.CERTIFICATE_JWT_PRIVATE_KEY);
String certificatePath = setting.getString(KapuaCertificateSettingKeys.CERTIFICATE_JWT_CERTIFICATE);
if (Strings.isNullOrEmpty(privateKeyPath) && Strings.isNullOrEmpty(certificatePath)) {
LOG.error("No private key and certificate path specified.\nPlease set authentication.session.jwt.private.key and authentication.session.jwt.certificate system properties.");
throw new KapuaCertificateException(KapuaCertificateErrorCodes.CERTIFICATE_ERROR);
} else {
certificate = CertificateUtils.readCertificateAsString(KapuaFileUtils.getAsFile(certificatePath));
privateKey = CertificateUtils.readPrivateKeyAsString(KapuaFileUtils.getAsFile(privateKeyPath));
}
});
}
@Override
public Certificate create(CertificateCreator creator) throws KapuaException {
throw new UnsupportedOperationException();
}
@Override
public Certificate find(KapuaId scopeId, KapuaId entityId) throws KapuaException {
throw new UnsupportedOperationException();
}
@Override
public CertificateListResult query(KapuaQuery<Certificate> query) throws KapuaException {
//
// Argument Validation
ArgumentValidator.notNull(query, "query");
//
// Check Access
AUTHORIZATION_SERVICE.checkPermission(PERMISSION_FACTORY.newPermission(CertificateDomains.CERTIFICATE_DOMAIN, Actions.write, query.getScopeId()));
//
// Create the default certificate
CertificateUsage jwtCertificateUsage = new CertificateUsageImpl("JWT");
Set<CertificateUsage> certificateUsages = Sets.newHashSet(jwtCertificateUsage);
KeyUsageSetting keyUsageSetting = new KeyUsageSettingImpl();
keyUsageSetting.setKeyUsage(KeyUsage.DIGITAL_SIGNATURE);
keyUsageSetting.setAllowed(true);
keyUsageSetting.setKapuaAllowed(true);
KapuaCertificateSetting setting = KapuaCertificateSetting.getInstance();
Certificate kapuaCertificate = new CertificateImpl(KapuaId.ONE);
kapuaCertificate.setPrivateKey(privateKey);
kapuaCertificate.setCertificate(certificate);
kapuaCertificate.getKeyUsageSettings().add(keyUsageSetting);
kapuaCertificate.setCertificateUsages(certificateUsages);
kapuaCertificate.setPassword(setting.getString(KapuaCertificateSettingKeys.CERTIFICATE_JWT_PRIVATE_KEY_PASSWORD));
CertificateListResult result = CERTIFICATE_FACTORY.newListResult();
result.addItems(Lists.newArrayList(kapuaCertificate));
return result;
}
@Override
public long count(KapuaQuery<Certificate> query) throws KapuaException {
throw new UnsupportedOperationException();
}
@Override
public void delete(KapuaId scopeId, KapuaId entityId) throws KapuaException {
throw new UnsupportedOperationException();
}
@Override
public Certificate findByName(String name) throws KapuaException {
throw new UnsupportedOperationException();
}
@Override
public Certificate update(Certificate entity) throws KapuaException {
throw new UnsupportedOperationException();
}
@Override
public Certificate generate(CertificateGenerator generator) throws KapuaException {
throw new UnsupportedOperationException();
}
@Override
public List<Certificate> findAncestorsCertificates(KapuaId scopeId, CertificateUsage usage) {
throw new UnsupportedOperationException();
}
@Override
public KapuaTocd getConfigMetadata(KapuaId scopeId) throws KapuaException {
return EmptyTocd.INSTANCE;
}
@Override
public Map<String, Object> getConfigValues(KapuaId scopeId) throws KapuaException {
return Collections.emptyMap();
}
@Override
public void setConfigValues(KapuaId scopeId, KapuaId parentId, Map<String, Object> values) throws KapuaException {
throw new UnsupportedOperationException();
}
public static class EmptyTocd implements KapuaTocd {
private static final EmptyTocd INSTANCE = new EmptyTocd();
private EmptyTocd() {}
@Override
public void setOtherAttributes(Map<QName, String> otherAttributes) {}
@Override
public void setName(String value) {}
@Override
public void setId(String value) {}
@Override
public void setIcon(List<? extends KapuaTicon> icon) {}
@Override
public void setDescription(String value) {}
@Override
public void setAny(List<Object> any) {}
@Override
public void setAD(List<? extends KapuaTad> icon) {}
@Override
public Map<QName, String> getOtherAttributes() {
return Collections.emptyMap();
}
@Override
public String getName() {
return CertificateService.class.getSimpleName();
}
@Override
public String getId() {
return CertificateService.class.getName();
}
@Override
public List<KapuaTicon> getIcon() {
return Collections.emptyList();
}
@Override
public String getDescription() {
return null;
}
@Override
public List<Object> getAny() {
return Collections.emptyList();
}
@Override
public List<KapuaTad> getAD() {
return Collections.emptyList();
}
}
}