Skip to content

Commit 03c3053

Browse files
authored
in FIPS mode password must have minimum length of 14 characters, this is preparing for upgrade of credentials plugin version (#58)
* in FIPS mode password must have minimum length of 14 characters and ugrade of credentials plugin version --------- Signed-off-by: Olivier Lamy <[email protected]>
1 parent 90a488b commit 03c3053

File tree

7 files changed

+91
-9
lines changed

7 files changed

+91
-9
lines changed

pom.xml

+3
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,8 @@
5151

5252
<spotbugs.effort>Max</spotbugs.effort>
5353
<spotbugs.threshold>Low</spotbugs.threshold>
54+
55+
<hpi.compatibleSinceVersion>190</hpi.compatibleSinceVersion>
5456
</properties>
5557

5658
<dependencies>
@@ -72,6 +74,7 @@
7274
<dependency>
7375
<groupId>org.jenkins-ci.plugins</groupId>
7476
<artifactId>credentials</artifactId>
77+
<version>1378.v81ef4269d764</version>
7578
</dependency>
7679
<dependency>
7780
<groupId>org.jenkins-ci.plugins</groupId>

src/main/java/org/jenkinsci/plugins/kubernetes/credentials/OpenShiftBearerTokenCredentialImpl.java

+17-1
Original file line numberDiff line numberDiff line change
@@ -3,9 +3,13 @@
33
import com.cloudbees.plugins.credentials.CredentialsScope;
44
import com.cloudbees.plugins.credentials.impl.UsernamePasswordCredentialsImpl;
55
import hudson.Extension;
6+
import hudson.model.Descriptor;
7+
import hudson.util.FormValidation;
68
import hudson.util.Secret;
9+
import jenkins.security.FIPS140;
710
import net.sf.json.JSONObject;
811
import org.apache.commons.codec.binary.Base64;
12+
import org.apache.commons.lang.StringUtils;
913
import org.apache.http.Header;
1014
import org.apache.http.HttpHeaders;
1115
import org.apache.http.NameValuePair;
@@ -17,6 +21,8 @@
1721
import org.apache.http.impl.client.HttpClientBuilder;
1822
import org.apache.http.util.EntityUtils;
1923
import org.kohsuke.stapler.DataBoundConstructor;
24+
import org.kohsuke.stapler.QueryParameter;
25+
import org.kohsuke.stapler.interceptor.RequirePOST;
2026

2127
import java.io.IOException;
2228
import java.net.URI;
@@ -43,7 +49,8 @@ public class OpenShiftBearerTokenCredentialImpl extends UsernamePasswordCredenti
4349
private transient ConcurrentMap<String, Token> tokenCache = new ConcurrentHashMap<>();
4450

4551
@DataBoundConstructor
46-
public OpenShiftBearerTokenCredentialImpl(CredentialsScope scope, String id, String description, String username, String password) {
52+
public OpenShiftBearerTokenCredentialImpl(CredentialsScope scope, String id, String description, String username, String password)
53+
throws Descriptor.FormException {
4754
super(scope, id, description, username, password);
4855
}
4956

@@ -168,6 +175,15 @@ public static class DescriptorImpl extends BaseStandardCredentialsDescriptor {
168175
public String getDisplayName() {
169176
return "OpenShift Username and Password";
170177
}
178+
179+
@RequirePOST
180+
public FormValidation doCheckPassword(@QueryParameter String password) {
181+
if(FIPS140.useCompliantAlgorithms() && StringUtils.length(password) < 14) {
182+
return FormValidation.error(org.jenkinsci.plugins.kubernetes.credentials.Messages.passwordTooShortFIPS());
183+
}
184+
return FormValidation.ok();
185+
}
186+
171187
}
172188

173189
public static class Token {
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#
2+
# The MIT License
3+
#
4+
# Copyright (c) 2011-2013, Olivier Lamy.
5+
#
6+
# Permission is hereby granted, free of charge, to any person obtaining a copy
7+
# of this software and associated documentation files (the "Software"), to deal
8+
# in the Software without restriction, including without limitation the rights
9+
# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
# copies of the Software, and to permit persons to whom the Software is
11+
# furnished to do so, subject to the following conditions:
12+
#
13+
# The above copyright notice and this permission notice shall be included in
14+
# all copies or substantial portions of the Software.
15+
#
16+
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
22+
# THE SOFTWARE.
23+
#
24+
passwordTooShortFIPS=Password is too short (< 14 characters)

src/test/java/org/jenkinsci/plugins/kubernetes/credentials/AbstractOpenShiftBearerTokenCredentialFIPSTest.java

+3-3
Original file line numberDiff line numberDiff line change
@@ -96,9 +96,9 @@ public void unprepareFakeOAuthServer() {
9696
}
9797

9898
@Test
99-
public void ensureFIPSCompliantURIRequest() throws IOException {
100-
OpenShiftBearerTokenCredentialImpl cred;
101-
cred = new OpenShiftBearerTokenCredentialImpl(CredentialsScope.GLOBAL, "id", "description", "username", "password");
99+
public void ensureFIPSCompliantURIRequest() throws Exception {
100+
OpenShiftBearerTokenCredentialImpl cred =
101+
new OpenShiftBearerTokenCredentialImpl(CredentialsScope.GLOBAL, "id", "description", "username", "theaustraliancricketteamisthebest");
102102
try {
103103
cred.getToken(scheme + "://localhost:" + server.getAddress().getPort() + "/valid-response", null, skipTLSVerify);
104104
if (!shouldPass) {

src/test/java/org/jenkinsci/plugins/kubernetes/credentials/OpenShiftBearerTokenCredentialTest.java

+5-5
Original file line numberDiff line numberDiff line change
@@ -52,14 +52,14 @@ private String getURI() {
5252
}
5353

5454
@Test
55-
public void testValidResponse() throws IOException {
55+
public void testValidResponse() throws Exception {
5656
OpenShiftBearerTokenCredentialImpl t = new OpenShiftBearerTokenCredentialImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME, PASSWORD);
5757
String token = t.getToken(getURI() + "valid-response", null, true);
5858
assertEquals("1234", token);
5959
}
6060

6161
@Test
62-
public void testMultipleCachedTokens() throws IOException {
62+
public void testMultipleCachedTokens() throws Exception {
6363
OpenShiftBearerTokenCredentialImpl t = new OpenShiftBearerTokenCredentialImpl(CredentialsScope.GLOBAL, CREDENTIAL_ID, "sample", USERNAME, PASSWORD);
6464
String token1 = t.getToken(getURI() + "valid-response", null, true);
6565
String token2 = t.getToken(getURI() + "valid-response2", null, true);
@@ -70,7 +70,7 @@ public void testMultipleCachedTokens() throws IOException {
7070
}
7171

7272
@Test
73-
public void testBadStatusCode() throws IOException {
73+
public void testBadStatusCode() throws Exception {
7474
expectedEx.expect(IOException.class);
7575
expectedEx.expectMessage("The response from the OAuth server was invalid: The OAuth service didn't respond with a redirection but with '400: Bad Request'");
7676

@@ -79,7 +79,7 @@ public void testBadStatusCode() throws IOException {
7979
}
8080

8181
@Test
82-
public void testMissingLocation() throws IOException {
82+
public void testMissingLocation() throws Exception {
8383
expectedEx.expect(IOException.class);
8484
expectedEx.expectMessage("The response from the OAuth server was invalid: The OAuth service didn't respond with location header");
8585

@@ -88,7 +88,7 @@ public void testMissingLocation() throws IOException {
8888
}
8989

9090
@Test
91-
public void testBadLocation() throws IOException {
91+
public void testBadLocation() throws Exception {
9292
expectedEx.expect(IOException.class);
9393
expectedEx.expectMessage("The response from the OAuth server was invalid: The response contained no token");
9494

src/test/java/org/jenkinsci/plugins/kubernetes/credentials/OpenShiftBearerTokenCredentialWithFIPSTest.java

+18
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package org.jenkinsci.plugins.kubernetes.credentials;
22

3+
import hudson.util.FormValidation;
34
import jenkins.security.FIPS140;
5+
import org.apache.commons.text.StringEscapeUtils;
46
import org.junit.ClassRule;
7+
import org.junit.Test;
58
import org.junit.runner.RunWith;
69
import org.junit.runners.Parameterized;
710
import org.jvnet.hudson.test.FlagRule;
811

912
import java.util.Arrays;
1013
import java.util.Collection;
1114

15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.Matchers.containsString;
17+
import static org.hamcrest.Matchers.nullValue;
18+
1219
@RunWith(Parameterized.class)
1320
public class OpenShiftBearerTokenCredentialWithFIPSTest extends AbstractOpenShiftBearerTokenCredentialFIPSTest {
1421
@ClassRule
@@ -30,4 +37,15 @@ public static Collection<Object[]> parameters() {
3037
{"http", true, false, "TLS and TLS check are mandatory when in FIPS mode"},
3138
});
3239
}
40+
41+
@Test
42+
public void tooShortPassword() throws Exception {
43+
FormValidation formValidation = new OpenShiftBearerTokenCredentialImpl.DescriptorImpl().doCheckPassword("");
44+
assertThat(formValidation.getMessage(), containsString(StringEscapeUtils.escapeHtml4(Messages.passwordTooShortFIPS())));
45+
formValidation = new OpenShiftBearerTokenCredentialImpl.DescriptorImpl().doCheckPassword("tooshort");
46+
assertThat(formValidation.getMessage(), containsString(StringEscapeUtils.escapeHtml4(Messages.passwordTooShortFIPS())));
47+
formValidation = new OpenShiftBearerTokenCredentialImpl.DescriptorImpl().doCheckPassword("theaustraliancricketteamisthebest");
48+
assertThat(formValidation.getMessage(), nullValue());
49+
}
50+
3351
}

src/test/java/org/jenkinsci/plugins/kubernetes/credentials/OpenShiftBearerTokenCredentialWithoutFIPSTest.java

+21
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,21 @@
11
package org.jenkinsci.plugins.kubernetes.credentials;
22

3+
import hudson.util.FormValidation;
34
import jenkins.security.FIPS140;
5+
import org.apache.commons.text.StringEscapeUtils;
46
import org.junit.ClassRule;
7+
import org.junit.Test;
58
import org.junit.runner.RunWith;
69
import org.junit.runners.Parameterized;
710
import org.jvnet.hudson.test.FlagRule;
811

912
import java.util.Arrays;
1013
import java.util.Collection;
1114

15+
import static org.hamcrest.MatcherAssert.assertThat;
16+
import static org.hamcrest.Matchers.containsString;
17+
import static org.hamcrest.Matchers.nullValue;
18+
1219
@RunWith(Parameterized.class)
1320
public class OpenShiftBearerTokenCredentialWithoutFIPSTest extends AbstractOpenShiftBearerTokenCredentialFIPSTest {
1421
@ClassRule
@@ -29,4 +36,18 @@ public static Collection<Object[]> parameters() {
2936
{"http", false, true, "Not in FIPS mode, any combination should be valid"},
3037
});
3138
}
39+
40+
/**
41+
* similar test to {@link OpenShiftBearerTokenCredentialWithFIPSTest#tooShortPassword()} but here not in FIPS context
42+
* so it is accepted
43+
*/
44+
@Test
45+
public void tooShortPasswordForFIPS() throws Exception {
46+
FormValidation formValidation = new OpenShiftBearerTokenCredentialImpl.DescriptorImpl().doCheckPassword("");
47+
assertThat(formValidation.getMessage(), nullValue());
48+
formValidation = new OpenShiftBearerTokenCredentialImpl.DescriptorImpl().doCheckPassword("tooshort");
49+
assertThat(formValidation.getMessage(), nullValue());
50+
formValidation = new OpenShiftBearerTokenCredentialImpl.DescriptorImpl().doCheckPassword("theaustraliancricketteamisthebest");
51+
assertThat(formValidation.getMessage(), nullValue());
52+
}
3253
}

0 commit comments

Comments
 (0)