|
1 | 1 | package org.jenkinsci.plugins.kubernetes.credentials;
|
2 | 2 |
|
| 3 | +import jenkins.security.FIPS140; |
3 | 4 | import org.apache.commons.codec.binary.Base64;
|
4 | 5 |
|
| 6 | +import java.net.URI; |
5 | 7 | import java.nio.charset.StandardCharsets;
|
6 | 8 | import java.security.Key;
|
7 | 9 | import java.security.cert.Certificate;
|
8 | 10 | import java.security.cert.CertificateEncodingException;
|
9 |
| -import java.security.cert.X509Certificate; |
10 | 11 |
|
11 | 12 | public abstract class Utils {
|
12 | 13 |
|
| 14 | + /** |
| 15 | + * Error message used to indicate that skipping TLS verification is not accepted in FIPS mode. |
| 16 | + */ |
| 17 | + public static final String FIPS140_ERROR_MESSAGE = |
| 18 | + "Using an insecure connection and/or skipping TLS verification is not accepted in FIPS mode."; |
| 19 | + |
13 | 20 | public static String wrapWithMarker(String begin, String end, String encodedBody) {
|
14 | 21 | return new StringBuilder(begin).append("\n")
|
15 | 22 | .append(encodedBody).append("\n")
|
@@ -46,4 +53,55 @@ public static String encodeCertificate(Certificate certificate) throws Certifica
|
46 | 53 | public static String encodeKey(Key key) {
|
47 | 54 | return encodeBase64(wrapPrivateKey(Base64.encodeBase64String(key.getEncoded())));
|
48 | 55 | }
|
| 56 | + |
| 57 | + /** |
| 58 | + * Ensure that the URI request is FIPS compliant for the given HttpUriRequest object and skipTLSVerify option. |
| 59 | + * Throw an exception if the request is invalid. |
| 60 | + * A request is considered valid if the connection is either using TLS or a local pipe |
| 61 | + * and if the TLS verification is not skipped. |
| 62 | + * If FIPS mode is not enabled, this method does nothing. |
| 63 | + * |
| 64 | + * @param uri The request to validate |
| 65 | + * @param skipTLSVerify A flag indicating whether to skip TLS verification or not |
| 66 | + * @throws IllegalArgumentException If the request is invalid |
| 67 | + */ |
| 68 | + public static void ensureFIPSCompliantURIRequest(URI uri, boolean skipTLSVerify) { |
| 69 | + boolean isInsecure = uri.getScheme().equals("http"); |
| 70 | + ensureFIPSCompliant(isInsecure, skipTLSVerify); |
| 71 | + } |
| 72 | + |
| 73 | + /** |
| 74 | + * Ensure that the request is FIPS compliant for the given URL and skipTLSVerify option. |
| 75 | + * Throw an exception if the request is invalid. |
| 76 | + * A request is considered valid if the connection is either using TLS or a local pipe |
| 77 | + * and if the TLS verification is not skipped. |
| 78 | + * If FIPS mode is not enabled, this method does nothing. |
| 79 | + * |
| 80 | + * @param stringRequest The request to validate |
| 81 | + * @param skipTLSVerify A flag indicating whether to skip TLS verification or not |
| 82 | + * @throws IllegalArgumentException If the request is invalid |
| 83 | + */ |
| 84 | + public static void ensureFIPSCompliantRequest(String stringRequest, boolean skipTLSVerify) { |
| 85 | + boolean isInsecure = stringRequest.startsWith("http://"); |
| 86 | + ensureFIPSCompliant(isInsecure, skipTLSVerify); |
| 87 | + } |
| 88 | + |
| 89 | + /** |
| 90 | + * Ensure FIPS compliance based on the following rules: |
| 91 | + * <ul> |
| 92 | + * <li>Must use a secure connection</li> |
| 93 | + * <li>TLS verification is mandatory</li> |
| 94 | + * </ul> |
| 95 | + * Throw an exception if not compliant. |
| 96 | + * If FIPS mode is not enabled, this method does nothing. |
| 97 | + * |
| 98 | + * @param insecureConnection If the connection is insecure |
| 99 | + * @param skipTLSVerify A flag indicating whether to skip TLS verification or not |
| 100 | + * @throws IllegalArgumentException If not FIPS compliant |
| 101 | + */ |
| 102 | + private static void ensureFIPSCompliant(boolean insecureConnection, boolean skipTLSVerify) { |
| 103 | + if (FIPS140.useCompliantAlgorithms() && (insecureConnection || skipTLSVerify)) { |
| 104 | + throw new IllegalArgumentException(Utils.FIPS140_ERROR_MESSAGE); |
| 105 | + } |
| 106 | + } |
49 | 107 | }
|
0 commit comments