Skip to content

Commit 94de81f

Browse files
author
Anders Breid
authored
Add support for new authentication type BASIC_AUTH_JENKINS_CSRF (#294)
NON BACKWARDS COMPATIBLE CHANGES! * Add support for new authentication type BASIC_AUTH_JENKINS_CSRF - EI now fetches Jenkins crumb only when BASIC_AUTH_JENKINS_CSRF is used. - Better error handling caused by failure to connect to rest services
1 parent 99cbe15 commit 94de81f

File tree

13 files changed

+324
-200
lines changed

13 files changed

+324
-200
lines changed

.travis.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ before_install:
2121

2222

2323
# This is only run before integrationTests job
24-
# To ensure docker containers are fully up and running we sleep 20s
24+
# To ensure docker containers are fully up and running we sleep 60s
2525
before_script:
2626
- source src/main/docker/env.bash
2727
- docker-compose -f src/main/docker/docker-compose.yml up -d mongodb rabbitmq eiffel-er jenkins mail-server

pom.xml

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@
2929
<springLdapVersion>2.3.2.RELEASE</springLdapVersion>
3030
<springSecurityVersion>5.0.6.RELEASE</springSecurityVersion>
3131
<springSessionVersion>2.0.2.RELEASE</springSessionVersion>
32-
<forkCountTests>2.0C</forkCountTests>
32+
<forkCountTests>2C</forkCountTests>
3333
</properties>
3434

3535
<!-- Reporting Plugins -->
@@ -74,7 +74,7 @@
7474
<dependency>
7575
<groupId>com.github.eiffel-community</groupId>
7676
<artifactId>eiffel-commons</artifactId>
77-
<version>0.0.10</version>
77+
<version>0.0.12</version>
7878
</dependency>
7979

8080
<dependency>

src/functionaltests/java/com/ericsson/ei/notifications/ttl/TestTTLSteps.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
import org.springframework.test.context.TestPropertySource;
2626
import org.springframework.util.SocketUtils;
2727

28+
import com.ericsson.ei.exception.AuthenticationException;
2829
import com.ericsson.ei.handlers.MongoDBHandler;
2930
import com.ericsson.ei.subscription.InformSubscriber;
3031
import com.ericsson.ei.utils.FunctionalTestBase;
@@ -115,7 +116,7 @@ public void create_subscription_object() throws IOException, JSONException {
115116
}
116117

117118
@When("^I want to inform subscriber$")
118-
public void inform_subscriber() throws IOException {
119+
public void inform_subscriber() throws IOException, AuthenticationException {
119120
JsonNode aggregatedObject = eventManager.getJSONFromFile(AGGREGATED_OBJECT_FILE_PATH);
120121
informSubscriber.informSubscriber(aggregatedObject.toString(), subscriptionObject);
121122
}

src/functionaltests/java/com/ericsson/ei/rules/RulesHandlerSteps.java

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -57,11 +57,12 @@ public void file_with_path(String rulesPath) {
5757
@Given("^path is made absolute$")
5858
public void path_is_absolute() {
5959
rulesPath = new File(rulesPath).getAbsolutePath();
60+
rulesPath = rulesPath.replace("\\", "/");
6061
}
6162

6263
@Given("^path is URI with \"([^\"]*)\" scheme$")
6364
public void path_is_uri(String scheme) {
64-
rulesPath = scheme + "://" + rulesPath;
65+
rulesPath = scheme + rulesPath;
6566
}
6667

6768
@Then("^rules are loaded$")
@@ -79,8 +80,7 @@ public void rules_are_loaded_with_exception() throws Exception {
7980
}
8081

8182
/**
82-
* Create a new instance of RulesHandler using a rules.path
83-
* set by the rulesPath variable.
83+
* Create a new instance of RulesHandler using a rules.path set by the rulesPath variable.
8484
*
8585
* @throws Exception
8686
*/
@@ -100,7 +100,9 @@ private void setupRestEndpoints() {
100100

101101
LOGGER.debug("Setting up endpoints on host '" + HOST + "' and port '" + port + "'");
102102
mockClient = new MockServerClient(HOST, port);
103-
mockClient.when(request().withMethod("GET").withPath(ROUTE_RULES_FILE)).respond(response().withStatusCode(201).withBody(BODY));
104-
mockClient.when(request().withMethod("GET").withPath(ROUTE_RULES_FILE_EMPTY)).respond(response().withStatusCode(201).withBody(EMPTY));
103+
mockClient.when(request().withMethod("GET").withPath(ROUTE_RULES_FILE))
104+
.respond(response().withStatusCode(201).withBody(BODY));
105+
mockClient.when(request().withMethod("GET").withPath(ROUTE_RULES_FILE_EMPTY))
106+
.respond(response().withStatusCode(201).withBody(EMPTY));
105107
}
106108
}

src/functionaltests/resources/features/rulesHandler.feature

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -18,23 +18,23 @@ Feature: Test Rules Handler
1818
Scenario: Test URI with file scheme
1919
Given a file with path "src/main/resources/rules/ArtifactRules-Eiffel-Agen-Version.json"
2020
And path is made absolute
21-
And path is URI with "file" scheme
21+
And path is URI with "file:///" scheme
2222
Then rules are loaded
2323

2424
@RulesHandlerHttpURI
2525
Scenario: Test URI with http scheme
2626
Given a file with path "localhost:{port}/some/route/MyRules.json"
27-
And path is URI with "http" scheme
27+
And path is URI with "http://" scheme
2828
Then rules are loaded
2929

3030
@RulesHandlerIncorrectPath
3131
Scenario: Test incorrect rules path
3232
Given a file with path "localhost:{port}/wrong/route/MyRules.json"
33-
And path is URI with "http" scheme
33+
And path is URI with "http://" scheme
3434
Then rules are loaded with expected exception
3535

3636
@RulesHandlerEmptyRules
3737
Scenario: Test empty rules file
3838
Given a file with path "localhost:{port}/wrong/route/EmptyRules.json"
39-
And path is URI with "http" scheme
39+
And path is URI with "http://" scheme
4040
Then rules are loaded with expected exception

src/integrationtests/java/com/ericsson/ei/integrationtests/FlowStepsIT.java

Lines changed: 43 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -100,8 +100,7 @@ public void the_resulting_aggregated_object(String aggregatedObjectFilePath) thr
100100
public void the_upstream_input(String upstreamInputFile) throws Throwable {
101101
this.upstreamInputFile = upstreamInputFile;
102102

103-
final URL upStreamInput = new File(upstreamInputFile).toURI()
104-
.toURL();
103+
final URL upStreamInput = new File(upstreamInputFile).toURI().toURL();
105104
ArrayNode upstreamJson = (ArrayNode) objectMapper.readTree(upStreamInput);
106105
extraEventsCount = upstreamJson.size();
107106
}
@@ -112,8 +111,8 @@ public void jenkins_data_is_prepared() throws Throwable {
112111
}
113112

114113
@Given("^subscription object for \"([^\"]*)\" with name \"([^\"]*)\" is created$")
115-
public void subscription_object_for_with_name_is_created(String subscriptionType, String subscriptionName)
116-
throws Throwable {
114+
public void subscription_object_for_with_name_is_created(String subscriptionType,
115+
String subscriptionName) throws Throwable {
117116
if (subscriptionType.equalsIgnoreCase("Mail")) {
118117
subscriptionObject = new MailSubscriptionObject(subscriptionName);
119118
} else {
@@ -132,18 +131,22 @@ public void notification_meta_is_set_in_subscription(String notificationMeta) th
132131
subscriptionObject.setNotificationMeta(notificationMeta);
133132
}
134133

135-
@When("^basic_auth authentication with username \"([^\"]*)\" and password \"([^\"]*)\" is set in subscription$")
136-
public void basic_auth_authentication_with_username_and_password_is_set_in_subscription(String username,
137-
String password)
138-
throws Throwable {
139-
if (subscriptionObject instanceof RestPostSubscriptionObject) {
140-
((RestPostSubscriptionObject) subscriptionObject).setBasicAuth(username, password);
134+
@When("^\"([^\"]*)\" authentication with username \"([^\"]*)\" and password \"([^\"]*)\" is set in subscription$")
135+
public void basic_auth_authentication_with_username_and_password_is_set_in_subscription(
136+
String authenticationType, String username, String password) throws Throwable {
137+
138+
RestPostSubscriptionObject restPostSubscriptionObject = (RestPostSubscriptionObject) subscriptionObject;
139+
if (restPostSubscriptionObject != null) {
140+
restPostSubscriptionObject.setAuthenticationType(authenticationType)
141+
.setUsername(username)
142+
.setPassword(password);
143+
subscriptionObject = restPostSubscriptionObject;
141144
}
142145
}
143146

144147
@When("^rest post body media type is set to \"([^\"]*)\" is set in subscription$")
145-
public void rest_post_body_media_type_is_set_to_is_set_in_subscription(String restPostBodyMediaType)
146-
throws Throwable {
148+
public void rest_post_body_media_type_is_set_to_is_set_in_subscription(
149+
String restPostBodyMediaType) throws Throwable {
147150
subscriptionObject.setRestPostBodyMediaType(restPostBodyMediaType);
148151

149152
}
@@ -154,9 +157,10 @@ public void parameter_key_and_value_is_added_in_subscription(String formKey, Str
154157
}
155158

156159
@When("^condition \"([^\"]*)\" at requirement index '(\\d+)' is added in subscription$")
157-
public void requirement_for_condition_is_added_in_subscription(String condition, int requirementIndex)
158-
throws Throwable {
159-
subscriptionObject.addConditionToRequirement(requirementIndex, new JSONObject().put("jmespath", condition));
160+
public void requirement_for_condition_is_added_in_subscription(String condition,
161+
int requirementIndex) throws Throwable {
162+
subscriptionObject.addConditionToRequirement(requirementIndex, new JSONObject().put(
163+
"jmespath", condition));
160164
}
161165

162166
@When("^the eiffel events are sent$")
@@ -166,8 +170,7 @@ public void eiffel_events_are_sent() throws Throwable {
166170

167171
@When("^the upstream input events are sent")
168172
public void upstream_input_events_are_sent() throws IOException {
169-
final URL upStreamInput = new File(upstreamInputFile).toURI()
170-
.toURL();
173+
final URL upStreamInput = new File(upstreamInputFile).toURI().toURL();
171174
ArrayNode upstreamJson = (ArrayNode) objectMapper.readTree(upStreamInput);
172175
if (upstreamJson != null) {
173176
for (JsonNode event : upstreamJson) {
@@ -210,7 +213,7 @@ public void the_jenkins_job_should_have_been_triggered() throws Throwable {
210213
}
211214
}
212215

213-
assertEquals(true, jobStatusDataFetched);
216+
assertEquals("Failed to fetch Job Status Data: ", true, jobStatusDataFetched);
214217
}
215218

216219
@Then("^the expected aggregated object ID is \"([^\"]*)\"$")
@@ -219,7 +222,8 @@ public void the_expected_aggregated_object_ID_is(String aggregatedObjectID) thro
219222
}
220223

221224
@Then("^verify jenkins job data timestamp is after test subscription was created$")
222-
public void verify_jenkins_job_data_timestamp_is_after_test_subscription_was_created() throws Throwable {
225+
public void verify_jenkins_job_data_timestamp_is_after_test_subscription_was_created()
226+
throws Throwable {
223227
long jenkinsTriggeredTime = jobStatusData.getLong("timestamp");
224228
assert (jenkinsTriggeredTime >= startTime) : "Jenkins job was triggered before execution of this test.";
225229
}
@@ -246,15 +250,12 @@ public void mongodb_should_contain_mails(int amountOfMails) throws Exception {
246250

247251
if (newestMailJson != null) {
248252
JsonNode to = newestMailJson.get("to");
249-
assertEquals("Sent mails " + to.size() + ". Expected " + amountOfMails, amountOfMails, to.size());
253+
assertEquals("Sent mails " + to.size() + ". Expected " + amountOfMails,
254+
amountOfMails, to.size());
250255

251-
String createdDate = newestMailJson.get("created")
252-
.get("$date")
253-
.asText();
256+
String createdDate = newestMailJson.get("created").get("$date").asText();
254257

255-
createdDateInMillis = ZonedDateTime.parse(createdDate)
256-
.toInstant()
257-
.toEpochMilli();
258+
createdDateInMillis = ZonedDateTime.parse(createdDate).toInstant().toEpochMilli();
258259
mailHasBeenDelivered = createdDateInMillis >= startTime;
259260
}
260261

@@ -267,8 +268,8 @@ public void mongodb_should_contain_mails(int amountOfMails) throws Exception {
267268

268269
@Then("^jenkins is set up with job name \"([^\"]*)\"$")
269270
public void jenkins_is_set_up_with_job_name(String JobName) throws Throwable {
270-
jenkinsManager = new JenkinsManager(jenkinsProtocol, jenkinsHost, jenkinsPort, jenkinsUsername,
271-
jenkinsPassword);
271+
jenkinsManager = new JenkinsManager(jenkinsProtocol, jenkinsHost, jenkinsPort,
272+
jenkinsUsername, jenkinsPassword);
272273
jenkinsManager.forceCreateJob(JobName, jenkinsXmlData.getXmlAsString());
273274
jenkinsJobName = JobName;
274275
}
@@ -285,8 +286,7 @@ public void subscription_is_uploaded() throws URISyntaxException {
285286
.setPort(port)
286287
.setEndpoint("/subscriptions")
287288
.addHeader("Content-type", "application/json")
288-
.setBody(subscriptionObject.getAsSubscriptions()
289-
.toString());
289+
.setBody(subscriptionObject.getAsSubscriptions().toString());
290290

291291
ResponseEntity<String> response = postRequest.performRequest();
292292
assertEquals(200, response.getStatusCodeValue());
@@ -340,7 +340,8 @@ private String extractValueForKeyInJobData(String key) {
340340
}
341341

342342
/**
343-
* Iterates the parameter array and returns the value if the key is found in given parameter array.
343+
* Iterates the parameter array and returns the value if the key is found in given parameter
344+
* array.
344345
*
345346
* @param parameters
346347
* @param key
@@ -361,7 +362,8 @@ private String extractValueFromParameters(JSONArray parameters, String key) {
361362
}
362363

363364
private JsonNode getNewestMailFromDatabase() throws Exception {
364-
ArrayList<String> allMails = mongoDBHandler.getAllDocuments(MAILHOG_DATABASE_NAME, "messages");
365+
ArrayList<String> allMails = mongoDBHandler.getAllDocuments(MAILHOG_DATABASE_NAME,
366+
"messages");
365367

366368
if (allMails.size() > 0) {
367369
String mailString = allMails.get(allMails.size() - 1);
@@ -372,8 +374,9 @@ private JsonNode getNewestMailFromDatabase() throws Exception {
372374
}
373375

374376
/**
375-
* Replaces given input parameters if user wishes with test defined parameters. If user want the user may specify
376-
* the host, port job name and token directly in the feauture file and they will not be replaced.
377+
* Replaces given input parameters if user wishes with test defined parameters. If user want the
378+
* user may specify the host, port job name and token directly in the feauture file and they
379+
* will not be replaced.
377380
* <p>
378381
* ${jenkinsHost} is replaced with jenkins host.
379382
* <p>
@@ -388,9 +391,12 @@ private JsonNode getNewestMailFromDatabase() throws Exception {
388391
*/
389392
private String replaceVariablesInNotificationMeta(String notificationMeta) {
390393
notificationMeta = notificationMeta.replaceAll("\\$\\{jenkinsHost\\}", jenkinsHost);
391-
notificationMeta = notificationMeta.replaceAll("\\$\\{jenkinsPort\\}", String.valueOf(jenkinsPort));
392-
notificationMeta = notificationMeta.replaceAll("\\$\\{jenkinsJobName\\}", this.jenkinsJobName);
393-
notificationMeta = notificationMeta.replaceAll("\\$\\{jenkinsJobToken\\}", this.jenkinsJobToken);
394+
notificationMeta = notificationMeta.replaceAll("\\$\\{jenkinsPort\\}", String.valueOf(
395+
jenkinsPort));
396+
notificationMeta = notificationMeta.replaceAll("\\$\\{jenkinsJobName\\}",
397+
this.jenkinsJobName);
398+
notificationMeta = notificationMeta.replaceAll("\\$\\{jenkinsJobToken\\}",
399+
this.jenkinsJobToken);
394400
return notificationMeta;
395401
}
396402
}

src/integrationtests/resources/features/SourceChangeFlowIT.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Feature: Source change flow integrationtest
2323
# Setup subscription
2424
Given subscription object for "REST/POST" with name "ParameterizedTriggerSubscription" is created
2525
When notification meta "http://${jenkinsHost}:${jenkinsPort}/job/${jenkinsJobName}/buildWithParameters?token='test-token-123'&test_key=id" is set in subscription
26-
And basic_auth authentication with username "admin" and password "admin" is set in subscription
26+
And "BASIC_AUTH_JENKINS_CSRF" authentication with username "admin" and password "admin" is set in subscription
2727
And rest post body media type is set to "application/x-www-form-urlencoded" is set in subscription
2828
And condition "id=='sb6efi4n-25fb-4d77-b9fd-5f2xrrefe66de47'" at requirement index '0' is added in subscription
2929
Then subscription is uploaded
@@ -60,7 +60,7 @@ Feature: Source change flow integrationtest
6060
# Setup subscription
6161
Given subscription object for "REST/POST" with name "ParameterInBodyTriggerSubscription" is created
6262
When notification meta "http://${jenkinsHost}:${jenkinsPort}/job/${jenkinsJobName}/build?token='test-token-123'" is set in subscription
63-
And basic_auth authentication with username "admin" and password "admin" is set in subscription
63+
And "BASIC_AUTH_JENKINS_CSRF" authentication with username "admin" and password "admin" is set in subscription
6464
And rest post body media type is set to "application/x-www-form-urlencoded" is set in subscription
6565
And parameter form key "json" and form value "{parameter: [{name:'test_param_1', value:'Test Input Value'}, {name:'test_param_2', value:id}]}" is added in subscription
6666
And condition "id=='sb6efi4n-25fb-4d77-b9fd-5f2xrrefe66de47'" at requirement index '0' is added in subscription

src/integrationtests/resources/features/TestExecutionFlowIT.feature

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@ Feature: Test execution flow integrationtest
2323
# Setup subscription
2424
Given subscription object for "REST/POST" with name "ParameterizedTriggerSubscription" is created
2525
When notification meta "http://${jenkinsHost}:${jenkinsPort}/job/${jenkinsJobName}/buildWithParameters?token='test-token-123'&test_key=activity_triggered_event_id" is set in subscription
26-
And basic_auth authentication with username "admin" and password "admin" is set in subscription
26+
And "BASIC_AUTH_JENKINS_CSRF" authentication with username "admin" and password "admin" is set in subscription
2727
And rest post body media type is set to "application/x-www-form-urlencoded" is set in subscription
2828
And condition "activity_triggered_event_id=='e46ef12d-25gb-4d7y-b9fd-8763re66de47'" at requirement index '0' is added in subscription
2929
Then subscription is uploaded
@@ -58,7 +58,7 @@ Feature: Test execution flow integrationtest
5858
# Setup subscription
5959
Given subscription object for "REST/POST" with name "ParameterInBodyTriggerSubscription" is created
6060
When notification meta "http://${jenkinsHost}:${jenkinsPort}/job/${jenkinsJobName}/build?token='test-token-123'" is set in subscription
61-
And basic_auth authentication with username "admin" and password "admin" is set in subscription
61+
And "BASIC_AUTH_JENKINS_CSRF" authentication with username "admin" and password "admin" is set in subscription
6262
And rest post body media type is set to "application/x-www-form-urlencoded" is set in subscription
6363
And parameter form key "json" and form value "{parameter: [{name:'test_param_1', value:'test_value'}, {name:'test_param_2', value:activity_triggered_event_id}]}" is added in subscription
6464
And condition "activity_triggered_event_id=='e46ef12d-25gb-4d7y-b9fd-8763re66de47'" at requirement index '0' is added in subscription
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
/*
2+
Copyright 2019 Ericsson AB.
3+
For a full list of individual contributors, please see the commit history.
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
*/
17+
package com.ericsson.ei.exception;
18+
19+
public class AuthenticationException extends Exception {
20+
21+
private static final long serialVersionUID = 2L;
22+
23+
public AuthenticationException() {
24+
super();
25+
}
26+
27+
public AuthenticationException(String message) {
28+
super(message);
29+
}
30+
31+
public AuthenticationException(String message, Throwable e) {
32+
super(message, e);
33+
}
34+
35+
}

src/main/java/com/ericsson/ei/rules/RulesHandler.java

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,8 @@ private String readRulesFileContent() throws Exception {
125125
* @throws URISyntaxException
126126
*/
127127
private String readRulesFileFromURI() throws IOException, URISyntaxException {
128-
return IOUtils.toString(new URI(rulesFilePath), "UTF-8");
128+
URI fileUri = new URI(rulesFilePath);
129+
return IOUtils.toString(fileUri, "UTF-8");
129130
}
130131

131132
/**

0 commit comments

Comments
 (0)