Skip to content

Commit

Permalink
Add support for new interface to AuthenticaionTls (apache#1147)
Browse files Browse the repository at this point in the history
This adds EncodedAuthenticationParameterSupport interface to
AuthenticationTls and AuthenticationDisabled.

Also, mark some methods deprecated as preparation for 2.0
  • Loading branch information
maskit authored and merlimat committed Feb 7, 2018
1 parent dbbb79c commit 8d88407
Show file tree
Hide file tree
Showing 6 changed files with 83 additions and 32 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -27,20 +27,17 @@
import java.net.URISyntaxException;
import java.security.PrivateKey;
import java.util.Base64;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.concurrent.TimeUnit;

import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.AuthenticationDataProvider;
import org.apache.pulsar.client.api.AuthenticationUtil;
import org.apache.pulsar.client.api.EncodedAuthenticationParameterSupport;
import org.apache.pulsar.client.api.PulsarClientException;
import org.apache.pulsar.client.api.PulsarClientException.GettingAuthenticationDataException;
import org.apache.pulsar.common.util.ObjectMapperFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.google.common.base.Splitter;
import com.yahoo.athenz.auth.ServiceIdentityProvider;
import com.yahoo.athenz.auth.impl.SimpleServiceIdentityProvider;
Expand Down Expand Up @@ -108,15 +105,10 @@ public void configure(String encodedAuthParamString) {
throw new IllegalArgumentException("authParams must not be empty");
}

// Convert JSON to Map
try {
ObjectMapper jsonMapper = ObjectMapperFactory.create();
Map<String, String> authParamsMap = jsonMapper.readValue(encodedAuthParamString,
new TypeReference<HashMap<String, String>>() {
});
setAuthParams(authParamsMap);
setAuthParams(AuthenticationUtil.configureFromJsonString(encodedAuthParamString));
} catch (IOException e) {
throw new IllegalArgumentException("Failed to parse authParams");
throw new IllegalArgumentException("Failed to parse authParams", e);
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,11 @@ public interface Authentication extends Closeable, Serializable {
* Configure the authentication plugins with the supplied parameters
*
* @param authParams
* @deprecated This method will be deleted on version 2.0, instead please use configure(String
* encodedAuthParamString) which is in EncodedAuthenticationParameterSupport for now and will be
* integrated into this interface.
*/
@Deprecated
void configure(Map<String, String> authParams);

/**
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@
package org.apache.pulsar.client.api;

import java.util.Map;
import java.util.HashMap;

import static org.apache.commons.lang3.StringUtils.isNotBlank;
import org.apache.pulsar.client.api.PulsarClientException.UnsupportedAuthenticationException;
Expand All @@ -28,21 +27,6 @@

public final class AuthenticationFactory {

private static Map<String, String> parseAuthParamsString(String authParamsString) {
Map<String, String> authParams = new HashMap<>();

if (isNotBlank(authParamsString)) {
String[] params = authParamsString.split(",");
for (String p : params) {
String[] kv = p.split(":");
if (kv.length == 2) {
authParams.put(kv[0], kv[1]);
}
}
}
return authParams;
}

/**
* Create an instance of the Authentication-Plugin
*
Expand All @@ -63,7 +47,7 @@ public static final Authentication create(String authPluginClassName, String aut
((EncodedAuthenticationParameterSupport) auth).configure(authParamsString);
} else {
// Parse parameters by default parse logic.
auth.configure(parseAuthParamsString(authParamsString));
auth.configure(AuthenticationUtil.configureFromPulsar1AuthParamString(authParamsString));
}
return auth;
} else {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
/**
* 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.client.api;

import static org.apache.commons.lang3.StringUtils.isNotBlank;

import java.io.IOException;
import java.util.HashMap;
import java.util.Map;

import org.apache.pulsar.common.util.ObjectMapperFactory;

import com.fasterxml.jackson.core.type.TypeReference;
import com.fasterxml.jackson.databind.ObjectMapper;

public class AuthenticationUtil {
public static Map<String, String> configureFromJsonString(String authParamsString) throws IOException {
ObjectMapper jsonMapper = ObjectMapperFactory.create();
return jsonMapper.readValue(authParamsString, new TypeReference<HashMap<String, String>>() {
});
}

public static Map<String, String> configureFromPulsar1AuthParamString(String authParamsString) {
Map<String, String> authParams = new HashMap<>();

if (isNotBlank(authParamsString)) {
String[] params = authParamsString.split(",");
for (String p : params) {
String[] kv = p.split(":");
if (kv.length == 2) {
authParams.put(kv[0], kv[1]);
}
}
}
return authParams;
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,10 @@

import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.AuthenticationDataProvider;
import org.apache.pulsar.client.api.EncodedAuthenticationParameterSupport;
import org.apache.pulsar.client.api.PulsarClientException;

public class AuthenticationDisabled implements Authentication {
public class AuthenticationDisabled implements Authentication, EncodedAuthenticationParameterSupport {

protected final AuthenticationDataProvider nullData = new AuthenticationDataNull();
/**
Expand All @@ -47,6 +48,11 @@ public AuthenticationDataProvider getAuthData() throws PulsarClientException {
}

@Override
public void configure(String encodedAuthParamString) {
}

@Override
@Deprecated
public void configure(Map<String, String> authParams) {
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@

import org.apache.pulsar.client.api.Authentication;
import org.apache.pulsar.client.api.AuthenticationDataProvider;
import org.apache.pulsar.client.api.AuthenticationUtil;
import org.apache.pulsar.client.api.EncodedAuthenticationParameterSupport;
import org.apache.pulsar.client.api.PulsarClientException;

/**
Expand All @@ -32,7 +34,7 @@
* tlsCertFile: A file path for a client certificate. tlsKeyFile: A file path for a client private key.
*
*/
public class AuthenticationTls implements Authentication {
public class AuthenticationTls implements Authentication, EncodedAuthenticationParameterSupport {

private static final long serialVersionUID = 1L;

Expand All @@ -59,14 +61,24 @@ public AuthenticationDataProvider getAuthData() throws PulsarClientException {
}

@Override
public void configure(String encodedAuthParamString) {
setAuthParams(AuthenticationUtil.configureFromPulsar1AuthParamString(encodedAuthParamString));
}

@Override
@Deprecated
public void configure(Map<String, String> authParams) {
certFilePath = authParams.get("tlsCertFile");
keyFilePath = authParams.get("tlsKeyFile");
setAuthParams(authParams);
}

@Override
public void start() throws PulsarClientException {
// noop
}

private void setAuthParams(Map<String, String> authParams) {
certFilePath = authParams.get("tlsCertFile");
keyFilePath = authParams.get("tlsKeyFile");
}

}

0 comments on commit 8d88407

Please sign in to comment.