forked from apache/pulsar
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
PIP-25: Token based authentication (apache#2888)
* PIP-25: Token based authentication * Addressed comments * Use Authorization header * Update to support env: data: and file: as sources for keys and tokens * Fixed cli description * Updated broker.conf * Improved consistency in reading keys and CLI tools * Fixed check for http headers * Accept rel time with no specified unit * Fixed reading data: URL * Addressed comments * Added integration tests * Addressed comments * Added CLI command to validate token against key * Fixed integration tests * Removed env: * Fixed rel time parsing
- Loading branch information
Showing
32 changed files
with
1,891 additions
and
62 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
126 changes: 126 additions & 0 deletions
126
...on/src/main/java/org/apache/pulsar/broker/authentication/AuthenticationProviderToken.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,126 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.authentication; | ||
|
||
import io.jsonwebtoken.Claims; | ||
import io.jsonwebtoken.Jwt; | ||
import io.jsonwebtoken.JwtException; | ||
import io.jsonwebtoken.Jwts; | ||
|
||
import java.io.IOException; | ||
import java.security.Key; | ||
|
||
import javax.naming.AuthenticationException; | ||
|
||
import org.apache.commons.lang3.StringUtils; | ||
import org.apache.pulsar.broker.ServiceConfiguration; | ||
import org.apache.pulsar.broker.authentication.utils.AuthTokenUtils; | ||
|
||
public class AuthenticationProviderToken implements AuthenticationProvider { | ||
|
||
public final static String HTTP_HEADER_NAME = "Authorization"; | ||
final static String HTTP_HEADER_VALUE_PREFIX = "Bearer "; | ||
|
||
// When simmetric key is configured | ||
final static String CONF_TOKEN_SECRET_KEY = "tokenSecretKey"; | ||
|
||
// When public/private key pair is configured | ||
final static String CONF_TOKEN_PUBLIC_KEY = "tokenPublicKey"; | ||
|
||
private Key validationKey; | ||
|
||
@Override | ||
public void close() throws IOException { | ||
// noop | ||
} | ||
|
||
@Override | ||
public void initialize(ServiceConfiguration config) throws IOException { | ||
this.validationKey = getValidationKey(config); | ||
} | ||
|
||
@Override | ||
public String getAuthMethodName() { | ||
return "token"; | ||
} | ||
|
||
@Override | ||
public String authenticate(AuthenticationDataSource authData) throws AuthenticationException { | ||
String token = null; | ||
|
||
if (authData.hasDataFromCommand()) { | ||
// Authenticate Pulsar binary connection | ||
token = authData.getCommandData(); | ||
} else if (authData.hasDataFromHttp()) { | ||
// Authentication HTTP request. The format here should be compliant to RFC-6750 | ||
// (https://tools.ietf.org/html/rfc6750#section-2.1). Eg: | ||
// | ||
// Authorization: Bearer xxxxxxxxxxxxx | ||
String httpHeaderValue = authData.getHttpHeader(HTTP_HEADER_NAME); | ||
if (httpHeaderValue == null || !httpHeaderValue.startsWith(HTTP_HEADER_VALUE_PREFIX)) { | ||
throw new AuthenticationException("Invalid HTTP Authorization header"); | ||
} | ||
|
||
// Remove prefix | ||
token = httpHeaderValue.substring(HTTP_HEADER_VALUE_PREFIX.length()); | ||
} else { | ||
throw new AuthenticationException("No token credentials passed"); | ||
} | ||
|
||
// Validate the token | ||
try { | ||
@SuppressWarnings("unchecked") | ||
Jwt<?, Claims> jwt = Jwts.parser() | ||
.setSigningKey(validationKey) | ||
.parse(token); | ||
|
||
return jwt.getBody().getSubject(); | ||
} catch (JwtException e) { | ||
throw new AuthenticationException("Failed to authentication token: " + e.getMessage()); | ||
} | ||
} | ||
|
||
/** | ||
* Try to get the validation key for tokens from several possible config options. | ||
*/ | ||
private static Key getValidationKey(ServiceConfiguration conf) throws IOException { | ||
final boolean isPublicKey; | ||
final String validationKeyConfig; | ||
|
||
if (conf.getProperty(CONF_TOKEN_SECRET_KEY) != null | ||
&& !StringUtils.isBlank((String) conf.getProperty(CONF_TOKEN_SECRET_KEY))) { | ||
isPublicKey = false; | ||
validationKeyConfig = (String) conf.getProperty(CONF_TOKEN_SECRET_KEY); | ||
} else if (conf.getProperty(CONF_TOKEN_PUBLIC_KEY) != null | ||
&& !StringUtils.isBlank((String) conf.getProperty(CONF_TOKEN_PUBLIC_KEY))) { | ||
isPublicKey = true; | ||
validationKeyConfig = (String) conf.getProperty(CONF_TOKEN_PUBLIC_KEY); | ||
} else { | ||
throw new IOException("No secret key was provided for token authentication"); | ||
} | ||
|
||
byte[] validationKey = AuthTokenUtils.readKeyFromUrl(validationKeyConfig); | ||
|
||
if (isPublicKey) { | ||
return AuthTokenUtils.decodePublicKey(validationKey); | ||
} else { | ||
return AuthTokenUtils.decodeSecretKey(validationKey); | ||
} | ||
} | ||
} |
106 changes: 106 additions & 0 deletions
106
...er-common/src/main/java/org/apache/pulsar/broker/authentication/utils/AuthTokenUtils.java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
/** | ||
* Licensed to the Apache Software Foundation (ASF) under one | ||
* or more contributor license agreements. See the NOTICE file | ||
* distributed with this work for additional information | ||
* regarding copyright ownership. The ASF licenses this file | ||
* to you under the Apache License, Version 2.0 (the | ||
* "License"); you may not use this file except in compliance | ||
* with the License. You may obtain a copy of the License at | ||
* | ||
* http://www.apache.org/licenses/LICENSE-2.0 | ||
* | ||
* Unless required by applicable law or agreed to in writing, | ||
* software distributed under the License is distributed on an | ||
* "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY | ||
* KIND, either express or implied. See the License for the | ||
* specific language governing permissions and limitations | ||
* under the License. | ||
*/ | ||
package org.apache.pulsar.broker.authentication.utils; | ||
|
||
import com.google.common.io.ByteStreams; | ||
|
||
import io.jsonwebtoken.JwtBuilder; | ||
import io.jsonwebtoken.Jwts; | ||
import io.jsonwebtoken.SignatureAlgorithm; | ||
import io.jsonwebtoken.io.Decoders; | ||
import io.jsonwebtoken.io.Encoders; | ||
import io.jsonwebtoken.security.Keys; | ||
|
||
import java.io.IOException; | ||
import java.io.InputStream; | ||
import java.security.Key; | ||
import java.security.KeyFactory; | ||
import java.security.PrivateKey; | ||
import java.security.PublicKey; | ||
import java.security.spec.PKCS8EncodedKeySpec; | ||
import java.security.spec.X509EncodedKeySpec; | ||
import java.util.Date; | ||
import java.util.Optional; | ||
|
||
import javax.crypto.SecretKey; | ||
|
||
import lombok.experimental.UtilityClass; | ||
|
||
import org.apache.pulsar.client.api.url.URL; | ||
|
||
@UtilityClass | ||
public class AuthTokenUtils { | ||
|
||
public static SecretKey createSecretKey(SignatureAlgorithm signatureAlgorithm) { | ||
return Keys.secretKeyFor(signatureAlgorithm); | ||
} | ||
|
||
public static SecretKey decodeSecretKey(byte[] secretKey) { | ||
return Keys.hmacShaKeyFor(secretKey); | ||
} | ||
|
||
public static PrivateKey decodePrivateKey(byte[] key) throws IOException { | ||
try { | ||
PKCS8EncodedKeySpec spec = new PKCS8EncodedKeySpec(key); | ||
KeyFactory kf = KeyFactory.getInstance("RSA"); | ||
return kf.generatePrivate(spec); | ||
} catch (Exception e) { | ||
throw new IOException("Failed to decode private key", e); | ||
} | ||
} | ||
|
||
public static PublicKey decodePublicKey(byte[] key) throws IOException { | ||
try { | ||
X509EncodedKeySpec spec = new X509EncodedKeySpec(key); | ||
KeyFactory kf = KeyFactory.getInstance("RSA"); | ||
return kf.generatePublic(spec); | ||
} catch (Exception e) { | ||
throw new IOException("Failed to decode public key", e); | ||
} | ||
} | ||
|
||
public static String encodeKeyBase64(Key key) { | ||
return Encoders.BASE64.encode(key.getEncoded()); | ||
} | ||
|
||
public static String createToken(Key signingKey, String subject, Optional<Date> expiryTime) { | ||
JwtBuilder builder = Jwts.builder() | ||
.setSubject(subject) | ||
.signWith(signingKey); | ||
|
||
if (expiryTime.isPresent()) { | ||
builder.setExpiration(expiryTime.get()); | ||
} | ||
|
||
return builder.compact(); | ||
} | ||
|
||
public static byte[] readKeyFromUrl(String keyConfUrl) throws IOException { | ||
if (keyConfUrl.startsWith("data:") || keyConfUrl.startsWith("file:")) { | ||
try { | ||
return ByteStreams.toByteArray((InputStream) new URL(keyConfUrl).getContent()); | ||
} catch (Exception e) { | ||
throw new IOException(e); | ||
} | ||
} else { | ||
// Assume the key content was passed in base64 | ||
return Decoders.BASE64.decode(keyConfUrl); | ||
} | ||
} | ||
} |
Oops, something went wrong.