2 * ============LICENSE_START=======================================================
4 * ================================================================================
5 * Copyright (C) 2017 AT&T Intellectual Property. All rights reserved.
6 * ================================================================================
7 * Licensed under the Apache License, Version 2.0 (the "License");
8 * you may not use this file except in compliance with the License.
9 * You may obtain a copy of the License at
11 * http://www.apache.org/licenses/LICENSE-2.0
13 * Unless required by applicable law or agreed to in writing, software
14 * distributed under the License is distributed on an "AS IS" BASIS,
15 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 * See the License for the specific language governing permissions and
17 * limitations under the License.
18 * ============LICENSE_END=========================================================
22 package org.openecomp.sdc.ci.tests.execute.general;
24 import static org.testng.AssertJUnit.assertEquals;
25 import static org.testng.AssertJUnit.assertFalse;
26 import static org.testng.AssertJUnit.assertTrue;
28 import java.util.ArrayList;
29 import java.util.HashMap;
30 import java.util.List;
33 import org.apache.commons.codec.binary.Base64;
34 import org.junit.Rule;
35 import org.junit.rules.TestName;
36 import org.openecomp.sdc.be.dao.api.ActionStatus;
37 import org.openecomp.sdc.be.datatypes.elements.ConsumerDataDefinition;
38 import org.openecomp.sdc.be.datatypes.enums.ComponentTypeEnum;
39 import org.openecomp.sdc.be.datatypes.enums.ResourceTypeEnum;
40 import org.openecomp.sdc.be.model.Resource;
41 import org.openecomp.sdc.be.model.Service;
42 import org.openecomp.sdc.be.model.User;
43 import org.openecomp.sdc.ci.tests.api.ComponentBaseTest;
44 import org.openecomp.sdc.ci.tests.api.Urls;
45 import org.openecomp.sdc.ci.tests.datatypes.ArtifactReqDetails;
46 import org.openecomp.sdc.ci.tests.datatypes.ComponentInstanceReqDetails;
47 import org.openecomp.sdc.ci.tests.datatypes.ResourceReqDetails;
48 import org.openecomp.sdc.ci.tests.datatypes.ServiceReqDetails;
49 import org.openecomp.sdc.ci.tests.datatypes.enums.ArtifactTypeEnum;
50 import org.openecomp.sdc.ci.tests.datatypes.enums.UserRoleEnum;
51 import org.openecomp.sdc.ci.tests.datatypes.expected.ExpectedAuthenticationAudit;
52 import org.openecomp.sdc.ci.tests.datatypes.http.HttpHeaderEnum;
53 import org.openecomp.sdc.ci.tests.datatypes.http.RestResponse;
54 import org.openecomp.sdc.ci.tests.utils.DbUtils;
55 import org.openecomp.sdc.ci.tests.utils.general.AtomicOperationUtils;
56 import org.openecomp.sdc.ci.tests.utils.general.ElementFactory;
57 import org.openecomp.sdc.ci.tests.utils.rest.ArtifactRestUtils;
58 import org.openecomp.sdc.ci.tests.utils.rest.BaseRestUtils;
59 import org.openecomp.sdc.ci.tests.utils.rest.ComponentInstanceRestUtils;
60 import org.openecomp.sdc.ci.tests.utils.rest.ConsumerRestUtils;
61 import org.openecomp.sdc.ci.tests.utils.rest.LifecycleRestUtils;
62 import org.openecomp.sdc.ci.tests.utils.validation.AuditValidationUtils;
63 import org.openecomp.sdc.ci.tests.utils.validation.ErrorValidationUtils;
64 import org.openecomp.sdc.common.util.ValidationUtils;
65 import org.testng.AssertJUnit;
66 import org.testng.annotations.AfterMethod;
67 import org.testng.annotations.BeforeMethod;
68 import org.testng.annotations.Test;
70 public class BasicHttpAuthenticationTest extends ComponentBaseTest {
72 protected static final String AUTH_FAILED_INVALID_AUTHENTICATION_HEADER = "AUTH_FAILED_INVALID_AUTHENTICATION_HEADER";
74 protected static final String AUTH_SUCCESS = "AUTH_SUCCESS";
76 protected static final String AUTH_FAILED_INVALID_PASSWORD = "AUTH_FAILED_INVALID_PASSWORD";
78 protected static final String AUTH_FAILED_USER_NOT_FOUND = "AUTH_FAILED_USER_NOT_FOUND";
80 protected static final String AUTH_REQUIRED = "AUTH_REQUIRED";
82 protected static final String WWW_AUTHENTICATE = "WWW-Authenticate";
84 // user ci password 123456
85 // protected final String authorizationHeader = "Basic Y2k6MTIzNDU2";
86 // user ci password 123456
87 protected final String USER = "ci";
89 protected final String PASSWORD = "123456";
91 protected final String SALT = "2a1f887d607d4515d4066fe0f5452a50";
93 protected final String HASHED_PASSWORD = "0a0dc557c3bf594b1a48030e3e99227580168b21f44e285c69740b8d5b13e33b";
95 protected User sdncAdminUserDetails;
97 protected ConsumerDataDefinition consumerDataDefinition;
98 protected ResourceReqDetails resourceDetails;
99 protected ServiceReqDetails serviceDetails;
100 protected User sdncUserDetails;
102 protected ArtifactReqDetails deploymentArtifact;
104 protected ExpectedAuthenticationAudit expectedAuthenticationAudit;
106 protected final String auditAction = "HttpAuthentication";
108 protected String expectedDownloadServiceUrl;
109 protected String expectedDownloadResourceUrl;
110 protected ComponentInstanceReqDetails componentInstanceReqDetails;
113 public static TestName name = new TestName();
115 public BasicHttpAuthenticationTest() {
116 super(name, BasicHttpAuthenticationTest.class.getName());
120 public void init() throws Exception {
122 sdncUserDetails = ElementFactory.getDefaultUser(UserRoleEnum.DESIGNER);
123 Resource resourceObject = AtomicOperationUtils.createResourceByType(ResourceTypeEnum.VFC, UserRoleEnum.DESIGNER, true).left().value();
124 resourceDetails = new ResourceReqDetails(resourceObject);
125 Service serviceObject = AtomicOperationUtils.createDefaultService(UserRoleEnum.DESIGNER, true).left().value();
126 serviceDetails = new ServiceReqDetails(serviceObject);
128 deploymentArtifact = ElementFactory.getDefaultDeploymentArtifactForType(ArtifactTypeEnum.HEAT.getType());
129 RestResponse response = ArtifactRestUtils.addInformationalArtifactToResource(deploymentArtifact, sdncUserDetails, resourceDetails.getUniqueId());
130 AssertJUnit.assertTrue("add HEAT artifact to resource request returned status:" + response.getErrorCode(), response.getErrorCode() == 200);
132 componentInstanceReqDetails = ElementFactory.getDefaultComponentInstance();
133 // certified resource
134 response = LifecycleRestUtils.certifyResource(resourceDetails);
135 AssertJUnit.assertTrue("certify resource request returned status:" + response.getErrorCode(), response.getErrorCode() == 200);
137 // add resource instance with HEAT deployment artifact to the service
138 componentInstanceReqDetails.setComponentUid(resourceDetails.getUniqueId());
139 response = ComponentInstanceRestUtils.createComponentInstance(componentInstanceReqDetails, sdncUserDetails, serviceDetails.getUniqueId(), ComponentTypeEnum.SERVICE);
140 AssertJUnit.assertTrue("response code is not 201, returned: " + response.getErrorCode(), response.getErrorCode() == 201);
141 expectedAuthenticationAudit = new ExpectedAuthenticationAudit();
143 // RestResponse addDeploymentArtifactResponse =
144 // ArtifactRestUtils.addInformationalArtifactToService(deploymentArtifact,
145 // sdncUserDetails, serviceDetails.getUniqueId());
146 // assertEquals("didn't succeed to upload deployment artifact", 200,
147 // addDeploymentArtifactResponse.getErrorCode().intValue());
150 // String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL,
151 // ValidationUtils.convertToSystemName(serviceDetails.getServiceName()),
152 // serviceDetails.getVersion(),
153 // ValidationUtils.normalizeFileName(deploymentArtifact.getArtifactName()));
155 expectedDownloadResourceUrl = String.format(Urls.DISTRIB_DOWNLOAD_RESOURCE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceDetails.getVersion(),
156 ValidationUtils.convertToSystemName(resourceDetails.getName()), resourceDetails.getVersion(), ValidationUtils.normalizeFileName(deploymentArtifact.getArtifactName()));
157 expectedDownloadResourceUrl = expectedDownloadResourceUrl.substring("/asdc/".length(), expectedDownloadResourceUrl.length());
159 expectedDownloadServiceUrl = String.format(Urls.DISTRIB_DOWNLOAD_SERVICE_ARTIFACT_RELATIVE_URL, ValidationUtils.convertToSystemName(serviceDetails.getName()), serviceDetails.getVersion(),
160 ValidationUtils.normalizeFileName(deploymentArtifact.getArtifactName()));
161 expectedDownloadServiceUrl = expectedDownloadServiceUrl.substring("/asdc/".length(), expectedDownloadServiceUrl.length());
163 sdncAdminUserDetails = ElementFactory.getDefaultUser(UserRoleEnum.ADMIN);
164 consumerDataDefinition = createConsumer();
165 RestResponse deleteResponse = ConsumerRestUtils.deleteConsumer(consumerDataDefinition, sdncAdminUserDetails);
166 BaseRestUtils.checkStatusCode(deleteResponse, "delete operation filed", false, 404, 200);
169 RestResponse createResponse = ConsumerRestUtils.createConsumer(consumerDataDefinition, sdncAdminUserDetails);
170 BaseRestUtils.checkCreateResponse(createResponse);
175 public void tearDown() throws Exception {
176 RestResponse deleteResponse = ConsumerRestUtils.deleteConsumer(consumerDataDefinition, sdncAdminUserDetails);
177 BaseRestUtils.checkStatusCode(deleteResponse, "delete operation filed", false, 404, 200);
182 public void sendAuthenticatedRequestTest_success() throws Exception, Exception {
183 DbUtils.cleanAllAudits();
184 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(USER, PASSWORD);
185 // RestResponse restResponse =
186 // ArtifactRestUtils.downloadServiceArtifact(serviceDetails,
187 // deploymentArtifact, sdncUserDetails, authorizationHeader);
188 RestResponse restResponse = ArtifactRestUtils.downloadResourceArtifact(serviceDetails, resourceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
189 AssertJUnit.assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());
190 AssertJUnit.assertFalse(restResponse.getHeaderFields().containsKey(HttpHeaderEnum.WWW_AUTHENTICATE.getValue()));
192 validateAuditAuthentication(USER, AUTH_SUCCESS, ComponentTypeEnum.RESOURCE);
196 protected void validateAuditAuthentication(String userName, String AuthStatus, ComponentTypeEnum compType) throws Exception {
197 if (compType.equals(ComponentTypeEnum.RESOURCE)) {
198 expectedAuthenticationAudit = new ExpectedAuthenticationAudit(expectedDownloadResourceUrl, userName, auditAction, AuthStatus);
200 expectedAuthenticationAudit = new ExpectedAuthenticationAudit(expectedDownloadServiceUrl, userName, auditAction, AuthStatus);
202 AuditValidationUtils.validateAuthenticationAudit(expectedAuthenticationAudit);
205 protected ConsumerDataDefinition createConsumer() {
206 ConsumerDataDefinition consumer = new ConsumerDataDefinition();
207 consumer.setConsumerName(USER);
208 consumer.setConsumerSalt(SALT);
209 consumer.setConsumerPassword(HASHED_PASSWORD);
215 public void sendAuthenticatedRequestWithoutHeadersTest() throws Exception, Exception {
216 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, new HashMap<String, String>());
217 assertEquals("Check response code after download artifact", 401, restResponse.getErrorCode().intValue());
218 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.AUTH_REQUIRED.name(), new ArrayList<String>(), restResponse.getResponse());
219 assertTrue(restResponse.getHeaderFields().containsKey(WWW_AUTHENTICATE));
220 List<String> getAuthenticateHeader = restResponse.getHeaderFields().get(WWW_AUTHENTICATE);
221 assertEquals("www-authenticate header contains more then one value", 1, getAuthenticateHeader.size());
222 assertTrue(getAuthenticateHeader.get(0).equals("Basic realm=" + "\"ASDC\""));
224 validateAuditAuthentication("", AUTH_REQUIRED, ComponentTypeEnum.SERVICE);
228 public void sendAuthenticatedRequestTest_userIsNotProvsioned() throws Exception, Exception {
229 String userName = "shay";
230 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(userName, "123456");
231 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
232 assertEquals("Check response code after download artifact", 403, restResponse.getErrorCode().intValue());
233 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.AUTH_FAILED.name(), new ArrayList<String>(), restResponse.getResponse());
234 assertFalse(restResponse.getHeaderFields().containsKey(WWW_AUTHENTICATE));
236 validateAuditAuthentication(userName, AUTH_FAILED_USER_NOT_FOUND, ComponentTypeEnum.SERVICE);
240 public void sendAuthenticatedRequestTest_userIsNull() throws Exception, Exception {
241 String userName = "";
242 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(userName, "123456");
243 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
244 assertEquals("Check response code after download artifact", 403, restResponse.getErrorCode().intValue());
245 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.AUTH_FAILED.name(), new ArrayList<String>(), restResponse.getResponse());
246 assertFalse(restResponse.getHeaderFields().containsKey(WWW_AUTHENTICATE));
248 validateAuditAuthentication(userName, AUTH_FAILED_USER_NOT_FOUND, ComponentTypeEnum.SERVICE);
252 public void sendAuthenticatedRequestTest_passwordIsNull() throws Exception, Exception {
253 String userName = "ci";
254 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(userName, "");
255 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
256 assertEquals("Check response code after download artifact", 403, restResponse.getErrorCode().intValue());
257 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.AUTH_FAILED.name(), new ArrayList<String>(), restResponse.getResponse());
258 assertFalse(restResponse.getHeaderFields().containsKey(WWW_AUTHENTICATE));
260 validateAuditAuthentication(userName, AUTH_FAILED_INVALID_PASSWORD, ComponentTypeEnum.SERVICE);
264 public void sendAuthenticatedRequestTest_passowrdIsNotValidated() throws Exception, Exception {
265 String userCi = "ci";
266 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(userCi, "98765");
267 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
268 assertEquals("Check response code after download artifact", 403, restResponse.getErrorCode().intValue());
269 ErrorValidationUtils.checkBodyResponseOnError(ActionStatus.AUTH_FAILED.name(), new ArrayList<String>(), restResponse.getResponse());
270 assertFalse(restResponse.getHeaderFields().containsKey(HttpHeaderEnum.WWW_AUTHENTICATE.getValue()));
272 validateAuditAuthentication(userCi, AUTH_FAILED_INVALID_PASSWORD, ComponentTypeEnum.SERVICE);
276 public void sendAuthenticatedRequestTest_InvalidHeader() throws Exception, Exception {
277 String userCredentials = USER + ":" + PASSWORD;
278 byte[] encodeBase64 = Base64.encodeBase64(userCredentials.getBytes());
279 String encodedUserCredentials = new String(encodeBase64);
280 Map<String, String> authorizationHeader = new HashMap<String, String>();
281 authorizationHeader.put(HttpHeaderEnum.AUTHORIZATION.getValue(), encodedUserCredentials);
282 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
283 assertEquals("Check response code after download artifact", 400, restResponse.getErrorCode().intValue());
284 assertFalse(restResponse.getHeaderFields().containsKey(HttpHeaderEnum.WWW_AUTHENTICATE.getValue()));
286 validateAuditAuthentication("", AUTH_FAILED_INVALID_AUTHENTICATION_HEADER, ComponentTypeEnum.SERVICE);
289 @Test(enabled = false)
290 public void sendTwoAuthenticatedRequestsTest() throws Exception, Exception {
291 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(USER, PASSWORD);
292 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
293 assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());
295 RestResponse secondRestResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
296 assertEquals("Check response code after second download artifact", 200, secondRestResponse.getErrorCode().intValue());
299 @Test(enabled = false)
300 public void sendAuthenticatedRequestTest_userValidation_1() throws Exception, Exception {
302 ConsumerDataDefinition consumer = new ConsumerDataDefinition();
303 consumer.setConsumerName("cI2468");
304 consumer.setConsumerPassword(HASHED_PASSWORD);
305 consumer.setConsumerSalt(SALT);
306 RestResponse deleteResponse = ConsumerRestUtils.deleteConsumer(consumer, sdncAdminUserDetails);
307 BaseRestUtils.checkStatusCode(deleteResponse, "delete operation filed", false, 404, 200);
309 RestResponse createResponse = ConsumerRestUtils.createConsumer(consumer, sdncAdminUserDetails);
310 BaseRestUtils.checkCreateResponse(createResponse);
312 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(consumer.getConsumerName(), PASSWORD);
313 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
314 assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());
316 deleteResponse = ConsumerRestUtils.deleteConsumer(consumer, sdncAdminUserDetails);
317 BaseRestUtils.checkStatusCode(deleteResponse, "delete operation filed", false, 404, 200);
320 // ECOMP Consumer Name - UTF-8 string up to 255 characters containing the
321 // following characters : ( maybe to limit 4-64 chars ? )
322 // Lowercase characters {a-z}
323 // Uppercase characters {A-Z}
325 // Dash {-}; this character is not supported as the first character in the
327 // Period {.}; this character is not supported as the first character in the
330 // @Ignore("add manually user:password 24-!68:123456 to
331 // users-configuration.yaml in runtime")
332 @Test(enabled = false)
333 public void sendAuthenticatedRequestTest_userValidation_2() throws Exception, Exception {
334 ConsumerDataDefinition consumer = new ConsumerDataDefinition();
335 consumer.setConsumerName("24-!68");
336 consumer.setConsumerPassword(HASHED_PASSWORD);
337 consumer.setConsumerSalt(SALT);
338 RestResponse deleteResponse = ConsumerRestUtils.deleteConsumer(consumer, sdncAdminUserDetails);
339 BaseRestUtils.checkStatusCode(deleteResponse, "delete operation filed", false, 404, 200);
341 RestResponse createResponse = ConsumerRestUtils.createConsumer(consumer, sdncAdminUserDetails);
342 BaseRestUtils.checkCreateResponse(createResponse);
343 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader(consumer.getConsumerName(), PASSWORD);
344 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
345 assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());
347 deleteResponse = ConsumerRestUtils.deleteConsumer(consumer, sdncAdminUserDetails);
348 BaseRestUtils.checkStatusCode(deleteResponse, "delete operation filed", false, 404, 200);
351 // this is invalide becouse we do not use the : any more
352 // @Ignore("can't exectue, yaml file does not allow to enter more then one
353 // colon continuously (\":\") ")
354 @Test(enabled = false)
355 public void sendAuthenticatedRequestTest_userValidation_3() throws Exception, Exception {
356 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader("a:", "123456");
357 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
358 assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());
362 // * ECOMP Consumer Password - expected to be SHA-2 256 encrypted value (
363 // SALT + "real" password ) => maximal length 256 bytes = 32 characters
364 // Before storing/comparing please convert upper case letter to lower.
365 // The "normalized" encrypted password should match the following format :
367 // @Ignore("add manually user:password 2468:123:456 to
368 // users-configuration.yaml in runtime")
369 @Test(enabled = false)
370 public void sendAuthenticatedRequestTest_passwordValidation_1() throws Exception, Exception {
371 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader("A1", "123:456");
372 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
373 assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());
376 // * ECOMP Consumer Password - expected to be SHA-2 256 encrypted value (
377 // SALT + "real" password ) => maximal length 256 bytes = 32 characters
378 // Before storing/comparing please convert upper case letter to lower.
379 // The "normalized" encrypted password should match the following format :
381 @Test(enabled = false)
382 // @Ignore("add manually user:password 2468:Sq123a456B to
383 // users-configuration.yaml in runtime")
384 public void sendAuthenticatedRequestTest_passwordValidation_2() throws Exception, Exception {
385 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader("B2", "Sq123a456B");
386 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
387 assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());
390 // * ECOMP Consumer Password - expected to be SHA-2 256 encrypted value (
391 // SALT + "real" password ) => maximal length 256 bytes = 32 characters
392 // Before storing/comparing please convert upper case letter to lower.
393 // The "normalized" encrypted password should match the following format :
396 // @Ignore("add C3:111T-0-*# to file")
397 public void sendAuthenticatedRequestTest_passwordValidation_3() throws Exception, Exception {
398 Map<String, String> authorizationHeader = BaseRestUtils.addAuthorizeHeader("C3", "111T-0-*#");
399 RestResponse restResponse = ArtifactRestUtils.downloadServiceArtifact(serviceDetails, deploymentArtifact, sdncUserDetails, authorizationHeader);
400 assertEquals("Check response code after download artifact", 200, restResponse.getErrorCode().intValue());