2 * Copyright 2016-2017, Nokia Corporation
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
8 * http://www.apache.org/licenses/LICENSE-2.0
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
18 import com.google.common.annotations.VisibleForTesting;
19 import com.google.common.base.Joiner;
20 import com.google.common.io.BaseEncoding;
21 import com.google.gson.Gson;
22 import com.google.gson.annotations.SerializedName;
24 import org.apache.http.conn.ssl.DefaultHostnameVerifier;
25 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.api.VnfmInfoProvider;
26 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.StoreLoader;
27 import org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.util.SystemFunctions;
28 import org.onap.vnfmdriver.model.VnfmInfo;
29 import org.slf4j.Logger;
30 import org.springframework.beans.factory.annotation.Autowired;
31 import org.springframework.beans.factory.annotation.Value;
32 import org.springframework.stereotype.Component;
33 import org.springframework.util.StringUtils;
35 import javax.net.ssl.*;
36 import java.io.IOException;
37 import java.nio.charset.StandardCharsets;
38 import java.security.*;
39 import java.security.cert.CertificateException;
40 import java.security.cert.X509Certificate;
41 import java.security.spec.InvalidKeySpecException;
44 import static org.slf4j.LoggerFactory.getLogger;
45 import static org.springframework.http.HttpHeaders.CONTENT_TYPE;
46 import static org.springframework.http.MediaType.APPLICATION_FORM_URLENCODED_VALUE;
49 * Responsible for providing a token to access CBAM APIs
52 public class CbamTokenProvider {
53 public static final int MAX_RETRY_COUNT = 5;
54 private static final String CBAM_TOKEN_PATH = "/realms/cbam/protocol/openid-connect/token";
55 private static Logger logger = getLogger(CbamTokenProvider.class);
56 private final VnfmInfoProvider vnfmInfoProvider;
57 @Value("${cbamKeyCloakBaseUrl}")
58 private String cbamKeyCloakBaseUrl;
59 @Value("${cbamUsername}")
60 private String username;
61 @Value("${cbamPassword}")
62 private String password;
63 @Value("${trustedCertificates}")
64 private String trustedCertificates;
65 @Value("${skipCertificateVerification}")
66 private boolean skipCertificateVerification;
67 @Value("${skipHostnameVerification}")
68 private boolean skipHostnameVerification;
69 private volatile CurrentToken token;
72 CbamTokenProvider(VnfmInfoProvider vnfmInfoProvider) {
73 this.vnfmInfoProvider = vnfmInfoProvider;
77 * @return the token to access CBAM APIs (ex. 123456)
79 public String getToken(String vnfmId) {
80 VnfmInfo vnfmInfo = vnfmInfoProvider.getVnfmInfo(vnfmId);
81 return getToken(vnfmInfo.getUserName(), vnfmInfo.getPassword());
84 private String getToken(String clientId, String clientSecret) {
85 logger.trace("Requesting token for accessing CBAM API");
87 long now = SystemFunctions.systemFunctions().currentTimeMillis();
88 if (token == null || token.refreshAfter < now) {
90 logger.debug("No token: getting first token");
92 logger.debug("Token expired " + (now - token.refreshAfter) + " ms ago");
94 refresh(clientId, clientSecret);
96 logger.debug("Token will expire in " + (now - token.refreshAfter) + " ms");
99 return token.token.accessToken;
104 private void refresh(String clientId, String clientSecret) {
105 FormBody body = new FormBody.Builder()
106 .add("grant_type", "password")
107 .add("client_id", clientId)
108 .add("client_secret", clientSecret)
109 .add("username", username)
110 .add("password", password).build();
111 Request request = new Request.Builder().url(cbamKeyCloakBaseUrl + CBAM_TOKEN_PATH).addHeader(CONTENT_TYPE, APPLICATION_FORM_URLENCODED_VALUE).post(body).build();
112 OkHttpClient.Builder builder = new OkHttpClient.Builder();
113 SSLSocketFactory sslSocketFac = buildSSLSocketFactory();
114 HostnameVerifier hostnameVerifier = buildHostnameVerifier();
115 OkHttpClient client = builder.sslSocketFactory(sslSocketFac).hostnameVerifier(hostnameVerifier).build();
116 Exception lastException = null;
117 for (int i = 0; i < MAX_RETRY_COUNT; i++) {
119 Response response = execute(client.newCall(request));
120 if (response.isSuccessful()) {
121 String json = response.body().string();
122 TokenResponse tokenResponse = new Gson().fromJson(json, TokenResponse.class);
123 //token is scheduled to be refreshed in the half time before expiring
124 token = new CurrentToken(tokenResponse, getTokenRefreshTime(tokenResponse));
127 throw new RuntimeException();
129 } catch (Exception e) {
131 logger.warn("Unable to get token to access CBAM API (" + (i + 1) + "/" + MAX_RETRY_COUNT + ")", e);
134 logger.error("Unable to get token to access CBAM API (giving up retries)", lastException);
135 throw new RuntimeException(lastException);
139 Response execute(Call call) throws IOException {
140 return call.execute();
144 * - a new token is requested after the half of the time has expired till which the currently
145 * used token is valid
147 * @param token the currently held token
148 * @return the point in time after which a new token must be requested
150 private long getTokenRefreshTime(TokenResponse token) {
151 return SystemFunctions.systemFunctions().currentTimeMillis() + token.expiresIn * (1000 / 2);
154 private HostnameVerifier buildHostnameVerifier() {
155 if (skipHostnameVerification) {
156 return new HostnameVerifier() {
158 public boolean verify(String hostname, SSLSession session) {
163 return new DefaultHostnameVerifier();
168 SSLSocketFactory buildSSLSocketFactory() {
170 TrustManager[] trustManagers = buildTrustManager();
171 SSLContext sslContext = SSLContext.getInstance("TLS");
172 sslContext.init(null, trustManagers, new SecureRandom());
173 return sslContext.getSocketFactory();
174 } catch (GeneralSecurityException e) {
175 logger.error("Unable to create SSL socket factory", e);
176 throw new RuntimeException(e);
181 TrustManager[] buildTrustManager() throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, CertificateException {
182 if (skipCertificateVerification) {
183 return new TrustManager[]{new AllTrustedTrustManager()};
185 if (StringUtils.isEmpty(trustedCertificates)) {
186 throw new IllegalArgumentException("If the skipCertificateVerification is set to false (default) the trustedCertificates can not be empty");
188 Set<String> trustedPems;
190 trustedPems = StoreLoader.getCertifacates(new String(BaseEncoding.base64().decode(trustedCertificates), StandardCharsets.UTF_8));
191 } catch (Exception e) {
192 throw new RuntimeException("The trustedCertificates must be a base64 encoded collection of PEM certificates", e);
194 KeyStore keyStore = StoreLoader.loadStore(Joiner.on("\n").join(trustedPems), "password", "password");
195 TrustManagerFactory trustManagerFactory = TrustManagerFactory.getInstance(TrustManagerFactory.getDefaultAlgorithm());
196 trustManagerFactory.init(keyStore);
197 return trustManagerFactory.getTrustManagers();
202 private static class CurrentToken {
203 private final TokenResponse token;
204 private final long refreshAfter;
206 CurrentToken(TokenResponse token, long refreshAfter) {
207 this.refreshAfter = refreshAfter;
212 static class AllTrustedTrustManager implements X509TrustManager {
214 public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
219 public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
223 public X509Certificate[] getAcceptedIssuers() {
224 return new X509Certificate[0];
229 * Represents the token received from CBAM
231 //FIXME use authentication swagger client instead
232 private static class TokenResponse {
233 @SerializedName("access_token")
235 @SerializedName("expires_in")
237 @SerializedName("id_token")
239 @SerializedName("not-before-policy")
241 @SerializedName("refresh_expires_in")
242 int refreshExpiresIn;
243 @SerializedName("refresh_token")
245 @SerializedName("session_state")
247 @SerializedName("token_type")