summaryrefslogtreecommitdiffstats
path: root/src/main/java/org/onap/dmaap/dmf/mr/service/impl/ApiKeysServiceImpl.java
blob: d6fd89e97c52128301d95233418ae54b8a7fcba3 (plain)
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
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
/*******************************************************************************
 *  ============LICENSE_START=======================================================
 *  org.onap.dmaap
 *  ================================================================================
 *  Copyright © 2017 AT&T Intellectual Property. All rights reserved.
 *  ================================================================================
 *  Licensed under the Apache License, Version 2.0 (the "License");
 *  you may not use this file except in compliance with the License.
 *  You may obtain a copy of the License at
 *        http://www.apache.org/licenses/LICENSE-2.0
*  
 *  Unless required by applicable law or agreed to in writing, software
 *  distributed under the License is distributed on an "AS IS" BASIS,
 *  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 *  See the License for the specific language governing permissions and
 *  limitations under the License.
 *  ============LICENSE_END=========================================================
 *  
 *  ECOMP is a trademark and service mark of AT&T Intellectual Property.
 *  
 *******************************************************************************/
package org.onap.dmaap.dmf.mr.service.impl;

import java.io.IOException;

import org.json.JSONArray;
import org.json.JSONObject;
import org.springframework.stereotype.Service;

import org.onap.dmaap.dmf.mr.beans.ApiKeyBean;
import org.onap.dmaap.dmf.mr.beans.DMaaPContext;
import org.onap.dmaap.dmf.mr.constants.CambriaConstants;
import org.onap.dmaap.dmf.mr.security.DMaaPAuthenticatorImpl;
import org.onap.dmaap.dmf.mr.service.ApiKeysService;
import org.onap.dmaap.dmf.mr.utils.ConfigurationReader;
import org.onap.dmaap.dmf.mr.utils.DMaaPResponseBuilder;
import org.onap.dmaap.dmf.mr.utils.Emailer;
import com.att.eelf.configuration.EELFLogger;
import com.att.eelf.configuration.EELFManager;
import com.att.nsa.configs.ConfigDbException;
import com.att.nsa.drumlin.service.standards.HttpStatusCodes;
import com.att.nsa.security.NsaApiKey;
import com.att.nsa.security.ReadWriteSecuredResource.AccessDeniedException;
import com.att.nsa.security.db.NsaApiDb;
import com.att.nsa.security.db.NsaApiDb.KeyExistsException;
import com.att.nsa.security.db.simple.NsaSimpleApiKey;

/**
 * Implementation of the ApiKeysService, this will provide the below operations,
 * getAllApiKeys, getApiKey, createApiKey, updateApiKey, deleteApiKey
 * 
 * @author nilanjana.maity
 */
@Service
public class ApiKeysServiceImpl implements ApiKeysService {

	
	private static final EELFLogger log = EELFManager.getInstance().getLogger(ApiKeysServiceImpl.class.toString());
	/**
	 * This method will provide all the ApiKeys present in kafka server.
	 * 
	 * @param dmaapContext
	 * @throws ConfigDbException
	 * @throws IOException
	 */
	public void getAllApiKeys(DMaaPContext dmaapContext)
			throws ConfigDbException, IOException {

		ConfigurationReader configReader = dmaapContext.getConfigReader();

		log.info("configReader : " + configReader.toString());

		final JSONObject result = new JSONObject();
		final JSONArray keys = new JSONArray();
		result.put("apiKeys", keys);

		NsaApiDb<NsaSimpleApiKey> apiDb = configReader.getfApiKeyDb();

		for (String key : apiDb.loadAllKeys()) {
			keys.put(key);
		}
		log.info("========== ApiKeysServiceImpl: getAllApiKeys: Api Keys are : "
				+ keys.toString() + "===========");
		DMaaPResponseBuilder.respondOk(dmaapContext, result);
	}

	/**
	 * @param dmaapContext
	 * @param apikey
	 * @throws ConfigDbException
	 * @throws IOException
	 */
	@Override
	public void getApiKey(DMaaPContext dmaapContext, String apikey)
			throws ConfigDbException, IOException {

		String errorMsg = "Api key name is not mentioned.";
		int errorCode = HttpStatusCodes.k400_badRequest;
		
		if (null != apikey) {
			NsaSimpleApiKey simpleApiKey = getApiKeyDb(dmaapContext)
					.loadApiKey(apikey);
			
		
			if (null != simpleApiKey) {
				JSONObject result = simpleApiKey.asJsonObject();
				DMaaPResponseBuilder.respondOk(dmaapContext, result);
				log.info("========== ApiKeysServiceImpl: getApiKey : "
						+ result.toString() + "===========");
				return;
			} else {
				errorMsg = "Api key [" + apikey + "] does not exist.";
				errorCode = HttpStatusCodes.k404_notFound;
				log.info("========== ApiKeysServiceImpl: getApiKey: Error : API Key does not exist. "
						+ "===========");
				DMaaPResponseBuilder.respondWithError(dmaapContext, errorCode,
						errorMsg);
				throw new IOException();
			}
		}

	}

	/**
	 * @param dmaapContext
	 * @param nsaApiKey
	 * @throws KeyExistsException
	 * @throws ConfigDbException
	 * @throws IOException
	 */
	@Override
	public void createApiKey(DMaaPContext dmaapContext, ApiKeyBean nsaApiKey)
			throws KeyExistsException, ConfigDbException, IOException {

		log.debug("TopicService: : createApiKey....");
		
			String contactEmail = nsaApiKey.getEmail();
			final boolean emailProvided = contactEmail != null && contactEmail.length() > 0 && contactEmail.indexOf("@") > 1 ;
			 String kSetting_AllowAnonymousKeys= com.att.ajsc.filemonitor.AJSCPropertiesMap.getProperty(CambriaConstants.msgRtr_prop,"apiKeys.allowAnonymous");
			 if(null==kSetting_AllowAnonymousKeys) {
				 kSetting_AllowAnonymousKeys ="false";
			 }
	    
			 if ( kSetting_AllowAnonymousKeys.equalsIgnoreCase("true")    &&  !emailProvided   )
	      {
	        DMaaPResponseBuilder.respondWithErrorInJson(dmaapContext, 400, "You must provide an email address.");
	        return;
	      }
		
		
		final NsaApiDb<NsaSimpleApiKey> apiKeyDb = getApiKeyDb(dmaapContext);
		String apiKey = nsaApiKey.getKey();
		String sharedSecret = nsaApiKey.getSharedSecret();
		final NsaSimpleApiKey key = apiKeyDb.createApiKey(apiKey,
				sharedSecret);
		if (null != key) {

			if (null != nsaApiKey.getEmail()) {
				key.setContactEmail(nsaApiKey.getEmail());
			}

			if (null != nsaApiKey.getDescription()) {
				key.setDescription(nsaApiKey.getDescription());
			}

			log.debug("=======ApiKeysServiceImpl: createApiKey : saving api key : "
					+ key.toString() + "=====");
			apiKeyDb.saveApiKey(key);
			
			// email out the secret to validate the email address
			if ( emailProvided )
			{
				String body = "\n" + "Your email address was provided as the creator of new API key \""
				+ apiKey + "\".\n" + "\n" + "If you did not make this request, please let us know."
				 + "but don't worry -"
				+ " the API key is useless without the information below, which has been provided "
				+ "only to you.\n" + "\n\n" + "For API key \"" + apiKey + "\", use API key secret:\n\n\t"
				+ sharedSecret + "\n\n" + "Note that it's normal to share the API key"
				+ " (" + apiKey + "). " 			
				+ "This is how you are granted access to resources " + "like a UEB topic or Flatiron scope. "
				+ "However, you should NOT share the API key's secret. " + "The API key is associated with your"
				+ " email alone. ALL access to data made with this " + "key will be your responsibility. If you "
				+ "share the secret, someone else can use the API key " + "to access proprietary data with your "
				+ "identity.\n" + "\n" + "Enjoy!\n" + "\n" + "The GFP/SA-2020 Team";
	
		        Emailer em = dmaapContext.getConfigReader().getSystemEmailer();
		        em.send(contactEmail, "New API Key", body);
			}
			log.debug("TopicService: : sending response.");
	
			JSONObject o = key.asJsonObject();
			
			o.put ( NsaSimpleApiKey.kApiSecretField,
					emailProvided ?
						"Emailed to " + contactEmail + "." :
						key.getSecret ()
				);
			DMaaPResponseBuilder.respondOk(dmaapContext,
					o);
			
			return;
		} else {
			log.debug("=======ApiKeysServiceImpl: createApiKey : Error in creating API Key.=====");
			DMaaPResponseBuilder.respondWithError(dmaapContext,
					HttpStatusCodes.k500_internalServerError,
					"Failed to create api key.");
			throw new KeyExistsException(apiKey);
		}
	}

	/**
	 * @param dmaapContext
	 * @param apikey
	 * @param nsaApiKey
	 * @throws ConfigDbException
	 * @throws IOException
	 * @throws AccessDeniedException
	 */
	@Override
	public void updateApiKey(DMaaPContext dmaapContext, String apikey,
			ApiKeyBean nsaApiKey) throws ConfigDbException, IOException, AccessDeniedException {

		String errorMsg = "Api key name is not mentioned.";
		int errorCode = HttpStatusCodes.k400_badRequest;

		if (null != apikey) {
			final NsaApiDb<NsaSimpleApiKey> apiKeyDb = getApiKeyDb(dmaapContext);
			final NsaSimpleApiKey key = apiKeyDb.loadApiKey(apikey);
			boolean shouldUpdate = false;

			if (null != key) {
				final NsaApiKey user = DMaaPAuthenticatorImpl
						.getAuthenticatedUser(dmaapContext);

				if (user == null || !user.getKey().equals(key.getKey())) {
					throw new AccessDeniedException("You must authenticate with the key you'd like to update.");
				}

				if (null != nsaApiKey.getEmail()) {
					key.setContactEmail(nsaApiKey.getEmail());
					shouldUpdate = true;
				}

				if (null != nsaApiKey.getDescription()) {
					key.setDescription(nsaApiKey.getDescription());
					shouldUpdate = true;
				}

				if (shouldUpdate) {
					apiKeyDb.saveApiKey(key);
				}

				log.info("======ApiKeysServiceImpl : updateApiKey : Key Updated Successfully :"
						+ key.toString() + "=========");
				DMaaPResponseBuilder.respondOk(dmaapContext,
						key.asJsonObject());
				return;
			}
		} else {
			errorMsg = "Api key [" + apikey + "] does not exist.";
			errorCode = HttpStatusCodes.k404_notFound;
			DMaaPResponseBuilder.respondWithError(dmaapContext, errorCode,
					errorMsg);
			log.info("======ApiKeysServiceImpl : updateApiKey : Error in Updating Key.============");
			throw new IOException();
		}
	}

	/**
	 * @param dmaapContext
	 * @param apikey
	 * @throws ConfigDbException
	 * @throws IOException
	 * @throws AccessDeniedException
	 */
	@Override
	public void deleteApiKey(DMaaPContext dmaapContext, String apikey)
			throws ConfigDbException, IOException, AccessDeniedException {

		String errorMsg = "Api key name is not mentioned.";
		int errorCode = HttpStatusCodes.k400_badRequest;

		if (null != apikey) {
			final NsaApiDb<NsaSimpleApiKey> apiKeyDb = getApiKeyDb(dmaapContext);
			final NsaSimpleApiKey key = apiKeyDb.loadApiKey(apikey);

			if (null != key) {

				final NsaApiKey user = DMaaPAuthenticatorImpl
						.getAuthenticatedUser(dmaapContext);
				if (user == null || !user.getKey().equals(key.getKey())) {
					throw new AccessDeniedException("You don't own the API key.");
				}

				apiKeyDb.deleteApiKey(key);
				log.info("======ApiKeysServiceImpl : deleteApiKey : Deleted Key successfully.============");
				DMaaPResponseBuilder.respondOkWithHtml(dmaapContext,
						"Api key [" + apikey + "] deleted successfully.");
				return;
			}
		} else {
			errorMsg = "Api key [" + apikey + "] does not exist.";
			errorCode = HttpStatusCodes.k404_notFound;
			DMaaPResponseBuilder.respondWithError(dmaapContext, errorCode,
					errorMsg);
			log.info("======ApiKeysServiceImpl : deleteApiKey : Error while deleting key.============");
			throw new IOException();
		}
	}

	/**
	 * 
	 * @param dmaapContext
	 * @return
	 */
	private NsaApiDb<NsaSimpleApiKey> getApiKeyDb(DMaaPContext dmaapContext) {
		ConfigurationReader configReader = dmaapContext.getConfigReader();
		return configReader.getfApiKeyDb();
	}

}