Updating Nokia driver
[vfc/nfvo/driver/vnfm/svnfm.git] / nokiav2 / driver / src / main / java / org / onap / vfc / nfvo / driver / vnfm / svnfm / nokia / vnfm / CbamTokenProvider.java
1 /*
2  * Copyright 2016-2017, Nokia Corporation
3  *
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
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
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.
15  */
16 package org.onap.vfc.nfvo.driver.vnfm.svnfm.nokia.vnfm;
17
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;
23 import okhttp3.*;
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;
34
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;
42 import java.util.Set;
43
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;
47
48 /**
49  * Responsible for providing a token to access CBAM APIs
50  */
51 @Component
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;
70
71     @Autowired
72     CbamTokenProvider(VnfmInfoProvider vnfmInfoProvider) {
73         this.vnfmInfoProvider = vnfmInfoProvider;
74     }
75
76     /**
77      * @return the token to access CBAM APIs (ex. 123456)
78      */
79     public String getToken(String vnfmId) {
80         VnfmInfo vnfmInfo = vnfmInfoProvider.getVnfmInfo(vnfmId);
81         return getToken(vnfmInfo.getUserName(), vnfmInfo.getPassword());
82     }
83
84     private String getToken(String clientId, String clientSecret) {
85         logger.trace("Requesting token for accessing CBAM API");
86         synchronized (this) {
87             long now = SystemFunctions.systemFunctions().currentTimeMillis();
88             if (token == null || token.refreshAfter < now) {
89                 if (token == null) {
90                     logger.debug("No token: getting first token");
91                 } else {
92                     logger.debug("Token expired " + (now - token.refreshAfter) + " ms ago");
93                 }
94                 refresh(clientId, clientSecret);
95             } else {
96                 logger.debug("Token will expire in " + (now - token.refreshAfter) + " ms");
97             }
98         }
99         return token.token.accessToken;
100     }
101
102     ;
103
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++) {
118             try {
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));
125                     return;
126                 } else {
127                     throw new RuntimeException();
128                 }
129             } catch (Exception e) {
130                 lastException = e;
131                 logger.warn("Unable to get token to access CBAM API (" + (i + 1) + "/" + MAX_RETRY_COUNT + ")", e);
132             }
133         }
134         logger.error("Unable to get token to access CBAM API (giving up retries)", lastException);
135         throw new RuntimeException(lastException);
136     }
137
138     @VisibleForTesting
139     Response execute(Call call) throws IOException {
140         return call.execute();
141     }
142
143     /**
144      * - a new token is requested after the half of the time has expired till which the currently
145      * used token is valid
146      *
147      * @param token the currently held token
148      * @return the point in time after which a new token must be requested
149      */
150     private long getTokenRefreshTime(TokenResponse token) {
151         return SystemFunctions.systemFunctions().currentTimeMillis() + token.expiresIn * (1000 / 2);
152     }
153
154     private HostnameVerifier buildHostnameVerifier() {
155         if (skipHostnameVerification) {
156             return new HostnameVerifier() {
157                 @Override
158                 public boolean verify(String hostname, SSLSession session) {
159                     return true;
160                 }
161             };
162         } else {
163             return new DefaultHostnameVerifier();
164         }
165     }
166
167     @VisibleForTesting
168     SSLSocketFactory buildSSLSocketFactory() {
169         try {
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);
177         }
178     }
179
180     @VisibleForTesting
181     TrustManager[] buildTrustManager() throws KeyStoreException, NoSuchProviderException, NoSuchAlgorithmException, InvalidKeySpecException, CertificateException {
182         if (skipCertificateVerification) {
183             return new TrustManager[]{new AllTrustedTrustManager()};
184         } else {
185             if (StringUtils.isEmpty(trustedCertificates)) {
186                 throw new IllegalArgumentException("If the skipCertificateVerification is set to false (default) the trustedCertificates can not be empty");
187             }
188             Set<String> trustedPems;
189             try {
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);
193             }
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();
198
199         }
200     }
201
202     private static class CurrentToken {
203         private final TokenResponse token;
204         private final long refreshAfter;
205
206         CurrentToken(TokenResponse token, long refreshAfter) {
207             this.refreshAfter = refreshAfter;
208             this.token = token;
209         }
210     }
211
212     static class AllTrustedTrustManager implements X509TrustManager {
213         @Override
214         public void checkClientTrusted(X509Certificate[] chain, String authType) throws CertificateException {
215
216         }
217
218         @Override
219         public void checkServerTrusted(X509Certificate[] chain, String authType) throws CertificateException {
220         }
221
222         @Override
223         public X509Certificate[] getAcceptedIssuers() {
224             return new X509Certificate[0];
225         }
226     }
227
228     /**
229      * Represents the token received from CBAM
230      */
231     //FIXME use authentication swagger client instead
232     private static class TokenResponse {
233         @SerializedName("access_token")
234         String accessToken;
235         @SerializedName("expires_in")
236         int expiresIn;
237         @SerializedName("id_token")
238         String tokenId;
239         @SerializedName("not-before-policy")
240         int notBeforePolicy;
241         @SerializedName("refresh_expires_in")
242         int refreshExpiresIn;
243         @SerializedName("refresh_token")
244         String refreshToken;
245         @SerializedName("session_state")
246         String sessionState;
247         @SerializedName("token_type")
248         String tokenType;
249     }
250 }