Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(jwt): implement JWT auth for asset downloads #719

Merged
merged 42 commits into from
Nov 22, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
42 commits
Select commit Hold shift + click to select a range
2ffe87d
tinkering with JWT
andrewazores Oct 13, 2021
5b3ba7d
refactor
andrewazores Oct 13, 2021
638b938
more tinkering
andrewazores Oct 14, 2021
6cbd4a6
refactor JWT consuming handlers not to use V2 response API format
andrewazores Oct 14, 2021
6a49ab7
nossl fixups
andrewazores Oct 14, 2021
4f0eb56
add jwt targetreportgethandler
andrewazores Oct 14, 2021
bf137e8
add jwt targettemplategethandler
andrewazores Oct 14, 2021
a301f3e
add jwt report and recording get handlers
andrewazores Oct 14, 2021
40d53d2
FIXME drop commit until merged in web-client upstream. update to late…
andrewazores Oct 14, 2021
1386e2d
allow relative resource URLs
andrewazores Oct 14, 2021
6c03faa
pass X-JMX-Authorization into jmxauth claim
andrewazores Oct 14, 2021
beb1cf2
quick, dirty, reflective method for looking up handler to retrieve re…
andrewazores Oct 15, 2021
e82f7f0
fix(tokens): change jwt path to auth/token
andrewazores Oct 15, 2021
9d15e25
fix(devcerts): use same generation settings as entrypoint selfsigned
andrewazores Oct 15, 2021
983f509
switch to Nimbus JWT library
andrewazores Oct 19, 2021
88b1858
Reduce token expiry to 2 minutes
andrewazores Oct 20, 2021
10116fd
Perform token permissions validation at consumption time
andrewazores Oct 20, 2021
671ea7f
add todo
andrewazores Oct 20, 2021
83645f6
cleanup no longer used pieces
andrewazores Oct 20, 2021
1397429
stricter resource claim validation and extract constants
andrewazores Oct 26, 2021
3898ba3
use direct AES encryption and clean up disabling token security
andrewazores Oct 26, 2021
65b68b8
Extract JwtModule
andrewazores Oct 26, 2021
9cd4919
update to rebase web-client
andrewazores Oct 26, 2021
40040c7
rebase fix
andrewazores Oct 26, 2021
30b7e29
fixup! FIXME drop commit until merged in web-client upstream. update …
andrewazores Oct 29, 2021
73197ed
remove testing env var
andrewazores Nov 10, 2021
3c7334b
rename JwtFactory to AssetJwtHelper
andrewazores Nov 10, 2021
e224148
add AuthTokenPostHandlerTest
andrewazores Nov 10, 2021
b05b72e
add TargetTemplateGetHandlerTest
andrewazores Nov 10, 2021
1bed688
add TargetReportGetHandlerTest
andrewazores Nov 10, 2021
bb9311f
apply spotless
andrewazores Nov 10, 2021
f8a13ec
add TargetRecordingGetHandlerTest
andrewazores Nov 10, 2021
cfccac1
add ReportGetHandlerTest
andrewazores Nov 10, 2021
fbfc3d2
add RecordingGetHandlerTest
andrewazores Nov 10, 2021
c237e0f
add AbstractJwtConsumingHandlerTest
andrewazores Nov 10, 2021
bc24edd
only require "sub" claim if HTTP auth is used
andrewazores Nov 11, 2021
031d3c6
include more unauth explanation
andrewazores Nov 11, 2021
a4cbb54
add itests
andrewazores Nov 11, 2021
5b57083
correct itest cleanup failure
andrewazores Nov 12, 2021
a7aa67c
include CONTENT_DISPOSITION response headers
andrewazores Nov 17, 2021
15a1e58
update web-client to latest
andrewazores Nov 17, 2021
aa33190
remove unused dependency
andrewazores Nov 17, 2021
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
12 changes: 12 additions & 0 deletions pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -70,6 +70,8 @@
<org.apache.httpcomponents.version>4.5.13</org.apache.httpcomponents.version>
<io.fabric8.client.version>5.4.1</io.fabric8.client.version>
<io.vertx.web.version>3.9.9</io.vertx.web.version>
<com.nimbusds.jose.jwt.version>9.15.2</com.nimbusds.jose.jwt.version>
<org.bouncycastle.version>1.69</org.bouncycastle.version>
<org.slf4j.version>1.7.30</org.slf4j.version>
<com.google.code.gson.version>2.8.6</com.google.code.gson.version>
<com.github.ben-manes.caffeine.version>3.0.1</com.github.ben-manes.caffeine.version>
Expand Down Expand Up @@ -140,6 +142,16 @@
<artifactId>vertx-web-client</artifactId>
<version>${io.vertx.web.version}</version>
</dependency>
<dependency>
<groupId>com.nimbusds</groupId>
<artifactId>nimbus-jose-jwt</artifactId>
<version>${com.nimbusds.jose.jwt.version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
<artifactId>bcprov-jdk15on</artifactId>
<version>${org.bouncycastle.version}</version>
</dependency>
<dependency>
<groupId>org.slf4j</groupId>
<artifactId>slf4j-jdk14</artifactId>
Expand Down
4 changes: 4 additions & 0 deletions src/main/java/io/cryostat/Cryostat.java
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,7 @@
*/
package io.cryostat;

import java.security.Security;
import java.util.concurrent.CompletableFuture;

import javax.inject.Singleton;
Expand All @@ -52,13 +53,16 @@
import io.cryostat.rules.RuleProcessor;
import io.cryostat.rules.RuleRegistry;

import com.nimbusds.jose.crypto.bc.BouncyCastleProviderSingleton;
import dagger.Component;

class Cryostat {

public static void main(String[] args) throws Exception {
CryostatCore.initialize();

Security.addProvider(BouncyCastleProviderSingleton.getInstance());

final Logger logger = Logger.INSTANCE;
final Environment environment = new Environment();

Expand Down
2 changes: 2 additions & 0 deletions src/main/java/io/cryostat/net/NetworkModule.java
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,7 @@
import io.cryostat.core.sys.FileSystem;
import io.cryostat.core.tui.ClientWriter;
import io.cryostat.net.reports.ReportsModule;
import io.cryostat.net.security.SecurityModule;
import io.cryostat.net.web.WebModule;

import com.github.benmanes.caffeine.cache.Scheduler;
Expand All @@ -71,6 +72,7 @@
includes = {
WebModule.class,
ReportsModule.class,
SecurityModule.class,
})
public abstract class NetworkModule {

Expand Down
12 changes: 9 additions & 3 deletions src/main/java/io/cryostat/net/SslConfiguration.java
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@
import io.vertx.core.net.PfxOptions;
import org.apache.commons.lang3.tuple.Pair;

class SslConfiguration {
public class SslConfiguration {
private final Environment env;
private final FileSystem fs;
private final Logger logger;
Expand All @@ -67,6 +67,12 @@ class SslConfiguration {
this.fs = fs;
this.logger = logger;

if (env.hasEnv("CRYOSTAT_DISABLE_SSL")) {
strategy = new NoSslStrategy();
logger.info("Selected NoSSL strategy");
return;
}

{
Path path = obtainKeyStorePathIfSpecified();
if (path != null) {
Expand Down Expand Up @@ -199,11 +205,11 @@ Pair<Path, Path> discoverKeyCertPathPairInDefaultLocations() {
return Pair.of(key, cert);
}

HttpServerOptions applyToHttpServerOptions(HttpServerOptions options) {
public HttpServerOptions applyToHttpServerOptions(HttpServerOptions options) {
return strategy.applyToHttpServerOptions(options);
}

boolean enabled() {
public boolean enabled() {
return strategy.enabled();
}

Expand Down
48 changes: 48 additions & 0 deletions src/main/java/io/cryostat/net/security/SecurityModule.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat.net.security;

import io.cryostat.net.security.jwt.JwtModule;

import dagger.Module;

@Module(
includes = {
JwtModule.class,
})
public abstract class SecurityModule {}
155 changes: 155 additions & 0 deletions src/main/java/io/cryostat/net/security/jwt/AssetJwtHelper.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,155 @@
/*
* Copyright The Cryostat Authors
*
* The Universal Permissive License (UPL), Version 1.0
*
* Subject to the condition set forth below, permission is hereby granted to any
* person obtaining a copy of this software, associated documentation and/or data
* (collectively the "Software"), free of charge and under any and all copyright
* rights in the Software, and any and all patent rights owned or freely
* licensable by each licensor hereunder covering either (i) the unmodified
* Software as contributed to or provided by such licensor, or (ii) the Larger
* Works (as defined below), to deal in both
*
* (a) the Software, and
* (b) any piece of software and/or hardware listed in the lrgrwrks.txt file if
* one is included with the Software (each a "Larger Work" to which the Software
* is contributed by such licensors),
*
* without restriction, including without limitation the rights to copy, create
* derivative works of, display, perform, and distribute the Software and make,
* use, sell, offer for sale, import, export, have made, and have sold the
* Software and the Larger Work(s), and to sublicense the foregoing rights on
* either these or other terms.
*
* This license is subject to the following condition:
* The above copyright notice and either this complete permission notice or at
* a minimum a reference to the UPL must be included in all copies or
* substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
* SOFTWARE.
*/
package io.cryostat.net.security.jwt;

import java.net.MalformedURLException;
import java.net.SocketException;
import java.net.URISyntaxException;
import java.net.UnknownHostException;
import java.text.ParseException;
import java.time.Instant;
import java.util.Date;
import java.util.HashSet;
import java.util.Set;

import io.cryostat.core.log.Logger;
import io.cryostat.net.web.WebServer;

import com.nimbusds.jose.EncryptionMethod;
import com.nimbusds.jose.JOSEException;
import com.nimbusds.jose.JWEAlgorithm;
import com.nimbusds.jose.JWEDecrypter;
import com.nimbusds.jose.JWEEncrypter;
import com.nimbusds.jose.JWEHeader;
import com.nimbusds.jose.JWEObject;
import com.nimbusds.jose.JWSAlgorithm;
import com.nimbusds.jose.JWSHeader;
import com.nimbusds.jose.JWSSigner;
import com.nimbusds.jose.JWSVerifier;
import com.nimbusds.jose.Payload;
import com.nimbusds.jwt.JWT;
import com.nimbusds.jwt.JWTClaimsSet;
import com.nimbusds.jwt.SignedJWT;
import com.nimbusds.jwt.proc.BadJWTException;
import com.nimbusds.jwt.proc.DefaultJWTClaimsVerifier;
import dagger.Lazy;

public class AssetJwtHelper {

public static final String RESOURCE_CLAIM = "resource";
public static final String JMXAUTH_CLAIM = "jmxauth";

private final Lazy<WebServer> webServer;
private final JWSSigner signer;
private final JWSVerifier verifier;
private final JWEEncrypter encrypter;
private final JWEDecrypter decrypter;
private final boolean subjectRequired;

AssetJwtHelper(
Lazy<WebServer> webServer,
JWSSigner signer,
JWSVerifier verifier,
JWEEncrypter encrypter,
JWEDecrypter decrypter,
boolean subjectRequired,
Logger logger) {
this.webServer = webServer;
this.signer = signer;
this.verifier = verifier;
this.encrypter = encrypter;
this.decrypter = decrypter;
this.subjectRequired = subjectRequired;
}

public String createAssetDownloadJwt(String subject, String resource, String jmxauth)
throws JOSEException, SocketException, UnknownHostException, URISyntaxException,
MalformedURLException {
String issuer = webServer.get().getHostUrl().toString();
Date now = Date.from(Instant.now());
Date expiry = Date.from(now.toInstant().plusSeconds(120));
jan-law marked this conversation as resolved.
Show resolved Hide resolved
JWTClaimsSet claims =
new JWTClaimsSet.Builder()
.issuer(issuer)
.audience(issuer)
.issueTime(now)
.notBeforeTime(now)
.expirationTime(expiry)
.subject(subject)
.claim(RESOURCE_CLAIM, resource)
.claim(JMXAUTH_CLAIM, jmxauth)
.build();

SignedJWT jwt = new SignedJWT(new JWSHeader.Builder(JWSAlgorithm.HS256).build(), claims);
jan-law marked this conversation as resolved.
Show resolved Hide resolved
jwt.sign(signer);

JWEHeader header =
new JWEHeader.Builder(JWEAlgorithm.DIR, EncryptionMethod.A256GCM)
.contentType("JWT")
.build();
JWEObject jwe = new JWEObject(header, new Payload(jwt));
jwe.encrypt(encrypter);

return jwe.serialize();
}

public JWT parseAssetDownloadJwt(String rawToken)
throws ParseException, JOSEException, BadJWTException, SocketException,
UnknownHostException, URISyntaxException, MalformedURLException {
JWEObject jwe = JWEObject.parse(rawToken);
jwe.decrypt(decrypter);

SignedJWT jwt = jwe.getPayload().toSignedJWT();
jwt.verify(verifier);

// TODO extract this claims verifier
// TODO add a SecurityContext
String cryostatUri = webServer.get().getHostUrl().toString();
JWTClaimsSet exactMatchClaims =
new JWTClaimsSet.Builder().issuer(cryostatUri).audience(cryostatUri).build();
Set<String> requiredClaimNames =
new HashSet<>(Set.of("exp", "nbf", "iat", "iss", "aud", RESOURCE_CLAIM));
if (subjectRequired) {
requiredClaimNames.add("sub");
}
new DefaultJWTClaimsVerifier<>(cryostatUri, exactMatchClaims, requiredClaimNames)
.verify(jwt.getJWTClaimsSet(), null);

return jwt;
}
}
Loading